Helm: How to Add Repositories, Dependencies & Hooks - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki

Adding Repositories:

The official Helm charts repo:- https://bitnami.com/stacks/helm

  • Add Repositories
helm repo add bitnami https://charts.bitnami.com/bitnami
  • List Repositories
helm repo list

*Download repository onto your machine:

helm pull bitnami/mariadb
  • Download and Untar
helm pull bitnami/mariadb --untar
  • Install Repo:

No need to download (by executing pull) to run the below command.

helm install my-release bitnami/mariadb
  • Push Charts to a Registry
helm push [chart] [remote] [flags]

======

Dependencies

Dependenices are defined in Charts.yaml like below.

Charts.yaml

apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
dependencies:
  - name: apache
    version: 8.5.2
    repository: https://charts.bitnami.com/bitnami
  - name: mysql
    version: 8.5.5
    repository: https://charts.bitnami.com/bitnami

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"

The configured dependencies need to exist in the chart's path, otherwise it will throw the below error.

helm install myapp ./myapp

Error: INSTALLATION FAILED: found in Chart.yaml, but missing in charts/ directory: apache, mysql

  • Download Depencies
 helm dependency update

Output:

Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈
Saving 2 charts
Downloading apache from repo https://charts.bitnami.com/bitnami
Downloading mysql from repo https://charts.bitnami.com/bitnami
Deleting outdated charts

Note: You could see the dependencies apache & mysql have been downloaded under "./charts" directory.

  • Install Release
helm install myapp .
  • Check whether the objects are created including the dependencies'
 kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/myapp-567b668757-5wbgm          1/1     Running   0          14s
pod/myapp-apache-76df548b49-zttlc   0/1     Running   0          13s
pod/myapp-mysql-0                   0/1     Pending   0          13s

NAME                           TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/myapp                  ClusterIP      10.109.5.170    <none>        80/TCP                       15s
service/myapp-apache           LoadBalancer   10.101.22.114   <pending>     80:30437/TCP,443:30539/TCP   15s
service/myapp-mysql            ClusterIP      10.102.35.179   <none>        3306/TCP                     15s
service/myapp-mysql-headless   ClusterIP      None            <none>        3306/TCP                     15s

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp          1/1     1            1           15s
deployment.apps/myapp-apache   0/1     1            0           15s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-567b668757          1         1         1       15s
replicaset.apps/myapp-apache-76df548b49   1         1         0       15s

NAME                           READY   AGE
statefulset.apps/myapp-mysql   0/1     14s

========

Chart Hooks

Refer: https://helm.sh/docs/topics/charts_hooks/

**Helm provides a hook mechanism to allow chart developers to intervene at certain points in a release's life cycle. **

**Hooks are just Kubernetes manifest files with special annotations in the metadata section. **

For example, you can use hooks to:

  • Load a ConfigMap or Secret during install before any other charts are loaded.
  • Execute a Job to back up a database before installing a new chart, and then execute a second job after the upgrade in order to restore data.
  • Run a Job before deleting a release to gracefully take a service out of rotation before removing it.
  • Hooks work like regular templates, but they have special annotations that cause Helm to utilize them differently. In this section, we cover the basic usage pattern for hooks.

Example:

This template, stored in templates/post-install-job.yaml, declares a job to be run on post-install:

apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}"
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-install
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}"
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    spec:
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: "alpine:3.3"
        command: ["/bin/sleep","{{ default "10" .Values.sleepyTime }}"]

What makes this template a hook is the annotation:

annotations:
  "helm.sh/hook": post-install

One resource can implement multiple hooks:

annotations:
  "helm.sh/hook": post-install,post-upgrade
⚠️ **GitHub.com Fallback** ⚠️