安装helm
1 curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
更多信息参考:Helm官方文档
helm命令
repo相关
1 2 3 4 5 6 7 8 9 10 helm repo list [-o yaml] helm repo add <repo_name> <repo_url> helm repo update [REPO] helm repo remove [REPO] helm repo index <chart_path> --merge <已有的index.yaml> --url <url> helm registry helm registry login <host> -u <username> -p <password> helm registry logout <host>
chart相关
1 2 3 4 5 6 7 8 helm search helm search repo [repo_name] <keyword> helm search hub <keyword> helm show helm show chart <chart_path> helm show values <chart_path> helm show crds <chart_path> helm show all <chart_path>
1 2 3 4 helm create <chart_path> helm package <chart_path> helm pull <repo/chart_name> [--version] helm push <chart_path> <repo>
Dependency相关
1 2 3 helm dep list <chart_path> helm dep update <chart_path> helm dep build <chart_path>
Release相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 helm list -a -A -n --deployed --failed --pending --superseded --uninstalled --uninstalling helm get helm get values <release_name> --revision int -o, --output format helm get notes <release_name> helm get manifest <release_name> helm get hooks <release_name> helm get metadata <release_name> helm get all <release_name> helm status <release_name> helm install <release_name> <chart_path> helm template <release_name> <chart_path> helm uninstall <release_name> helm upgrade <release_name> <chart_path> helm history <release_name> helm rollback <release_name> <revision> helm test <release_name>
chart包详解 helm工具安装的都是chart包,chart包是一组相关联的 Kubernetes 资源文件集合,一个大文件夹里包含了很多资源配置文件。
以wordpress为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 wordpress/ ├── Chart.lock ├── Chart.yaml ├── README.md ├── charts/ │ ├── common/ │ ├── mariadb/ │ └── memcached/ ├── crds/ ├── templates/ │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── deployment.yaml │ ├── ...........yaml ├── values.schema.json └── values.yaml
Chart.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 apiVersion: v2 name: wordpress version: 24.1 .5 appVersion: 6.7 .1 type: application dependencies: - name: memcached version: 7. x.x repository: "file://../memcached" description: functional websites keywords: - blog - wordpress
templates目录
所有的模板文件都在chart包的templates目录下面,当Helm渲染templates目录中的模板文件的时候,模板文件依赖外部提供的value值,主要有三种方式提供:
命令行中指定的 --set
参数,这种方式优先级最高,但并不常用
用户使用 helm install -f
来指定的value文件
默认的 values.yaml
文件
上面的三种方式按照优先级对低优先级提供的值有覆盖效果。除此之外,模板中还可以预定义一些value值,比如Release.Name
,Chart.Version
等内置对象。
templates目录中的模板文件遵循Go模板的标准约定,详细语法参考
values.yaml
下面是一个values.yaml的示例:
1 2 3 4 5 6 7 8 9 10 11 12 global: imageRegistry: "" imagePullSecrets: [] image: registry: docker.io repository: bitnami/wordpress tag: 6.7 .1 -debian-12-r9 memcached: max_connections: 100 password: xxxxxx
在values.yaml
对模板文件进行传值的时候,有作用域的概念:
对于当前应用的模板文件来说,可以访问一切values.yaml
文件中的内容,用法如下:
1 2 .Values.image.registry .Values.memcached.password
对于依赖chart包中的模板文件来说,它所能访问的内容,只包含全局参数,以它的chart包命名的键名包含的参数:
1 2 3 .Values.global.imageRegistry .Values.max_connections .Values.password
dependencies包
在 Helm 中,一个 chart 包可能会依赖许多其他的 chart,可以在Chart.yaml文件中通过dependencies
字段指定,定义了依赖项后,可以运行 helm dependency update
来更新依赖项,它会解析 Chart.yaml 中定义的依赖项,会将他们作为 chart 包文件下载存放到 charts/ 目录。
在某些场景下,一个业务Chart需要对一个依赖Chart复用多次,此时可以通过alias
字段实现
1 2 3 4 5 6 7 8 9 10 11 12 13 apiVersion: v2 name: logapp description: A Helm chart for a logging application version: 1.0 .0 dependencies: - name: log-component version: "0.1.0" repository: "file://../log-component" alias: log-collector - name: log-component version: "0.1.0" repository: "file://../log-component" alias: log-processor
在上面这个Chart.yaml文件中,可以看到对同一个依赖chart包引用了两次,指定了不同的alias
字段,可以看作是不同的chart包,然后通过Values.yaml文件传值的时候,可以根据alias
定义不同的作用域
1 2 3 4 log-collector: replicaCount: 2 log-processor: replicaCount: 1
chart包开发
内置对象 除了从values.yaml文件中取值,还可以从一些内置对象中取值
Release
获取Release信息
1 2 3 4 5 .Release.Name # Release名称 .Release.Namespace # Release所在的名称空间 .Release.IsInstall # 是否是首次安装 .Release.IsUpgrade # 是否是升级 .Release.Revision # 修订号
Chart
获取Chart包信息
1 2 3 .Chart.Name # Chart包的名称 .Chart.Version # Chart包的版本号 .Chart.AppVersion # Chart包的应用版本号
Files
获取其他文件的内容
1 .Files.Get "username.txt" | trim # 获取username.txt文件的内容,并通过管道和trim函数去除空白
Capabilities
获取有关 Kubernetes 集群的信息
1 2 3 .Capabilities.KubeVersion # 当前kubernetes版本 .Capabilities.KubeVersion.Major # 主版本号 .Capabilities.KubeVersion.Minor # 次版本号
Template
获取Template目录信息
1 2 .Template.Name # 当前模板文件的路径,精确到当前yaml文件 .Template.BasePath # 当前模板文件的目录
函数与管道 用法: {{ 函数名 参数1 参数2 }}
结合管道:{{ 值 | 函数名 }}
常见函数:
1 2 3 4 5 6 {{.Values.favorite.drink | quote}} {{.Values.favorite.food | upper | quote}} {{.Values.favorite.drink | repeat 5}} {{.Values.favorite.drink | default "tea" | quote}} {{ .Values.favorite.food | default (printf "%s-tea" (include "mychart.name" .)) }}
流程控制
条件判断 语法:
1 2 3 4 5 6 7 {{ if 条件1 }} # Do something {{ else if 条件2 }} # Do something else {{ else }} # Default case {{ end }}
示例:
1 2 3 4 5 6 7 {{- if .Values.db.debugMode | not}} # 判断值是否存在,然后对结果取反 name: DB_DEBUG1 value: "false" {{- else }} name: DB_DEBUG2 value: "true" {{- end }}
判断条件可以是比较运算符:eq
、ne
、lt
、gt
1 2 3 4 5 6 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: {{ if eq .Values.favorite.drink "coffee" }}mug: true {{ end }}
判断条件里也可以引入and、or ,也可以用括号()
进行分割
1 2 3 4 5 6 7 8 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: {{- if and (eq .Values.favorite.drink "coffee" ) (gt (int .Values.replicaCount) 3 ) }} mug: true {{- end }}
with作用域 需要连续地渲染一些层级比较多的参数,可以把公共的层级放到with作用域里面
values.yaml:
1 2 3 4 5 6 db: debugMode: true dbinfo: user: egon pass: "123" port: 3306
yaml清单:
1 2 3 4 5 6 7 8 9 10 11 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: {{- with .Values.db.dbinfo }} user: {{ .user }} pass: {{ .pass }} port: {{ .port }} {{- end }} release: {{ .Release.Name }}
range作用域 可以从给定的一组对象中遍历取参数
values.yaml
1 2 3 4 5 6 7 8 favorite: drink: coffee food: pizza names: - egon - lili - jack - tom
资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" nameList1: |- {{- $relname := .Release.Name -}} # 这里定义了一个变量,代替了一个对象的值 {{- range .Values.names }} - "{{ . | title }}-{{ $relname }}" # 在作用域中使用变量,解决了作用域中不能使用内置对象的缺陷 {{- end }} nameList2: |- {{- range tuple "EGON1" "EGON2" "EGON3" }} # 直接声明一个数组来遍历 - {{ . }}-{{ $relname }} {{- end }} nameList3: |- {{- range .Values.favorite }} - {{ . | quote }} {{- end }} nameList4: |- {{- range $key,$value := .Values.favorite }} {{ $key }}: {{ $value | quote }} {{- end }} nameList5: |- {{- range $index,$value := .Values.names }} {{ $index }}: {{ $value | quote }} {{- end }}
空行控制
模版语法渲染之后遗留的换行符用 -
去掉
内容部分的缩进空格,可以自己人为控制缩进空格数,也可以使用 indent
和 nindent
控制
indent
往右缩进n个空格
nindent
往左缩进n个空格
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | default "tea" | quote }} food: {{ .Values.favorite.food |default "abc" | upper | quote }} {{- if and (eq .Values.favorite.drink "coffee" ) (gt (int .Values.replicaCount) 3 ) }} {{ nindent 2 "mug: true" }} {{- end }} xxx: aaaa yyy: bbbb
命名模板 可以在templates目录中创建一个__helpers.tpl
文件,里面定义一些可复用的模板,可以被其他资源清单文件直接引用
_helpers.tpl
1 2 3 4 5 6 {{/* Generate basic labels */ }} {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }}
清单文件
1 2 3 4 5 6 7 8 9 10 11 apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap {{- template "mychart.labels" }} {{ include "mychart.app" . | indent 4 }} data: myvalue: "Hello World" {{- range $key , $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end }}
注意:
1 2 如果命名模板中包含了类似与.Chart.Version的全局参数,需要在清单文件中提供一个模板范围,否则无法渲染 {{- template "mychart.labels" . }}
harbor仓库 日常的部署工作经常会用到一些公共仓库,非常的方便。如果需要自己开发chart包的话,通常推送到私有仓库,本次以harbor为例,介绍将harbor作为chart仓库的用法。
从 Helm3开始,可以使用具有 OCI 支持的容器注册中心来存储和共享chart包。从Helm v3.8.0开始,默认启用OCI支持。
Harbor作为一款云原生制品仓库,同样启用了OCI支持。
Harbor v2.8 版本之前
如果Harbor版本低于v2.8,安装harbor时需要启用chartmuseum
1 ./install.sh --with-chartmuseum
如果是后期修改配置文件,可以使用 ./prepare --with-chartmuseum
后,再 docker-compose up -d
。
启用后Harbor中会有独立的Helm Charts页面。支持UI上传、helm push命令行 两种上传chart的方式。
Harbor v2.8 版本之后
如果Harbor版本大于等于v2.8,页面中取消了单独的 Helm Charts 页面,Charts包与 Image 保存在相同目录下。只能通过helm push命令行方式推送。
1 2 3 4 5 6 7 8 helm registry login <host> -u <username> -p <password> helm package <chart_path> helm push <chart_name-version.tgz> oci://<host>/<project>/ helm pull oci://<host>/<project>/<chart_name> --version <version>