K8s - Helm的使用

Anliven發表於2024-10-21

安裝Helm

  • https://helm.sh/zh/docs/
  • https://github.com/helm/helm/releases
  • https://get.helm.sh/helm-v3.16.2-linux-amd64.tar.gz
  • 在 master 節點安裝Helm
[root@k8s-master ~]# tar -xvzf helm-v3.16.2-linux-amd64.tar.gz 
linux-amd64/
linux-amd64/LICENSE
linux-amd64/helm
linux-amd64/README.md
[root@k8s-master ~]# mv linux-amd64/helm /usr/local/bin
[root@k8s-master ~]# 
[root@k8s-master ~]# helm version
version.BuildInfo{Version:"v3.16.2", GitCommit:"13654a52f7c70a143b1dd51416d633e1071faffb", GitTreeState:"clean", GoVersion:"go1.22.7"}
[root@k8s-master ~]# 

Helm國內映象源

# 移除原先的stable源並增加國內映象源
[root@k8s-master ~]# helm repo remove stable
"stable" has been removed from your repositories
[root@k8s-master ~]# 
[root@k8s-master ~]# helm repo add azure http://mirror.azure.cn/kubernetes/charts
"azure" has been added to your repositories
[root@k8s-master ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories
[root@k8s-master ~]# helm repo add offical https://charts.helm.sh/incubator
"offical" has been added to your repositories
[root@k8s-master ~]#

# 列出已配置的Helm庫
[root@k8s-master ~]# helm repo list
NAME    URL                                                   
azure   http://mirror.azure.cn/kubernetes/charts              
aliyun  https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
offical https://charts.helm.sh/incubator                      
[root@k8s-master ~]# 

# 搜尋測試
[root@k8s-master ~]# helm search repo nginx
NAME                            CHART VERSION   APP VERSION     DESCRIPTION                                       
aliyun/nginx-ingress            0.9.5           0.10.2          An nginx Ingress controller that uses ConfigMap...
aliyun/nginx-lego               0.3.1                           Chart for nginx-ingress-controller and kube-lego  
azure/nginx-ingress             1.41.3          v0.34.1         DEPRECATED! An nginx Ingress controller that us...
azure/nginx-ldapauth-proxy      0.1.6           1.13.5          DEPRECATED - nginx proxy with ldapauth            
azure/nginx-lego                0.3.1                           Chart for nginx-ingress-controller and kube-lego  
aliyun/gcloud-endpoints         0.1.0                           Develop, deploy, protect and monitor your APIs ...
azure/gcloud-endpoints          0.1.2           1               DEPRECATED Develop, deploy, protect and monitor...
[root@k8s-master ~]# 
[root@k8s-master ~]# helm search hub nginx |head
URL                                                     CHART VERSION   APP VERSION                             DESCRIPTION                                       
https://artifacthub.io/packages/helm/krakazyabr...      1.0.0           1.19.0                                  Nginx Helm chart for Kubernetes                   
https://artifacthub.io/packages/helm/bitnami/nginx      18.2.3          1.27.2                                  NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/dysnix/nginx       7.1.8           1.19.4                                  Chart for the nginx server                        
https://artifacthub.io/packages/helm/bitnami-ak...      13.2.12         1.23.2                                  NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/ashu-nginx...      0.1.0           1.16.0                                  A Helm chart for Kubernetes                       
https://artifacthub.io/packages/helm/test-nginx...      0.1.0           1.16.0                                  A Helm chart for Kubernetes                       
https://artifacthub.io/packages/helm/shubhamtat...      0.1.12          1.19.6                                  Nginx Helm chart for Kubernetes                   
https://artifacthub.io/packages/helm/dhinesh/nginx      18.2.2          1.27.2                                  NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/wiremind/n...      2.1.1                                                   An NGINX HTTP server                              
[root@k8s-master ~]# 

# 更新本地chart庫資訊
[root@k8s-master ~]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "offical" chart repository
...Successfully got an update from the "azure" chart repository
Update Complete. ⎈Happy Helming!⎈
[root@k8s-master ~]# 

一些Helm命令

helm search repo <chart-name> -o yaml   # 從repo查詢Chart
helm search hub <chart-name> -o yaml    # 從hub查詢Chart
helm show all <chart-name>              # 檢視Chart詳情

helm lint <chart-dir>       # 檢查依賴和模版配置
helm package <chart-dir>    # 打包應用

helm install <release-name> ./<chart-dir>                                # 離線部署
helm install <release-name> ./<chart-dir> -f ./<file-dir>/<file>.yaml    # 根據配置檔案離線部署

製作Chart模版

建立Chart模版,作為製作新Chart的參考和基礎。

[root@k8s-master ~]# helm create chart-demo
Creating chart-demo
[root@k8s-master ~]# tree chart-demo/
chart-demo/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files
[root@k8s-master ~]# 

Chart模版目錄結構說明

├── charts # 可選目錄,存放與當前Chart相關的子Chart
├── Chart.yaml # 用於描述 Chart 資訊,如名稱、版本、描述等
├── templates # 主要用於存放Kubernetes資源的模板檔案
│   ├── deployment.yaml # Kubernetes資原始檔
│   ├── _helpers.tpl # 存放 Helm 模板函式,可以在模板檔案中重複使用,以簡化模板檔案的編寫,並提高可維護性
│   ├── hpa.yaml
│   ├── ingress.yaml # Kubernetes資原始檔
│   ├── NOTES.txt # 在安裝或升級 Chart 後顯示的提示和說明
│   ├── serviceaccount.yaml # Kubernetes資原始檔
│   ├── service.yaml # Kubernetes資原始檔
│   └── tests
│       └── test-connection.yaml
└── values.yaml # 用於定義Chart的預設值,通常在模板檔案中引用,可以在安裝 Chart時修改這些值,以靈活地改變應用程式的配置

一些配置欄位說明

apiVersion: Chart API 版本
name: 名稱
description: 簡要描述
type: 型別
version: 版本號
appVersion: 應用程式的版本號
replicas: 定義預設的副本數
image: 映象倉庫地址和標籤
service: Service型別、埠和目標埠

雙花括號"{{}}"            用於定義和插入變數、表示式和函式
“.Release.Name”           內建變數,用於安裝Chart時指定的Release名稱
以".Values"開頭的變數      用於引用在 values.yaml 檔案中定義的變數
雙花括號"{{}}"中的橫槓“-”  用於去除表示式執行後遺留的空白行,使得渲染的結果更加緊湊和美觀

語法說明

indent函式   縮排空格,可以指定空格數目
nindent函式  先新增一個換行再縮排空格,可以指定空格數目
toYaml函式   用於將values.yaml檔案中的片段插入模版檔案中的指定位置
range函式    用於遍歷陣列、物件等資料結構
include函式  用來引用_helpers.tpl檔案中的模版程式碼

{{if}}...{{end}}    
條件判斷,用於根據不同的條件生成不同的內容,結合配置中的布林型欄位使用

{{with}}...{{end}}
用於建立一個區域性作用域,在這個作用域中執行特定的模組程式碼塊
在with塊內不支援引用外部變數,但可以在with塊之前定義一個變數,但並在內部引用該變數
如果with的變數未定義或者為空,with函式將不執行裡面的模組程式碼塊,
可以將一些不常用的配置設定為空,使用者可以執行helm命令時,透過--set引數來自定義

內建物件".Files”,用於將檔案內容插入模板中的指定位置處
“.Files.Get”方法讀取檔案內容並將其插入當前位置
“.Files.Glob”方法獲取匹配指定萬用字元模式的檔案列表,並透過range函式遍歷這些檔案以讀取其內容

建立Chart

[root@k8s-master ~]# kubectl create namespace test-helm
namespace/test-helm created
[root@k8s-master ~]# 

建立目錄及檔案

[root@k8s-master ~]# cd helm-chartdemo/
[root@k8s-master helm-chartdemo]# tree
.
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── NOTES.txt
│   └── service.yaml
└── values.yaml

1 directory, 5 files
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# cat Chart.yaml 
apiVersion: v2 
name: helm-chartdemo
description: A Helm chart for Kubernetes 
type: application 
version: 0.1.0 
appVersion: "1.16.0"
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# cat values.yaml 
replicas: 1
image:
  repository: nginx
  tag: latest
service:
  type: ClusterIP
  port: 80
  targetPort: 80
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }} 
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      release-name: {{ .Release.Name }}
  template:
    metadata:
      labels:
        release-name: {{ .Release.Name }}
    spec:
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }} 
        name: web
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# cat templates/NOTES.txt 
歡迎使用Chart!這是一個Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# cat templates/service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }} 
spec:
  type: {{ .Values.service.type }}
  selector:
    release-name: {{ .Release.Name }}
  ports:
    - protocol: TCP
      port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.targetPort }}
