Kubernetes Field Guide · Chapter 12 of 16
Helm and GitOps: packaging and releasing apps
Seven environments means seven copies of almost the same YAML, drifting apart. Helm turns them into one chart plus a small values file per environment, and records every release so you can roll back. GitOps then makes Git the only way changes reach the cluster.
- 35 min
- Advanced
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
Why Helm
By now one app is a Deployment, a Service, an Ingress, an HPA, a ConfigMap and a PodDisruptionBudget. Multiply that by dev, staging and production, and copy-pasted YAML starts to drift: someone raises a limit in production and forgets staging.
Helm packages those files as a chart: templates with placeholders, plus values that fill them in. It also tracks each install as a release with numbered revisions, and installs community charts (Traefik in chapter 8, Prometheus in chapter 14) with one command.
Chart anatomy
web-chart/
├── Chart.yaml # name, version, appVersion
├── values.yaml # defaults
├── values.schema.json # optional: validates values
└── templates/
├── _helpers.tpl # named snippets: names, labels
├── deployment.yaml
├── service.yaml
├── ingress.yaml
├── hpa.yaml
└── NOTES.txt # printed after installapiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "web-chart.fullname" . }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
template:
metadata:
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
resources:
{{- toYaml .Values.resources | nindent 12 }}- Filled from values.yaml or your overrides.
- Leave replicas out when an HPA manages them, so every upgrade does not reset the count.
- Hash of the ConfigMap: changing config rolls the pods (chapter 6).
- Copies a whole block from values, indented to fit.
- If no tag is given, use the app version from Chart.yaml.
Values per environment
One chart, a values file per environment. Switch environments and compare the rendered output:
Interactive
One chart, three environments
replicas: {{ .Values.replicas }}
image: shop:{{ .Values.image.tag }}
resources:
requests:
cpu: {{ .Values.resources.cpu }}
memory: {{ .Values.resources.memory }}
{{- if .Values.autoscaling.enabled }}
# + HorizontalPodAutoscaler (min 4, max 12)
{{- end }}
host: {{ .Values.ingress.host }}Values merge in order: the chart's values.yaml, then each -f file left to right, then --set flags. Keep secrets out of values files; reference Secrets synced by External Secrets instead (chapter 6).
Releases, upgrades and rollbacks
helm upgrade --install shop ./shop -n prod -f values-prod.yaml --wait --timeout 5m
helm history shop -n prod
helm rollback shop 3 -n prod
upgrade --install- Installs the first time and upgrades after. Use it everywhere, especially in CI.
--wait- Waits until pods are ready, so a broken release fails the pipeline. Add --atomic (Helm 3) or --rollback-on-failure (Helm 4) to roll back automatically on failure.
rollback shop 3- Re-applies revision 3 as a new revision. History is never rewritten.
Helm stores each revision as a Secret in the release's namespace, so the cluster itself knows what is installed.
GitOps with Argo CD
So far you run commands against the cluster. With GitOps, a controller in the cluster watches a Git repository and applies whatever it says. Merging a pull request is the deploy; reverting it is the rollback; nobody needs production credentials on their laptop.
Step through it
A release with GitOps: merge is the deploy
- Engineerpull request
- CIbuild and test
- Deploy repodesired state
- Argo CDin the cluster
- Clusterlive state
Step 0 of 6
Press Play, or step through with Next.
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: shop-prod
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/example/deploy.git
path: charts/shop
targetRevision: main
helm:
valueFiles: [values-prod.yaml]
destination:
server: https://kubernetes.default.svc
namespace: prod
syncPolicy:
automated:
prune: true
selfHeal: true- Where the desired state lives: a chart in Git, with the prod values file.
- Resources removed from Git are removed from the cluster.
- Manual changes in the cluster are reverted to match Git.
Try it. Change production the right way and the wrong way, with self-heal on and off:
Interactive · GitOps
Git is the source of truth. What happens when someone goes around it?
Git: main
Argo CD
Cluster
Argo CD and Flux are the two common GitOps tools. A typical split: CI builds and tests the image, then commits the new tag to the deploy repository; the GitOps controller rolls it out.
Hands-on: build a chart
The lab is docked at the bottom of your screen.
Live lab · no install
Build and ship a chart
- Tasks0/6
Scaffold a chart
Its values file appears in the Files tab, ready to edit.
helm create web-chartRender it without installing
Read what Helm will send before you send it.
helm template demo ./web-chart | head -n 30Install a dev release
helm install web-dev ./web-chart -n dev --create-namespace helm list -AInstall prod from the same chart
values-prod.yaml overrides the replica count and resources. One chart, two environments.
helm upgrade --install web-prod ./web-chart -n prod --create-namespace -f values-prod.yaml --wait kubectl get deploy -n prodShip a release that cannot start
The tag does not exist. --wait makes Helm report the failure instead of leaving it half-done.
helm upgrade web-prod ./web-chart -n prod -f values-prod.yaml --set image.tag=does-not-exist --wait --timeout 30sRevision 2 is marked failed, and the rolling update kept the old pods serving.
Read the history and roll back
helm history web-prod -n prod helm rollback web-prod 1 -n prod --wait helm history web-prod -n prod- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
helm create web-chart
helm template demo ./web-chart | head -n 30
helm install web-dev ./web-chart -n dev --create-namespace
helm list -A
helm create- Scaffolds a working chart that runs nginx, with every common template.
helm template- Renders without installing. Read it before every first install.
Save the production overrides as values-prod.yaml and install a second release from the same chart:
replicaCount: 3
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
memory: 128Mihelm upgrade --install web-prod ./web-chart -n prod --create-namespace -f values-prod.yaml --wait
kubectl get deploy -n dev
kubectl get deploy -n prod
NAME READY UP-TO-DATE AVAILABLE AGE
web-dev-web-chart 1/1 1 1 1m
NAME READY UP-TO-DATE AVAILABLE AGE
web-prod-web-chart 3/3 3 3 20sMini mission
Fail a release, then roll it back
Upgrade web-prod to an image tag that does not exist, with a 60-second timeout. Then use the history to put production back.
Reveal one safe solution
helm upgrade web-prod ./web-chart -n prod -f values-prod.yaml --set image.tag=does-not-exist --wait --timeout 60s
helm history web-prod -n prod
helm rollback web-prod 1 -n prod --wait
helm history web-prod -n prod
The upgrade fails after 60 seconds and revision 2 is marked failed, while the rolling update kept the old pods serving. The rollback creates revision 3 with revision 1's manifests. Clean up with helm uninstall web-dev -n dev and helm uninstall web-prod -n prod.
Check yourself
Quiz
0 of 4 answered