Helm 动态参数配置模版

1.我们重新创建一个新的模板 helm create <应用名称>

[root@master01 hpro]# helm create mychart
Creating mychart

2.我们接着看文件

# 查看文件
[root@master01 mychart]# ls
charts  Chart.yaml  templates  values.yaml

# 我们进入 templates
[root@master01 templates]# ls
deployment.yaml  _helpers.tpl  hpa.yaml  ingress.yaml  NOTES.txt  serviceaccount.yaml  service.yaml  tests

# 我们随便查看一个yaml
[root@master01 templates]# vim service.yaml
# 这个是一个 helm创建的模板
apiVersion: v1
kind: Service
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "mychart.selectorLabels" . | nindent 4 }}
  

3.模板取值格式

# 取参数的格式
# {{ .Values.变量名称 }}
# {{ .Release.Name }}

4.修改 values.yaml

[root@master01 mychart]# ls
charts  Chart.yaml  templates  values.yaml

# 把里面所有清空
[root@master01 mychart]# vim values.yaml

# 写入以下内容

# 定义副本数
replicas: 1
# 镜像
image: nginx
# 版本
tag: 1.16
# 标签
label: nginx
# 端口
port: 80

5. 修改 templates 文件夹中的deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{.Values.label }}
  name: {{ .Release.Name}}-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{.Values.label }}
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: {{.Values.label }}
    spec:
      containers:
      - image: {{.Values.image }}
        name: {{.Values.image }}
        resources: {}
status: {}

6. 修改 templates 文件夹中的 service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: web-test
  name: {{ .Release.Name}}-svc
spec:
  ports:
  - port: {{ .Values.port}}
    protocol: TCP
    targetPort: 80
  selector:
    app: {{ .Values.label}}
  type: NodePort

7.这是创建的模板文件

[root@master01 templates]# ls
deploy.yaml  service.yaml

8.测试模板文件  helm install --dry-run <应用名称> <模板文件夹>

命令的解释如下:

  • helm install: 这是 Helm 的安装命令,用于安装一个 chart。
  • --dry-run: 这个选项表示在执行安装命令时只进行模拟操作,不实际安装 chart。
  • web2: 这是要安装的 release 的名称。
  • mychart/: 这是要安装的 chart 的路径。

helm install --dry-run web2 mychart/ 这个命令将模拟安装位于 mychart/ 的 chart,不会实际执行任何操作。可以通过这个命令来检查安装过程中可能出现的问题或验证 chart 的配置是否正确。

 

# 正常打印表示文件没有问题
[root@master01 hpro]# helm install --dry-run web2 mychart/
NAME: web2
LAST DEPLOYED: Sun Jan 14 02:49:27 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
.
.
.

9.我们看一下打印的内容,填充的内容都已经填充上了

# 取参数的格式
# {{ .Values.变量名称 }}
# {{ .Release.Name }}

# 这下明白怎么取值了吧

.Values 取得是 values.yaml 文件中的属性

.Release 取得是 install 中定义的名字
[root@master01 hpro]# helm install --dry-run web2 mychart/
NAME: web2
LAST DEPLOYED: Sun Jan 14 02:49:27 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: web-test
  name: web2-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
---
# Source: mychart/templates/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: web2-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

10.安装自定义应用

[root@master01 hpro]# helm install web2 mychart/
NAME: web2
LAST DEPLOYED: Sun Jan 14 02:58:44 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master01 hpro]# helm list
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
web2    default         1               2024-01-14 02:58:44.222147967 -0800 PST deployed        mychart-0.1.0   1.16.0
[root@master01 hpro]# kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
web2-deploy-f89759699-rqc6m   1/1     Running   0          21s
[root@master01 hpro]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        28d
web2-svc     NodePort    10.108.230.159   <none>        80:31223/TCP   25s
posted @ 2025-08-07 14:28  david_cloud  阅读(13)  评论(0)    收藏  举报