[root@k8s-master helm-chartdemo]# 

安裝Chart及檢視Release

# 從本地安裝
[root@k8s-master helm-chartdemo]# helm install chartdemo . -n test-helm
NAME: chartdemo
LAST DEPLOYED: Wed Oct 16 10:21:05 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
歡迎使用Chart!這是一個Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]# 

# 檢視Release等資訊
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
chartdemo       test-helm       1               2024-10-16 10:21:05.380344629 +0800 CST deployed        helm-chartdemo-0.1.0    1.16.0     
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-mfjzr   1/1     Running   0          18m   10.244.235.205   k8s-master   <none>           <none>

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo   1/1     1            1           18m   web          nginx:latest   release-name=chartdemo

NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/chartdemo   ClusterIP   10.96.229.216   <none>        80/TCP    18m   release-name=chartdemo
[root@k8s-master helm-chartdemo]# 

動態更新values.yaml值

[root@k8s-master helm-chartdemo]# helm install chartdemo2 . --set service.type=NodePort -n test-helm
NAME: chartdemo2
LAST DEPLOYED: Wed Oct 16 10:50:40 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
歡迎使用Chart!這是一個Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
chartdemo       test-helm       1               2024-10-16 10:21:05.380344629 +0800 CST deployed        helm-chartdemo-0.1.0    1.16.0     
chartdemo2      test-helm       1               2024-10-16 10:50:40.287432354 +0800 CST deployed        helm-chartdemo-0.1.0    1.16.0     
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-mfjzr    1/1     Running   0          30m   10.244.235.205   k8s-master   <none>           <none>
pod/chartdemo2-7b65dfd898-z7775   1/1     Running   0          69s   10.244.235.206   k8s-master   <none>           <none>

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo    1/1     1            1           30m   web          nginx:latest   release-name=chartdemo
deployment.apps/chartdemo2   1/1     1            1           69s   web          nginx:latest   release-name=chartdemo2

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/chartdemo    ClusterIP   10.96.229.216   <none>        80/TCP         30m   release-name=chartdemo
service/chartdemo2   NodePort    10.104.7.166    <none>        80:30456/TCP   69s   release-name=chartdemo2
[root@k8s-master helm-chartdemo]# 

