# Helm: packaging Kubernetes apps

Stop copying YAML between environments. Package shop-web as a Helm chart, change it with values, upgrade it, watch a bad upgrade fail, roll back, and install a public chart from a repository.

Course: [Kubernetes & Helm](https://devops.rajeev.pro/courses/kubernetes), lesson 13. About 30 minutes, intermediate. Source: https://devops.rajeev.pro/courses/kubernetes/13-helm

## What you will learn

- Explain what a chart, values and a release are
- Create a chart, lint it and render it with helm template
- Install, upgrade and roll back a release and read its history
- Find and install a chart from a repository and from an OCI registry

> **Lab status.** **Verified lab:** helm create, lint and template, then install, upgrade, a failed upgrade and a rollback of the shop chart, and a public chart installed from an OCI registry, on the course's EKS cluster with Helm 4.3.1. The output shown is real. <br />**Code walkthrough:** publishing your own chart to a registry. <br />**Cloud lab:** none needed; works on kind.

By now shop-web is a Deployment, a Service, a ConfigMap, an Ingress and more. For staging you want 1 replica
and a test image; for production 3 replicas and the release tag. Copying the YAML and editing it by hand works
until the copies drift. **Helm** packages the YAML once as templates and fills in the differences from values.
It's also how most third-party software is installed on Kubernetes: the load balancer controller (lesson 5) and
the Cluster Autoscaler (lesson 10) both came from Helm charts.

## Charts

A **chart** is a folder of templates plus defaults. `helm create` makes a working starter:

```text
$ helm create shop
Creating shop
$ find shop -type f | sort
shop/.helmignore
shop/Chart.yaml
shop/templates/NOTES.txt
shop/templates/_helpers.tpl
shop/templates/deployment.yaml
shop/templates/hpa.yaml
shop/templates/httproute.yaml
shop/templates/ingress.yaml
shop/templates/service.yaml
shop/templates/serviceaccount.yaml
shop/templates/tests/test-connection.yaml
shop/values.yaml
```

`Chart.yaml` holds the chart's name and version, `values.yaml` the defaults, and `templates/` the Kubernetes
YAML with placeholders. The course chart changed only a few defaults from the starter:

```yaml
# values.yaml (changed lines)
replicaCount: 2
image:
  repository: 626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web
  tag: 'v1'
livenessProbe:
  httpGet:
    path: /healthz
    port: http
readinessProbe:
  httpGet:
    path: /healthz
    port: http
```

(plus `containerPort: 8080` in the Deployment template). Before installing anything, check and preview:

```text
$ helm lint shop
==> Linting shop
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed
$ helm template demo shop --set replicaCount=3
...
# Source: shop/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-shop
  labels:
    helm.sh/chart: shop-0.1.0
    app.kubernetes.io/name: shop
    app.kubernetes.io/instance: demo
...
spec:
  replicas: 3
```

`helm template` renders the YAML locally without touching the cluster. Make it a habit: it's how you see
exactly what a values change will do, and it's what CI should check.

## Values

Templates read values with `{{ .Values.replicaCount }}`. You override the defaults per install, from most to
least common:

- a file per environment: `helm install shop ./shop -f values-prod.yaml`
- a single value on the command line: `--set image.tag=v2`

Later sources win. Keep environment files in Git; use `--set` for the one value CI changes (the image tag).
Never put secrets in values files committed to Git (lesson 6 covers the alternatives).

> **Figure.** Left: the chart folder shop with Chart.yaml, values.yaml and templates holding deployment.yaml, service.yaml, ingress.yaml, hpa.yaml, _helpers.tpl, NOTES.txt and tests. Below it, values in priority order, later wins: values.yaml, then -f values-prod.yaml, then --set image.tag=v2. Both feed the Helm template engine, which turns .Values.replicaCount into replicas: 3. The result is plain YAML: a Deployment shop with replicas 3 and image shop-web:v2, a Service and an Ingress; helm template stops here. helm install or upgrade sends it to namespace shop as release shop: a Deployment with 3 Pods, the Service and Ingress, and a Secret sh.helm.release.v1.shop.v2 that records the revision.

## Releases, upgrades and rollbacks

An installed chart is a **release**, and every change makes a new numbered **revision**:

```text
$ helm install shop ./helm/shop --namespace shop --create-namespace --wait --timeout 4m
NAME: shop
LAST DEPLOYED: Mon Nov  9 06:39:55 2026
NAMESPACE: shop
STATUS: deployed
REVISION: 1
$ kubectl get deployment,pods -n shop
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/shop   2/2     2            2           36s
NAME                        READY   STATUS    RESTARTS   AGE
pod/shop-59757954bc-gts7r   1/1     Running   0          36s
pod/shop-59757954bc-hhvjb   1/1     Running   0          36s
$ helm upgrade shop ./helm/shop -n shop --set image.tag=v2 --set replicaCount=3 --wait --timeout 4m
Release "shop" has been upgraded. Happy Helming!
REVISION: 2
```

`--wait` makes Helm wait until the new Pods are ready before calling the upgrade a success. Now a bad release:

```text
$ helm upgrade shop ./helm/shop -n shop --set image.tag=v3-broken --set replicaCount=3 --wait --timeout 90s
level=WARN msg="upgrade failed" name=shop error="resource Deployment/shop/shop not ready. status: InProgress, message: Updated: 1/3\ncontext deadline exceeded"
Error: UPGRADE FAILED: resource Deployment/shop/shop not ready. status: InProgress, message: Updated: 1/3
context deadline exceeded
$ helm history shop -n shop
REVISION  UPDATED                   STATUS      CHART       APP VERSION  DESCRIPTION
1         Mon Nov  9 06:39:55 2026  superseded  shop-0.1.0  1.16.0       Install complete
2         Mon Nov  9 06:40:47 2026  deployed    shop-0.1.0  1.16.0       Upgrade complete
3         Mon Nov  9 06:41:25 2026  failed      shop-0.1.0  1.16.0       Upgrade "shop" failed: resource Deployment/shop/shop not ready. ...
```

Only one new Pod was created, it crashed, and the Deployment's rolling update stopped there, so the v2 Pods
kept serving. Helm marked revision 3 **failed**. Roll back to the last good one:

```text
$ helm rollback shop 2 -n shop --wait --timeout 4m
Rollback was a success! Happy Helming!
$ helm history shop -n shop
...
3         Mon Nov  9 06:41:25 2026  failed      shop-0.1.0  1.16.0       Upgrade "shop" failed: ...
4         Mon Nov  9 06:43:13 2026  deployed    shop-0.1.0  1.16.0       Rollback to 2
$ kubectl get pods -n shop -o custom-columns=POD:.metadata.name,IMAGE:.spec.containers[0].image,READY:.status.containerStatuses[0].ready
POD                     IMAGE                                                       READY
shop-7b46566b6b-624j9   626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2   true
shop-7b46566b6b-6zl7s   626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2   true
shop-7b46566b6b-7j4js   626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2   true
$ helm get values shop -n shop
USER-SUPPLIED VALUES:
image:
  tag: v2
replicaCount: 3
```

The rollback became revision 4; history is never rewritten. `helm get values` shows what's actually deployed,
the first thing to check when someone asks "what's running in production?". (`APP VERSION 1.16.0` is the
starter chart's default `appVersion`; set it in `Chart.yaml` to match your app.)

> **Figure.** Animation of the lab release shop. Revision 1, install: shop-web v1 with 2 replicas, deployed, two v1 Pods serving. Revision 2, upgrade: v2 with 3 replicas, deployed, revision 1 becomes superseded and three v2 Pods serve. Revision 3, upgrade to v3-broken: one new Pod goes into CrashLoopBackOff, Helm reports Updated 1/3 and times out, the revision is failed, and the three v2 Pods keep serving. Revision 4, helm rollback shop 2: deployed, running v2 with 3 replicas on new Pods shop-7b46566b6b, and revision 2 becomes superseded. A rollback is a new revision; history is never rewritten.

Helm stores each revision as a Secret in the release's namespace, and `helm uninstall` removes the release and
everything it created.

## Chart repositories

Charts are shared through **repositories** (an HTTP index) and, increasingly, **OCI registries**, the same
registries that hold images. Artifact Hub is the place to search public charts.

```text
$ helm repo add podinfo https://stefanprodan.github.io/podinfo
"podinfo" has been added to your repositories
$ helm search repo podinfo
NAME             CHART VERSION  APP VERSION  DESCRIPTION
podinfo/podinfo  6.17.0         6.17.0       Podinfo Helm chart for Kubernetes
$ helm install demo oci://ghcr.io/stefanprodan/charts/podinfo -n shop --set replicaCount=1 --wait --timeout 4m
Pulled: ghcr.io/stefanprodan/charts/podinfo:6.17.0
Digest: sha256:258d0da545724b4eea8de5d5e724cd9a75aab3de79a87ba8d82ec402745cea58
NAME: demo
STATUS: deployed
REVISION: 1
$ helm list -n shop
NAME  NAMESPACE  REVISION  STATUS    CHART           APP VERSION
demo  shop       1         deployed  podinfo-6.17.0  6.17.0
shop  shop       4         deployed  shop-0.1.0      1.16.0
$ helm uninstall demo -n shop
release "demo" uninstalled
```

The OCI install needs no `repo add`; the digest identifies exactly what was pulled. Before installing a public
chart, read its values (`helm show values`) and run `helm template`, as you would review any code you're about
to run in your cluster. Pin the chart version with `--version` so a new release doesn't arrive by surprise.

## Mission: Ship, break and roll back with Helm

Install the shop chart, upgrade it to v2 with 3 replicas, upgrade to v3-broken with --wait and a 90 second timeout, then roll back to the last good revision. Record the history and the deployed values.

- You need: The helm/shop chart
- You'll have: ~/helm.txt

```sh
$ helm lint ./shop && helm template demo ./shop | grep -E "kind:|replicas:"
$ helm install shop ./shop -n shop --create-namespace --wait
$ helm upgrade shop ./shop -n shop --set image.tag=v2 --set replicaCount=3 --wait
$ helm upgrade shop ./shop -n shop --set image.tag=v3-broken --set replicaCount=3 --wait --timeout 90s
$ helm rollback shop 2 -n shop --wait
$ helm history shop -n shop > ~/helm.txt
$ helm get values shop -n shop >> ~/helm.txt
$ helm uninstall shop -n shop
```

Expect four revisions: deployed, superseded, failed and "Rollback to 2".

## Try it

- Run `helm create shop` and look at the files it makes.
- Run `helm template demo shop --set replicaCount=3` and find the replicas line.
- Install the chart, upgrade it to v2 and read `helm history`.
- Upgrade to v3-broken with --wait and roll back.
- Do the mini mission and keep `~/helm.txt`.

## Quiz

1. You upgrade a release with --wait and the new Pods never become ready. What does Helm do?
   - Marks the upgrade successful anyway
   - **Marks the revision failed after the timeout; the old Pods keep serving until you roll back** (answer)
   - Deletes the release
   Why: In the lab revision 3 was marked failed with "Updated 1/3", and helm rollback shop 2 made revision 4.

2. Where do you put the image tag that differs between staging and production?
   - Hard-coded in templates/deployment.yaml
   - **In values (values.yaml defaults, overridden with -f or --set per environment)** (answer)
   - In Chart.yaml
   Why: Templates stay the same everywhere; values change per environment.

3. What does helm rollback shop 2 create?
   - It deletes revisions 3 and up
   - **A new revision (4) with revision 2's configuration** (answer)
   - Nothing, it only prints the old values
   Why: History is append-only, so you can see that a rollback happened and roll forward again.

## Resources

- [Helm documentation](https://helm.sh/docs/) (Helm)
- [Chart template guide](https://helm.sh/docs/chart_template_guide/) (Helm)
- [Use OCI-based registries](https://helm.sh/docs/topics/registries/) (Helm)
- [Artifact Hub](https://artifacthub.io/) (Artifact Hub)
