DEV Community

Suresh Kumar for Kubernetes Community Days Chennai

Posted on • Originally published at sureshdsk.dev

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 --> https://kubectl.docs.kubernetes.io/installation/kustomize/

how kustomize works?

kustomize

kustomization.yaml

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 --> https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing

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 --> https://github.com/sureshdsk/kustomize-k8s-example

kustomize-k8s
├── base
│   ├── configmap.yaml
│   ├── kustomization.yaml
└── overlays
    ├── production
    │   ├── configmap-patch.yaml
    │   ├── kustomization.yaml
    └── staging
        ├── configmap-patch.yaml
        ├── kustomization.yaml
Enter fullscreen mode Exit fullscreen mode

base/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: django-configmap
data:
  DJANGO_AUTH_PUBLIC_URI: "http://dj.192.168.0.139.sslip.io"
  DEBUG: "True"
Enter fullscreen mode Exit fullscreen mode

base/kustomization.yaml

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

# resources that needs to be kustomized
resources:
  - configmap.yaml

Enter fullscreen mode Exit fullscreen mode

Now, lets change the DJANGO_AUTH_PUBLIC_URI value for staging environment.

overlays/staging/configmap-patch.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: django-configmap
data:
  DJANGO_AUTH_PUBLIC_URI: "http://staging.192.168.0.139.sslip.io"
Enter fullscreen mode Exit fullscreen mode

overlays/staging/kustomization.yaml

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

Enter fullscreen mode Exit fullscreen mode

Clone the repo

git clone git@github.com:sureshdsk/kustomize-k8s-example.git
cd kustomize-k8s-example
Enter fullscreen mode Exit fullscreen mode

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 -

Enter fullscreen mode Exit fullscreen mode

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

Resources

Top comments (2)

Collapse
 
masonharper profile image
Mason Marper

Really like this

Collapse
 
pythonsky profile image
Sky

nice tip!