快速建立一個chart模板,helm create mychart
,執行命令後本地生成一個mychart目錄.html
一個最小的chart目錄,只須要包含一個Chart.yaml,和templates目錄下一個k8s資源文件.如: api
# mychart/Chart.yaml apiVersion: v1 appVersion: 2.9.0 version: 1.1.1 # mychart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: mychart-configmap data: myvalue: "Hello World"
- go-template雙 - sprig
模板引用方式,{{ .Release.Name }}
, 經過雙括號注入,小數點開頭表示從最頂層命名空間引用.app
# Release, release相關屬性 # Chart, Chart.yaml文件中定義的內容 # Values, values.yaml文件中定義的內容
apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | repeat 5 | quote }} food: {{ .Values.favorite.food | upper | quote }}
{{ if PIPELINE }} # Do something {{ else if OTHER PIPELINE }} # Do something else {{ else }} # Default case {{ end }}
操做符, and/eq/or/notide
{{/* include the body of this if statement when the variable .Values.fooString exists and is set to "foo" */}} {{ if and .Values.fooString (eq .Values.fooString "foo") }} {{ ... }} {{ end }} {{/* do not include the body of this if statement because unset variables evaluate to false and .Values.setVariable was negated with the not function. */}} {{ if or .Values.anUnsetVariable (not .Values.aSetVariable) }} {{ ... }} {{ end }}
{{- if ...}}
的方式消除此空行.如: apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" {{- if eq .Values.favorite.drink "coffee"}} mug: true {{- end}}
apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} {{- end }}
range命令實現循環,如:函數
# values.yaml favorite: drink: coffee food: pizza pizzaToppings: - mushrooms - cheese - peppers - onions #configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" toppings: |- {{- range .Values.pizzaToppings }} - {{ . }} # .表示range的命令空間下的取值 {{- end }} {{- range $key, $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end}}
ApiVersion: v1 Kind: ConfigMap Metadata: name: {{ .Release.Name }}-configmap Data: myvalue: "Hello World" # 因爲下方的with語句引入相對命令空間,沒法經過.Release引入,提早定義relname變量 {{- $relname := .Release.Name -}} {{- with .Values.favorite }} food: {{ .food }} release: {{ $relname }} # 或者能夠使用$符號,引入全局命名空間 release: {{ $.Release.Name }} {{- end }}
公共模板,define定義,template引入,在templates目錄中默認下劃線_開頭的文件爲公共模板(_helpers.tpl)ui
# _helpers.tpl文件 {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }} # configmap.yaml文件 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap {{- template "mychart.labels" }} data: myvalue: "Hello World"
template語句的升級版本include,template是語句沒法在後面接管道符來對引入變量作定義,
include實現了此功能.this
# _helpers.tpl文件 {{- define "mychart.app" -}} app_name: {{ .Chart.Name }} app_version: "{{ .Chart.Version }}+{{ .Release.Time.Seconds }}" {{- end -}} # configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap labels: {{- include "mychart.app" . | nindent 4 }} data: myvalue: "Hello World" {{- range $key, $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end }} {{- include "mychart.app" . | nindent 2 }} # 若是使用template只能手動空格,不能使用管道後的nindent函數來作縮進
helm install stable/drupal --set image=my-registry/drupal:0.1.0 --set livenessProbe.exec.command=[cat,docroot/CHANGELOG.txt] --set livenessProbe.httpGet=null<br/>
,livenessProbe在values.yaml中定義了httpGet,須要手動設置爲null,而後設置exec的探針.