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?
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,
- patchesStrategicMerge
- 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
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"
base/kustomization.yaml
# common labels to be added on all manifests
commonLabels:
app: demo
# resources that needs to be kustomized
resources:
- configmap.yaml
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"
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
Clone the repo
git clone git@github.com:sureshdsk/kustomize-k8s-example.git
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