[kubernetes]-Helm Chart模板详述和样例

helm create mychart
cd mychart/templates
rm -rf  *

cat  > configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart-configmap
data:
  mychart: "Hello world"
EOF

cd ..
helm install  mychart  ./
# 或者随机名称 helm install  ./ --generate-name

获取资源的配置信息

helm  get manifest mychart

通过release上传 避免出现无法重复使用一个configmap的情况

cat  > configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
EOF

helm  upgrade   mychart ./

如果是用随机名称生成的会是下面这样

helm  install   ./ --generate-name
# 查看chart-1619506963的yaml
helm get manifest chart-1619506963

删除

# helm3 似乎没有--purge 
helm  delete  chart-1619506963
helm  list --all

dry-run功能

helm install --dry-run --debug --generate-name  ./

通过修改values.yaml 引入自己的变量

cat /dev/null > values.yaml
cat > values.yaml <<EOF
favoriteDrink: coffee
EOF
 
cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favoriteDrink  }}
EOF

helm install --dry-run --debug --generate-name  ./
helm install --dry-run --debug --generate-name  --set favoriteDrink=slume ./

模版中结构化数据

cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza
EOF
 
cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink  }}
  food: {{  .Values.favorite.food  }}
EOF

helm install --dry-run --debug --generate-name  ./

如果需要livenessProbe

cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza
livenessProbe:
  httpGet:
    path: /user/login
    port: http
  initialDelaySeconds: 120
EOF
 
cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink  }}
  food: {{  .Values.favorite.food  }}
EOF

helm install --dry-run --debug --generate-name  ./

# 使用exec检测的话 需要置空httpGet  不允许2种同时存在 是不合法的
helm install --set livenessProbe.exec.command=[cat, docroot/changelog.txt] --set livenessProbe.httpGet=null  .

函数的用法

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  quote .Values.favorite.drink  }}
  food: {{  quote .Values.favorite.food  }}
EOF

发现参数上有双引号 说明函数quote(引用函数)生效了

通过管道传递函数

管道可串联多个函数

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | repeat 5 | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}
EOF

给drink 定义一个默认的值

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | default "tea" | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}
EOF

cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
#  drink: coffee
  food: pizza

EOF

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | default (printf "%s-tea" .Release.Name) | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}
EOF

运算符函数

# 不运行
cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | default (printf "%s-tea" .Release.Name) | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}

{{ if and .Values.foo (eq .Values.foo "foo") }}
EOF

模版中的if else语句

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | default (printf "%s-tea" .Release.Name) | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}

{{ if eq .Values.foo "foo" }}
  # do something 
{{ else }}
  # default case
{{ end }}
EOF
cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  drink: {{  .Values.favorite.drink | default (printf "%s-tea" .Release.Name) | quote  }}
  food: {{  .Values.favorite.food | upper | quote  }}

# 先判断drink存不存在 if and .Values.favorite.drink ;  再判断favorite是不是coffee  eq .Values.favorite.drink "coffee"
  #{{  if and .Values.favorite.drink (eq .Values.favorite.drink "coffee") }}mug: true{{ end }}
  #上面这行分开写如下  {{- }}用于删除空行 避免语句换行
  {{- if and .Values.favorite.drink (eq .Values.favorite.drink "coffee") }}
  mug: true
  {{- end }}

EOF
cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza

EOF

使用with语句控制作用域位置

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  {{- end }}
  release: {{ .Release.Name }}


EOF
cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza

EOF

range循环

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  {{- end }}
  release: {{ .Release.Name }}
  toppings: |-
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }}


EOF
cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza
pizzaToppings:
  - mushrooms
  - cheese
  - peppers
EOF

渲染元组

cat  > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  {{- end }}
  release: {{ .Release.Name }}
  sizes: |-
    {{- range tuple "small" "medium"  "large"}}
    - {{ . }}
    {{- end }}

EOF
cat > values.yaml <<EOF
favoriteDrink: coffee
favorite:
  drink: coffee
  food: pizza
pizzaToppings:
  - mushrooms
  - cheese
  - peppers
EOF

变量的使用

cat > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- \$relname := .Release.Name }}
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$relname }}
  {{- end }}
