helm模板文件chart编写语法详解
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,charts编写介绍开始快速创建一个chart模板,helm create mychart,执行命令后本地生成一个mychart目录.chart目录结构Chart.yaml: 该chart的描述文件,
千家信息网最后更新 2025年12月02日helm模板文件chart编写语法详解
charts编写介绍
开始
快速创建一个chart模板,helm create mychart,执行命令后本地生成一个mychart目录.
chart目录结构
- Chart.yaml: 该chart的描述文件,包括ico地址,版本信息等
- vakues.yaml: 给模板文件使用的变量
- charts: 依赖其他包的charts文件
- requirements.yaml: 依赖的charts
- README.md: 开发人员自己阅读的文件
- 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双- sprighelm模板语法
模板引用方式,
{{ .Release.Name }}, 通过双括号注入,小数点开头表示从最顶层命名空间引用.- helm内置对象
# Release, release相关属性# Chart, Chart.yaml文件中定义的内容# Values, values.yaml文件中定义的内容 - 模板中使用管道
apiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmapdata: 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: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmapdata: myvalue: "Hello World" {{- if eq .Values.favorite.drink "coffee"}} mug: true {{- end}} - 引入相对命名空间,with命令:
apiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmapdata: myvalue: "Hello World" {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} {{- end }} range命令实现循环,如:
# values.yamlfavorite: drink: coffee food: pizzapizzaToppings: - mushrooms - cheese - peppers - onions#configmap.yamlapiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmapdata: myvalue: "Hello World" toppings: |- {{- range .Values.pizzaToppings }} - {{ . }} # .表示range的命令空间下的取值 {{- end }} {{- range $key, $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end}}- 变量赋值
ApiVersion: v1Kind: ConfigMapMetadata: name: {{ .Release.Name }}-configmapData: 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: v1kind: ConfigMapmetadata: 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.yamlapiVersion: v1kind: ConfigMapmetadata: 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的探针.
文件
模板
目录
命令
空间
语句
变量
内容
开头
语法
下划线
手动
方式
版本
空行
管道
资源
最小
人员
信息
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
华为服务器管理口v2
奇安信融媒体的网络安全
网络技术学习目标
科技互联网巨头股票
强化ipv6网络安全保障
橘子平台更新哪个服务器
2018年金融行业网络安全
酒店租房软件开发
绝地求生应该选什么服务器
民警检查网络安全
软件开发计算机培训学校招生要求
google管理服务器
代理服务器通讯设置失败
江苏省技能大赛网络技术
四川网络安全工业园
软件开发和网络工程哪个好学
计算机网络技术编程是什么
网络安全人才培养工程
卓航网络技术服务有限公司
我国现行的网络安全
张莉家庭教育与网络安全视频
更新了怎么服务器未响应
服务器管理器怎么进
初中学历学软件开发合适吗
幼儿园网络安全活动小结
c 软件开发面试自我介绍
php获取数据库行数据
数据库创建角色报错
2u服务器详解
4g手机无线网络安全思路