Skip to Content

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 , founding engineer running Kubernetes in production. Updated .

chart: podinfoa packagetemplates/values.yamlChart.yaml--set replicas=3helm installDeploymentServiceIngressrendered YAMLv1deployedv2failedv3rollback to v1
The chapter in one pictureA chart is templates plus values. Every install or upgrade is a numbered release you can roll back.

By the end you can

0 of 5

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

tree
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 install
templates/deployment.yaml (excerpt)
apiVersion: 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

templates/deployment.yaml (excerpt)
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-prod.yaml
helm template → rendered

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

Terminal
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

  1. Engineerpull request
  2. CIbuild and test
  3. Deploy repodesired state
  4. Argo CDin the cluster
  5. Clusterlive state

Step 0 of 6

Press Play, or step through with Next.

application.yaml
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

    Live lab · no install

    Build and ship a chart

    kind-learn
    1. Tasks0/6
    2. Scaffold a chart

      Its values file appears in the Files tab, ready to edit.

      helm create web-chart
    3. Render it without installing

      Read what Helm will send before you send it.

      helm template demo ./web-chart | head -n 30
    4. Install a dev release
      helm install web-dev ./web-chart -n dev --create-namespace
      helm list -A
    5. Install 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 prod
    6. Ship 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 30s

      Revision 2 is marked failed, and the rolling update kept the old pods serving.

    7. 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
    8. Lab complete. Keep experimenting, or press Reset to start again.
    ↑↓ history · Tab complete · Ctrl+C stop
    Cluster map live
    • ready
    • starting / not ready
    • failing
    • terminating
    Prefer your own machine? The same steps on a local cluster
    Terminal
    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:

    values-prod.yaml
    replicaCount: 3
    resources:
      requests:
        cpu: 50m
        memory: 64Mi
      limits:
        memory: 128Mi
    Terminal
    helm 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           20s

    Mini 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
    Terminal
    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

    1. 1. What is the right way to give production more replicas than dev with Helm?
    2. 2. What does helm template do?
    3. 3. Why add a checksum of the ConfigMap as a pod annotation?
    4. 4. With Argo CD and selfHeal on, someone runs kubectl scale by hand in prod. What happens?

    Further reading