{"id":36256,"date":"2023-06-28T08:35:53","date_gmt":"2023-06-28T08:35:53","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=36256"},"modified":"2023-06-28T10:17:53","modified_gmt":"2023-06-28T10:17:53","slug":"helm-tutorials-complete-tutorials-of-helm-hooks","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/helm-tutorials-complete-tutorials-of-helm-hooks\/","title":{"rendered":"Helm Tutorials: Complete tutorials of Helm Hooks"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Step 1: Understanding Helm Hooks<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Helm hooks are a feature introduced in Helm 2.0 that allows you to perform actions during the release lifecycle of a Helm chart. Hooks can be used to execute scripts, run commands, or trigger other actions at specific points in the release process. Helm supports several types of hooks, including pre-install, post-install, pre-upgrade, post-upgrade, pre-delete, and post-delete hooks.<\/p>\n\n\n\n<p id=\"92df\">Hooks is a helm feature that allows helm to launch resources into the cluster in response to helm lifecycle events (such as a chart being installed). All it takes to turn a regular Kubernetes object into a helm hook is to apply an annotation that indicates which lifecycle event the object should be tied to.<\/p>\n\n\n\n<p id=\"3d93\">Helm hooks provide a way to apply certain Kubernetes resources (usually Jobs) at a particular stage of a deployment. Most developers leverage hooks to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load a ConfigMap or Secret during install before any other charts are loaded<\/li>\n\n\n\n<li>Execute a Job to back up a database before installing a new chart and a second job to restore the data in the database<\/li>\n\n\n\n<li>Run a job before deleting a release to gracefully take out a service before completely removing it<\/li>\n<\/ul>\n\n\n\n<p id=\"2f66\">Hook works like a regular template, but they have special annotations that cause helm to utilize them differently. In this section, we will cover the basic usage pattern for hooks.<\/p>\n\n\n\n<p>We can define Helm hooks that will be executed at these points in the release\u2019s lifecycle:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pre-install<\/strong>&nbsp;\u2013 Before the release is installed.<\/li>\n\n\n\n<li><strong>post-install<\/strong>&nbsp;\u2013 After the release is installed<\/li>\n\n\n\n<li><strong>pre-delete<\/strong>&nbsp;\u2013 Before the release is uninstalled.<\/li>\n\n\n\n<li><strong>post-delete&nbsp;<\/strong>\u2013 After the release is uninstalled.<\/li>\n\n\n\n<li><strong>pre-upgrade<\/strong>&nbsp;\u2013 Before the release is upgraded.<\/li>\n\n\n\n<li><strong>post-upgrade<\/strong>&nbsp;\u2013 After the release is upgraded.<\/li>\n\n\n\n<li><strong>pre-rollback<\/strong>&nbsp;\u2013 Before the release is rolled back to a previous version.<\/li>\n\n\n\n<li><strong>post-rollback<\/strong>\u00a0\u2013 After the release is rolled back to a previous version.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"85c8\">Helm Hook<\/h1>\n\n\n\n<p id=\"92df\">Hooks is a helm feature that allows helm to launch resources into the cluster in response to helm lifecycle events (such as a chart being installed). All it takes to turn a regular Kubernetes object into a helm hook is to apply an annotation that indicates which lifecycle event the object should be tied to.<\/p>\n\n\n\n<p id=\"3d93\">Helm hooks provide a way to apply certain Kubernetes resources (usually Jobs) at a particular stage of a deployment. Most developers leverage hooks to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load a ConfigMap or Secret during install before any other charts are loaded<\/li>\n\n\n\n<li>Execute a Job to back up a database before installing a new chart and a second job to restore the data in the database<\/li>\n\n\n\n<li>Run a job before deleting a release to gracefully take out a service before completely removing it<\/li>\n<\/ul>\n\n\n\n<p id=\"2f66\">Hook works like a regular template, but they have special annotations that cause helm to utilize them differently. In this section, we will cover the basic usage pattern for hooks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Creating a Helm Chart<\/h2>\n\n\n\n<p>Before diving into hooks, let&#8217;s assume you have a basic understanding of creating Helm charts. If you&#8217;re new to Helm, it&#8217;s recommended to familiarize yourself with chart creation first. You can refer to the official Helm documentation for creating a simple chart.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Adding a Pre-Install Hook<\/h2>\n\n\n\n<p>To add a pre-install hook to your Helm chart, you need to create a <code>templates<\/code> directory in your chart&#8217;s root directory (if it doesn&#8217;t exist already). Inside the <code>templates<\/code> directory, create a YAML file for your hook. For example, let&#8217;s create a file named <code>pre-install-hook.yaml<\/code>.<\/p>\n\n\n\n<p>In the <code>pre-install-hook.yaml<\/code> file, define the hook using the following structure:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">apiVersion: batch\/v1\n<span class=\"hljs-attr\">kind<\/span>: Job\n<span class=\"hljs-attr\">metadata<\/span>:\n  name: {{ include <span class=\"hljs-string\">\"mychart.fullname\"<\/span> . }}-pre-install-hook\n  <span class=\"hljs-attr\">labels<\/span>:\n    app.kubernetes.io\/managed-by: {{ .Release.Service | quote }}\n    helm.sh\/hook: pre-install\n    helm.sh\/hook-weight: <span class=\"hljs-string\">\"1\"<\/span>\n    helm.sh\/hook-<span class=\"hljs-keyword\">delete<\/span>-policy: hook-succeeded\n<span class=\"hljs-attr\">spec<\/span>:\n  template:\n    spec:\n      containers:\n        - name: {{ .Chart.Name }}\n          <span class=\"hljs-attr\">image<\/span>: busybox\n          <span class=\"hljs-attr\">command<\/span>: &#91;<span class=\"hljs-string\">'sh'<\/span>, <span class=\"hljs-string\">'-c'<\/span>, <span class=\"hljs-string\">'echo \"Running pre-install hook\"'<\/span>]\n      <span class=\"hljs-attr\">restartPolicy<\/span>: Never\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>In this example, we define a Kubernetes Job that runs a container with the <code>busybox<\/code> image and executes a simple command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Adding a Post-Install Hook<\/h2>\n\n\n\n<p>Similar to the pre-install hook, you can add a post-install hook to your chart. Create a new YAML file in the <code>templates<\/code> directory, for example, <code>post-install-hook.yaml<\/code>.<\/p>\n\n\n\n<p>In the <code>post-install-hook.yaml<\/code> file, define the hook using the following structure:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">apiVersion: batch\/v1\n<span class=\"hljs-attr\">kind<\/span>: Job\n<span class=\"hljs-attr\">metadata<\/span>:\n  name: {{ include <span class=\"hljs-string\">\"mychart.fullname\"<\/span> . }}-post-install-hook\n  <span class=\"hljs-attr\">labels<\/span>:\n    app.kubernetes.io\/managed-by: {{ .Release.Service | quote }}\n    helm.sh\/hook: post-install\n    helm.sh\/hook-weight: <span class=\"hljs-string\">\"1\"<\/span>\n    helm.sh\/hook-<span class=\"hljs-keyword\">delete<\/span>-policy: hook-succeeded\n<span class=\"hljs-attr\">spec<\/span>:\n  template:\n    spec:\n      containers:\n        - name: {{ .Chart.Name }}\n          <span class=\"hljs-attr\">image<\/span>: busybox\n          <span class=\"hljs-attr\">command<\/span>: &#91;<span class=\"hljs-string\">'sh'<\/span>, <span class=\"hljs-string\">'-c'<\/span>, <span class=\"hljs-string\">'echo \"Running post-install hook\"'<\/span>]\n      <span class=\"hljs-attr\">restartPolicy<\/span>: Never\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>This hook follows a similar structure as the pre-install hook but with a different name and the <code>helm.sh\/hook<\/code> set to <code>post-install<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Updating the Chart&#8217;s <code>hooks<\/code> Section<\/h2>\n\n\n\n<p>Now that we have defined our hooks, we need to update the chart&#8217;s <code>hooks<\/code> section in the <code>templates\/hooks.yaml<\/code> file. If the file doesn&#8217;t exist, create it.<\/p>\n\n\n\n<p>Add the following code to the <code>hooks.yaml<\/code> file:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">{{- <span class=\"hljs-keyword\">if<\/span> .Values.hooks.preInstall.enabled -}}\npreInstall:\n  - name: {{ <span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">\"mychart.fullname\"<\/span> . }}-pre-install\n    manifest: {{ <span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">\"mychart.pre\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>https:\/\/medium.com\/@pratyush.mathur\/introduction-to-helm-hooks-460c704021b<\/li>\n\n\n\n<li>https:\/\/kodekloud.com\/blog\/chart-hooks\/#<\/li>\n\n\n\n<li>https:\/\/medium.com\/@pratyush.mathur\/introduction-to-helm-hooks-460c704021b<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Understanding Helm Hooks Helm hooks are a feature introduced in Helm 2.0 that allows you to perform actions during the release lifecycle of a Helm&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-36256","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/36256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=36256"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/36256\/revisions"}],"predecessor-version":[{"id":36272,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/36256\/revisions\/36272"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=36256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=36256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=36256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}