Skip to content

Instantly share code, notes, and snippets.

@tdr2d
Created November 5, 2020 17:07
Show Gist options
  • Save tdr2d/9c1f0fa50c6defc666d423198354adc8 to your computer and use it in GitHub Desktop.
Save tdr2d/9c1f0fa50c6defc666d423198354adc8 to your computer and use it in GitHub Desktop.
Helm 3 Usage Cheatsheet

Helm Usage Cheatsheet

1. Generate a new helm chart

helm create portail-web

This will generate a helm chart structured like:

portail-web/
  Chart.yaml
  values.yaml
  charts/
  templates/
  ...
  • templates/ contains the templated kubernetes objetcs
  • values.yaml contains the default values of the chart
  • Chart.yaml contains a description of the chart
  • charts/ directory is used to define sub-charts
  • .helmignore to list the files not included in the helm chart package

2. Helm chart Lifecycle

2.1 Install on kubernetes
helm install test ./portail-web -n test-namespace --dry-run    --values=values.yml  --wait
#           <name>   <helm chart>  [namespace]       [test debug] [new values]         [synchronous]
2.2 List/Get deployed releases
helm list
helm get manifest test
2.3 Update the deployed release
helm upgrade test ./portail-web -n test-namespace --values=new-values.yml 
2.4 Delete the deployed release
helm delete test

3. Remote helm registry

3.1 Add a new repository locally
helm repo add helm_demo https://my.artifactory.priv:443/artifactory/helm_demo --insecure-skip-tls-verify
#             <repo_dir> <repo_url>                                         [skipt tls verification]
3.2 List the available charts from the remote repository
$ helm search repo helm_demo

NAME                    CHART VERSION   APP VERSION     DESCRIPTION
helm_demo/default_chart 0.0.2           1.16.0          A Helm chart for Kubernetes
helm_demo/my-nginx      0.0.2           1.16.0          A Helm chart for Kubernetes

3.3 Install a remote helm chart

# Using repo name
helm install test-name helm_demo/my-nginx --version 0.0.2

# Using a full .tgz url
helm install test-name https://my.artifactory.priv:443/artifactory/helm_demo/my-nginx-0.0.2.tgz --insecure-skip-tls-verify
Package and Push to the remote artifactory repository
helm package ./portail-web --destination /tmp
# Successfully packaged chart and saved it to: /tmp/portail-web-0.1.0.tgz

curl -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASSWORD} -k \
      -T /tmp/${chart_name}-${chart_version}.tgz \
      "${repo_url}/${chart_name}-${chart_version}.tgz"  # If api_key, use -H "X-JFrog-Art-Api:${ARTIFACTORY_API_KEY}"
Remove repository
helm repo remove helm_demo

4. Advanced Usage and lifehack

4.1 Rolling update
  • Leverage automatic rolling updates by defining deployments.
  • If you change the values and execute helm upgrade, it won't update the pods unless there is a change in the image or a variable in the spec of the pod definition of the deployment.
  • If you want the pod to automatically redeploy when a configMap or secret is updated, you need to add an annotation in the .spec.template.metadata section of the deployment
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    
    with configmap.yaml existing in the templates of the helm chart
  • If you always want rolling update, you can use this annotation in the deployment
    rollme: {{ randAlphaNum 5 | quote }}
    
4.2 Inside templates, you can use the tpl function and templated values
# values.yaml
COUCHBASE_CLUSTER_IP: "couchbase://couchbase-mock.{{ .Release.Namespace }}.svc.cluster.local"

# templates/configmap.yaml.tpl
{...}
pepit_server_api_couchbase_cluster_ip: {{ tpl .Values.COUCHBASE_CLUSTER_IP . }}

# This will evaluate `.Release.Namespace` and generate this:
{...}
pepit_server_api_couchbase_cluster_ip: couchbase://couchbase-mock.my-namespace.svc.cluster.local
4.3 use the .tpl extension for the template files and use VScode gotemplate-syntax
4.4 Write proper if blocks
  • simple if
data:
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug: true
  {{ end }}

generates

data:
  food: "PIZZA"
**
  mug: true

with the '*' character representing a space

  • if with - prefix
data:
  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee" }}
  mug: true
  {{- end }}

The - prefix will remove all the space on the left side, it generates

data:
  food: "PIZZA"
  mug: true

This is very useful to have a clean yaml file without extra lines

4.5 Usage

Instal or update in one command

helm upgrade --install <release name> --values <values file> <chart directory>

Additional content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment