Syntax highlighting of f9224ed ~( devops/kubernetes/helm)

# Helm

[TOC]

[https://helm.sh/](https://helm.sh/)

[Installing Helm](https://helm.sh/docs/intro/install/)

https://codersociety.com/blog/articles/helm-best-practices

## Commands

### Env

```bash
helm env
# ...
# HELM_NAMESPACE
```

### repo

https://helm.sh/docs/helm/helm_repo/

```bash
# --- список "добавленных" репозиториев
helm repo list

# --- добавить репозиторий
helm repo add \
  nexus https://repo.corp.tander.ru/repository/cross-docking-charts \
  --ca-file ./certs/TanderCorpCA.crt

# --- обновление репозитория
#     выполняется всегда, после helm add
helm repo update
```

### search

https://helm.sh/docs/helm/helm_search/

Helm Hub
```bash
# --- search for charts in Helm Hub
helm search hub loki
# --- search for carts without output truncate
helm search hub loki --max-col-width=0
```

Repo
```bash
# --- поиск крайней версии схемы в репозиториях, которые добавляются через helm repo addи (всех добавленных репозиториях?)
helm search repo kdk-standalone
# --- поиск крайней версии схемы для мажорной версии 0
#     подходят версии: 0.0.1, 0.1.0, 0.1.1, 0.123.123
#     не подходят версии: 1.0.0, 2.х.y
helm search repo kdk-standalone --version 0
# --- поиск крайней версии для мажорной и минорной версии
#     подходят версии: 1.2.0, 1.2.1, 1.2.123
#     не подходят версии: 1.1.0, 1.3.0, 2.x.y
helm search repo kdk-standalone --version 1.2

# --- поиск всех версий схемы
helm search repo kdk-cluster --versions
# NAME                    CHART VERSION   APP VERSION     DESCRIPTION
# nexus/kdk-cluster       2.5.7           2               cross-docking cluster
# nexus/kdk-cluster       2.5.6           2               cross-docking cluster
# nexus/kdk-cluster       2.5.5           2               cross-docking cluster
# nexus/kdk-cluster       2.5.3           2               cross-docking cluster
# nexus/kdk-cluster       2.5.2           2               cross-docking cluster
```

### show chart info

```bash
# --- show the chart definition
helm show chart bitnami/wordpress
# --- show chart README
helm show readme bitnami/wordpress
# --- show chart values
helm show values bitnami/wordpress
# --- show all information about chart
helm show all bitnami/wordpress
```

### inspecting release

```bash
helm get hooks RELEASE_NAME --namespace NAMESPACE
helm get manifest RELEASE_NAME --namespace NAMESPACE
helm get notes RELEASE_NAME --namespace NAMESPACE
helm get values RELEASE_NAME --namespace NAMESPACE
# --- history
helm history RELEASE_NAME
```

### install / upgrade
```bash
helm install --debug --dry-run test .

# --set    - values from command line
# --values - values from YAML file or URL

# --- install wordpress
helm install wordpress bitnami/wordpress \
  --values=wordpress-values.yaml \
  --namespace chapter3 \
  --version 8.1.0
# --- upgrade wordpress
helm upgrade ...
```

### rollback / uninstall

```bash
helm rollback RELEASE_NAME [REVISION] [flags]
helm uninstall RELEASE_NAME
```

## Creating chart

Scaffolding the initial file scructure
```bash
helm create guestbook
cd guestbook
```




Modifying values
```yaml
# values.yaml
redis:
  fullnameOverride: redis
  auth.enabled: false
  commonConfiguration: |-
    appendonly no
```
Install chart
```bash
helm install my-guestbook .
```
Access to application
```bash
k get po
export NODE_PORT=$(kubectl get --namespace chapter5 -o jsonpath="{.spec.ports[0].nodePort}" services my-guestbook)
export NODE_IP=$(kubectl get nodes --namespace chapter5 -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
```

## Dependencies

> See p. 120 in Learn Helm Packt https://trello.com/c/FmnGbXCi



Find char dependency
```bash
helm search hub redis
helm add repo bitnami https://charts.bitnami.com
helm search repo redis --versions | head
```

Adding dependency and downloading it
```yaml
# Chart.yaml
dependencies:
  - name: redis
    version: 16.8.9
    repository: https://charts.bitnami.com/bitnami
```
```bash
helm dependency update
```

Show dependency values
```bash
helm show values charts/redis-VERSION.tgz
```

## Templates

### if / else
```yaml
readinessProbe:
{{- if .Values.probeType.httpGet }}
  httpGet:
    path: /healthz
    port: 8080
    scheme: HTTP
{{- else }}
  tcpSocket:
    port: 8080
{{- end }}
  initialDelaySeconds: 30
  periodSeconds: 10
```

### with

```yaml
# --- NO with
cpu: {{ .Values.application.resources.limits.cpu }}
memory: {{ .Values.application.resources.limits.memory }}

# --- YES with
{{- with .Values.application.resources.limits }}
cpu: {{ .cpu }}
memory: {{ .memory }}
{{- end }}
```

### range

```yaml
# values.yaml
servicePorts:
  - name: http
    port: 8080
  - name: https
    port: 8443
  - name: jolokia
    port: 8778

# templates/foo.yaml
spec:
  ports:
{{- range .Values.servicePorts }}
    - name: {{ - name }}
      port: {{ .port }}
{{- end }}
# --- or
{{- range .Values.servicePorts }}
  - name: {{ $.Release.Name }}-{{ .name }}
    port: {{ .port }}
{{- end }}
```

### Variables

```yaml
{{ $myvar := 'Hello World!' }}
{{ $myvar := .Values.greeting }}
# ...
data:
  greeting.txt: |
    {{ $myvar }}
```
Using variables in a range block with index
```yaml
data:
  greetings.txt: |
{{- range $index, $value := .Values.greetings }}
    Greeting {{ $index }}: {{ $value }}
{{- end }}
# --- same as
data:
  greetings.txt: |
    Greeting 0: Hello
    Greeting 1: Hola
    Greeting 2: Hallo
```
Using variables in a range block with key (???)
```yaml
data:
  greetings.txt: |
{{- range $key, $val := .Values.greetings }}
    Greeting in {{ $key }}: {{ $val }}
{{- end }}
# --- same as
data:
  greetings.txt: |
    Greeting in English: Hello
    Greeting in Spain: Hola
    Greeting in German: Hallo
```
Using variables to refer to a value outside of the current scope
```yaml
# --- FAIL
#     .Release.Name is not under scope of .Values.application.configuration
{{- with .Values.application.configuration }}
My application is called {{ .Release.Name }}
{{- end }}

# --- OK
{{ $appName := .Release.Name }}
{{- with .Values.application.configuration }}
My application is called {{ $appName }}
{{- end }}
```

### Template functions

See https://helm.sh/docs/chart_template_guide/function_list/

### Named Templates

```yaml
# templates/_helper.tpl
{{- define 'mychart.labels' }}
labels:
  'app.kubernetes.io/instance': {{ .Release.Name }}
  'app.kubernetes.io/managed-by': {{ .Release.Service }}
{{- end }}
```
Using named template by `include [TEMPLATE_NAME] [SCOPE]`
```yaml
# templates/some-resource.yaml
metadata:
  name: {{ .Release.Name }}
{{- include 'mycharts.labels' . | indent 2 }}
```
We can use `template` action instead `include`, but we can't use pipelines in this case.

## Hooks

```yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: helm-auditing
  annotations:
    'helm.sh/hook': pre-install,post-install
spec:
  template:
    metadata:
      name: helm-auditing
    spec:
      restartPolicy: Never
      containers:
        - name: helm-auditing
          command: ["/bin/sh", "-c", "echo Hook executed at $(date)"]
          image: alpine
```

![helm hook annotations](https://clck.ru/sSM2x)

## Troubleshooting

### Error: directory charts/redis not found

Для новых зависимостей команда обновления выдает ошибку
```bash
helm dependency update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈
Error: directory charts/redis not found
```

Ошибка выдавалась по причине ошибки в слове `repository` в описании зависимости в Chart.yaml.install
```bash
helm install --debug --dry-run test .
```


> Written with [StackEdit](https://stackedit.io/).