Helm包管理工具——内置对象

1、简介

在 Helm 中,模板文件是通过 Go 模板语法(Go templating)进行渲染的。在这个过程中,Helm 提供了许多内置对象,这些对象包含了当前 Kubernetes 集群的相关信息、Chart 的配置参数、以及一些通用的帮助函数。通过这些内置对象,开发者能够动态生成 Kubernetes 资源的定义文件。

2、常见内置对象

以下是一些在 Helm 模板中常用的内置对象:

内置 描述
Release.Name release 名称
Release.Time release 的时间
Release.Namespace release 的命名空间
Release.Service release 服务的名称
Release.Revision release 的修订版本号,从1开始累加
3、Values

.Values 是 Helm 中最常用的内置对象之一,表示用户在安装或升级 Chart 时提供的值。用户可以通过 values.yaml 配置文件或者通过 –set 参数来传递这些值。通过 .Values 对象,可以在模板中引用这些值。

示例

在 values.yaml 文件中定义变量:

[root@mast01 huqi]# vim mychart/values.yaml
replicaCount: 3
image:
repository: nginx
tag: stable
在模板文件 deployment.yaml 中引用 .Values:

[root@mast01 huqi]# vim mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
spec:
containers:
– name: nginx
image: “{{ .Values.image.repository }}:{{ .Values.image.tag }}”
执行

[root@mast01 huqi]# helm install –dry-run web mychart
NAME: web
LAST DEPLOYED: Fri Jan 3 14:55:45 2025
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
– name: nginx
image: “nginx:stable”
4、Release

.Release 对象包含了当前 Helm Release 的相关信息,如 Release 的名称、版本等。在 Helm 中,每次安装或升级 Chart 都会生成一个 Release。这个对象有助于为每个应用实例生成唯一的名称和配置。

示例

在模板文件中引用 .Release 对象:

[root@mast01 huqi]# vim mychart/templates/deployment.yaml
metadata:
name: {{ .Release.Namespace }}-myapp
labels:
release: {{ .Release.Name }}
这里的 {{ .Release.Name }} 会替换为当前 Release 的名称。

.Release 常用字段:

Name: 当前 Release 的名称。

Namespace: 当前 Release 部署的命名空间。

IsUpgrade: 如果是升级操作,则为 true,否则为 false。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 15:13:57 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: test111-myapp
labels:
release: web222
spec:
replicas: 3
selector:
matchLabels:
app: web222
template:
metadata:
labels:
app: web222
spec:
containers:
– name: nginx
image: “nginx:stable”
5、Chart

.Chart 对象包含了当前 Helm Chart 的信息,如 Chart 名称、版本、描述等。这个对象通常用于为 Kubernetes 资源添加版本信息、帮助信息等。

示例

在模板中引用 .Chart 对象:

[root@mast01 huqi]# vim mychart/templates/deployment.yaml
metadata:
name: {{ .Release.Name }}-{{ .Chart.Name }}
labels:
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
.Chart 常用字段:

Name: Chart 的名称。

Version: Chart 的版本。

Description: Chart 的描述信息。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 15:19:30 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: web222-mychart
labels:
chart: mychart-0.1.0
spec:
replicas: 3
selector:
matchLabels:
app: web222
template:
metadata:
labels:
app: web222
spec:
containers:
– name: nginx
image: “nginx:stable”
6、Files

.Files 对象允许 Helm 在模板中读取和使用 Chart 中的文件。它允许你将任意的文件嵌入到 Helm 模板中,如配置文件、证书、密钥等。

示例

在模板中使用 .Files 引用文件内容:

[root@mast01 huqi]# vim mychart/files/app-config.yaml
app:
name: my-app
version: 1.0.0

[root@mast01 huqi]# vim mychart/templates/ConfigMap.yaml

templates/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: {{ .Release.Namespace }}
data:
config.yaml: |
{{ .Files.Get “files/app-config.yaml” }}
在这个例子中,Helm 会将 Chart 中 config/config.yaml 文件的内容读取并嵌入到生成的 ConfigMap 中。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 17:01:39 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/ConfigMap.yaml

