<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Helm on avni.sh</title>
    <link>http://www.avni.sh/tags/helm/</link>
    <description>Recent content in Helm on avni.sh</description>
    <image>
      <title>avni.sh</title>
      <url>http://www.avni.sh/cover.webp</url>
      <link>http://www.avni.sh/cover.webp</link>
    </image>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Tue, 16 May 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="http://www.avni.sh/tags/helm/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Helm Charts</title>
      <link>http://www.avni.sh/posts/cloud-native/helm-charts/</link>
      <pubDate>Tue, 16 May 2023 00:00:00 +0000</pubDate>
      <guid>http://www.avni.sh/posts/cloud-native/helm-charts/</guid>
      <description>Helm is a package manager for Kubernetes</description>
      <content:encoded><![CDATA[<p>Package managers like <code>dnf</code> and <code>apt</code> increase the convenience of installing, updating, and maintaining applications on operating systems. For developers, a package manager provides a standardized way of packaging and distributing their applications.</p>
<p><strong>Helm</strong> is a package manager for Kubernetes. It is implemented in Go and installed as a binary <code>helm</code>. It interacts with the Kubernetes cluster using Kubernetes API.</p>
<h1 id="charts">Charts</h1>
<p>Helm distributes Kubernetes-based applications in a format called <strong>Chart</strong>.<br>
Charts can deploy all kinds of Kubernetes resources such as Deployments, Pods, Services, Persistent Volumes, etc.</p>
<h2 id="structure-of-a-helm-chart">Structure of a Helm Chart</h2>
<p>Chart&rsquo;s directory contains the following files and sub-directories:</p>
<ul>
<li><code>Chart.yaml</code> contains the chart&rsquo;s metadata</li>
<li><code>charts/</code> directory contains dependencies (other charts)</li>
<li><code>crds/</code> directory contains the custom resources required by the chart</li>
<li><code>templates/</code> stores the templates for Kubernetes resources</li>
<li><code>values.yaml</code> stores the configuration values for templates</li>
</ul>
<h3 id="chartyaml"><code>Chart.yaml</code></h3>
<p>The chart&rsquo;s metadata includes</p>
<ul>
<li>Chart&rsquo;s name</li>
<li>Application&rsquo;s version</li>
<li>Chart&rsquo;s type (<code>application</code> or <code>library</code>)
<ul>
<li><code>application</code> charts package Kubernetes applications that could be installed directly on the cluster.</li>
<li><code>library</code> charts are used by other charts to extend their functionality.</li>
</ul>
</li>
<li>Compatible Kubernetes versions</li>
<li>Keywords related to the chart, its homepage, and link to the chart&rsquo;s source code</li>
<li>Dependencies</li>
<li>Contact information of chart&rsquo;s maintainers</li>
</ul>
<h3 id="templates-and-values"><code>templates</code> and <code>values</code></h3>
<p>Resource templates are based on the <em>Go template</em> convention. Example of a Deployment resource template</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">apiVersion</span><span class="p">:</span><span class="w"> </span><span class="l">apps/v1</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">Deployment</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">metadata</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">nginx-deployment</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">labels</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">app</span><span class="p">:</span><span class="w"> </span><span class="l">nginx</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">spec</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">replicas</span><span class="p">:</span><span class="w"> </span>{{<span class="w"> </span><span class="l">.Values.replicaNum }}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">selector</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">matchLabels</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">app</span><span class="p">:</span><span class="w"> </span><span class="l">nginx</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">template</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">metadata</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">labels</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">app</span><span class="p">:</span><span class="w"> </span><span class="l">nginx</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">spec</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">containers</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span>{{<span class="w"> </span><span class="l">.Values.containerName }}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span>{{<span class="w"> </span><span class="l">.Values.containerImage }}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">ports</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span>- <span class="nt">containerPort</span><span class="p">:</span><span class="w"> </span>{{<span class="w"> </span><span class="l">.Values.containerPort }}</span><span class="w">
</span></span></span></code></pre></div><p>The values to be substituted in this template will be provided from <code>values.yaml</code> file in the chart&rsquo;s root directory. Example of a <code>values.yaml</code> file for the template</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">replicaNum</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;3&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">containerName</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;nginx-test&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">containerImage</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;docker.io/library/nginx:1.14.2&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">containerPort</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;80&#34;</span><span class="w">
</span></span></span></code></pre></div><p>The structure of the <code>values.yaml</code> file is defined in <code>values.schema.json</code>. Chart developers can also define the datatype of each value. Example of a <code>values.schema.json</code> file</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;$schema&#34;</span><span class="p">:</span> <span class="s2">&#34;https://json-schema.org/draft-07/schema#&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;properties&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;replicaNum&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;string&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;containerName&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;string&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;containerImage&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;string&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;containerPort&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;string&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;required&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;replicaNum&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;containerName&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;containerImage&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;containerPort&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">],</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;title&#34;</span><span class="p">:</span> <span class="s2">&#34;Values&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;object&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><h3 id="crds"><code>crds</code></h3>
<p>Custom Resources (CRs) are installed before any other resource is created by the helm chart. Some limitations imposed on the CRs created by the helm charts:</p>
<ul>
<li>YAML manifests of CRs could not be templated for values.</li>
<li>CRs would not be reinstalled with a chart.</li>
<li>Upgrades and rollbacks do not impact the CRs installed by the chart.</li>
<li>Helm doesn&rsquo;t delete CRs upon uninstallation.</li>
</ul>
<h1 id="chart-repository">Chart Repository</h1>
<p>Helm charts are distributed through HTTP servers called <strong>Repositories</strong>. The metadata of available charts in the repository is stored in <code>index.yaml</code>.</p>
<p>A repository could be added using</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm repo add jenkins-x https://storage.googleapis.com/chartmuseum.jenkins-x.io 
</span></span></code></pre></div><p>Here <code>jenkins-x</code> is the name of the helm chart repository and <code>https://storage.googleapis.com/chartmuseum.jenkins-x.io</code> is the link to the repository.</p>
<p>Once a repository is added, it is recommended to update the information of available charts in the local cache.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm repo update
</span></span></code></pre></div><p>The <code>repo</code> command also provides <code>list</code> and <code>remove</code> for listing and removing repositories respectively.</p>
<h1 id="searching-helm-charts-in-repositories-and-artifacthub">Searching Helm Charts in Repositories and ArtifactHub</h1>
<p>To search a helm chart within the added repositories the <code>search</code> command could be used</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm search repo tekton
</span></span></code></pre></div><p><a href="https://artifacthub.io/" target="_blank">ArtifactHub</a> is a web platform that provides access to multiple Kubernetes-based applications including helm charts. <code>helm search</code> can also be used for searching packages in ArtifactHub</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm search hub tekton
</span></span></code></pre></div><h1 id="installing-helm-charts">Installing Helm Charts</h1>
<p>Each instance of a helm chart installation is called a <strong>Release</strong>.</p>
<p>A release will be created after executing the following command.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm install tekton-pipelines jenkins-x/tekton
</span></span></code></pre></div><p>Here, <code>tekton-pipelines</code> is the name of the release and <code>jenkins-x/tekton</code> is the name of the helm chart to be installed.</p>
<p>To install a local helm chart, the chart name will be substituted with the path to the chart&rsquo;s directory or archive</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm install test-helm-chart ./test-helm-chart
</span></span></code></pre></div><p>Configuration values are passed from a file using the <code>--values</code> flag.</p>
<p>During development, if developers want to inspect the manifests created during release and avoid installing the chart on a cluster then they can run the <code>install</code> command with <code>--debug</code> and <code>--dry-run</code> flags.</p>
<h1 id="upgrading-and-rolling-back-releases">Upgrading and Rolling Back Releases</h1>
<p>Helm performs the least invasive upgrades on chart releases i.e. updates only the changed resources. The <code>upgrade</code> command is used to upgrade a chart release</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm upgrade -f newValues.yaml test-helm-chart ./test-helm-chart
</span></span></code></pre></div><p>Release <code>test-helm-chart</code> will be updated with values in <code>newValues.yaml</code>.</p>
<p>On every upgrade, the chart release&rsquo;s <strong>revision number</strong> will be incremented by 1. To view the history of releases with their respective revision numbers</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm <span class="nb">history</span> test-helm-chart
</span></span></code></pre></div><p>For some reason, if the chart upgrade doesn&rsquo;t go as planned it could be rolled back to a previous revision number.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm rollback test-helm-chart <span class="m">1</span>
</span></span></code></pre></div><p>will rollback the <code>test-helm-chart</code> release to its first revision.</p>
<h1 id="uninstalling-helm-chart">Uninstalling Helm Chart</h1>
<p>The <code>uninstall</code> command will remove all the resources created by the helm chart release from the Kubernetes cluster (except CRs). It will also wipe the history of the release, to retain history the <code>--keep-history</code> flag could be used with the command.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm uninstall test-helm-chart
</span></span></code></pre></div><h1 id="example-of-a-helm-chart-grafana-helm-chart">Example of a Helm Chart: Grafana Helm Chart</h1>
<p>Grafana is an analytics tool that provides utilities for creating dashboards with graphs and alerts. Using Grafana developers can create dashboards for monitoring services, presenting business metrics, etc.</p>
<p><a href="https://artifacthub.io/packages/helm/grafana/grafana" target="_blank">Grafana&rsquo;s Helm Chart</a> will perform the following changes to the Kubernetes cluster</p>
<ul>
<li>Create pods and other workload resources required by Grafana</li>
<li>Create services and security policies</li>
<li>Create ConfigMaps and Secrets for pods</li>
<li>Create PersistentVolumes</li>
<li>Create RBAC resources</li>
</ul>
<h1 id="creating-helm-charts">Creating Helm Charts</h1>
<p>The boilerplate code for helm charts is generated using <code>create</code> command.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm create test-helm-chart
</span></span></code></pre></div><p>will create a directory <code>test-helm-chart</code> containing basic chart resources (<code>Chart.yaml</code>, <code>charts/</code>, <code>templates/</code>, <code>values.yaml</code>, etc.).</p>
<h1 id="testing-a-helm-chart">Testing a Helm Chart</h1>
<p>Developers can define tests for their helm charts in the <code>templates</code> directory (or <code>templates/tests</code> for better organization).</p>
<p>Tests are defined as Kubernetes Pods with the following annotation.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="w">  </span><span class="nt">annotations</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">&#34;helm.sh/hook&#34;: </span><span class="l">test</span><span class="w">
</span></span></span></code></pre></div><p>Test pods should finish with exit code <code>0</code> to be marked successful.</p>
<p>To execute tests defined with a release</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm <span class="nb">test</span> test-helm-chart
</span></span></code></pre></div><h1 id="packaging-helm-charts">Packaging Helm Charts</h1>
<p>Charts are packaged as GZIP compressed archives with extension <code>tgz</code>. The <code>package</code> subcommand will create the archive from the chart&rsquo;s directory</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm package ./test-helm-chart
</span></span></code></pre></div><p>The archive could be signed (<code>--sign</code>) by its developer using a key passed with a <code>--key</code> flag.</p>
<h1 id="distributing-helm-charts">Distributing Helm Charts</h1>
<h2 id="chart-repositories">Chart Repositories</h2>
<p>The chart repository server contains</p>
<ul>
<li>an <code>index.yaml</code> containing chart&rsquo;s metadata</li>
<li>charts packaged as archives (for example <code>test-helm-chart-0.0.1.tgz</code>)</li>
<li>chart&rsquo;s provenance files (<code>test-helm-chart-0.0.1.tgz.prov</code>) containing integrity and validation data</li>
</ul>
<p>A publicly available helm chart repository could be created using GitHub pages. Public helm chart repositories could also be added to ArtifactHub.</p>
<h2 id="oci-based-registries">OCI-based registries</h2>
<p>Some OCI-based registries like DockerHub support helm chart distribution. To distribute a chart through DockerHub the user has to login to the registry through <code>helm</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm registry login -u bovem docker.io
</span></span></code></pre></div><p>Here <code>bovem</code> is the DockerHub username and <code>docker.io</code> is the link to the DockerHub registry. To push the chart to DockerHub it has to be packaged as an archive</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm push test-helm-chart-0.0.1.tgz oci://docker.io/bovem
</span></span></code></pre></div><p>Similarly, a specific version of the chart could be pulled from DockerHub using the <code>pull</code> command.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">helm pull oci://docker.io/bovem/test-helm-chart --version 0.0.1
</span></span></code></pre></div><h1 id="resources">Resources</h1>
<p><a href="https://docs.fedoraproject.org/en-US/quick-docs/dnf/" target="_blank">Using the DNF software package manager</a><br>
<a href="https://helm.sh/" target="_blank">Helm</a><br>
<a href="https://helm.sh/docs/topics/architecture/" target="_blank">Helm Architecture</a><br>
<a href="https://helm.sh/docs/intro/install/" target="_blank">Installing Helm</a><br>
<a href="https://helm.sh/docs/topics/charts/" target="_blank">Charts</a><br>
<a href="https://helm.sh/docs/topics/chart_repository/" target="_blank">The Chart Repository Guide</a><br>
<a href="https://artifacthub.io/packages/helm/jenkins-x/tekton" target="_blank">Tekton&rsquo;s Helm Chart</a><br>
<a href="https://artifacthub.io/" target="_blank">ArtifactHub</a><br>
<a href="https://artifacthub.io/packages/helm/grafana/grafana" target="_blank">Grafana&rsquo;s Helm Chart</a></p>
]]></content:encoded>
    </item>
  </channel>
</rss>