更新、回滾和解除安裝 Release

# 當前狀態
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
chartdemo       test-helm       1               2024-10-16 10:21:05.380344629 +0800 CST deployed        helm-chartdemo-0.1.0    1.16.0     
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Wed Oct 16 10:21:05 2024        deployed        helm-chartdemo-0.1.0    1.16.0          Install complete
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-mfjzr   1/1     Running   0          44m   10.244.235.205   k8s-master   <none>           <none>

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo   1/1     1            1           44m   web          nginx:latest   release-name=chartdemo

NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/chartdemo   ClusterIP   10.96.229.216   <none>        80/TCP    44m   release-name=chartdemo
[root@k8s-master helm-chartdemo]# 

# 更新 Release
[root@k8s-master helm-chartdemo]# helm upgrade chartdemo . --set replicas=3,service.type=NodePort -n test-helm
Release "chartdemo" has been upgraded. Happy Helming!
NAME: chartdemo
LAST DEPLOYED: Wed Oct 16 11:07:49 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
歡迎使用Chart!這是一個Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Wed Oct 16 10:21:05 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Install complete
2               Wed Oct 16 11:07:49 2024        deployed        helm-chartdemo-0.1.0    1.16.0          Upgrade complete
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-2nvq2   1/1     Running   0          11s   10.244.235.209   k8s-master   <none>           <none>
pod/chartdemo-86697c564b-ghp7d   1/1     Running   0          11s   10.244.235.208   k8s-master   <none>           <none>
pod/chartdemo-86697c564b-mfjzr   1/1     Running   0          46m   10.244.235.205   k8s-master   <none>           <none>

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo   3/3     3            3           46m   web          nginx:latest   release-name=chartdemo

