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
時間是個偉大的作者,必將給出完美的答案。

浙公網安備 33010602011771號