1、if else条件判断
# 语法
{{ if 条件 }}
xxxx
{{ else }}
xxxx
{{ end }}
# 示例
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort # 将service的type添加进去
NodePort: 30000
[root@master-11 mychart]# cat templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-svc
labels:
app: {{ .Chart.Name }}
spec:
type: {{ .Values.type }}
selector:
app: {{ .Chart.Name }}
ports:
- port: 80
targetPort: 80
{{ if eq .Values.type "NodePort" }} # 在此处添加if条件判断,当type=NodePort时添加暴漏端口
nodePort: {{ .Values.NodePort }}
{{ end }}
[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: mychart-svc
labels:
app: mychart
spec:
type: NodePort
selector:
app: mychart
ports:
- port: 80
targetPort: 80
nodePort: 30000 # 此处nodePort被成功添加,但是前面有一个空行,这里可以使用{{- if }}在前面加一个“-”可以将空行去掉
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mychart
name: mychart
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
template:
metadata:
labels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
spec:
containers:
- image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:v1"
name: nginx
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 1
memory: 1Gi
env:
- name: address_a
value: 'bei'
- name: address_b
value: 'shang'
- name: address_c
value: 'zhou'
2、range遍历
# 基础
{{ range }}
xxx
{{ end }}
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort
NodePort: 30000
data: # 将下面这个数组添加上去
- aaa
- bbb
- ccc
- ddd
- eee
[root@master-11 mychart]# cat templates/configmap.yaml # 创建一个configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
{{- range .Values.data }} # 此处range .Values.data表示遍历data字段下的数组
- {{ quote . }} # .表示所有的内容,"-"每次遍历时都会在前面添加
{{- end }}
# 测试
[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data: # 此处数组已被遍历上
- "aaa"
- "bbb"
- "ccc"
- "ddd"
- "eee"
---
# 赋值
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort
NodePort: 30000
data: # 修改data的样式为环境变量的形式
name: range
age: 12
address: beijing
[root@master-11 mychart]# cat templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
{{- range $key,$value := .Values.data }} # 这里将.Values.data后两个字段的值赋给两个变量$key,$value,等同于$key=.Values.data.name字段,$key=.Values.data.name/age/address的值
{{ $key }}: {{ quote $value }}
{{- end }}
# 查看效果
[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
address: "beijing"
age: "12"
name: "range"
# 利用range创建多个configmap资源
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort
NodePort: 30000
config: # 在此处添加测试数据
nginx:
data:
comment: this is my nginx
address: http://www.nginx.local:80
tomcat:
data:
comment: this is my tomcat
address: http://www.tomcat.local:8080
mysql:
data:
comment: this is mysql
connection: 10.0.0.11:3306
[root@master-11 mychart]# cat templates/configmap.yaml
{{- range $name,$config := .Values.config }} # 在开头添加range遍历整个configmap,并将config后两个字段赋值给$name,$config
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $name }}
data:
{{- range $key,$value := $config.data }} # 再次将data字段的后两个值赋给$key,$value并进行遍历
{{ $key }}: {{ quote $value }}
{{- end }}
---
{{- end }}
[root@master-11 mychart]# helm template nginx . # 此时三个configmap成功生成
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
data:
comment: "this is mysql"
connection: "10.0.0.11:3306"
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx
data:
address: "http://www.nginx.local:80"
comment: "this is my nginx"
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: tomcat
data:
address: "http://www.tomcat.local:8080"
comment: "this is my tomcat"
---
3、with定义作用域
# 示例
{{ with }}
xxx
{{ end }}
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Chart.Name }}
name: {{ .Chart.Name }}
namespace: kube-system
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
app: {{ .Chart.Name | upper }}
version: {{ quote .Values.app.version }}
enable: {{ squote .Values.enable }}
chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
template:
metadata:
labels:
app: {{ .Chart.Name | upper }}
version: {{ quote .Values.app.version }}
enable: {{ squote .Values.enable }}
chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
spec:
containers:
{{- with .Values }} # 定义作用域为.Values后下面的变量就默认在values下进行查找,这样就可以省去打那么多.Values的麻烦了
- image: "{{ .image.repository }}:{{ .image.tag | default "v1" }}" # 下面的.Values全部去掉
name: nginx
resources: {{ toYaml .resources | nindent 10 }}
env:
- name: address_a
value: {{ squote ( .address.a | trunc 3 ) }}
- name: address_b
value: {{ squote ( .address.b | trimSuffix "hai" ) }}
- name: address_c
value: {{ squote ( .address.c | trimPrefix "guang" ) }}
{{ end }}
[root@master-11 mychart]# helm template nginx .
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mychart
name: mychart
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
template:
metadata:
labels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
spec:
containers: # 去掉values后面的内容正常显示
- image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:v1"
name: nginx
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 1
memory: 1Gi
env:
- name: address_a
value: 'bei'
- name: address_b
value: 'shang'
- name: address_c
value: 'zhou'
4、自定义模板
# 示例
{{ define name }}
xxx
{{ end }}
# 使用
{{- template name . }}
[root@master-11 mychart]# cat templates/_helpers.tpl # 创建一个模板文件_helpers.tpl并将deployment.yaml文件的标签部分全部剪切过来
{{- define "label" }} # 定义模板的开头格式,label是名字
selector:
matchLabels:
app: {{ .Chart.Name | upper }}
version: {{ quote .Values.app.version }}
enable: {{ squote .Values.enable }}
chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
template:
metadata:
labels:
app: {{ .Chart.Name | upper }}
version: {{ quote .Values.app.version }}
enable: {{ squote .Values.enable }}
chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
{{- end }} # 结尾
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Chart.Name }}
name: {{ .Chart.Name }}
namespace: kube-system
spec:
replicas: {{ .Values.replicas }}
{{- template "label" . -}} # 此处直接引用我们创建的自定义模板,label为自定义模板的名字,记得后面要加一个 .
{{- with .Values }}
spec:
containers:
- image: "{{ .image.repository }}:{{ .image.tag | default "v1" }}"
name: nginx
resources: {{ toYaml .resources | nindent 10 }}
env:
- name: address_a
value: {{ squote ( .address.a | trunc 3 ) }}
- name: address_b
value: {{ squote ( .address.b | trimSuffix "hai" ) }}
- name: address_c
value: {{ squote ( .address.c | trimPrefix "guang" ) }}
{{ end }}
# 安装
[root@master-11 mychart]# helm template nginx .
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mychart
name: mychart
namespace: kube-system
spec:
replicas: 1
selector: # 可以看见标签部分被添加上去了
matchLabels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
template:
metadata:
labels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
spec:
containers:
- image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:v1"
name: nginx
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 1
memory: 1Gi
env:
- name: address_a
value: 'bei'
- name: address_b
value: 'shang'
- name: address_c
value: 'zhou'
5、注释
# 格式
{{/* */}}
# 示例
[root@master-11 mychart]# cat templates/deployment.yaml
{{/* this is mychart */}} # 随便写点
{{/*
nginx chart
*/}}
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Chart.Name }}
name: {{ .Chart.Name }}
namespace: kube-system
spec:
replicas: {{ .Values.replicas }}
{{- template "label" . -}}
{{- with .Values }}
spec:
containers:
- image: "{{ .image.repository }}:{{ .image.tag | default "v1" }}"
name: nginx
resources: {{ toYaml .resources | nindent 10 }}
env:
- name: address_a
value: {{ squote ( .address.a | trunc 3 ) }}
- name: address_b
value: {{ squote ( .address.b | trimSuffix "hai" ) }}
- name: address_c
value: {{ squote ( .address.c | trimPrefix "guang" ) }}
{{ end }}
[root@master-11 mychart]# helm template nginx .
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mychart
name: mychart
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
template:
metadata:
labels:
app: MYCHART
version: "1"
enable: 'true'
chart: 'mychart_0.1.0'
spec:
containers:
- image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:v1"
name: nginx
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 1
memory: 1Gi
env:
- name: address_a
value: 'bei'
- name: address_b
value: 'shang'
- name: address_c
value: 'zhou'
6、子charts控制方法
# 创建一个mysql charts示例
[root@master-11 mychart]# cat charts/mysql/values.yaml
replicaCount: 1
image:
repository: mysql
tag: "8.0"
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 3306
mysqlRootPassword: rootpassword
mysqlDatabase: mydb
resources: {}
[root@master-11 mychart]# cat charts/mysql/Chart.yaml
apiVersion: v2
name: mysql
description: A simple MySQL chart example
type: application
version: 0.1.0
appVersion: "8.0"
[root@master-11 mychart]# cat charts/mysql/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-mysql
labels:
app: {{ .Release.Name }}-mysql
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Release.Name }}-mysql
template:
metadata:
labels:
app: {{ .Release.Name }}-mysql
spec:
containers:
- name: mysql
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: {{ .Values.mysqlRootPassword | quote }}
- name: MYSQL_DATABASE
value: {{ .Values.mysqlDatabase | quote }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
[root@master-11 mychart]# cat charts/mysql/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-mysql
labels:
app: {{ .Release.Name }}-mysql
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 3306
selector:
app: {{ .Release.Name }}-mysql
# 此时mysql 的 charts已经添加完成了,接下来编辑父charts,mychart的values.yaml与Chart.yaml文件
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort
NodePort: 30000
config:
nginx:
data:
comment: this is my nginx
address: http://www.nginx.local:80
tomcat:
data:
comment: this is my tomcat
address: http://www.tomcat.local:8080
mysql:
data:
comment: this is mysql
connection: 10.0.0.11:3306
mysql: # 在此添加mysql字段,并使用enabled去控制是否启用mysql子charts
enabled: true
[root@master-11 mychart]# cat Chart.yaml
apiVersion: v2
name: mychart
description: nginx
type: application
version: 0.1.0
appVersion: "1.0.0"
dependencies: # 添加dependencies字段
- name: mysql
version: 5.7
condition: mysql.enabled # 这个字段添加以后会自动去values文件中去寻找mysql.enabled字段对应的值,来判断是否需要去渲染子charts
# 安装
[root@master-11 mychart]# helm template nginx .
···
···
# Source: mychart/charts/mysql/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-mysql
labels:
app: nginx-mysql
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector:
app: nginx-mysql
---
# Source: mychart/charts/mysql/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-mysql
labels:
app: nginx-mysql
spec:
replicas: 1
selector:
matchLabels:
app: nginx-mysql
template:
metadata:
labels:
app: nginx-mysql
spec:
containers:
- name: mysql
image: "mysql:8.0"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "rootpassword"
- name: MYSQL_DATABASE
value: "mydb"
resources:
{}
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
# tag: aaa
app:
version: 1
enable: true
resources:
requests:
cpu: 1
memory: 1Gi
limits:
cpu: 2
memory: 2Gi
address:
a: beijing
b: shanghai
c: guangzhou
type: NodePort
NodePort: 30000
config:
nginx:
data:
comment: this is my nginx
address: http://www.nginx.local:80
tomcat:
data:
comment: this is my tomcat
address: http://www.tomcat.local:8080
mysql:
data:
comment: this is mysql
connection: 10.0.0.11:3306
mysql:
enabled: true
replicaCount: 2 # 在父charts中添加replicaCount,子charts里的replicaCount字段不要删,比较以下两个的优先级
# 安装
[root@master-11 mychart]# helm template nginx .
···
···
# Source: mychart/charts/mysql/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-mysql
labels:
app: nginx-mysql
spec:
replicas: 2 # 此处的副本数被改为了2个,可见父charts的优先级比子charts的优先级要高
selector:
matchLabels:
app: nginx-mysql
template:
metadata:
labels:
app: nginx-mysql
spec:
containers:
- name: mysql
image: "mysql:8.0"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "rootpassword"
- name: MYSQL_DATABASE
value: "mydb"
resources:
{}
7、helm package 配合 minion 实现charts拉取
[root@master-11 minio]# cat .helmignore # 这个文件中会匹配文件的类型,在打包的时候会忽略掉这些文件
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
# OWNERS file for Kubernetes
# 上传文件
[root@master-11 minio]# helm package .
[root@master-11 minio]# helm repo index . --url https://cache.wodcloud.local/k8s # 执行这条命令时,命令一定要有.tgz在此目录,然后目录下会生成一个index.yaml的文件,此处仅为简单部署,每次上传新的文件都需要执行指一条命令
[root@master-11 minio]# mc cp minio-5.4.0.tgz local/k8s/ # 接着将文件全部上传到minio里,k8s是我的桶名记得创建好并设置为public
[root@master-11 minio]# mc cp index.yaml local/k8s/
# 回到mychart的目录修改Chart.yaml文件
[root@master-11 mychart]# cat Chart.yaml
apiVersion: v2
name: mychart
description: nginx
type: application
version: 0.1.0
appVersion: "1.0.0"
dependencies:
- name: mysql
version: 0.1.0
condition: mysql.enabled
repository: "file://charts/mysql" # file://为本地charts的形式,他会去本地的charts目录下打包文件
- name: minio
version: 5.4.0
repository: https://cache.wodcloud.local/k8s # 此处为minion的地址
# 拉取charts文件
[root@master-11 mychart]# cd ..
[root@master-11 charts]# helm dependency update ./mychart # 将依赖子charts全部更新进去
Getting updates for unmanaged Helm repositories...
...Successfully got an update from the "https://cache.wodcloud.local/k8s" chart repository
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "ali-stable" chart repository
...Successfully got an update from the "yinzhengjie-ingress-nginx" chart repository
...Successfully got an update from the "harbor" chart repository
...Successfully got an update from the "ingress-nginx" chart repository
...Successfully got an update from the "minio" chart repository
...Successfully got an update from the "cilium" chart repository
...Successfully got an update from the "prometheus-community" chart repository
...Successfully got an update from the "metallb" chart repository
...Successfully got an update from the "traefik" chart repository
...Successfully got an update from the "grafana" chart repository
Update Complete. ⎈Happy Helming!⎈
Saving 2 charts
Downloading minio from repo https://cache.wodcloud.local/k8s
Deleting outdated charts
[root@master-11 charts]# ls mychart/charts/ # 此处两个包已经生成
minio-5.4.0.tgz mysql mysql-0.1.0.tgz