Understanding the Workflow
When you install or upgrade a Helm chart using commands likehelm install or helm upgrade, Helm first validates the chart’s files and templates. It then renders the manifest files in their final form before applying them as resources in your Kubernetes cluster. This overall process is typically broken down into several phases:
- Verification Phase: Ensures all files and templates are correct.
- Rendering Phase: Transforms templates into deployable manifest files.
- Execution Phase: Applies these manifests to the Kubernetes cluster.
- Pre-install and Post-install Hooks: Execute during the installation.
- Pre-delete and Post-delete Hooks: Run during resource deletion.
- Pre-rollback and Post-rollback Hooks: Operate during rollback operations.
| Command | Phases Sequence |
|---|---|
| helm install | verify → render → pre-install → install → post-install |
| helm delete | verify → render → pre-delete → delete → post-delete |
| helm upgrade | verify → render → pre-upgrade → upgrade → post-upgrade |
Configuring Hooks
Consider a scenario where you have a script namedbackup.sh designed to perform a one-time database backup. In Kubernetes, instead of running this script as a continuously running pod, you can run it as a Job.
Below is an example manifest file that creates a Job using an Alpine image. This file should be placed in the chart’s templates directory alongside your other Kubernetes manifests. To ensure the Job runs as a hook (e.g., a pre-upgrade hook), you need to add specific annotations that instruct Helm to treat the file differently from standard templates.
To designate a resource as a hook, add the annotation key
helm.sh/hook with a value such as pre-upgrade. This tells Helm to execute the Job as part of the pre-upgrade process rather than a regular installation.- The annotation
"helm.sh/hook": pre-upgradesignals that the Job should run before the upgrade phase. - The
"helm.sh/hook-weight": "5"annotation controls the execution order. Hooks with lower weights execute before those with higher weights. If hooks share the same weight, they are sorted by resource kind and name. - The
"helm.sh/hook-delete-policy": hook-succeededannotation ensures that the Job resource is deleted after successfully running, which prevents potential conflicts from duplicate objects during future upgrades.
If the hook fails, the Job resource will not be deleted automatically. This can be useful for debugging, but be mindful of lingering resources that might require manual cleanup.
hook-failed: Deletes the resource even if the hook fails.before-hook-creation: Deletes any existing resource before launching a new hook.
before-hook-creation) helps avoid conflicts with duplicate resources during subsequent Helm upgrades.
By using hooks strategically, you ensure that necessary pre- and post-deployment actions are executed, making your Helm deployments more robust and reliable.
That concludes our comprehensive look at Helm chart hooks, their role in the deployment lifecycle, and how to configure them for various Kubernetes operations. Happy charting, and see you in the next article!
For further reading on Kubernetes deployments and Helm charts, check out these resources: