一、基础语法实验准备¶
在演示下面内容之前自定义一个Chart示例
# 创建目录
[root@master01 ~]# mkdir -p /root/3/testfunction
# 创建values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# touch values.yaml
# 创建templates目录
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# mkdir templates
# 创建Chart.yaml
[root@master01 ~]# cd /root/3/
[root@master01 3]# helm create myapp
[root@master01 3]# cd /root/3
[root@master01 3]# cp myapp/Chart.yaml testfunction/
# 清除Chart.yaml文件中的多余内容,并重新定义名字
[root@master01 testfunction]# vim Chart.yaml
apiVersion: v2
name: testfunction
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
# 创建NOTES.txt
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# echo "hello testfunction" > NOTES.txt
# 查看
[root@master01 testfunction]# tree ../testfunction/
../testfunction/
├── Chart.yaml
├── NOTES.txt
├── templates
└── values.yaml
1 directory, 3 files
二、Helm 内置对象¶
下面是常用的内置对象:
| 内置 | 作用 |
|---|---|
| Release.Name | release 名称 |
| Release.Time | release 的时间 |
| Release.Namespace | release 的命名空间 |
| Release.Service | release 服务的名称 |
| Release.Revision | release 的修订版本号,从1开始累加 |
三、常用的内置函数¶
1、quote and squote
该函数将值转换成字符串用 双引号 (quot e ) 或者 单引号 (squote ) 括起来。示例如下:
# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
drink: coffee
food: pizza
# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favorite.drink | quote }}
food: {{ .Values.favorite.food | upper | squote }}
# 渲染部署,观察到quote代表双引号,squote代表单引号
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 14:40:44 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
data:
myvalue: "Hello World"
drink: "coffee"
food: 'PIZZA'
2、default
这个函数允许你在模板中指定一个默认值,以防这个值被忽略。
# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
food: pizza
# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favorite.drink | default "tea" | quote }}
food: {{ .Values.favorite.food | upper | squote }}
# 渲染部署,观察到如果.Values.favorite.drink是非空值,则使用它,否则会返回tea。
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 14:45:43 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
data:
myvalue: "Hello World"
drink: "tea"
food: 'PIZZA'
3、 indent 和 nindent
indent / nindent 都是缩进字符串,主要区别在于nindent会在缩进前多添加一个换行符
# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
drink: coffee
food: pizza
resources:
limits:
cpu: 200m
memory: 500m
requests:
cpu: 200m
memory: 500m
# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favorite.drink | quote }}
food: {{ .Values.favorite.food | upper | squote }}
resources1: {{ .Values.resources.limits.memory | indent 4 }}
resources2: {{ .Values.resources.limits.memory | nindent 4 }}
# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 15:23:05 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
data:
myvalue: "Hello World"
drink: "coffee"
food: 'PIZZA'
resources1: 500m
resources2:
500m
# 说明
{{ .Values.resources.limits.memory | indent 4 }}
上述结果会在当前位置开始缩进4个空格。
{{ .Values.resources.limits.memory | nindent 4 }}
上述结果会在换行后的开头位置开始缩进4个空格
4、date
date函数格式化日期,日期格式化为YEAR-MONTH-DAY:
now | date "2006-01-02"
5、lower
将整个字符串转换成小写
lower "HELLO"
结果为: hello
6、upper
将整个字符串转换成大写:
upper "hello"
结果为: HELLO
7、title
首字母转换成大写:
title "hello world"
结果为: Hello World
8、toYaml
引用一块YAML内容
在values.yaml里写结构化数据,引用内容块
在values增加resources资源配额。
# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
drink: coffee
food: pizza
resources:
limits:
cpu: 200m
memory: 500m
requests:
cpu: 200m
memory: 500m
# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favorite.drink | quote }}
food: {{ .Values.favorite.food | upper | squote }}
resources: {{ toYaml .Values.resources | nindent 4 }}
# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 15:27:40 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
data:
myvalue: "Hello World"
drink: "coffee"
food: 'PIZZA'
resources:
limits:
cpu: 200m
memory: 500m
requests:
cpu: 200m
memory: 500m