Mastering Helm Hooks: Control Your Release Lifecycle
Helm hooks exist to give you, the chart developer, control over the release lifecycle. They allow you to perform operations at strategic points, such as before or after an installation. This is crucial for managing complex deployments where certain tasks must be completed before or after the main resources are created. For example, during a helm install, the default lifecycle can be extended by implementing hooks that execute at specific times, like pre-install and post-install.
To configure hooks, you use annotations in your resource definitions. The key parameters include helm.sh/hook, which defines the resource as a hook, helm.sh/hook-weight, which sets the execution order of hooks, and helm.sh/hook-delete-policy, which determines when to delete the hook resources. For instance, a post-install job can be defined with a weight to ensure it runs after other hooks. Be aware that the crd-install hook has been removed in Helm 3, and this is a blocking operation, meaning the Helm client will pause until the job completes. Additionally, if you create resources in a hook, you cannot rely on helm uninstall to clean them up, which can lead to resource clutter if not managed properly.
In production, understanding the implications of hook execution is vital. You need to consider the blocking nature of hooks and the potential for resource leakage if not handled correctly. Starting from Helm 3.2.0, hooks with the same weight are installed in the same order as non-hook resources, which can simplify your deployment logic. Always test your hooks thoroughly to ensure they behave as expected in your specific environment.
Key takeaways
- →Define hooks using the annotation `helm.sh/hook` to control lifecycle events.
- →Use `helm.sh/hook-weight` to establish a deterministic execution order for your hooks.
- →Implement `helm.sh/hook-delete-policy` to manage the lifecycle of hook resources effectively.
- →Be aware that hooks can block the Helm client, affecting deployment speed.
- →Remember that resources created in hooks won't be cleaned up by `helm uninstall`.
Why it matters
In production, effective use of Helm hooks can streamline complex deployments and ensure that necessary operations are completed at the right times. This control can prevent failures and improve the reliability of your applications.
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: The Key to Efficient Kubernetes Deployments
Helm chart repositories are essential for managing your Kubernetes applications effectively. By understanding how to create and work with these repositories, you can streamline your deployments and maintain a clean architecture. Learn how to generate an index.yaml file and package your charts for optimal organization.
Mastering Helm Charts: Templates That Work in Production
Helm charts simplify Kubernetes deployments, but mastering templates is key to unlocking their full potential. When Helm evaluates a chart, it processes files in the templates/ directory, which can lead to powerful configurations. Dive in to learn how to leverage this for your applications.
Mastering Helm: The Essential Tool for Kubernetes Package Management
Helm is your go-to solution for managing applications in Kubernetes, streamlining deployments and updates. With Helm, you can easily install, upgrade, and manage your applications using charts, which package all necessary resources. Dive into how it works and what you need to watch out for in production.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.