Mastering Helm Hooks: Control Your Release Lifecycle
Helm hooks exist to give chart developers the power to intervene at key moments during a release's lifecycle. This capability is crucial for ensuring that necessary operations, such as database migrations or configuration adjustments, are executed at the right time. For instance, during a helm install, the default lifecycle is straightforward: the user runs the command, Helm processes the templates, and resources are loaded into Kubernetes. However, by implementing hooks like pre-install and post-install, you can modify this flow to include waiting for certain conditions to be met before proceeding.
To configure hooks, you use specific annotations in your Kubernetes resource definitions. The key parameters include helm.sh/hook, which defines the resource as a hook, and helm.sh/hook-weight, which allows you to set a deterministic order for execution. For example, a job can be defined as a post-install hook with a weight of -5, ensuring it runs after other hooks with higher weights. Additionally, the helm.sh/hook-delete-policy annotation determines when to clean up the hook resources, which is essential for managing your cluster's state effectively. Be cautious, though: if you create resources in a hook, you cannot rely on helm uninstall to remove them, as noted in the removal of the crd-install hook in Helm 3.
In production, understanding the nuances of Helm hooks is vital. They can streamline your deployment processes, but they also introduce complexity. Always test your hooks thoroughly to avoid unexpected behaviors. Remember that the version you're using can impact available features, so keep an eye on updates and changes in Helm's behavior as you upgrade.
Key takeaways
- →Define hooks using the annotation `helm.sh/hook` to control lifecycle events.
- →Set hook weights with `helm.sh/hook-weight` for deterministic execution order.
- →Implement hook deletion policies using `helm.sh/hook-delete-policy` to manage resource cleanup.
- →Be aware that resources created in hooks won't be removed by `helm uninstall`.
Why it matters
In production, the ability to control the release lifecycle with hooks can prevent downtime and ensure that critical operations are performed in the right sequence, enhancing overall reliability.
Code examples
1apiVersion:batch/v1
2kind:Job
3metadata:
4 name: "{{ .Release.Name }}"
5 labels:
6 app.kubernetes.io/managed-by: {{.Release.Service | quote}}
7 app.kubernetes.io/instance: {{.Release.Name | quote}}
8 app.kubernetes.io/version: {{.Chart.AppVersion}}
9 helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
10 annotations:
11 "helm.sh/hook": post-install
12 "helm.sh/hook-weight": "-5"
13 "helm.sh/hook-delete-policy": hook-succeeded
14spec:
15 template:
16 metadata:
17 name: "{{ .Release.Name }}"
18 labels:
19 app.kubernetes.io/managed-by: {{.Release.Service | quote}}
20 app.kubernetes.io/instance: {{.Release.Name | quote}}
21 helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
22 spec:
23 restartPolicy: Never
24 containers:
25 - name: post-install-job
26 image: "alpine:3.3"
27 command: ["/bin/sleep", "{{default "10" .Values.sleepyTime}}"]annotations:
"helm.sh/hook": post-install,post-upgradeannotations:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeededWhen NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsUnified observability — logs, uptime monitoring, and on-call in one place. Used by 50,000+ engineering teams to ship faster and sleep better.
Try Better Stack free →Mastering Helm Chart Repositories: A Practical Guide
Helm chart repositories are essential for sharing and managing your Kubernetes applications. Learn how to create a repository and generate the crucial index.yaml file that keeps everything organized.
Creating Your First Helm Chart: Templates and ConfigMaps
Dive into Helm charts and learn how to create your first template. By understanding how Helm processes templates, you can streamline your Kubernetes deployments effectively.
Mastering Helm: Efficient Package Management for Kubernetes
Helm is your go-to tool for managing Kubernetes applications, simplifying deployments and updates. With Helm, you can easily install a chart, which is a package containing all necessary resource definitions. This article dives into how Helm works and what you need to know to leverage it effectively in production.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.