DEV Community

Cover image for Kubernetes Kustomize Tutorial: A Beginner-Friendly Developer Guide!
Pavan Belagatti
Pavan Belagatti

Posted on

Kubernetes Kustomize Tutorial: A Beginner-Friendly Developer Guide!

Kustomize, 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:

Tutorial

In our example repo, 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
Enter fullscreen mode Exit fullscreen mode

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 module and start your free plan where you get free builds.
Harness CD module

Go to the pipeline studio and create a new pipeline.
new pipeline

Create the deploy stage.
deploy stage

Add 'Kubernetes' as the deployment type.
kubernetes deployment type

Create a new service. Name the service as 'nginx'.
new service

Add the manifest details.
manifest details

Select kustomize while adding the manifest details.
details of manifest

Add the manifest details stored in your GitHub repo.
GitHub repo

This is the GitHub repo url - https://github.com/wings-software/harness-docs.git
add git creds

Add your GitHub username and password (add password through a secret manager), as shown below.
user creds below

Connect these details with a Harness Delegate.
add Harness delegate

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 on your local machine.

Once the installation is complete, start the Minikube cluster with the following command.

minikube start
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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

Next, create an environment and infrastructure as shown below.
envi and infra

Finally, in the execution step, select the rolling deployment strategy.
rolling deployment

Apply changes, save and run the pipeline. You should see a successful execution of the pipeline.
pipeline execution

You can see the console view also.
console view

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.

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.

Top comments (0)