kubernetes的自定义Chart语法说明和案例

 

 

自定义 Chart 实现部署升级回滚版本管理

 自定义 Chart

 

 

案例:可变配置的 Chart

helm create myweb-chart

root@master1 helm]# tree myweb-chart
myweb-chart
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── httproute.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 11 files

rm -rf myweb-chart/templates/* myweb-chart/charts/

root@master1 helm]# tree myweb-chart
myweb-chart
├── Chart.yaml
├── templates
└── values.yaml

1 directory, 2 files
[root@master1 helm]# 

 

#生成模板文件

kubectl create deployment myweb --image registry.cnbeijing.aliyuncs.com/wangxiaochun/pod-test:v0.1 --replicas 3 --dry-run=client -o yaml > myweb-chart/templates/myweb-deployment.yaml
kubectl create service loadbalancer myweb --tcp 80:80 --dry-run=client -o yaml > myweb-chart/templates/myweb-service.yaml

 

#创建默认值文件

[root@master1 helm]# cat myweb-chart/values.yaml    
deployment:
  deployment_name: myweb-deployment
  replicas: 3
  pod_label: myweb-pod-label
  image: registry.cn-beijing.aliyuncs.com/wangxiaochun/pod-test
  imageTag: v0.1
  container_name: myweb-container
service:
  service_name: myweb-service
  port: 80
  targetport: 80
  containerport: 80
  #nodeport: 30080

 

#修改相关资源的模板文件,添加变量引用

[root@master1 helm]# cat myweb-chart/templates/myweb-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.deployment.deployment_name }}
  namespace: {{ .Release.Namespace }}
spec:
  replicas: {{ .Values.deployment.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.deployment.pod_label }}
  template:
    metadata:
      labels:
        app: {{ .Values.deployment.pod_label }}
    spec:
      containers:
      - image: {{ .Values.deployment.image }}:{{ .Values.deployment.imageTag }}
        name: {{ .Values.deployment.container_name }}
        ports:
        - containerPort: {{ .Values.service.containerport }}

 

[root@master1 helm]# cat myweb-chart/templates/myweb-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.service_name }}
  namespace: {{ .Release.Namespace }}
spec:
  type: LoadBalancer
  ports:
  - port: {{ .Values.service.port }}
    protocol: TCP
    targetPort: {{ .Values.service.targetport }}
  selector:
    app: {{ .Values.deployment.pod_label }}

 

 

#Chart.yaml此处使用默认值

root@master1 helm]# grep -v "#" myweb-chart/Chart.yaml
apiVersion: v2
name: myweb-chart
description: A Helm chart for Kubernetes

type: application

version: 0.1.0

appVersion: "1.16.0"

 

[root@master1 helm]# {{ .Values.service.port }}
-bash: {{: command not found
[root@master1 helm]# 
[root@master1 helm]# tree myweb-chart/
myweb-chart/
├── Chart.yaml
├── templates
│   ├── myweb-deployment.yaml
│   └── myweb-service.yaml
└── values.yaml

1 directory, 4 files

 

#检查语法

helm lint myweb-chart/

 

[root@master1 helm]# helm lint myweb-chart/
==> Linting myweb-chart/
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

 

#打包

helm package ./myweb-chart

[root@master1 helm]# ll -h myweb-chart-0.1.0.tgz 
-rw-r--r-- 1 root root 985 May 30 20:04 myweb-chart-0.1.0.tgz

#安装:使用默认值安装

#方法1:

helm install myweb ./myweb-chart/ --create-namespace --namespace demo

#方法2

helm install myweb myweb-chart-0.1.0.tgz --create-namespace --namespace demo

[root@master1 helm]# helm install myweb myweb-chart-0.1.0.tgz --create-namespace --namespace demo
NAME: myweb
LAST DEPLOYED: Sat May 30 20:15:59 2026
NAMESPACE: demo
STATUS: deployed
REVISION: 1
DESCRIPTION: Install complete
TEST SUITE: None
[root@master1 helm]# 

 

root@master1 helm]# helm list -n demo
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
myweb   demo            1               2026-05-30 20:15:59.098874044 +0800 CST deployed        myweb-chart-0.1.0       1.16.0   

 

root@master1 helm]# kubectl get deploy,svc -n demo
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myweb-deployment   3/3     3            3           58s

NAME                    TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
service/myweb-service   LoadBalancer   10.96.148.168   192.168.3.11   80:31181/TCP   58s

#定制项较少时,使用--set 实现定制安装

helm install --set deployment.replicas=1,deployment.imageTag=v0.2 myweb myweb-chart-0.1.0.tgz --create-namespace --namespace test

root@master1 helm]# helm install --set deployment.replicas=1,deployment.imageTag=v0.2 myweb myweb-chart-0.1.0.tgz --create-namespace --namespace test
NAME: myweb
LAST DEPLOYED: Sat May 30 20:19:20 2026
NAMESPACE: test
STATUS: deployed
REVISION: 1
DESCRIPTION: Install complete
TEST SUITE: None

 

[root@master1 helm]# helm list -n test
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
myweb   test            1               2026-05-30 20:19:20.668110034 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     

 

[root@master1 helm]# kubectl get pod -o wide -n test
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-78bbf96999-d557v   1/1     Running   0          55s   10.244.1.89   node1.org   <none>           <none>
[root@master1 helm]# 
[root@master1 helm]# curl 10.244.1.89
kubernetes pod-test v0.2!! ClientIP: 10.244.0.0, ServerName: myweb-deployment-78bbf96999-d557v, ServerIP: 10.244.1.89!

 

#定制项较多时,生成定义制值配置文件

helm show values ./myweb-chart-0.1.0.tgz > myweb-values.yaml

[root@master1 helm]# cat myweb-values.yaml
deployment:
  deployment_name: myweb-deployment
  replicas: 2
  pod_label: myweb-pod-label
  image: registry.cn-beijing.aliyuncs.com/wangxiaochun/pod-test
  imageTag: v0.3
  container_name: myweb-container
service:
  service_name: myweb-service
  port: 88
  targetport: 80
  containerport: 80
  #nodeport: 30080

 

helm install myweb2 myweb-chart-0.1.0.tgz -f myweb-values.yaml --create-namespace --namespace demo2

[root@master1 helm]# helm list -A
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
myweb   demo            1               2026-05-30 20:15:59.098874044 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     
myweb   test            1               2026-05-30 20:19:20.668110034 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     
myweb2  demo2           1               2026-05-30 20:24:50.0367798 +0800 CST   deployed        myweb-chart-0.1.0       1.16.0     

 

[root@master1 helm]# kubectl get pod -n demo2 -o wide
NAME                                READY   STATUS    RESTARTS   AGE    IP            NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-6f9bd489b8-4nqt6   1/1     Running   0          2m1s   10.244.2.82   node2.org   <none>           <none>
myweb-deployment-6f9bd489b8-n5p24   1/1     Running   0          2m1s   10.244.1.90   node1.org   <none>           <none>
[root@master1 helm]# curl 10.244.1.90
kubernetes pod-test v0.3!! ClientIP: 10.244.0.0, ServerName: myweb-deployment-6f9bd489b8-n5p24, ServerIP: 10.244.1.90!

 

[root@master1 helm]# kubectl get svc -n demo2
NAME            TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
myweb-service   LoadBalancer   10.109.137.91   192.168.3.13   88:31123/TCP   2m35s

 

验证 Chart

#查看生成的清单文件内容

helm get manifest -n demo myweb

 

升级和回滚

[root@master1 helm]# kubectl get pod -o wide -n demo
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-56676c868b-2swl4   1/1     Running   0          14m   10.244.1.87   node1.org   <none>           <none>
myweb-deployment-56676c868b-9c5nl   1/1     Running   0          14m   10.244.1.88   node1.org   <none>           <none>
myweb-deployment-56676c868b-bl9wc   1/1     Running   0          14m   10.244.2.81   node2.org   <none>           <none>
[root@master1 helm]# 
[root@master1 helm]# curl 10.244.1.87
kubernetes pod-test v0.1!! ClientIP: 10.244.0.0, ServerName: myweb-deployment-56676c868b-2swl4, ServerIP: 10.244.1.87!

#升级方法1

helm upgrade myweb ./myweb-chart -n demo --set deployment.image=registry.cn-beijing.aliyuncs.com/wangxiaochun/pod-test --set deployment.imageTag=v0.2

#显示如下

[root@master1 helm]# helm upgrade myweb ./myweb-chart -n demo --set deployment.image=registry.cn-beijing.aliyuncs.com/wangxiaochun/pod-test --set deployment.imageTag=v0.2
Release "myweb" has been upgraded. Happy Helming!
NAME: myweb
LAST DEPLOYED: Sat May 30 20:31:58 2026
NAMESPACE: demo
STATUS: deployed
REVISION: 2
DESCRIPTION: Upgrade complete
TEST SUITE: None

 

[root@master1 helm]# helm list -A
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
myweb   demo            2               2026-05-30 20:31:58.462759202 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     
myweb   test            1               2026-05-30 20:19:20.668110034 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     
myweb2  demo2           1               2026-05-30 20:24:50.0367798 +0800 CST   deployed        myweb-chart-0.1.0       1.16.0 

 

#升级方法2

vim myweb-chart/values.yaml

imageTag: v0.2

helm upgrade myweb ./myweb-chart -n demo -f myweb-chart/values.yaml

#查看版本历史

 

[root@master1 helm]# helm history -n demo myweb 
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Sat May 30 20:15:59 2026        superseded      myweb-chart-0.1.0       1.16.0          Install complete
2               Sat May 30 20:31:58 2026        deployed        myweb-chart-0.1.0       1.16.0          Upgrade complete

#验证升级成功

[root@master1 helm]# kubectl get pod -o wide -n demo
NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-78bbf96999-bxxqs   1/1     Running   0          2m24s   10.244.2.83   node2.org   <none>           <none>
myweb-deployment-78bbf96999-qbchp   1/1     Running   0          2m20s   10.244.2.84   node2.org   <none>           <none>
myweb-deployment-78bbf96999-tg6mg   1/1     Running   0          2m22s   10.244.1.91   node1.org   <none>           <none>
[root@master1 helm]# 
[root@master1 helm]# 
[root@master1 helm]# curl 10.244.2.83
kubernetes pod-test v0.2!! ClientIP: 10.244.0.0, ServerName: myweb-deployment-78bbf96999-bxxqs, ServerIP: 10.244.2.83!

#回滚到指定REVESION号,如果不指定版本,默认回滚至上一个版本,而且只能回滚一个版本

helm rollback -n demo myweb 1

[root@master1 helm]# helm history -n demo myweb     
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Sat May 30 20:15:59 2026        superseded      myweb-chart-0.1.0       1.16.0          Install complete
2               Sat May 30 20:31:58 2026        superseded      myweb-chart-0.1.0       1.16.0          Upgrade complete
3               Sat May 30 20:36:17 2026        deployed        myweb-chart-0.1.0       1.16.0          Rollback to 1   

 

[root@master1 helm]# kubectl get pod -o wide -n demo
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-56676c868b-5t9r5   1/1     Running   0          75s   10.244.2.85   node2.org   <none>           <none>
myweb-deployment-56676c868b-5tbmh   1/1     Running   0          73s   10.244.1.93   node1.org   <none>           <none>
myweb-deployment-56676c868b-r6qrl   1/1     Running   0          77s   10.244.1.92   node1.org   <none>           <none>
[root@master1 helm]# 
[root@master1 helm]# curl 10.244.1.93
kubernetes pod-test v0.1!! ClientIP: 10.244.0.0, ServerName: myweb-deployment-56676c868b-5tbmh, ServerIP: 10.244.1.93!

打包至文件

helm package ./myweb-chart/

ll myweb-chart-0.1.0.tgz 

 

删除 release

#卸载

helm uninstall -n demo myweb 

helm list -n demo

kubectl get all -n demo

 

 

 上传到 Harbor

 

范例: 基于OCI协议将自定义的chart上传和下载至harbor
如果是自签名的HTTPS证书,需要先下载Habor的服务器证书到文件中

 

image

 

 导出

image

 

 

[root@master1 helm]# cat harbor.ming.org.crt
-----BEGIN CERTIFICATE-----
MIIDNTCCAh2gAwIBAgIRAJwUCDN6oUjcQH89dvd+mbswDQYJKoZIhvcNAQELBQAw
FDESMBAGA1UEAxMJaGFyYm9yLWNhMB4XDTI2MDUzMDE1MTExN1oXDTI3MDUzMDE1
MTExN1owGjEYMBYGA1UEAxMPaGFyYm9yLm1pbmcub3JnMIIBIjANBgkqhkiG9w0B
AQEFAAOCAQ8AMIIBCgKCAQEAyCRjvmnJ8Ic0dCWO0vLUQXVjtwl6lej/yiDCjdKB
fEuoPj2qSQNTaneONSrdwpFq9QExwg02FVRjIhKvwZMC1kEyD6SNGIk7UTy54Ua+
I7BrEHKC8/Vuq/9JnrYrNQWTM6hwz+e8Ry98I2y+wKCavOtH4aoF9I8FrL03Qoi9
V/8Zg0RSktwyyOWIjHf4d3AsIgRMCo7wIjcaapoyJW26zV2F5xxcUK+Oruzvu+iW
avnPAVLgN04ABZQfEiw2BUdrLa1opqXISrdOxvArik8NYFbDOKkFsJDzUBuhRerD
v5RBNBSccvXA226XVN82TSAjYJCAnPR+gM8gdG608Thd4QIDAQABo3wwejAOBgNV
HQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1Ud
EwEB/wQCMAAwHwYDVR0jBBgwFoAUqrrcf+IBHKo3R1kLC2++VzkY+P8wGgYDVR0R
BBMwEYIPaGFyYm9yLm1pbmcub3JnMA0GCSqGSIb3DQEBCwUAA4IBAQCsVONF5dZe
2njOH2sdMiiSBJEyg8vzOSLABRswdeSbw9kjvqsgxcOJrEVQlXdqrlzv5PdIAP7j
rThiLdPBdsnCsG4fecTuiNuupeeLIcrdf/mAV/GMAZgzQKxH0AWUFaSPlCDFLpBD
LZWPzIf5Au6BMXRqRWd8ACVIeaLKWNr5R7AAMGaJCSbB5+ahHZ8HFegguR5E0Fbf
co1VhFvXchFquGMjlJYXxWMjC/ApfbIi9EEckNyiGbyIllCByC3Dz6Yt+r0HkTyn
jJAauxPL1Lzf+g3MJoZmk7pH/iEV/lg8uenN+C3Rd1QBRXDOXLcL+c/nya+uOlm6
4Jc7yce/QwHu
-----END CERTIFICATE-----
[root@master1 helm]# 

 

#OCI协议只支持HTTPS,如果https使用自签名证书,需要Helm客户端信任Harbor证书的CA

cat harbor.ming.org.crt >> /etc/ssl/certs/ca-certificates.crt

 

登录先

#http需要加--insecure选项

helm registry login --insecure harbor.ming.org -u admin -p 123456

root@master1 helm]# helm registry login --insecure harbor.ming.org -u admin -p 123456
level=WARN msg="using --password via the CLI is insecure. Use --password-stdin"
Login Succeeded

 

#上传chart到harobr

 helm push myweb-chart-0.1.0.tgz oci://harbor.ming.org/helm

[root@master1 helm]# helm push myweb-chart-0.1.0.tgz oci://harbor.ming.org/helm
Pushed: harbor.ming.org/helm/myweb-chart:0.1.0
Digest: sha256:b813c78d60996827466b80b38f9ce7fb9a7ba7ac5a57d558942d034c0882d618

#在线安装

helm install helloapp -n demo3 oci://harbor.ming.org/helm/myweb-chart --version 0.1.0 --create-namespace

[root@master1 helm]# 
[root@master1 helm]# helm install helloapp -n demo3 oci://harbor.ming.org/helm/myweb-chart --version 0.1.0 --create-namespace
Pulled: harbor.ming.org/helm/myweb-chart:0.1.0
Digest: sha256:b813c78d60996827466b80b38f9ce7fb9a7ba7ac5a57d558942d034c0882d618
NAME: helloapp
LAST DEPLOYED: Sun May 31 01:15:28 2026
NAMESPACE: demo3
STATUS: deployed
REVISION: 1
DESCRIPTION: Install complete
TEST SUITE: None

 

[root@master1 helm]# helm list -n demo3
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
helloapp        demo3           1               2026-05-31 01:15:28.146599525 +0800 CST deployed        myweb-chart-0.1.0       1.16.0     

 

[root@master1 helm]# kubectl get pod -o wide -n demo3
NAME                                READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
myweb-deployment-56676c868b-6829l   1/1     Running   0          83s   10.244.2.104   node2.org   <none>           <none>
myweb-deployment-56676c868b-brh69   1/1     Running   0          83s   10.244.2.105   node2.org   <none>           <none>
myweb-deployment-56676c868b-mzhrk   1/1     Running   0          83s   10.244.1.111   node1.org   <none>           <none>

 

e3316db7e55809ed1133ec0b0bfcca04

 

#测试从harbor下载chart

helm pull oci://harbor.ming.org/helm/myapp-chart --version 0.1.0

[root@master1 test]# helm pull oci://harbor.ming.org/helm/myweb-chart --version 0.1.0   
Pulled: harbor.ming.org/helm/myweb-chart:0.1.0
Digest: sha256:b813c78d60996827466b80b38f9ce7fb9a7ba7ac5a57d558942d034c0882d618
[root@master1 test]# ll
total 4
-rw-r--r-- 1 root root 986 May 31 01:38 myweb-chart-0.1.0.tgz

 

 
posted @ 2026-05-31 01:40  minger_lcm  阅读(2)  评论(0)    收藏  举报