Helm Charts and Template Basics — Part 2

Using values in templates

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 }}
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 }}
helm install .\myhelm1\ --name test5 --dry-run --debug
app.kubernetes.io/instance: test5
app.kubernetes.io/managed-by: Tiller
app.kubernetes.io/instance: test5
- name: myhelm1
image: "radial/busyboxplus:base"
imagePullPolicy: IfNotPresent
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
image:
repository: radial/busyboxplus
tag: base
pullPolicy: IfNotPresent
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: myhelm1
version: 0.1.0

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
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 }}

Helm variables and range

{{- 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:
enabled: false
hosts:
- chart-example.local
tls:
- secretName: chart-example-tls
hosts:
- chart-example.local-1
- chart-example.local-2
- chart-example.local-3
{{- 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
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

{{- define "myhelm1.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "myhelm1.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
if condition
do something
else
do something else
end

learn Helm template syntax fast

  • edit as few files as possible
  • only show as few rendered template lines as possible
  • do no live installs, only debug dry-runs
replicaCount: 1terminationGracePeriodSeconds: 30image:
repository: radial/busyboxplus
tag: base
pullPolicy: IfNotPresent
NOTES.txt
test-connection.yaml
service.yaml
ingress.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 }}
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
helm install .\myhelm1\  --name test5 --dry-run --debug | grep -vE 'debug]|NAME|REVIS|RELEA|ART:|OKS:|FEST:'
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
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
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 }}
containers:
- name: myhelm1
image1: "radial/busyboxplus:base"
imagePullPolicy1: "IfNotPresent" imagePullPolicy2: "IfNotPresent" imagePullPolicy3: "IfNotPresent" imagePullPolicy4: IFNOTPRESENT imagePullPolicy5: ifnotpresent variable: $variable
variable: 123
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
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:'

learn from other templates

  • the official Helm reference docs
  • these 2 practical tutorials
  • 300 superb Helm chart examples
  • the fast hack method that you can use to cut-paste and speed learn those 3 sources via quick interactive exercises
{{- 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 }}
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 }}
{{- 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 }}
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 }}

how to use .Files.Get

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com