Sub topic 16
Customizing Manifests with Kustomize
This chapter assumes you can write a Deployment and a Service, apply them with kubectl apply, and read the output of kubectl diff. Kustomize is built into kubectl, so you need no extra install.
The problem: one set of manifests, several environments
You often need the same manifests with small differences. A staging cluster may use a different image tag or namespace than production. Copying the files and editing each copy works at first. Over time the copies drift apart, and a fix made in one is missed in the others.
Kustomize avoids the copies. You keep the base manifests as they are and describe your changes in a file named kustomization.yaml. Kustomize reads the manifests, applies the changes, and prints the result. It does not edit your source files.
A minimal kustomization
Put deployment.yaml and service.yaml from the earlier chapters in one directory. Add this file next to them:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: demo
resources:
- deployment.yaml
- service.yaml
images:
- name: nginx
newTag: "1.27.1"The fields do the following:
resourceslists the manifests to include. Entries can also be directories that contain their ownkustomization.yaml.namespacesets the namespace on every namespaced resource in the list.imagesreplaces the tag of any container image namednginx. The Deployment file still saysnginx:1.27, but the output saysnginx:1.27.1.
Previewing and applying
Render the result without touching the cluster:
kubectl kustomize .Apply it with the -k flag, which takes a directory instead of a file:
kubectl diff -k .
kubectl apply -k .kubectl diff -k works the same way as kubectl diff -f. It shows what would change in the cluster. Use it before every apply.
Overlays for environments
A common layout has a shared base directory and one overlay directory per environment. An overlay lists the base as a resource and adds its own changes.
app/
base/
deployment.yaml
service.yaml
kustomization.yaml
overlays/
production/
kustomization.yamlThe production overlay can look like this:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: demo
replicas:
- name: web
count: 5
images:
- name: nginx
newTag: "1.27.1"Run kubectl apply -k app/overlays/production to deploy it. The replicas field sets the replica count of the Deployment named web. A staging overlay can use different values from the same base.
Common errors
- Wrong path:
kubectl apply -kneeds a directory that containskustomization.yaml. Passing the file itself fails. - Image name mismatch:
images.namemust match the image name in the manifest without the tag. If it does not match, nothing changes and no error appears. Check the output ofkubectl kustomize .. - Forgetting pruning limits: Kustomize only renders manifests. Removing a resource from
resourcesdoes not delete it from the cluster unless you delete it or use pruning, as covered in the previous chapter.
Exercise
You run kubectl apply -k . in a directory that holds deployment.yaml, service.yaml, and a kustomization.yaml that lists only deployment.yaml under resources. Is the Service created, and what command shows you what Kustomize will apply?
Show answer
The Service is not created, because only listed resources are included. kubectl kustomize . renders the output, and kubectl diff -k . shows the difference from the cluster.