Sub topic 14
Using Kustomize or Helm instead of raw manifests
This chapter assumes you have read the earlier chapters on building an image tagged with the commit SHA, authenticating to the cluster, and deploying with kubectl. You should also know how Deployments and Namespaces work.
Why raw manifests stop scaling
So far the pipeline ran kubectl set image against a Deployment that already existed. That works for one environment. Once you have staging and production, the manifests start to differ in replica counts, hostnames, resource limits, and namespaces. Copying YAML files per environment leads to drift, because a fix in one copy is easy to miss in the other.
Two tools solve this in different ways:
| Tool | Approach | Built into kubectl | Best for |
|---|---|---|---|
| Kustomize | Patches plain YAML with overlays | Yes (`kubectl apply -k`) | Your own app with a few per-environment differences |
| Helm | Renders templates from values, packaged as charts | No, separate `helm` binary | Reusable packages, third-party software, many parameters |
Kustomize: a base plus overlays
A base holds the shared manifests. Each overlay references the base and changes only what differs. Use this layout:
k8s/
base/
deployment.yaml
kustomization.yaml
overlays/
production/
kustomization.yamlThe base lists its resources:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yamlThe production overlay sets the namespace, changes the replica count, and names the image:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: shop
resources:
- ../../base
replicas:
- name: web
count: 4
images:
- name: ghcr.io/example/shop
newTag: placeholderThe images block matches any container using the image name ghcr.io/example/shop and replaces its tag. This is how the commit SHA enters the manifests without editing the Deployment file.
Using it in the workflow
Replace the kubectl set image step with two steps. The first sets the tag from the commit SHA. The second applies the result and waits for the rollout.
- name: Set image tag
working-directory: k8s/overlays/production
run: kustomize edit set image ghcr.io/example/shop="ghcr.io/example/shop:$GITHUB_SHA"
- name: Apply
run: kubectl apply -k k8s/overlays/production
- name: Wait for rollout
run: kubectl rollout status deployment/web -n shop --timeout=120skustomize edit set image changes only the checked-out copy of the overlay on the runner. Nothing is committed back to the repository. The kustomize binary is preinstalled on GitHub-hosted Ubuntu runners. To preview the output locally, run kubectl kustomize k8s/overlays/production.
Helm: templates and values
A Helm chart is a folder of templated manifests plus a values.yaml file with defaults. A template reads values such as {{ .Values.image.tag }}. A release is one installed instance of a chart. Helm records each release revision in the cluster, which gives you its own rollback.
The workflow step looks like this:
- name: Deploy with Helm
run: |
helm upgrade --install web ./chart \
--namespace shop \
--values chart/values-production.yaml \
--set image.tag="$GITHUB_SHA" \
--atomic --timeout 2m--install creates the release if it does not exist. --atomic waits for readiness and rolls back automatically if the upgrade fails. That replaces the separate rollout status and rollout undo steps.
RBAC changes
Both tools still call the Kubernetes API with your deployer identity. Kustomize applies whatever objects the overlay contains, so the Role must allow every kind in it, not just Deployments. Helm 3 stores release state in Secrets in the target namespace, so the Role also needs access to secrets there. Grant these deliberately and no wider.
Choosing between them
- Choose Kustomize when the manifests are yours and the differences are small and structural.
- Choose Helm when you install third-party charts, need many parameters, or want release history and automatic rollback.
- Mixing is allowed, but it adds complexity. Start with one.
Exercise
Your overlay sets newTag: placeholder. Why does the workflow run kustomize edit set image before kubectl apply -k, and does that step change the repository?
Show answer
The overlay must carry the real tag, the commit SHA, before it is applied, otherwise the cluster would try to pull the placeholder tag. The edit modifies only the runner's checked-out files. It does not commit or push, so the repository still contains placeholder.
Exercise
What does --atomic do in helm upgrade --install, and which kubectl commands from the previous chapters does it replace?
Show answer
It waits for the release resources to become ready and rolls back to the previous revision if the upgrade fails or times out. It replaces kubectl rollout status and kubectl rollout undo.