Sub topic 7
Hooks and the release lifecycle
This chapter assumes you know Helm 3 basics (install, upgrade, uninstall, release), Kubernetes Jobs and Pods, and how templates in templates/ render. It explains what hooks are and when Helm runs them. Ordering, cleanup, and pitfalls come in the next two chapters.
What a hook is
A hook is an ordinary Kubernetes manifest in templates/ that carries a helm.sh/hook annotation. Helm does not treat it as part of the normal release manifest. It creates the hook resource at a specific point in the release lifecycle and, for Jobs and Pods, waits for it to finish before it continues.
Typical uses are database migrations before an upgrade, seeding data after an install, and cleanup or backup tasks before a delete.
Hook events
The annotation value names one or more events. Separate several events with commas.
| Event | When it runs |
|---|---|
| `pre-install` | After templates render, before any resource is created |
| `post-install` | After all resources are loaded |
| `pre-upgrade` | After templates render, before any resource is updated |
| `post-upgrade` | After all resources are upgraded |
| `pre-delete` | Before any resource is deleted |
| `post-delete` | After all resources are deleted |
| `pre-rollback` | After templates render, before any resource is rolled back |
| `post-rollback` | After all resources are rolled back |
| `test` | When you run `helm test` |
How hooks fit into the lifecycle
Take helm install. Helm renders the templates, runs the pre-install hooks, creates the regular resources, then runs the post-install hooks. Helm waits for each hook to complete before it moves to the next step. If a pre-install Job fails, Helm stops and the chart's regular resources are never created.
Upgrade, delete, and rollback follow the same pattern with their own event names.
A worked example: a migration Job
This Job runs a database migration before every install and upgrade.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "webapp.fullname" . }}-db-migrate
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
backoffLimit: 0
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
command: ["./migrate", "up"]Read the annotations one at a time:
helm.sh/hooklists the events. This Job runs on bothpre-installandpre-upgrade.helm.sh/hook-weightorders hooks that share an event. Lower numbers run first. The annotation value must be a quoted string.helm.sh/hook-delete-policysays when Helm removes the hook resource. Here it deletes any old copy before creating a new one, and deletes the Job once it succeeds.
The next chapter covers weights and delete policies in detail.
Hooks are not part of the release
Helm tracks hook resources separately from the resources the release manages. Three consequences follow:
helm uninstalldoes not delete hook resources by default. Without a delete policy, a finished Job stays in the cluster.- A second install or upgrade can fail with an "already exists" error if the previous hook object is still there and has the same name. The
before-hook-creationpolicy prevents this. helm templaterenders hook manifests along with everything else. Check thehelm.sh/hookannotation in the output to tell them apart.
Seeing hooks in practice
You can render the chart and look at the hook manifests before you install it.
helm template demo ./webapp --show-only templates/db-migrate-job.yamlDuring a real install, watch the hook Job in a second terminal.
helm install demo ./webapp --wait &
kubectl get jobs --watchIf a hook fails, helm install prints the error and the release status is failed. Inspect the Job's Pod logs with kubectl logs job/demo-webapp-db-migrate.
Exercise
A chart has a Job with the annotation "helm.sh/hook": post-install. During helm install, the Job's container exits with a non-zero code. The Deployment and Service from the same chart were already created. What does Helm do with the release, and are the Deployment and Service removed?
Show answer
Helm waits for the post-install hook and sees it fail, so the install is reported as failed and the release is marked failed. The regular resources were created before the post-install hook ran, and Helm does not roll them back automatically, so the Deployment and Service remain in the cluster. You can fix the problem and run helm upgrade, or run helm uninstall to remove the release.
Exercise
You add a pre-upgrade Job named webapp-migrate with no delete policy. The first upgrade works. The second upgrade fails with an "already exists" error. Why, and which annotation fixes it?
Show answer
Helm does not manage hook resources as part of the release, so the Job from the first upgrade is still in the cluster with the same name. Adding "helm.sh/hook-delete-policy": before-hook-creation makes Helm delete the old Job before it creates the new one.