k8s1.14-helm模板内置对象

定义Mychart

一个 chart 包就是一个文件夹的集合,文件夹名称就是 chart 包的名称,比如创建一个 mychart 的 chart 包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@master101 sy]# helm create mychart
Creating mychart
[root@master101 sy]# cd mychart/
[root@master101 mychart]# tree
.
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── service.yaml
│   └── tests
│   └── test-connection.yaml
└── values.yaml

3 directories, 8 files

templates 目录下面的文件:

  • NOTES.txt:chart 的 “帮助文本”。这会在用户运行 helm install 时显示给用户。
  • deployment.yaml:创建 Kubernetes deployment 的基本 manifest
  • service.yaml:为 deployment 创建 service 的基本 manifest
  • ingress.yaml: 创建 ingress 对象的资源清单文件
  • _helpers.tpl:放置模板助手的地方,可以在整个 chart 中重复使用

创建简单模板

1
[root@master101 sy]# rm -rf mychart/templates/*

创建configmap.yaml:

1
2
3
4
5
6
7
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"

需要注意的是kubernetes资源对象的 labels 和 name 定义被限制在63个字符,所以需要注意名称的定义。

1
2
3
4
5
6
7
Labels are key/value pairs. Valid label keys have two segments: an optional prefix and name, separated by a slash (/). The name segment is required and must be 63 characters or less, beginning and ending with an alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between. The prefix is optional. If specified, the prefix must be a DNS subdomain: a series of DNS labels separated by dots (.), not longer than 253 characters in total, followed by a slash (/).

If the prefix is omitted, the label Key is presumed to be private to the user. Automated system components (e.g. kube-scheduler, kube-controller-manager, kube-apiserver, kubectl, or other third-party automation) which add labels to end-user objects must specify a prefix.

The kubernetes.io/ and k8s.io/ prefixes are reserved for Kubernetes core components.

Valid label values must be 63 characters or less and must be empty or begin and end with an alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.

调试

--dry-run --debug这个可选参数

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@master101 sy]# helm install  --dry-run --debug ./mychart
[debug] Created tunnel using local port: '34909'

[debug] SERVER: "127.0.0.1:34909"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: broken-mink
REVISION: 1
RELEASED: Thu June 18 16:59:36 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
pullPolicy: IfNotPresent
repository: nginx
tag: stable
imagePullSecrets: []
ingress:
annotations: {}
enabled: false
hosts:
- host: chart-example.local
paths: []
tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
port: 80
type: ClusterIP
tolerations: []

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: broken-mink-configmap
data:
myvalue: "Hello World"

内置对象

Release:这个对象描述了 release 本身。它里面有几个对象:

  • Release.Name:release 名称
  • Release.Time:release 的时间
  • Release.Namespace:release 的 namespace(如果清单未覆盖)
  • Release.Service:release 服务的名称(始终是 Tiller)。
  • Release.Revision:此 release 的修订版本号,从1开始累加。
  • Release.IsUpgrade:如果当前操作是升级或回滚,则将其设置为 true。
  • Release.IsInstall:如果当前操作是安装,则设置为 true。

Values:从values.yaml文件和用户提供的文件传入模板的值。默认情况下,Values 是空的。

1
2
3
4
5
6
7
例子:

{{ .Values.replicaCount }}

#引用嵌套对象例子,跟引用json嵌套对象类似

{{ .Values.image.repository }}

Chart:Chart.yaml的内容。chart版本可以从Chart.Version和维护人员 Chart.Maintainers一起获得。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: The chart API version, always "v1" (必须参数)
name: The name of the chart (必须参数)
version: A SemVer 2 version (必须参数)
kubeVersion: A SemVer range of compatible Kubernetes versions (可选参数)
description: A single-sentence description of this project (可选参数)
keywords:
- A list of keywords about this project (可选参数)
home: The URL of this project's home page (可选参数)
sources:
- A list of URLs to source code for this project (可选参数)
maintainers: # (可选参数)
- name: The maintainer's name (required for each maintainer)
email: The maintainer's email (optional for each maintainer)
url: A URL for the maintainer (optional for each maintainer)
engine: gotpl # The name of the template engine (optional, defaults to gotpl)
icon: A URL to an SVG or PNG image to be used as an icon (optional).
appVersion: The version of the app that this contains (optional). This needn't be SemVer.
deprecated: Whether this chart is deprecated (optional, boolean)
tillerVersion: The version of Tiller that this chart requires. This should be expressed as a SemVer range: ">2.0.0" (optional)

