实战

准备

创建namespace

kubectl create ns study-ns

创建Chart

helm create study

所需文件

tree ./study/
./study/
├── charts
├── Chart.yaml
├── templates
   ├── NOTES.txt
   ├── web-deployment.yaml
   └── web-svc.yaml
├── values-2.yaml
└── values.yaml

study/templates/NOTES.txt

# ----------- Welcome to Helm's World ------------- #
Congratulation:
  {{ .Values.study_apps.image }}:{{ .Values.study_apps.tags }}
Deploy succeed!

study/templates/web-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
  namespace: {{ .Values.study_apps.namespace }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.labels.app }}
  template:
    metadata:
      labels:
        app: {{ .Values.labels.app }}
    spec:
      containers:
      - name: nginx
        image: {{ .Values.study_apps.image }}:{{ .Values.study_apps.tags }}
        imagePullPolicy: {{ .Values.study_apps.imagePullPolicy }}
        ports:
        - containerPort: 80

study/templates/web-svc.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: {{ .Values.labels.app }}
  name: web-svc
  namespace: {{ .Values.study_apps.namespace }}
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080
  selector:
    app: web

study/values.yaml

study_apps:
  namespace: study-ns
  image: nginx
  tags: 1.16.0
  imagePullPolicy: IfNotPresent

replicas: 2
labels:
  app: web

study/values-v2.yaml

study_apps:
  namespace: study-ns
  image: nginx
  tags: latest
  imagePullPolicy: IfNotPresent

replicas: 2
labels:
  app: web

study/Chart.yaml

...
version: 1.0
appVersion: "1.16.0"

部署

helm install web study -n study-ns

查看

helm list -n study-ns

helm -n study-ns history web
curl -I 127.0.0.1:30080
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Fri, 21 Mar 2025 15:12:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 10:18:21 GMT
Connection: keep-alive
ETag: "5cbee66d-264"
Accept-Ranges: bytes

升级

# 在升级时,确保 Chart 目录中包含更新后的 Chart.yaml 文件,
...
version: 2.0
appVersion: "latest"
...

# 然后运行
helm -n study-ns upgrade web study -f study/values-v2.yaml

查看

helm list -n study-ns

helm -n study-ns history web
REVISION        UPDATED                         STATUS          CHART   APP VERSION     DESCRIPTION     
1               Fri Mar 21 23:24:35 2025        superseded      study-1 1.16.0          Install complete
2               Fri Mar 21 23:29:03 2025        deployed        study-2 latest          Upgrade complete
curl -I 127.0.0.1:30080
HTTP/1.1 200 OK
Server: nginx/1.27.4
Date: Fri, 21 Mar 2025 15:14:11 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 05 Feb 2025 11:06:32 GMT
Connection: keep-alive
ETag: "67a34638-267"
Accept-Ranges: bytes

回滚

helm -n study-ns rollback web 1

查看

helm -n study-ns history web
REVISION        UPDATED                         STATUS          CHART   APP VERSION     DESCRIPTION     
1               Fri Mar 21 23:24:35 2025        superseded      study-1 1.16.0          Install complete
2               Fri Mar 21 23:29:03 2025        superseded      study-2 latest          Upgrade complete
3               Fri Mar 21 23:29:50 2025        deployed        study-1 1.16.0          Rollback to 1
curl -I 127.0.0.1:30080
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Fri, 21 Mar 2025 15:17:36 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 10:18:21 GMT
Connection: keep-alive
ETag: "5cbee66d-264"
Accept-Ranges: bytes

Last updated