重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
快速创建一个chart模板,helm create mychart
,执行命令后本地生成一个mychart目录.
创新互联自2013年起,先为蓬莱等服务建站,蓬莱等地企业,进行企业商务咨询服务。为蓬莱企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
一个最小的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"
- go-template双
- sprig
模板引用方式,{{ .Release.Name }}
, 通过双括号注入,小数点开头表示从最顶层命名空间引用.
# 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/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}}
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
,livenessProbe在values.yaml中定义了httpGet,需要手动设置为null,然后设置exec的探针.