Operating, scaling and debugging · 6 MIN READ
Package manifests per environment with Kustomize overlays, Helm charts and GitOps
Build per-environment manifests from a shared base with Kustomize transformers and kubectl apply -k, explain Helm charts, values and releases, and choose between them and a GitOps controller.
The same application runs in dev, staging and production with different image tags, replica counts, namespaces and settings. Copying the YAML for each environment drifts within weeks. Kustomize patches a shared base without templates and is built into kubectl; Helm renders a templated chart from values and tracks every release it installs.
kubernetes · A MAP OF THE IDEA
- Plain manifests in a base
- One overlay per environment
- Render and review in CI
- apply -k or a GitOps controller
Useful when
- Each overlay states exactly what differs per environment.
- Kustomize needs no extra tool because it ships inside kubectl.
Watch out
- Labels written into selectors break immutable Deployment selectors.
- Helm values files grow until the templates are hard to review.
Implementation & operation cost: A directory per environment and a CI step that renders and diffs the manifests before they reach a cluster.
Bases and overlays
A base is a directory of plain manifests plus a kustomization.yaml that lists them. An overlay is another directory whose kustomization.yaml lists the base under resources and adds the changes for one environment. Kustomize renders base plus changes into ordinary YAML, and kubectl apply -k DIR, long form --kustomize DIR, applies the result. Nothing in the base knows about environments, and each overlay is short enough to review on one screen. The GopherForge lab terminal follows a resources entry such as ../../base too; the lab below keeps the manifests and the kustomization in one directory to stay short.
# base/kustomization.yaml lists deployment.yaml and service.yaml.
# overlays/production/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: production
namePrefix: prod-
labels:
- pairs:
env: production
images:
- name: gopherforge/web
newTag: v2
replicas:
- name: web
count: 4
configMapGenerator:
- name: web-config
literals:
- LOG_LEVEL=warnThe overlay pulls in the base, moves every object into the production namespace, renames web to prod-web, labels everything env=production, sets the image tag to v2 and runs four replicas. configMapGenerator creates a ConfigMap whose name ends in a hash of its content, so changing LOG_LEVEL renames it, updates the Deployment that refers to it and rolls the pods. kubectl kustomize overlays/production prints the rendered YAML without applying it; that command is not available in the lab.
What each transformer does
namespace sets metadata.namespace on every namespaced object; the namespace itself must exist or be one of the resources. namePrefix and nameSuffix rename objects and the references Kustomize knows about, such as a Service named in an Ingress or a ConfigMap used by a pod, but never label values, so selectors keep matching. labels adds labels to object metadata; includeSelectors: true also writes them into selectors, and the older commonLabels always does, which breaks a live Deployment because its selector is immutable. images replaces the name, tag or digest of matching containers. replicas sets the count of Deployments, ReplicaSets and StatefulSets by name. patches applies strategic-merge or JSON 6902 patches for everything else, such as resources or probes per environment. The order of fields in kustomization.yaml does not matter.
Helm: charts, values and releases
A Helm chart is a directory of templates written in Go text/template with Sprig functions, a values.yaml with defaults and a Chart.yaml with metadata and dependencies. helm upgrade --install renders the templates with your values files and --set flags, applies the result and records a release: a named, numbered revision stored as a Secret in the release's namespace. helm rollback deploys an earlier revision as a new one, and helm uninstall deletes what the release created. helm template renders locally without touching a cluster, which is how CI reviews a chart. Charts are published in chart repositories or OCI registries. The GopherForge lab terminal simulates Helm as well: after helm repo add lab https://charts.lab.example, helm create, template, install, upgrade, history, rollback and uninstall work against its cluster.
helm create web
helm template web ./web --set image.repository=gopherforge/web --set image.tag=v2
helm upgrade --install web ./web -n staging --create-namespace -f values-staging.yaml
helm history web -n staging
helm rollback web 1 -n staging
helm upgrade --install metrics-server metrics-server --repo https://kubernetes-sigs.github.io/metrics-server/ -n kube-systemhelm create scaffolds a chart whose Deployment template reads image.repository and image.tag. helm template prints the rendered manifests to read or diff. upgrade --install creates the release on the first run and upgrades it afterwards. history lists the revisions, and rollback 1 redeploys the first as a new revision. The last line installs metrics-server from its official chart repository.
Choosing Kustomize or Helm
Use Kustomize for your own services: the base is the YAML you would write anyway, overlays are short, and a reviewer sees the exact change. Use Helm to install third-party software, which is usually published as a chart, and when you distribute a configurable package to other teams. The two combine: render a chart with helm template into a base and adjust it with an overlay. Each has a typical failure. Charts grow a values file that exposes every field until the templates are hard to review, and Helm installs CRDs from a chart's crds directory but never upgrades them. Overlays grow patches that duplicate the base, and labels written into selectors break immutable Deployment selectors. In both cases render in CI and review the rendered output, not only the source.
GitOps with Argo CD or Flux
GitOps moves the apply step into the cluster. A controller such as Argo CD or Flux watches a Git repository that holds Kustomize directories or Helm releases, renders them, compares the result with the live objects and applies the difference. It reports drift when someone changes the cluster by hand, can revert it (self-heal) and can delete objects that were removed from Git (prune). CI then only builds and pushes images and commits the new tag to an overlay; nothing outside the cluster needs credentials to apply. Git history becomes the audit log, and a rollback is a revert commit. Argo CD models a deployed directory as an Application; Flux uses Kustomization and HelmRelease objects.
Try it in the lab terminal
Write a Deployment and a Service, then a kustomization.yaml that puts them in a staging namespace, prefixes their names, labels them and swaps the image tag. Apply the directory with kubectl apply --kustomize, the long form of -k, check from inside the cluster that the renamed Service reaches the new image, apply again to see that nothing changes, and remove everything the kustomization created.
kind create cluster
mkdir -p staging
cat > staging/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: gopherforge/web:v1
ports:
- containerPort: 8080
EOF
cat > staging/service.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080
EOF
cat > staging/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: staging
namePrefix: staging-
labels:
- pairs:
env: staging
resources:
- deployment.yaml
- service.yaml
images:
- name: gopherforge/web
newTag: v2
EOF
kubectl create namespace staging
kubectl apply --kustomize staging
kubectl rollout status deploy/staging-web -n staging
kubectl get deploy,svc -n staging --show-labels
kubectl get deploy staging-web -n staging -o jsonpath='{.spec.template.spec.containers[0].image}'
kubectl run probe -n staging --rm -it --image=busybox --restart=Never -- wget -qO- http://staging-web/version
kubectl apply --kustomize staging
kubectl delete --kustomize stagingThe first apply prints deployment.apps/staging-web created and service/staging-web created. --show-labels shows env=staging on both objects; without includeTemplates the pods do not get it. The jsonpath prints gopherforge/web:v2 although deployment.yaml says v1, and wget to http://staging-web/version answers v2: the renamed Service still selects app=web. The second apply reports unchanged for both objects, and delete --kustomize removes exactly what the kustomization produced.
TAKE THIS WITH YOU
Keep one base of plain manifests and one short overlay per environment, applied with kubectl apply -k. Use Helm for third-party charts, render everything in CI, and let a GitOps controller apply what Git says.
Check your understanding
Your staging overlay must run gopherforge/web:v2 while the base Deployment says v1, without copying the Deployment. What do you add to the overlay's kustomization.yaml?
Go to the source
Progress stays in this browser. No account needed.