templates/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: test111
data:
config.yaml: |
app:
name: my-app
version: 1.0.0
7、Capabilities

.Capabilities 对象包含了 Kubernetes 集群的功能信息,比如当前集群支持的 API 版本等。可以通过 .Capabilities 来检查集群的特性并做不同的配置。

示例

[root@mast01 huqi]# cat mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
– name: myapp-container
image: myapp-image
ports:
– containerPort: 80
{{- if semverCompare “>= 1.18.0” .Capabilities.KubeVersion.Version }}
resources:
requests:
memory: “256Mi”
cpu: “250m”
{{- else }}
resources: {}
{{- end }}
在这个例子中,.Capabilities.KubeVersion.Gte “1.18” 用来判断 Kubernetes 集群的版本是否大于等于 1.18.0,则配置资源请求。如果版本低于 1.18.0,则不配置资源请求。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 15:48:41 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
– name: myapp-container
image: myapp-image
ports:
– containerPort: 80
resources:
requests:
memory: “256Mi”
cpu: “250m”
8、Now

.Now 返回当前时间。这个内置对象对于需要根据当前时间进行某些动态调整的场景非常有用,比如自动生成唯一的名称、设置过期时间等。

示例

[root@mast01 huqi]# vim mychart/templates/ConfigMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-{{ .Now | date “20060102-150405” }}
data:
config.yaml: |-
key: value
在这个例子中,{{ .Now | date “20060102-150405” }} 会将当前时间格式化为 YYYYMMDD-HHMMSS 形式,生成唯一的 ConfigMap 名称。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 15:48:41 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/ConfigMap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-20250103-154841
data:
config.yaml: |-

key: value

Source: mychart/templates/service.yaml

apiVersion: v1
kind: Service
metadata:
name: web222-service
spec:
ports:
– port: 80
targetPort: 8080
9、实际案例

下面是一个实际应用场景的例子,它将使用 .Values、.Release、.Chart 和 .Files 等对象来实现一个动态生成的 Kubernetes 资源。

示例

假设我们有一个 Chart 来部署一个 Nginx 服务,我们希望根据用户传入的配置文件生成 ConfigMap,并且在不同的环境中使用不同的配置文件。

[root@mast01 huqi]# vim mychart/values.yaml
replicaCount: 2
image:
repository: nginx
tag: stable
configFile: config/nginx.conf
[root@mast01 huqi]# vim mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-nginx
labels:
app: {{ .Release.Name }}-nginx
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Release.Name }}-nginx
template:
metadata:
labels:
app: {{ .Release.Name }}-nginx
spec:
containers:
– name: nginx
image: “{{ .Values.image.repository }}:{{ .Values.image.tag }}”
volumeMounts:
– name: config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
– name: config-volume
configMap:

name: {{ .Release.Name }}-nginx-config

apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-nginx-config
data:
nginx.conf: |-
{{ .Files.Get .Values.configFile | indent 4 }}
在这个例子中,nginx.conf 配置文件是从 Chart 中的文件获取的,replicaCount 和 image 等参数来自 values.yaml 文件,name 等动态字段来自 .Release。

执行

[root@mast01 huqi]# helm install –dry-run web222 mychart -n test111
NAME: web222
LAST DEPLOYED: Fri Jan 3 17:05:20 2025
NAMESPACE: test111
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:

MANIFEST:

Source: mychart/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: web222-nginx
labels:
app: web222-nginx
spec:
replicas: 2
selector:
matchLabels:
app: web222-nginx
template:
metadata:
labels:
app: web222-nginx
spec:
containers:
– name: nginx
image: “nginx:stable”
volumeMounts:
– name: config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
– name: config-volume
configMap:
name: web222-nginx-config
10、总结

Helm 内置对象为模板化配置提供了强大的支持,能够大大提高应用的灵活性和可配置性。通过 .Values、.Release、.Chart、.Files 等对象,Helm 允许我们在部署过程中动态生成和调整 Kubernetes 资源。这些内置对象在处理 Helm Charts 时是非常重要的,掌握这些对象的使用可以帮助开发者和运维人员更高效地管理 Kubernetes 集群中的应用。

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/424487.html

联系我们
联系我们
分享本页
返回顶部