charts編寫介紹html
開始api
快速建立一個chart模板,helm create mychart,執行命令後本地生成一個mychart目錄.app
chart目錄結構ide
Chart.yaml: 該chart的描述文件,包括ico地址,版本信息等函數
vakues.yaml: 給模板文件使用的變量ui
charts: 依賴其餘包的charts文件this
requirements.yaml: 依賴的chartslua
README.md: 開發人員本身閱讀的文件htm
templates: 存放k8s模板文件目錄對象
NOTES.txt 說明文件,helm install以後展現給用戶看的內容
deployment.yaml 建立k8s資源的yaml文件
_helpers.tpl: 下劃線開頭的文件,能夠被其餘模板引用.
一個最小的chart目錄,只須要包含一個Chart.yaml,和templates目錄下一個k8s資源文件.如:
# 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"
helm的模板語法實現原理:
- go-template雙
- sprig
helm模板語法
模板引用方式,{{ .Release.Name }}, 經過雙括號注入,小數點開頭表示從最頂層命名空間引用.
helm內置對象
# 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語句
{{ if PIPELINE }} # Do something {{ else if OTHER PIPELINE }} # Do something else {{ else }} # Default case {{ end }}
操做符, and/eq/or/not
{{/* 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}}
引入相對命名空間,with命令:
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)
# _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實現了此功能.
# _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的探針.