In this tutorial, we'll briefly discuss how Helm can help simplify the management of Kubernetes applications and learn how to use Helm to create a basic chart.
This tutorial explains how the Helm template is deployment.yaml
converted from template to YAML manifest.
The article does not correspond to the traditional programming reference manual: operators and functions are listed in alphabetical order.
The article explains the syntax of the template as needed to explain deployment.yaml
from start to finish.
Using Values ββin Templates
Here is the complete deployment.yaml file for reference:
apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "myhelm1.fullname" . }} labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} template: metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} command: ['sh', '-c', 'sleep 60'] {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.affinity }} affinity: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 8 }} {{- end }}
deployment.yaml
:
app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} replicas: {{ .Values.replicaCount }} app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/instance: {{ .Release.Name }} - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }}
.Release.Name
.Release.Service
β https://docs.helm.sh/chart_template_guide/ . , - β .
, deployment.yaml
, :
helm install .\myhelm1\ --name test5 --dry-run --debug
, . ( , , , β .)
( , , , helm install / )
app.kubernetes.io/instance: test5 app.kubernetes.io/managed-by: Tiller app.kubernetes.io/instance: test5 - name: myhelm1 image: "radial/busyboxplus:base" imagePullPolicy: IfNotPresent
3 values.yaml:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }}
values.yaml
image: repository: radial/busyboxplus tag: base pullPolicy: IfNotPresent
β name: {{ .Chart.Name }} Chart.yaml. .
apiVersion: v1 appVersion: "1.0" description: A Helm chart for Kubernetes name: myhelm1 version: 0.1.0
, β .
, YAML , {{ and }}
.
{{ }}
.
, , , (.)
.
, . .Chart.name
Β« , Chart
, name
Β».
With
**values.yaml** extract: nodeSelector: disktype: ssd gpu: Nvidia
**deployment.yaml** extract: {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }}
:
nodeSelector: disktype: ssd gpu: Nvidia
: YAML.
8 , deployment.yaml
{{- toYaml. | nindent 8}}
nindent 8
8 .
with
β . .Values.nodeSelector
: Yaml (toYaml
).
toYaml
β .Values.nodeSelector
. .
sum(1,34,454)
β¦ toYaml(.)
β¦ .
| , Linux.
affinity
: tolerations
: with
.
, , with . MODIFYING SCOPE USING WITH
include
, , deployment.yaml
values.yaml
, Chart.yaml
.
service.yaml
:
.
apiVersion: v1 kind: Service metadata: name: {{ include "myhelm1.fullname" . }} labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} spec: type: {{ .Values.service.type }} ports: - port: {{ .Values.service.port }} targetPort: http protocol: TCP name: http selector: app.kubernetes.io/name: {{ include "myhelm1.name" . }} app.kubernetes.io/instance: {{ .Release.Name }}
range () Helm
3 ingress.yaml
{{- if .Values.ingress.enabled -}} {{- $fullName := include "myhelm1.fullname" . -}} {{- $ingressPaths := .Values.ingress.paths -}} ... rest of yaml .... {{- end }} {{- end }} {{- end }}
**values.yaml** extract: ingress: enabled: false
ingress.yaml
if
..., 1 . β false, yaml β .
2 3 , Helm.
{{ }}
/ . {{-
-}}
, β β .
values.yaml
ingress: enabled: false hosts: - chart-example.local tls: - secretName: chart-example-tls hosts: - chart-example.local-1 - chart-example.local-2 - chart-example.local-3
deployment.yaml :
{{- if .Values.ingress.tls }} tls: {{- range .Values.ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} {{- end }} secretName: {{ .secretName }} {{- end }} {{- end }}
:
spec: tls: - hosts: - "chart-example.local-1" - "chart-example.local-2" - "chart-example.local-3" secretName: chart-example-tls
, (range
) . quote
.
range .Values.ingress.tls
, . 3 , .
Extract of **values.yaml** ingress: enabled: false hosts: - chart-example.local tls: - secretName: chart-example-tls-a hosts: - chart-example.local-1-a - chart-example.local-2-a - chart-example.local-3-a - secretName: chart-example-tls-b hosts: - chart-example.local-1-b - chart-example.local-2-b - secretName: chart-example-tls-c hosts: - chart-example.local-1-c - chart-example.local-2-c - chart-example.local-3-c - chart-example.local-4-c
:
tls: - hosts: - "chart-example.local-1-a" - "chart-example.local-2-a" - "chart-example.local-3-a" secretName: chart-example-tls-a - hosts: - "chart-example.local-1-b" - "chart-example.local-2-b" secretName: chart-example-tls-b - hosts: - "chart-example.local-1-c" - "chart-example.local-2-c" - "chart-example.local-3-c" - "chart-example.local-4-c" secretName: chart-example-tls-c
importance of -
.
{{- if .Values.ingress.tls }} tls: {{- range .Values.ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} {{- end }} secretName: {{ .secretName }} {{- end }} {{- end }}
:
{{ if .Values.ingress.tls }} tls: {{ range .Values.ingress.tls }} - hosts: {{ range .hosts }} - {{ . | quote }} {{ end }} secretName: {{ .secretName }} {{ end }} {{ end }}
:
tls: - hosts: - "chart-example.local-1-a" - "chart-example.local-2-a" - "chart-example.local-3-a" secretName: chart-example-tls-a - hosts: - "chart-example.local-1-b" - "chart-example.local-2-b" secretName: chart-example-tls-b - hosts: - "chart-example.local-1-c" - "chart-example.local-2-c" - "chart-example.local-3-c" - "chart-example.local-4-c" secretName: chart-example-tls-c
.
_helpers.tpl
, .
: name: {{include "myhelm1.fullname". }}
β .
include
include YAML-.
_helpers.tpl
β , .
_helpers.tpl:
{{- define "myhelm1.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} {{- end -}}
. , .
myhelm1.name : .Chart.Name.
, myhelm1.name .Values.nameOverride
.
trunc 63
63 .
trimSuffix "-"
-
.
trimSuffix "-
" -
.
( , , )
app.kubernetes.io/name: {{ include "myhelm1.name" . }}
app.kubernetes.io/name: myhelm1
:
helm.sh/chart: {{ include "myhelm1.chart" . }}
helm.sh/chart: myhelm1-0.1.0
:
{{- define "myhelm1.chart" -}} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} {{- end -}}
printf "%s-%s" .Chart.Name .Chart.Version
.Chart.Name
.Chart.Version
β .
replace "+" "_"
.
, , 10- myhelm1.fullname
.
, , if / else :
if condition do something else do something else end
β {{ }}
.
Helm
Helm .
The Chart Developer's Guide: https://helm.sh/docs/topics/charts/
The Chart Template Developer's Guide: https://docs.helm.sh/chart_template_guide/
β . β .
, Helm .
:
β’
β’
β’ .
β , , β .
values.yaml
, , :
replicaCount: 1 terminationGracePeriodSeconds: 30 image: repository: radial/busyboxplus tag: base pullPolicy: IfNotPresent
, ./myhelm1/.helmignore
, :
NOTES.txt test-connection.yaml service.yaml ingress.yaml
deployment.yaml, :
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: replicas: {{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
, , yaml ( ), .
:
helm install .\myhelm1\ --name test5 --dry-run --debug
, :
PS C:\k8> helm install .\myhelm1\ --name test5 --dry-run --debug [debug] Created tunnel using local port: '50327' [debug] SERVER: "127.0.0.1:50327" [debug] Original chart version: "" [debug] CHART PATH: C:\k8\myhelm1 NAME: test5 REVISION: 1 RELEASED: Fri Feb 15 13:47:49 2019 CHART: myhelm1-0.1.0 USER-SUPPLIED VALUES: {} COMPUTED VALUES: image: pullPolicy: IfNotPresent repository: radial/busyboxplus tag: base replicaCount: 1 terminationGracePeriodSeconds: 30 HOOKS: MANIFEST: --- # Source: myhelm1/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: myhelm1 helm.sh/chart: myhelm1-0.1.0 app.kubernetes.io/instance: test5 spec: replicas: 1 template: spec: containers: - name: myhelm1 image: "radial/busyboxplus:base" imagePullPolicy: IfNotPresent terminationGracePeriodSeconds: 30
«» grep.
helm install .\myhelm1\ --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
. , : yaml .
USER-SUPPLIED VALUES: {} COMPUTED VALUES: image: pullPolicy: IfNotPresent repository: radial/busyboxplus tag: base replicaCount: 1 terminationGracePeriodSeconds: 30 --- # Source: myhelm1/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: myhelm1 helm.sh/chart: myhelm1-0.1.0 app.kubernetes.io/instance: test5 spec: replicas: 1 template: spec: containers: - name: myhelm1 image: "radial/busyboxplus:base" imagePullPolicy: IfNotPresent terminationGracePeriodSeconds: 30
:
deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: #-------------------->> learn spacing << ------------------------ replicas1: {{ .Values.replicaCount }} replicas2: {{ .Values.replicaCount }} replicas3: {{ .Values.replicaCount }} replicas4: '{{ .Values.replicaCount }}' replicas5: "{{ .Values.replicaCount }}" replicas6: "{{ .Values.replicaCount }}" replicas7: "{{ .Values.replicaCount }}" replicas: "{{ .Values.replicaCount }}' replicas: '{{ .Values.replicaCount }}" replicas: {{ .Values.replicaCount }}" replicas: "{{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image1: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" image2: "{{ .Values.image.repository }} {{ .Values.image.tag }}" image3: "{{ .Values.image.repository }}{{ .Values.image.tag }}" image4: {{ .Values.image.repository }}{{ .Values.image.tag }} imagePullPolicy1: {{ .Values.image.pullPolicy }} imagePullPolicy2: {{ .Values.image.pullPolicyzzz }} imagePullPolicy3: {{ .Values.image.pullPolicyeeeeeeeeeee }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
:
spec: #-------------------->> learn spacing << ------------------------ replicas1: 1 replicas2: 1 replicas3: 1 replicas4: '1' replicas5: "1" replicas6: "1" replicas7: "1" template: spec: containers: - name: myhelm1 image1: "radial/busyboxplus:base" image2: "radial/busyboxplus base" image3: "radial/busyboxplusbase" image4: radial/busyboxplusbase imagePullPolicy1: IfNotPresent imagePullPolicy2: imagePullPolicy3: terminationGracePeriodSeconds: 30
, .
, , . , , .
deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: # replicas1: {{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image1: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy-correct: {{ .Values.image.pullPolicy }} imagePullPolicy1: {{ Values.image.pullPolicy }} imagePullPolicy2: {{ .Valu.image.pullPolicyzzz }} imagePullPolicy3: {{ ..Values.image.pullPolicyeeeeeeeeeee }}
:
helm install .\myhelm1\ --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
Error: parse error in "myhelm1/templates/deployment.yaml": template: myhelm1/templates/deployment.yaml:19: function "Values" not defined
, , .
Error: parse error in "myhelm1/templates/deployment.yaml": template: myhelm1/templates/deployment.yaml:21: unexpected . after term "."
, , .
Error: render error in "myhelm1/templates/deployment.yaml": template: myhelm1/templates/deployment.yaml:20:36: executing "myhelm1/templates/deployment.yaml" at <.Valu.image.pullPoli...>: can't evaluate field image in type interface {}
helm install .\myhelm1\ --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
:
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: # replicas1: {{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image1: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy1: {{ quote .Values.image.pullPolicy }} imagePullPolicy2: {{ .Values.image.pullPolicy | quote }} imagePullPolicy3: "{{ .Values.image.pullPolicy }}" imagePullPolicy4: {{ .Values.image.pullPolicy | upper }} imagePullPolicy5: {{ .Values.image.pullPolicy | lower }} {{ $variable := 123 }} variable: $variable variable: {{ $variable }}
. 3 β use those -, . 3 .
Helm , Python, .
:
containers: - name: myhelm1 image1: "radial/busyboxplus:base" imagePullPolicy1: "IfNotPresent" imagePullPolicy2: "IfNotPresent" imagePullPolicy3: "IfNotPresent" imagePullPolicy4: IFNOTPRESENT imagePullPolicy5: ifnotpresent variable: $variable variable: 123
. , imagePullPolicy 1 3 . ? :
deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: {{ include "myhelm1.name" . }} helm.sh/chart: {{ include "myhelm1.chart" . }} app.kubernetes.io/instance: {{ .Release.Name }} spec: # replicas1: {{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image1: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" # imagePullPolicy1: {{ quote .Values.image.pullPolicy }} imagePullPolicy1: {{ quote .Values.image.pullPolicy }} # imagePullPolicy2: {{ .Values.image.pullPolicy | quote }} imagePullPolicy2: {{ .Values.image.pullPolicy | quote }} imagePullPolicy3: " .Values.image.pullPolicy " imagePullPolicy3: "{{ .Values.image.pullPolicy }}" imagePullPolicy4: .Values.image.pullPolicy | upper imagePullPolicy4: {{ .Values.image.pullPolicy | upper }} imagePullPolicy5: .Values.image.pullPolicy | lower imagePullPolicy5: {{ .Values.image.pullPolicy | lower }} {{ $variable := 123 }} variable: $variable variable: {{ $variable }}
helm install .\myhelm1\ --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
:
- name: myhelm1 image1: "radial/busyboxplus:base" # imagePullPolicy1: "IfNotPresent" imagePullPolicy1: "IfNotPresent" # imagePullPolicy2: "IfNotPresent" imagePullPolicy2: "IfNotPresent" imagePullPolicy3: " .Values.image.pullPolicy " imagePullPolicy3: "IfNotPresent" imagePullPolicy4: .Values.image.pullPolicy | upper imagePullPolicy4: IFNOTPRESENT imagePullPolicy5: .Values.image.pullPolicy | lower imagePullPolicy5: ifnotpresent
. .
{{ and }}
. .
, , β { { and { }
{{ and }}
β¦ 1 .
{ and }
{{ and }}
imagePullPolicy1: { { quote .Values.image.pullPolicy } } imagePullPolicy1: {{ quote .Values.image.pullPolicy }} imagePullPolicy2: { .Values.image.pullPolicy | quote } imagePullPolicy2: {{ .Values.image.pullPolicy | quote }}
:
imagePullPolicy1: { { quote .Values.image.pullPolicy } } imagePullPolicy1: "IfNotPresent" imagePullPolicy2: { .Values.image.pullPolicy | quote } imagePullPolicy2: "IfNotPresent"
helm install .\myhelm1\ --set replicaCount={1,2,3} --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
Helm https://github.com/helm/charts 300 Helm.
: .
, 80% . ( 2 10 ).
4 :
β’ Helm
β’ 2
β’ 300 Helm
β’ , 3
Helm .
, Helm.
https://github.com/helm/charts/blob/master/stable/lamp/templates/NOTES.txt
, LAMP .Values.ingress.enabled
{{- if .Values.ingress.enabled }} INGRESS: Please make sure that you have an ingress controller instance {{ if .Values.ingress.ssl }}and a lego instance {{- end -}} running and that you have configured the A Records of {{ template "lamp.domain" . }} and its subdomains to point to your ingress controllers ip address. {{- else }}
, : .
NOTES.txt , : https://github.com/helm/charts/blob/master/stable/lamp/templates/NOTES.txt
1. You can now connect to the following services: {{- if not .Values.ingress.enabled }} export CHARTIP=$(kubectl get svc {{ template "lamp.fullname" . }} --output=jsonpath={.status.loadBalancer.ingress..ip}) {{- end }} Main Site: {{- if .Values.ingress.enabled }} http{{ if .Values.ingress.ssl }}s{{ end }}://{{ template "lamp.domain" . }} {{- else }} http://$CHARTIP {{- end }} {{- if .Values.phpmyadmin.enabled }} PHPMyAdmin: {{- if .Values.ingress.enabled }} http{{ if .Values.ingress.ssl }}s{{ end }}://{{ .Values.phpmyadmin.subdomain }}.{{ template "lamp.domain" . }} {{- else }} http://$CHARTIP:{{ .Values.phpmyadmin.port }} {{- end }} {{- end }}
β , : https://github.com/helm/charts/blob/master/stable/mongodb/templates/NOTES.txt
{{- if contains .Values.service.type "LoadBalancer" }} {{- if not .Values.mongodbRootPassword }} ------------------------------------------------------------------------------- WARNING By specifying "service.type=LoadBalancer" and not specifying "mongodbRootPassword" you have most likely exposed the MongoDB service externally without any authentication mechanism. For security reasons, we strongly suggest that you switch to "ClusterIP" or "NodePort". As alternative, you can also specify a valid password on the "mongodbRootPassword" parameter. ------------------------------------------------------------------------------- {{- end }} {{- end }}
, .Values.service.type
"NodePort
", "LoadBalancer
" "ClusterIP": https://github.com/helm/charts/blob/master/stable/mongodb/templates/NOTES.txt
To connect to your database from outside the cluster execute the following commands: {{- if contains "NodePort" .Values.service.type }} export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "mongodb.fullname" . }}) mongo --host $NODE_IP --port $NODE_PORT {{- if .Values.usePassword }} --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD{{- end }} {{- else if contains "LoadBalancer" .Values.service.type }} NOTE: It may take a few minutes for the LoadBalancer IP to be available. Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "mongodb.fullname" . }}' export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "mongodb.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") mongo --host $SERVICE_IP --port {{ .Values.service.nodePort }} {{- if .Values.usePassword }} --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD{{- end }} {{- else if contains "ClusterIP" .Values.service.type }} kubectl port-forward --namespace {{ .Release.Namespace }} svc/{{ template "mongodb.fullname" . }} 27017:27017 & mongo --host 127.0.0.1 {{- if .Values.usePassword }} --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD{{- end }} {{- end }}
3 . ( β¦ ).
.Files.Get
https://docs.helm.sh/chart_template_guide/β¦ β¦ ( ) , .
There are 80 use cases in the Helm repository .Files.Get.
https://github.com/helm/charts/search?utf8=%E2%9C%93&q=.Files.Get&type =
I found 5 different use cases in the first 10 results .Files.Get.
To learn more about Helm , visit https://github.com/helm/charts/tree/master/stable .