Files:这提供对 chart 中所有非特殊文件的访问。虽然无法使用它来访问模板,但可以使用它来访问 chart 中的其他文件。请参阅 “访问文件” 部分。

  • Files.Get 是一个按名称获取文件的函数(.Files.Get config.ini)
  • Files.GetBytes 是将文件内容作为字节数组而不是字符串获取的函数。这对于像图片这样的东西很有用。

Capabilities:这提供了关于 Kubernetes 集群支持的功能的信息。

  • Capabilities.APIVersions 是一组版本信息。
  • Capabilities.APIVersions.Has $version 指示是否在群集上启用版本(batch/v1)。
  • Capabilities.KubeVersion 提供了查找 Kubernetes 版本的方法。它具有以下值:Major,Minor,GitVersion,GitCommit,GitTreeState,BuildDate,GoVersion,Compiler,和 Platform。
  • Capabilities.TillerVersion 提供了查找 Tiller 版本的方法。它具有以下值:SemVer,GitCommit,和 GitTreeState。

  • Template:包含有关正在执行的当前模板的信息

  • Name:到当前模板的文件路径(例如 mychart/templates/mytemplate.yaml)
  • BasePath:当前 chart 模板目录的路径(例如 mychart/templates)。

values文件

1
2
3
4
5
6
7
8
9
values对象的值四种来源:

chart包values.yaml文件

如果包含子chart包,父chart包中的value.yaml中定义的值,将覆盖子chart包。

可以使用-f参数传递值到chart包的values.yaml中。如:(helm install -f myvals.yaml ./mychart)

使用命令行传入指定的值,如:(helm install --set foo=bar ./mychart):

chart 的 values.yaml 提供的值可以被用户提供的 values 文件覆盖,而该文件同样可以被--set提供的参数所覆盖。

1
2
[root@master101 mychart]# cat values.yaml
course: k8s

在上面的 templates/configmap.yaml 模板文件中就可以使用这个值了:(configmap.yaml)

1
2
3
4
5
6
7
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
course: {{ .Values.course }}

debug看下:

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
[root@master101 sy]# helm install --dry-run --debug ./mychart
[debug] Created tunnel using local port: '43779'

[debug] SERVER: "127.0.0.1:43779"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: bold-hyena
REVISION: 1
RELEASED: Thu June 18 17:37:29 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course: k8s

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: bold-hyena-configmap
data:
myvalue: "Hello World"
course: k8s

通过--set参数来轻松的覆盖 course 的值:

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
[root@master101 sy]# helm install --dry-run --debug --set course=python ./mychart
[debug] Created tunnel using local port: '36665'

[debug] SERVER: "127.0.0.1:36665"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: ungaged-butterfly
REVISION: 1
RELEASED: Thu Aug 22 17:38:17 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
course: python

COMPUTED VALUES:
course: python

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: ungaged-butterfly-configmap
data:
myvalue: "Hello World"
course: python

由于--set 比默认 values.yaml 文件具有更高的优先级,所以我们的模板生成为 course: python。

value多结构化内容

1
2
3
4
[root@master101 sy]# cat mychart/values.yaml
course:
k8s: devops
python: django
1
2
3
4
5
6
7
8
9
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
k8s: {{ .Values.course.k8s }}
python: {{ .Values.course.python }}
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
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '46611'

[debug] SERVER: "127.0.0.1:46611"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: eyewitness-mink
REVISION: 1
RELEASED: Fri June 16 18:27:01 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: eyewitness-mink-configmap
data:
myvalue: "Hello World"
k8s: devops
python: django
Donate