EOF

循环中使用变量

cat > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- \$relname := .Release.Name }}
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$relname }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
EOF

遍历字典

cat > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- \$relname := .Release.Name }}
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$relname }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
  {{- range \$key, \$val := .Values.favorite }}
  {{ \$key }}: {{ \$val | quote }}
  {{- end }}
    
EOF

变量的其他用法

cat > templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ $.Release.Name }}
  {{- end }}
  toppings: |-
    {{- range $index, $topping := .Values.pizzaToppings }}
    {{ $index }}: {{ $topping }}
    {{- end }}
  {{- range $key, $val := .Values.favorite }}
  {{ $key }}: {{ $val | quote }}
  {{- end }}
EOF

在文件中命名模版并使用

cat > templates/_helpers.tpl <<EOF
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
{{- end }}
EOF

cat >  templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
# 引用模版
  {{- template "mychart.labels" }}
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$.Release.Name }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
  {{- range \$key, \$val := .Values.favorite }}
  {{ \$key }}: {{ \$val | quote }}
  {{- end }}
EOF

helm install --dry-run --debug --generate-name  ./

cat > templates/_helpers.tpl <<EOF
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
    chart: {{ .Chart.Name }}
    version: {{ .Chart.Version }}
{{- end }}
EOF

cat >  templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
# 引用模版 并传入最顶级的作用域
  {{- template "mychart.labels" .  }}
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$.Release.Name }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
  {{- range \$key, \$val := .Values.favorite }}
  {{ \$key }}: {{ \$val | quote }}
  {{- end }}
EOF

helm install --dry-run --debug --generate-name  ./

关于如何公用标签mychart.app

这里我用helm3测试出问题了 报错at <.Release.Time.Seconds>: nil pointer evaluating interface {}.Seconds 等后续再验证

cat > templates/_helpers.tpl <<EOF
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
    chart: {{ .Chart.Name }}
    version: {{ .Chart.Version }}
{{- end }}
{{- define "mychart.app" -}}
app_name: {{ .Chart.Name }}
app_version: "{{ .Chart.Version }}+{{ .Release.Time.Seconds }}"
{{- end -}}
EOF

cat >  templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
# 引用模版 
  labels:
  {{ include "mychart.app" . | indent 4 }}
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$.Release.Name }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
  {{- range \$key, \$val := .Values.favorite }}
  {{ \$key }}: {{ \$val | quote }}
  {{- end }}
  # 引用模版   
  {{ include "mychart.app" . | indent 2 }}
EOF

helm install --dry-run --debug --generate-name  ./

在模版中读取文件

cat > config1.toml <<EOF
message = "hello from config1"
EOF

cat > config2.toml <<EOF
message = "hello from config2"
EOF



cat > templates/_helpers.tpl <<EOF
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
    chart: {{ .Chart.Name }}
    version: {{ .Chart.Version }}
{{- end }}
{{- define "mychart.app" -}}
app_name: {{ .Chart.Name }}
app_version: "{{ .Chart.Version }}+{{ .Release.Time.Seconds }}"
{{- end -}}
EOF

cat >  templates/configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{  .Release.Name  }}-configmap
# 引用模版 
  labels:
  {{ include "mychart.app" . | indent 4 }}
data:
  mychart: "Hello world"
  {{- with .Values.favorite }}
  drink: {{  .drink | quote  }}
  food: {{  .food | upper | quote  }}
  release: {{ \$.Release.Name }}
  {{- end }}
  toppings: |-
    {{- range \$index, \$topping := .Values.pizzaToppings }}
    {{ \$index }}: {{ \$topping }}
    {{- end }}
  {{- range \$key, \$val := .Values.favorite }}
  {{ \$key }}: {{ \$val | quote }}
  {{- end }}
  # 引用模版   
  {{ include "mychart.app" . | indent 2 }}
    {{- \$files := .Files }}
    {{- range tuple "config1.toml" "config2.toml" }}
    {{ . }}: |-
      {{ \$files.Get . }}
    {{- end}}
EOF

helm install --dry-run --debug --generate-name  ./

放个教程里成功的图

子模版 未做笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值