Kubernetes Kustomize Tutorial: A Beginner-Friendly Developer Guide!
Tags: kubernetes, devops, tutorial, kustomize
Published: 2023-04-24 06:23:17

[Kustomize](https://kustomize.io/), just like the name implies, is used for customizing Kubernetes deployments to help developers manage Kubernetes application configurations. With Kustomize, it is easy to define a base set of Kubernetes resources and create overlays on top of it in separate directories to modify or extend the base configuration. It allows you to have a single source of truth for all your Kubernetes application configuration while still being able to customize it for different environments or use cases. Kustomize is especially helpful while managing large or complex Kubernetes deployments, where maintaining separate YAML files for each environment or use case can be tricky and error-prone.

Using a rolling update strategy, this Kustomize tutorial will deploy multiple variants of a simple public Hello World server. So let's begin the show!!!

## Prerequisites:
- Free [Harness account](https://app.harness.io/auth/#/signup/?module=cd&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_kustomize_tutorial&utm_term=get-started) with free builds
- [Minikube](https://minikube.sigs.k8s.io/docs/start/) to have a single node cluster access
- A repo containing the kustomization yaml file. We'll use a publicly available [hellword kustomization](https://github.com/wings-software/harness-docs/tree/main/kustomize/helloWorld) cloned from Kustomize.

## Tutorial
In our example [repo](https://github.com/wings-software/harness-docs/tree/main/kustomize/helloWorld), you can see the `configMap.yaml`, `deployment.yaml`, `service.yaml` and `kustomization.yaml`. 

For any Kubernetes application deployment, we need deployment and service manifest files. In this tutorial, we are more interested in the kustomization manifest file. Why?

**Let's take a use case where kustomization comes in handy for developers and DevOps professionals.**

Suppose you're using a deployment package from a specific vendor that is almost suitable for your use case, but not entirely. Consequently, you create a fork of the deployment package, make necessary configuration changes, and apply it to your Kubernetes cluster. After a few months, the vendor releases a new version of the package that incorporates critical features that you require. To utilize those new features, you have to fork the updated package and reapply your configuration changes.

As your deployment scales, this repetitive process of reforking and customizing the deployment package can become a tedious task and lead to an increased likelihood of misconfigurations, which could compromise the stability of your product and services. This can be easily avoided by making use of kustomize.

### Let's get back to our tutorial 
Below is the `kustomization.yaml` file in our example repo, an example of how to use Kustomize to manage a Kubernetes deployment for a web server application called hello. 

```
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# metadata:
#  name: arbitrary

# Example configuration for the webserver
# at https://github.com/monopole/hello
commonLabels:
  app: hello

resources:
- deployment.yaml
- service.yaml
- configMap.yaml
```

Here is a breakdown of its contents:

- **apiVersion**: kustomize.config.k8s.io/v1beta1: This specifies the version of the Kustomize configuration format being used.
- **kind: Kustomization**: This indicates that this is a Kustomize configuration file.
- **commonLabels**: This is a section where you can define common labels that will be applied to all resources in this Kustomize configuration. In this case, it defines a label named app with a value of hello.
- **resources**: This section lists the Kubernetes resources that should be managed by Kustomize. In this example, there are three resources: deployment.yaml, service.yaml, and configMap.yaml. These files are expected to be in the same directory as the kustomization.yaml file.

By default, Kustomize concatenates all the resources listed in the resources section in the order they appear and applies any overlays if they are specified.

This is just a basic example of a `kustomization.yaml` file. You can add many more options and customizations to manage more complex deployments with multiple environments and configurations using Kustomize.

Login to your [Harness CD](https://app.harness.io/auth/#/signup/?module=cd&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_kustomize_tutorial&utm_term=get-started) module and start your free plan where you get free builds.
![Harness CD module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4d1ly3e4kzay2yw3pms.png)

Go to the pipeline studio and create a new pipeline.
![new pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95urwxydw78c3y0jxvbi.png)

Create the deploy stage.
![deploy stage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vno2r0f39qvg788lmgl6.png)

Add 'Kubernetes' as the deployment type.
![kubernetes deployment type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lakgbmrrovu876zb5a31.png)

Create a new service. Name the service as 'nginx'.
![new service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vodnlkrinz0if5hgl0r2.png)

Add the manifest details.
![manifest details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bi7n1gfspx5cbxm8l8f.png)

Select kustomize while adding the manifest details.
![details of manifest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx6b3zp71l5db1mec60i.png)

Add the manifest details stored in your GitHub repo.
![GitHub repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de7ppjq1vmhdqbj2c8ph.png)

This is the GitHub repo url - https://github.com/wings-software/harness-docs.git
![add git creds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buezeqssfyq562sxe68w.png)

Add your GitHub username and password (add password through a secret manager), as shown below.
![user creds below](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyay7i57caeb1r6d5tyd.png)

Connect these details with a Harness Delegate.
![add Harness delegate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxf67xcmyv1btz419r8i.png)

You need to have a Delegate up and running to deploy with Harness. Let's add Delegate. 

So, what is Harness Delegate?
A Delegate is a service that runs on your infrastructure to execute tasks on behalf of the Harness platform.

Now, you need to install this Harness Delegate on your Kubernetes cluster.

Make sure you have a cluster access from any cloud provider.
Nothing to worry about if you don't have access to any cluster access. We will make use of Minikube.

[Install Minikube](https://minikube.sigs.k8s.io/docs/start/) on your local machine.

Once the installation is complete, start the Minikube cluster with the following command.
```
minikube start
```
Get on to install a delegate.
There are many ways to install a delegate. You can choose whatever you are okay with. We choose 'Kubernetes' and 'Kubernetes manifest' from the options available.
![kube manifest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuc3oo4ar8x1vv6z16f2.png)

As guided there, download the harness delegate yml file and run the below command from the folder where your yaml file is downloaded.

```
kubectl apply -f harness-delegate.yaml
```
Make sure to verify if the delegate installation is successful.

Once the delegate is installed. Connect it with your manifest details and make sure the connection is successful.
![success with connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bjw2mpshoc7iskw7m4l.png)

Next, create an environment and infrastructure as shown below.
![envi and infra](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jjsydbwx58opjev9b03.png)

Finally, in the execution step, select the rolling deployment strategy. 
![rolling deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kcbn50t0e509l6gzta6.png)

Apply changes, save and run the pipeline. You should see a successful execution of the pipeline.
![pipeline execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ve7yp9fdpva4ml3w8wn.png)

You can see the console view also.
![console view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k7u8d28bicdtrib7sgm.png)

This is how you can deploy multiple variants of a simple public Hello World server using a rolling update strategy using Harness.

For more help, refer to the following [Harness developer hub link](https://developer.harness.io/docs/continuous-delivery/onboard-cd/cd-quickstarts/kustomize-quickstart/).

Also, if you like to learn more about CI/CD and practical examples, follow my tutorial where I shared a complete CI/CD setup from scratch.
{% embed https://dev.to/pavanbelagatti/learn-how-to-setup-a-cicd-pipeline-from-scratch-for-a-go-application-4m69 %}