Sub topic 9
Hook pitfalls and debugging
This chapter assumes you know how hooks are declared with the helm.sh/hook annotation and how hook-weight and hook-delete-policy work. Here you will see the problems that show up most often and how to find their cause.
Hooks are not part of the release manifest
Helm creates hook resources, but it does not manage them as part of the release. helm uninstall does not remove them, and helm upgrade does not track them. If you do not set a delete policy, hook resources stay in the cluster after they finish.
Two things follow from this:
- A leftover hook Job blocks the next run. Kubernetes rejects a second Job with the same name, and the release fails with an "already exists" error.
- Leftover Pods and Jobs keep using cluster resources until you delete them by hand.
The default delete policy is before-hook-creation. It removes the old hook resource just before a new one is created. Setting it explicitly makes your intent clear, and adding hook-succeeded cleans up after a good run.
Common pitfalls
| Symptom | Likely cause | Fix |
|---|---|---|
| `helm install` hangs, then times out | A hook Job never finishes, for example a crash loop or a missing image | Set `backoffLimit` and a timeout, then inspect the Job |
| "already exists" error on upgrade | An earlier hook Job was kept and its name did not change | Add `before-hook-creation` to the delete policy |
| Hook runs before the database exists | A `pre-install` hook depends on a resource that is created later | Use `post-install`, or run a hook that waits for the dependency |
| Failed hook logs are gone | `hook-failed` deletes the resource on failure | Remove `hook-failed` while debugging |
| Hook runs twice or in the wrong order | Weights are equal or compared as text | Quote the weight and use distinct integers |
A failed hook blocks the release
Helm waits for each hook to complete before it moves on. Pods and Jobs count as complete when they succeed. If a hook fails, the whole operation fails. A failed pre-upgrade hook means the upgrade never applies, and the release is marked failed.
This protects you, but it can also lock you out. A pre-delete hook that always fails stops helm uninstall from finishing. Keep hooks small and idempotent, which means running them twice gives the same result as running them once. Give every hook Job a backoffLimit so a bad hook fails quickly instead of retrying for a long time.
Debugging a hook
Work from the cheapest check to the most invasive one.
- Render the chart and confirm the annotations are what you expect.
- Run the release with debug output.
- Look at the hook resource and its logs directly.
helm template demo ./webapp --show-only templates/db-migrate-job.yaml
helm install demo ./webapp --debug --timeout 2m
kubectl get jobs,pods -l app.kubernetes.io/instance=demo
kubectl describe job demo-webapp-db-migrate
kubectl logs job/demo-webapp-db-migratehelm template shows hooks in its output, so you can check the annotations without touching the cluster. If you install with --debug, Helm prints each hook it creates and waits for. If the install fails, kubectl describe shows events such as image pull errors, and kubectl logs shows what your command printed before it exited.
To keep the failed Pod for inspection, drop hook-failed from the delete policy for that run:
annotations:
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": before-hook-creationWith only before-hook-creation, a failed Job stays in place until the next run replaces it, so you can read its logs.
Recovering from a stuck release
If a hook failure leaves a resource behind, delete it and retry:
kubectl delete job demo-webapp-db-migrate
helm upgrade demo ./webappIf the release is stuck in a pending-upgrade state, run helm history demo to see the revisions, then helm rollback demo <revision> to return to a good one. Fix the hook before you upgrade again.
Exercise
A pre-upgrade migration Job fails with an image pull error. The delete policy is before-hook-creation,hook-failed. You want to read the Pod events and logs. What do you change, and what happens to the release meanwhile?
Show answer
Remove hook-failed from the delete policy so the failed Job and Pod stay in the cluster, then rerun and inspect them with kubectl describe and kubectl logs. While the hook fails, Helm does not apply the upgrade, and the release is marked failed. The next run removes the old Job because of before-hook-creation.