cyj
微服务
Accelerator
About
# Helm 之 Chart 模板 > [Chart Template Guide](https://helm.sh/docs/chart_template_guide/getting_started/) 本文介绍 Chart 模板内容,信息来自官网文档。 ## Chart 文件结构 使用 Helm 创建一个 nginx chart demo,文件结构如下: ```bash $helm create nginx $tree nginx nginx ├── Chart.yaml # chart版本和配置信息 ├── charts # 依赖信息 ├── templates # K8s 资源模板信息, 结合 values.yaml 可生成K8s对象的manifest文件 │ ├── NOTES.txt # helm 提示信息 │ ├── _helpers.tpl # 下划线开头,作为子模板,可被其他模板文件引用,Helm不会交给K8s处理 │ ├── deployment.yaml │ ├── ingress.yaml │ ├── service.yaml │ ├── serviceaccount.yaml │ └── tests │ └── test-connection.yaml └── values.yaml # chart默认配置 3 directories, 9 files ``` * `templates/` 目录下的文件都会作为K8s的 manifests 处理 * NOTES.txt 不会被处理 * **"_"** 下划线开头的都不会被处理 ## Chart 模板 Helm 使用 [Go Template](https://godoc.org/text/template) 渲染chart的数据。 ### 内置对象 > [Build-in Objects](https://helm.sh/docs/chart_template_guide/builtin_objects/) Chart 预定义对象可直接在各模板中使用。 * Release:代表Release对象,属性包含:Release.Name、Release.Namespace、Release.Revision等 * Values:表示 `values.yaml` 文件数据 * Chart:表示`Chart.yaml` 数据 * Files:用于访问chart中非标准文件 * Capabilities:用于获取k8s集群的一些信息 * Capabilities.KubeVersion.Major:K8s的主版本 * Template:表示当前被执行的模板 * Name:表示模板名,如:`mychart/templates/mytemplate.yaml` * BasePath:表示路径,如:`mychart/templates` ### 流程控制 > [Flow Control](https://helm.sh/docs/chart_template_guide/control_structures/) #### if/else 语法: ```yaml {{ if PIPELINE }} # Do something {{ else if OTHER PIPELINE }} # Do something else {{ else }} # Default case {{ end }} ``` demo: ```yaml {{- if .Values.serviceAccount.create -}} apiVersion: v1 kind: ServiceAccount {{- end -}} ``` #### with 指定范围 with 用于修改作用域,正常 **"."** 代表全局作用域,with 可以修改 **"."** 的含义。 语法: ``` {{ with 被引用的对象 }} . 用来引用with指定的对象 {{ end }} ``` `values.yaml` : ```yaml ingress: enabled: false annotations: kubernetes.io/ingress.class: nginx kubernetes.io/tls-acme: "true" ``` 使用 with: ```yaml kind: Ingress metadata: # 指定引用对象为 ingress.annotations {{- with .Values.ingress.annotations }} annotations: # "." 点代表该对象 {{- toYaml . | nindent 4 }} {{- end }} ``` #### range 遍历 用于遍历数组 语法: ``` {{- range 数组 }} "." 用于引用数组元素 {{- end }} ``` 遍历map示例: `values.yaml` 数据: ```yaml env: open: JAVA_OPTS: "" SPRING_CLOUD_CONFIG_ENABLED: true ``` 使用示例: ```yaml spec: containers: env: {{- range $k, $v := .Values.env.open }} - name: {{ $k | quote }} value: {{ $v | quote }} {{- end }} ``` 遍历数组示例: ```yaml fruits: - apple - orange ``` ```yaml {{- range .Values.fruits}} # 用 . 表示数组元素 {{ . | quote }} {{- end}} ``` ### 函数与管道 模板中可使用的函数与管道,管道与Unix中类似,Helm 有超过60个函数。 官网例子: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: # 语法1: functionName arg1 arg2 food: {{ quote .Values.favorite.food }} # 语法2: 使用管道 drink: {{ .Values.favorite.drink | quote }} ``` 通过 **quote** 函数为参数添加 "双引号",渲染后的数据如下: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: food: "Pizza" drink: "Coffe" ``` #### include 函数 在chart中以 "下划线" 开头的文件,称为"子模版"。 例如在 `_helper.tpl` 中定义子模块,格式:`{{- define "模版名字" -}} 模版内容 {{- end -}}` ```yaml {{- define "nginx.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} {{- end -}} ``` 引用模板,格式:`{{ include "模版名字" 作用域}}` ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "nginx.fullname" . }} ``` #### default 默认值 通过 default 函数设置默认值 ```yaml drink: {{ .Values.favorite.drink | default "tea" | quote }} ``` #### nindent 缩进 indent 表示缩进。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "nginx.fullname" . }} labels: # 缩进 4个空格 {{- include "nginx.labels" . | nindent 4 }} ``` #### toYaml 转yaml 将数据转为yaml格式 ```yaml spec: strategy: {{ toYaml .Values.strategy | indent 4 }} ``` values.yaml数据: ```yaml strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 0 ``` 渲染效果: ```yaml spec: strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 0 ``` ### 变量定义与引用 ```yaml # 定义变量 fullName {{- $fullName := include "nginx.fullname" . -}} kind: Ingress metadata: # 引用变量 name: {{ $fullName }} ``` ### 子模版 子模版也叫 Named Templates、SubTemplate. #### 使用 define 定义子模版 语法: ``` {{ define "MY.NAME" }} # body of template here {{ end }} ``` 例子: ```yaml {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }} ``` #### 使用template 引用子模板 ```yaml {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }} apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap {{- template "mychart.labels" }} data: myvalue: "Hello World" {{- range $key, $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end }} ``` ### 调试模板 Helm提供几种调试命令: ```bash # 验证chart是否符合最佳实践 helm lint # 适合渲染本地的chart helm install --dry-run --debug helm template --debug chartDir # 适合用于查看服务器上安装好的chart helm get manifest ```
本文由
cyj
创作,可自由转载、引用,但需署名作者且注明文章出处。
文章标题:
Helm 之 Chart 模板
文章链接:
https://chenyongjun.vip/articles/136
扫码或搜索 cyjrun 关注微信公众号, 结伴学习, 一起努力