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?
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: v1kind: ConfigMapmetadata: name: django-configmapdata: DJANGO_AUTH_PUBLIC_URI: "http://dj.192.168.0.139.sslip.io" DEBUG: "True"
base/kustomization.yaml
# common labels to be added on all manifestscommonLabels: app: demo# resources that needs to be kustomizedresources: - configmap.yaml
Now, lets change the DJANGO_AUTH_PUBLIC_URI value for staging environment.
overlays/staging/configmap-patch.yaml
apiVersion: v1kind: ConfigMapmetadata: name: django-configmapdata: DJANGO_AUTH_PUBLIC_URI: "http://staging.192.168.0.139.sslip.io"
overlays/staging/kustomization.yaml
# prefix to be added to name of the resourcenamePrefix: staging-commonLabels: env: staging# directory contains base yamlbases: - ../../base# patch strategypatchesStrategicMerge: - configmap-patch.yaml
Clone the repo
git clone [email protected]:sureshdsk/kustomize-k8s-example.gitcd kustomize-k8s-example
Preview and apply manifests
We can preview the kustomize output using kustomize build
command.
# preview outputkustomize build overlays/staging# apply output to kuberneteskustomize build overlays/staging | kubectl apply -f -
We can also use kustomize under kubectl kustomize
as kubectl plugin.
# preview outputkubectl kustomize overlays/staging# apply output to kuberneteskubectl apply -k overlays/staging
Resources
Original Link: https://dev.to/kcdchennai/how-to-manage-k8s-yaml-manifests-for-multiple-environments-with-kustomize-450o
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To