1. Understanding Kustomize
Kustomize is a configuration management tool specifically designed for Kubernetes. Unlike traditional templating solutions, Kustomize works by layering patches on Kubernetes resource files, thus preserving the original structure and ensuring that configurations remain declarative and clean. It helps with managing variants of Kubernetes resources without the need for templates. This is perhaps its greatest strength and weakness at the same time. Some features that Helm offers cannot be provided by Kustomize due to this limitation. For example, the usage of control structures like loops or conditional blocks. Nevertheless, Kustomize keeps customizing simple by using fully valid yaml structures.
1.1 Key Features of Kustomize
Certain characteristics and constraints include:
- No Templating Language: Kustomize uses plain YAML, eliminating the need for any additional templating language.
- Overlay System: Overlays allow you to define variations of your base configurations without duplicating files.
- Native Integration: Kustomize is natively integrated into kubectl, the Kubernetes command-line tool, making it seamless to use.
- The kubectl CLI includes an integrated version, though it is typically not up-to-date. Therefore, the standalone binary must be used to benefit from the latest features.
- It manages variants of resources with overlaying and merging yaml files in a structured way.
- It provides convenient built-in features to generate common resources like ConfigMaps and Secrets.
- It has built-in transformers to modify resources.
- It can be extended via a plug-in mechanism.
- It is possible to dynamically change resources, but this is restricted to particular fields of a resource.
- It only manages the yaml files and does not actively manage the resources in the cluster.
STAY TUNED
Learn more about DevOpsCon
STAY TUNED
Learn more about DevOpsCon
1.2 Core Components
- Base: A set of common configurations that can be reused across multiple environments. It can be reused in all environments and patches can be added for each of these environments.
- Overlay: A set of environment-specific configurations that modify or extend the base configurations.
- kustomization.yaml: The main configuration file that defines how resources should be customized.
A base manifest and an overlay manifest create customized files. Each environment needs a kustomization.yaml file.
The kustomization.yaml file is the primary file used by the Kustomize tool. When the Kustomize build command is executed, Kustomize searches for this file. It contains a list of all Kubernetes manifests (YAML files) to be managed by Kustomize, along with any custom configurations for the manifests that will be generated.
Here is an example of kustomize.yaml file
- Patches: They enable modifications to particular resources or workloads in a Kubernetes cluster without impacting other fields. Three Parameters are important to achieve a patch:
- Operation Type: This can be add, remove, or replace. – Target: Represents the Kubernetes resource intended for modification.
- Value: Specifies the name of the value to be added or changed. This field remains blank when performing a removal operation.
- Operation Type: This can be add, remove, or replace. – Target: Represents the Kubernetes resource intended for modification.
There are two methods for patches: JSON 6902, which is managed by target and patches details, and the Strategic Merge Patching method, where the configuration provided follows the standard Kubernetes YAML format. The required changes can be specified in the relevant sections.
To clarify, we will provide some examples. Consider the following Kubernetes deployment:
A patch here may consist of change replicas to one for the deployment and change the container name.
Using JSON6902 we get:
Using the strategic merge patching will result in:

Kubernetes Training (German only)
Entdecke die Kubernetes Trainings für Einsteiger und Fortgeschrittene mit DevOps-Profi Erkan Yanar
- Transformers: Transformers refer to a concept that modifies or enhances the values specified in a configuration. By utilizing Transformers, we can adjust our basic Kubernetes YAML configurations according to the desired specifications. The following are some transformers and their applications:
- commonLabel: Adds a label to all Kubernetes manifests.
- namePrefix: Adds a common prefix to all manifest files.
- nameSuffix: Adds a common suffix to all manifest files.
- Namespace: Adds a common namespace to all manifests.
- commonAnnotations: Adds a common annotations to all manifests.
2. Setting Up Kustomize
2.1 Installation
To get started with Kustomize, you need to install it. Fortunately, Kustomize is bundled with kubectl, so if you have kubectl installed, you’re ready to go. However, you can also install Kustomize as a standalone tool if needed.
To install Kustomize, the following commands can be used:
# Install
curl -s "https://raw.githubusercontent.com/kubernetes sigs/kustomize/master/hack/install_kustomize.sh" | bash
# permission
sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
2.2 Deployment of an application using Kustomize
To understand how Kustomize simplifies Kubernetes deployments, let’s walk through a basic example.
Imagine we want to deploy a simple NGINX application. Instead of manually editing YAML files for each environment (like development or production), we can structure our resources cleanly with Kustomize.
Step 1: Create the Base Configuration
First, create a directory structure: mkdir -p kustomize-example/base
Inside base/, create the following files:
Step 2: Create an Overlay for Development
Now let’s create a custom configuration for a development environment: mkdir -p kustomize-example/overlays/dev
Inside overlays/dev/, create:
- We add a dev- prefix to resource names.
- We label all resources with environment: development.
- We patch the deployment to run 2 replicas instead of 1.
Step 3: Build the Final Manifests
Now, use Kustomize to build the manifests:
cd kustomize-example/overlays/dev kustomize build.
The output would show:
- A Deployment named dev-nginx with 2 replicas.
- A Service named dev-nginx.
- Labels added for the development environment.
3. CI/CD with Kustomize
Thus far, we have examined the utilization of Kustomize to generate Kubernetes manifest files that are prepared for deployment. Kustomize proves particularly effective when integrated into a CI/CD pipeline. By leveraging Kustomize, you can ensure that your Kubernetes configurations remain consistent and easily manageable across various environments. In this section, we will present a brief example illustrating how Kustomize can be incorporated into a CI/CD pipeline using a build tool such as Jenkins.
Jenkins is a widely-used open-source automation server that facilitates the automation of the CI/CD process. Here is a procedure for integrating Kustomize with Jenkins:
- Checkout the code (including Kustomize bases and overlays).
- Build the Docker image (optional, if you’re deploying a new app version).
- Update the Kustomize overlay (for example, to set a new image tag).
- Build the Kubernetes manifests with Kustomize.
- Apply the manifests using kubectl to the Kubernetes cluster.
Here is an example of Jenkinsfile:
pipeline {
agent any
environment {
REGISTRY = "your-docker-registry.io"
IMAGE_NAME = "your-app"
K8S_OVERLAY_DIR = "k8s/overlays/staging"
KUSTOMIZE_VERSION = "5.0.1" // adjust version if needed
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build Docker Image') {
steps {
script {
sh "docker build -t ${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} ."
sh "docker push ${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER}"
}
}
}
stage('Install Kustomize') {
steps {
sh '''
curl -s "https://raw.githubusercontent.com/kubernetes-
sigs/kustomize/master/hack/install_kustomize.sh" | bash
mv kustomize /usr/local/bin/
'
''
}
}
stage('Update Kustomize Image') {
steps {
dir("${K8S_OVERLAY_DIR}") {
sh "kustomize edit set image
${IMAGE_NAME}=${REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER}"
}
}
}
stage('Deploy to Kubernetes') {
steps {
dir("${K8S_OVERLAY_DIR}") {
sh '''
kubectl apply -k .
'
''
}
}
}
}
post {
always {
echo 'Cleaning up...'
}
}
}
4. Best Practices
Outlined below are several best practices to consider when working with Kustomize:
- Keep Your Base Configuration Simple: The base configuration should be as simple as possible. It should contain the common configurations that apply to all environments.
- Use Overlays for Environment-Specific Configurations: Overlays should contain only the configurations that are specific to a particular environment. This keeps your configurations clean and manageable.
- Version Control Your Configurations: Store your configurations in a version control system like Git. This allows you to track changes and collaborate with your team effectively.
- Automate Your CI/CD Pipeline: Automate your CI/CD pipeline to ensure that your deployments are consistent and repeatable. Use tools like Jenkins, GitLab CI, or GitHub Actions to automate your pipeline.
Conclusion
Kustomize is a powerful tool that can simplify your Kubernetes configurations and make them more manageable. By integrating Kustomize into your CI/CD pipeline, you can ensure that your deployments are consistent across all environments. With the examples and best practices provided in this article, you should be well-equipped to streamline your Kubernetes deployments using Kustomize.