How to manage k8s yaml manifests for multiple environments with kustomize?

How to manage k8s yaml manifests for multiple environments with kustomize?

What is Kustomize?

Kustomize is a CLI configuration manager for Kubernetes objects that leverage layering to preserve the base settings of the application. This is achieved by overlaying the declarative YAML artifacts to override default settings without actually making any changes to the original manifest. Kustomize is also integrated with kubectl.

Kustomize is aware of kubernetes resources and their fields and is not just a simple text templating solution like other tools.

With Kustomize you can reuse one of the base files across all environments (development, staging, production, etc.) and overlay specifications for each of those environments.

Kustomize can also be used with helm and CD solutions like argo CD.

To install kustomize checkout -->

how kustomize works?



Each directory contains a kustomization.yaml file, which is essentially a list of resources or manifests that describes how to generate or transform Kubernetes objects.

With Kustomize, you can configure raw, template-free YAML files, which allows you to modify settings/annotations between deployment and production easily.

Kustomize provides 2 methods to apply patch,

  1. patchesStrategicMerge
  2. patchesJson6902

patchesStrategicMerge is the most common and easy to use merge strategy. To know more about patching checkout -->

base folder

The base folder holds common resources, such as the deployment.yaml, service.yaml, and configmap.yaml. It contains the initial manifest and includes a namespace and label for the resources.

overlays folder

The overlays folder has environment-specific overlays, which use patches to allow YAML files to be defined and overlaid on top of the base for any changes.

Example structure, To create a base configmap resource and change configmap variable for staging and production. To get an full fledged example checkout -->

├── base
│   ├── configmap.yaml
│   ├── kustomization.yaml
└── overlays
    ├── production
    │   ├── configmap-patch.yaml
    │   ├── kustomization.yaml
    └── staging
        ├── configmap-patch.yaml
        ├── kustomization.yaml


apiVersion: v1
kind: ConfigMap
  name: django-configmap
  DEBUG: "True"


# common labels to be added on all manifests
  app: demo

# resources that needs to be kustomized
  - configmap.yaml

Now, lets change the DJANGO_AUTH_PUBLIC_URI value for staging environment.


apiVersion: v1
kind: ConfigMap
  name: django-configmap


# prefix to be added to name of the resource
namePrefix: staging-
  env: staging
# directory contains base yaml
  - ../../base
# patch strategy
  - configmap-patch.yaml

Clone the repo

git clone
cd kustomize-k8s-example

Preview and apply manifests

We can preview the kustomize output using kustomize build command.

# preview output
kustomize build overlays/staging

# apply output to kubernetes
kustomize build overlays/staging | kubectl apply -f -

We can also use kustomize under kubectl kustomize as kubectl plugin.

# preview output
kubectl kustomize overlays/staging

# apply output to kubernetes
kubectl apply -k overlays/staging


Did you find this article valuable?

Support Suresh Kumar by becoming a sponsor. Any amount is appreciated!