NAME                TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/chartdemo   NodePort   10.96.229.216   <none>        80:31710/TCP   46m   release-name=chartdemo
[root@k8s-master helm-chartdemo]# 


# 回滾 Release
[root@k8s-master helm-chartdemo]# helm rollback chartdemo -n test-helm
Rollback was a success! Happy Helming!
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-mfjzr   1/1     Running   0          50m   10.244.235.205   k8s-master   <none>           <none>

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo   1/1     1            1           50m   web          nginx:latest   release-name=chartdemo

NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/chartdemo   ClusterIP   10.96.229.216   <none>        80/TCP    50m   release-name=chartdemo
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Wed Oct 16 10:21:05 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Install complete
2               Wed Oct 16 11:07:49 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Upgrade complete
3               Wed Oct 16 11:11:05 2024        deployed        helm-chartdemo-0.1.0    1.16.0          Rollback to 1   
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm rollback chartdemo 2 -n test-helm
Rollback was a success! Happy Helming!
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION     
1               Wed Oct 16 10:21:05 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Install complete
2               Wed Oct 16 11:07:49 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Upgrade complete
3               Wed Oct 16 11:11:05 2024        superseded      helm-chartdemo-0.1.0    1.16.0          Rollback to 1   
4               Wed Oct 16 11:13:05 2024        deployed        helm-chartdemo-0.1.0    1.16.0          Rollback to 2   
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
pod/chartdemo-86697c564b-dtczs   1/1     Running   0          9s    10.244.235.210   k8s-master   <none>           <none>
pod/chartdemo-86697c564b-mfjzr   1/1     Running   0          52m   10.244.235.205   k8s-master   <none>           <none>
pod/chartdemo-86697c564b-vl8lk   1/1     Running   0          9s    10.244.235.211   k8s-master   <none>           <none>

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/chartdemo   3/3     3            3           52m   web          nginx:latest   release-name=chartdemo

NAME                TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/chartdemo   NodePort   10.96.229.216   <none>        80:31650/TCP   52m   release-name=chartdemo
[root@k8s-master helm-chartdemo]# 

# 刪除 Release
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
chartdemo       test-helm       4               2024-10-16 11:13:05.336732826 +0800 CST deployed        helm-chartdemo-0.1.0    1.16.0     
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm uninstall chartdemo -n test-helm
release "chartdemo" uninstalled
[root@k8s-master helm-chartdemo]# 
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION
[root@k8s-master helm-chartdemo]# 

自建Chart倉庫

在實際使用中通常使用Nexus3、Harbor,JFrog等製品庫工具來自建Chart倉庫。
如果想單獨部署一個純粹的Chart倉庫,可以使用ChartMuseum(Helm開源的Chart倉庫服務)。

  • https://chartmuseum.com/
  • https://github.com/helm/chartmuseum

相關文章