Sub topic 6
Overriding image tags and replica counts
This chapter assumes you can read Kubernetes Deployment YAML, know the base and overlay layout from the previous chapters, and can run kubectl kustomize. You will use two fields in an overlay's kustomization.yaml, images and replicas, to change the image tag and replica count without editing the base or writing a patch.
Why use dedicated fields
The base Deployment pins registry.example.com/web:1.0.0 and replicas: 1. Production needs a newer tag and more replicas. You could write a patch for each change. Kustomize offers shorter fields for these two cases because they come up in almost every project.
imagesrewrites the name, tag or digest of a container image wherever it appears.replicassets the replica count of a named workload.
Both fields leave the base file untouched. Each overlay states only the difference.
The overlay
This is the production overlay from the running example. It uses only the fields relevant to this chapter.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
replicas:
- name: web
count: 5
images:
- name: registry.example.com/web
newTag: 1.2.3Render it from the repository root:
kubectl kustomize overlays/productionIn the output, the Deployment has replicas: 5 and the container image is registry.example.com/web:1.2.3.
How the images field matches
The name in an images entry is matched against the image name in the manifest, without the tag. Here registry.example.com/web matches registry.example.com/web:1.0.0. It is not the container's name field. This is a frequent mix-up. If the two differ, the override silently does nothing.
An entry can change different parts of the reference:
| Field | Effect |
|---|---|
| `newTag` | Replaces the tag |
| `newName` | Replaces the image name, for example to point at a mirror registry |
| `digest` | Pins the image to a digest and removes the tag |
You can combine newName and newTag in one entry. Kustomize rewrites image fields in any resource it recognises as having containers, including init containers and the pod templates of Deployments, StatefulSets, DaemonSets and Jobs.
How the replicas field matches
The name in a replicas entry is the resource's metadata.name as it is in the base, before any namePrefix is applied. The overlay above sets namePrefix: prod- in the full example, and the entry still uses web, not prod-web. The field only accepts a count, and it works on resources that have a replica field, such as Deployments, StatefulSets and ReplicaSets.
Setting the tag from a script
CI pipelines often set the tag to a build number. The kustomize edit command changes the file for you:
cd overlays/production
kustomize edit set image registry.example.com/web=registry.example.com/web:1.2.4
kustomize edit set replicas web=4These commands rewrite kustomization.yaml, so you commit the result to Git. This keeps the deployed version recorded in the repository.
Choosing between the fields and a patch
Use the dedicated fields when they fit. They are shorter and easier to review. Use a patch for anything else, such as resource limits.
Exercise
An overlay sets namePrefix: prod-. The base Deployment is named web and its container is named app, running registry.example.com/web:1.0.0. What name values do you write in the replicas and images entries to override both?
Show answer
Use name: web in the replicas entry, because it matches the base metadata.name before the prefix is applied. Use name: registry.example.com/web in the images entry, because it matches the image name without the tag, not the container name app.
Exercise
You add images with name: web and newTag: 2.0.0, but the rendered output still shows registry.example.com/web:1.0.0. What is wrong and how do you fix it?
Show answer
The name must match the full image name, registry.example.com/web. web matches nothing, and Kustomize does not report an error. Change the name and render again to confirm the new tag.