Kustomizing your deployments
Helm, the package manager for kubernetes is a great tool for deploying applications . It provides the templating feature for the kubernetes manifest files. We can easily change the values for different environments and deploy them. There might be few scenarios where you would require to change/transform/patch the manifest files instead of just providing the values to the template. In such scenarios,we can use kustomize. Its provides a way of deploying your kubernetes manifest without having to create the templates.
Following are some of the scenarios where you could use kustomize.
- Add the appropriate labels to the manifest file based on the environment
- Add the resource constraints like cpu and memory for the kubernetes resources based on the environment
- Transform the existing helm charts to adhere to your environments.
For more details, refer the link here
Lets look at one scenario,which i used to set up my application in my test environment
My setup consists of the apps,infra and sql backend components having the manifest files.
We need to have the below folder structure for kustomize to work as shown. The base folder having the manifest file along with kustomization.yaml and a overlay folder consisting of kustomization yaml file which will overide the base manifest file.
In our example, we are not providing the image tag in our main manifest file and the image is added/transformed within the manifest file using kustomize. The base folder consist of poi.yaml and kustomization.yaml.
poi.yaml : This manifest file contains the deployment with the docker image name and without the image tag.
kustomization.yaml : This specifies the manifest files to be deployed
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - poi.yaml
In the overlay folder, we have another kustomization.yaml file, which specifies the base folder to use and the image tag to override as shown.
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization bases: - ../../base images: - name: demoacrk8s.azurecr.io/demoacrk8s/poi newTag: "1342"
We can run the below command for the kustomization to be applied
kubectl apply -k ./
We can verify the changes done by looking at the deployment and the tag that being used
Incase, you want to kustomize or transform your manifest in your devops pipline , you can use the command the below command as shown.
kustomize edit set image demoacrk8s.azurecr.io/demoacrk8s/poi:$(maytag)
More examples on using kustomization can be found here.