一、命名模板¶
命名模板类似于开发语言中的函数,指一段可以直接被另一段程序或代码引用的程序或代码。
在编写chart时,可以将一些重复使用的内容写在命名模板文件中供公共使用,这样可减少重复编写程序段和简化代码结构。
命名模块使用define定义,template(不支持管道)或 include 引入,在templates目录中默认下划线开头的文件为公共模板(_helpers.tpl)。
# 定义_helpers.tpl文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim _helpers.tpl
{{- define "fullname" -}}
{{- .Chart.Name -}}-{{ .Release.Name }}
{{- end -}}
# 定义deployment.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ .Release.Name |indent 6}}
app: {{ .Release.Name |nindent 6}}
name: {{ include "fullname" . }}
1、用define和template声明和使用模板
define操作允许我们在模板文件中创建一个命名模板,语法格式 如下:
{{- define "MY.NAME" }}
# body of template here
{{- end }}
比如我们可以定义一个模板用来封装控制器的标签
# 定义_helpers.tpl文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim _helpers.tpl
{{- define "mychart.labels" }}
labels:
generator: helm
date: {{ now | htmlDate }}
{{- end }}
现在我们将模板嵌入到了已有的配置映射中,然后使用 template 包含进来:
# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
{{- define "mychart.labels" }}
labels:
generator: helm
date: {{ now | htmlDate }} //用于将日期格式化为 HTML 可接受的格式。
{{- 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 }}
当模板引擎读取该文件时,它会存储mychart.labels的引用直到template"mychart.labels"被调用。 然后会按行渲染模板,因此结果类似这样:
# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 17:07:57 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mytestfunction-configmap
labels:
generator: helm
date: 2023-11-13
data:
myvalue: "Hello World"
drink: "coffee"
food: "pizza"
注意:define不会有输出,除非像本示例一样用模板调用它。
按照惯例,Helm chart将这些模板放置在局部文件中,一般是 _helpers.tpl 。
{{/* Generate basic labels */}}
{{- define "mychart.labels" }}
labels:
generator: helm
date: {{ now | htmlDate }}
{{- end }}
2、include 方法
以下是 include 指令的语法:
{{- include "path/to/template" . }}
其中,第一个参数是要包含的子模板的路径(可以是相对路径或绝对路径)。第二个参 数是当前上下文中可用的数据。
注意,当引用相对路径时,Helm 会在指定路径中寻找一个名为 _helpers.tpl 的文 件,并将其视为项的一部分,以便在包含的模板中使用。
以下是一个具体的示例,其中 {{- include "configmap.tpl" . }} 指令包含了 templates/configmap.tpl 文件中的模板:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.configMapName }}
data:
{{- include "configmap.tpl" . }}
include 相较于使用template,在helm中使用include被认为是更好的方式 只是为了更好地处理YAML文档的输出格式。
二、NOTES.txt 文件¶
在helm install 或 helm upgrade命令的最后,Helm会打印出对用户有用的信息。 使用模板可以高度自定义这部分信息。
要在chart添加安装说明,只需创建 templates/NOTES.txt文件即可。该文件是纯文本,但会像模板一样处理, 所有正常的模板函数和对象都是可用的。让我们创建一个简单的NOTES.txt文件:
# 定义NOTES.txt文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim NOTES.txt
Thank you for installing {{ .Chart.Name }}.
Your release is named {{ .Release.Name }}.
To learn more about the release, try:
$ helm status {{ .Release.Name }}
$ helm get all {{ .Release.Name }}
执行 helm install myfirsthelm ./mychart 会在底部看到
# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 18:58:17 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mytestfunction-configmap
labels:
generator: helm
date: 2023-11-13
data:
myvalue: "Hello World"
drink: "coffee"
food: "pizza"
NOTES:
Thank you for installing myapp.
Your release is named mytestfunction.
To learn more about the release, try:
$ helm status mytestfunction
$ helm get all mytestfunction
使用NOTES.txt这种方式是给用户提供关于如何使用新安装的chart细节信息的好方法。
尽管并不是必需的,强烈建议创建一个 NOTES.txt 文件。
三、Helm 模板调试¶
1、 helm --set 函数
通过 --set 选项,用户可以设置一些应用程序的值,这些值是可以替代values.yaml中的默认值;
例如,如果用户需要在部署 Helm Chart 时动态地设置 Redis 密码和端口号,可以使用以下命令:
$ helm install my-release bitnami/redis --set password=thisispwd
2、helm --dry-run函数
调试模板可能很棘手,因为渲染后的模板发送给了Kubernetes API server,可能会以格式化以外的原因拒绝YAML文件。以下命令有助于调试:
- 调试模板可能很棘手,因为渲染后的模板发送给了Kubernetes API server,可能会以格式化以外的原因拒绝YAML文件。以下命令有助于调试:
- helm get manifest : 这是查看安装在服务器上的模板的好方法。
当你的YAML文件解析失败,但你想知道生成了什么,检索YAML一个简单的方式是注释掉模板中有问题的部分, 然后重新运行helm install --dry-run ;
四、Helm Chart 详解¶
4.1 Chart 目录结构¶
创建一个chart示例
[root@master01 ~]# cd /root/3/
[root@master01 3]# helm create nginx
[root@master01 3]# tree nginx/
nginx/
├── charts #依赖其他包的charts文件
├── Chart.yaml #该chart的描述文件,包括ico地址,版本信息等
├── templates #存放k8s模板文件目录
│ ├── deployment.yaml #创建k8s资源的yaml模板
│ ├── _helpers.tpl #下划线开头的文件,可以被其他模板引用
│ ├── hpa.yaml #弹性扩缩容,配置服务资源CPU内存
│ ├── ingress.yaml #ingress 配合service域名访问的配置
│ ├── NOTES.txt #说明文件,helm install之后展示给用户看的内容
│ ├── serviceaccount.yaml #服务账号配置
│ ├── service.yaml #kubernetes Serivce yaml模板
│ └── tests #测试模块
│ └── test-connection.yaml
└── values.yaml #给模板文件使用的变量
4.2 Chart.yaml 文件¶
apiVersion: chart API 版本 (必需)
name: chart名称 (必需)
version: chart 版本号(必需)
kubeVersion: 兼容Kubernetes版本(可选)
description: 一句话对这个项目的描述(可选)
type: chart类型 (可选)
keywords:
- 关于项目的一组关键字(可选)
home: 项目home页面的URL (可选)
sources:
- 项目源码的URL列表(可选)
dependencies: # chart 必要条件列表 (可选)
- name: chart名称 (nginx)
version: chart版本 ("1.2.3")
repository: (可选)仓库URL ("https://example.com/charts") 或别名("@repo-name")
condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g.subchart1.enabled )
tags: # (可选)
- 用于一次启用/禁用 一组chart的tag
import-values: # (可选)
- ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项
alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用
maintainers: # (可选)
- name: 维护者名字 (每个维护者都需要)
email: 维护者邮箱 (每个维护者可选)
url: 维护者URL (每个维护者可选)
icon: 用做icon的SVG或PNG图片URL (可选)
appVersion: 包含的应用版本(可选)。不需要是语义化,建议使用引号
deprecated: 不被推荐的chart (可选,布尔值)
annotations:
example: 按名称输入的批注列表 (可选)
注意点:每个chart都必须有个版本号(version)
4.3 Chart 依赖管理¶
当前chart依赖的其他chart会在dependencies字段定义为一个列表。
dependencies:
- name: apache
version: 1.2.3
repository: https://example.com/charts
- name: mysql
version: 3.2.1
repository: https://another.example.com/charts
- name:字段是你需要的chart的名称
-
version:字段是你需要的chart的版本
-
repository:字段是chart仓库的完整URL。必须使用helm repo add在本地添加仓库,也可以使用仓库的名称代替URL;