Helm詳細介紹和使用
helm簡介
? Helm是Kubernetes 應用的包管理工具,主要用來管理 Charts,類似Linux系統的yum。
? Helm Chart是用來封裝Kubernetes原生應用程序的一系列YAML文件。可以在你部署應用的時候自定義應用程序的一些 Metadata,以便于應用程序的分發。
? 對于應用發布者而言 : 通過Helm打包應用、管理應用依賴關系、管理應用版本并發布應用到軟件倉庫。
? 對于使用者而言: 使用Helm后可以以簡單的方式在Kubernetes上查找、安裝、升級、回滾、卸載應用程序
安裝
[root@k8s-master ~]# ll [root@k8s-master ~]# tar xvf helm-v3.17.2-linux-amd64.tar.gz linux-amd64/ linux-amd64/LICENSE linux-amd64/helm linux-amd64/README.md [root@k8s-master ~]# ll drwxr-xr-x. 2 1001 docker 50 Mar 13 12:25 linux-amd64 [root@k8s-master ~]#cdlinux-amd64/ [root@k8s-master linux-amd64]#ls helm LICENSE README.md [root@k8s-master linux-amd64]# cp-p helm /usr/local/bin/ [root@k8s-master linux-amd64]# echo"source <(helm completion bash)"?>> ~/.bashrc [root@k8s-master linux-amd64]# source ~/.bashrc [root@k8s-master linux-amd64]# helm version version.BuildInfo{Version:"v3.17.2", GitCommit:"cc0bbbd6d6276b83880042c1ecb34087e84d41eb", GitTreeState:"clean", GoVersion:"go1.23.7"}
管理第三方repo源
? 阿里云倉庫:https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
? bitnami倉庫: https://charts.bitnami.com/bitnami
? 微軟倉庫:Index of /kubernetes/charts/
? 官方倉庫: https://hub.kubeapps.com/charts/incubator
#添加阿里云倉庫 [root@k8s-master linux-amd64]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts "aliyun"has been added to your repositories #天機bitnami倉庫 [root@k8s-master linux-amd64]# helm repo add bitnami https://charts.bitnami.com/bitnami "bitnami"has been added to your repositories #查看倉庫信息 [root@k8s-master linux-amd64]# helm repo list NAME URL aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts bitnami https://charts.bitnami.com/bitnami #查看倉庫存儲helm清單 [root@k8s-master linux-amd64]# helm search repo aliyun NAME CHART VERSION APP VERSION DESCRIPTION aliyun/acs-engine-autoscaler 2.1.3 2.1.1 Scales worker nodes within agent pools #刪除第三方存儲庫 [root@k8s-master helm]# helm repo remove aliyun
helm的使用方法
#查找chart [root@k8s-master .docker]# helm search repo redis NAME CHART VERSION APP VERSION DESCRIPTION aliyun/redis 1.1.15 4.0.8 Opensource, advanced key-value store. It is of... aliyun/redis-ha 2.0.1 Highly available Redis cluster with multiple se... bitnami/redis 20.11.3 7.4.2 Redis(R) is an opensource, advanced key-value ... bitnami/redis-cluster 11.4.5 7.4.2 Redis(R) is an opensource, scalable, distribut... aliyun/sensu 0.2.0 Sensu monitoring framework backed by the Redis ... bitnami/keydb 0.5.3 6.3.4 KeyDB is a high performance fork of Redis with ... #查看chart信息 [root@k8s-master .docker]# helm show chart aliyun/redis apiVersion: v1 appVersion: 4.0.8 description: Opensource, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. home: http://redis.io/ icon: https://bitnami.com/assets/stacks/redis/img/redis-stack-220x234.png keywords: - redis - keyvalue - database maintainers: - email: [email protected] name: bitnami-bot name: redis sources: - https://github.com/bitnami/bitnami-docker-redis version: 1.1.15 #安裝chart包 [root@k8s-master .docker]# helm install wyx aliyun/redis #查看項目發布狀態 [root@k8s-master ~]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION db default 3 2025-03-20 0420.165959026 -0400 EDT deployed mysql-1.6.9 5.7.30 [root@k8s-master ~]# helm status db #查看歷史 [root@k8s-master ~]# helmhistorydb REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Thu Mar 20 0446 2025 superseded mysql-1.6.9 5.7.30 Install complete 2 Thu Mar 20 0449 2025 superseded mysql-1.6.9 5.7.30 Upgrade complete 3 Thu Mar 20 0420 2025 deployed mysql-1.6.9 5.7.30 Upgrade complete
部署應用:helm部署MySQL
[root@k8s-master helm]# helm pull azure/mysql [root@k8s-master helm]#ls mysql-1.6.9.tgz [root@k8s-master helm]# tar xvf mysql-1.6.9.tgz [root@k8s-master helm]#ls mysql mysql-1.6.9.tgz [root@k8s-master helm]#cdmysql [root@k8s-master mysql]# ll total 36 -rw-r--r--. 1 root root 406 Nov 13 2020 Chart.yaml -rw-r--r--. 1 root root 23661 Nov 13 2020 README.md drwxr-xr-x. 3 root root 268 Mar 20 04:29 templates -rw-r--r--. 1 root root 6164 Nov 13 2020 values.yaml #Chart.yaml 是chart 的描述信息 #README.md 是此chart 的幫助信息 #templates 目錄里是各種模板,比如定義svc,定義pvc 等 #values.yaml里記錄的是chart 的各種信息,比如鏡像是什么,root 密碼是什么,是否使用持久性存儲等 [root@k8s-master mysql]# vim values.yaml #禁用持久化存儲 persistence: enabled:false [root@k8s-master mysql]# helm upgrade db . [root@k8s-master mysql]# kubectl get pod NAME READY STATUS RESTARTS AGE db-mysql-7678cd479-xzp2c 1/1 Running 0 2m33s [root@k8s-master mysql]# kubectl get pods -o wide --no-headers db-mysql-7678cd479-xzp2c 1/1 Running 0 2m49s 10.244.36.77 k8s-node1[root@k8s-master mysql]# mysql -u root -p123456 -h 10.244.36.77 Welcome to the MariaDB monitor. Commands end with ; or g. Your MySQL connectionidis 15 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type'help;'or'h'forhelp. Type'c'to clear the current input statement. MySQL [(none)]>
構建helm中的chart包
Helm Chart目錄結構
[root@k8s-master ~]# helm create wyx Creating wyx [root@k8s-master ~]# tree wyx/ wyx/ ├── charts #目錄里存放這個chart依賴的所有子chart ├── Chart.yaml #用于描述這個chart的基本信息 ├── templates #目錄里存放所有yaml模板文件 │ ├── deployment.yaml │ ├── _helpers.tpl #放置模板助手的地方,可以在整個chart中重復使用 │ ├── hpa.yaml │ ├── ingress.yaml │ ├── NOTES.txt │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml └── values.yaml#用于存儲templates目錄中模板文件用到變量的值
構建一個Helm Chart
helm chart 目錄結構
[root@k8s-master ~]# helm create mychart Creating mychart [root@k8s-master ~]# tree mychart/ mychart/ ├── 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
?charts/: 這個目錄用于存放chart依賴的其他charts。如果一個chart需要其他charts的資源,這些charts會被放置在這個目錄下。
?Chart.yaml: 這是一個必需的文件,包含了chart的元數據,例如名稱、版本、描述等。
?templates/: 這個目錄包含了chart的模板文件。這些模板文件是Kubernetes的manifest文件,它們使用Helm的模板語言來生成實際的Kubernetes資源文件。
?deployment.yaml: 定義了應用的Deployment資源,用于指定應用的副本數和相關的部署策略。
?_helpers.tpl: 這是一個可以包含模板函數和部分模板的文件,這些可以被其他模板文件重用。
?hpa.yaml: 定義了Horizontal Pod Autoscaler(水平自動伸縮器)資源,用于根據CPU使用率或其他指標自動調整Pod的數量。
?ingress.yaml: 如果應用需要外部訪問,這個文件定義了Ingress資源,用于管理外部訪問應用的路由規則。
?NOTES.txt: 在chart安裝后,這個文件中的內容會被展示給用戶,通常包含一些安裝后的重要信息或說明。
?serviceaccount.yaml: 定義了ServiceAccount資源,用于指定應用運行時使用的Kubernetes服務賬號。
?service.yaml: 定義了Service資源,用于定義應用的網絡訪問方式,如ClusterIP、NodePort或LoadBalancer。
?tests/: 這個目錄包含了用于測試chart的測試腳本。
?values.yaml: 這是一個配置文件,包含了chart的配置值。用戶可以通過修改這個文件來定制chart的行為。Helm會使用這個文件中的值來填充templates/目錄下的模板文件。
#部署 [root@k8s-master ~]# helm install web mychart/ NAME: web LAST DEPLOYED: Fri Apr 11 0511 2025 NAMESPACE: tekton-pipelines STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands: exportPOD_NAME=$(kubectl get pods --namespace tekton-pipelines -l"app.kubernetes.io/name=mychart,app.kubernetes.io/instance=web"-o jsonpath="{.items[0].metadata.name}") exportCONTAINER_PORT=$(kubectl get pod --namespace tekton-pipelines$POD_NAME-o jsonpath="{.spec.containers[0].ports[0].containerPort}") echo"Visit http://127.0.0.1:8080 to use your application" kubectl --namespace tekton-pipelines port-forward$POD_NAME8080:$CONTAINER_PORT #打包推送的charts倉庫共享別人使用 [root@k8s-master ~]# helm package mychart/ Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz [root@k8s-master ~]# ll mychart-0.1.0.tgz -rw-r--r-- 1 root root 4166 Apr 11 05:24 mychart-0.1.0.tgz
定義chart模板
[root@k8s-master ~]# helm create nginx Creating nginx [root@k8s-master ~]#cdnginx/templates/ [root@k8s-master templates]#ls deployment.yaml _helpers.tpl hpa.yaml ingress.yaml NOTES.txt serviceaccount.yaml service.yaml tests [root@k8s-master templates]# > ../values.yaml [root@k8s-master templates]#cat> ../values.yaml <replicas: 1 > image: nginx > tag: 1.17.1 > label: nginx > port: 80 > EOF [root@k8s-master templates]#rm-rf ./* [root@k8s-master templates]# kubectl create deployment web1 --image=nginx --dry-run=client -o yaml > deployment.yaml [root@k8s-master templates]# [root@k8s-master templates]# kubectl apply -f deployment.yaml deployment.apps/web1 created [root@k8s-master templates]# kubectl expose deployment web1 --port=80 --target-port=80 --dry-run=client -o yaml > service.yaml [root@k8s-master templates]# kubectl apply -f service.yaml service/web1 created [root@k8s-master templates]# kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE web-mychart 1/1 1 1 8m22s web1 1/1 1 1 49s [root@k8s-master templates]# kubectl get deployments,pod,services NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/web-mychart 1/1 1 1 8m53s deployment.apps/web1 1/1 1 1 80s NAME READY STATUS RESTARTS AGE pod/web-mychart-59769b9b9c-grfrs 1/1 Running 0 8m53s pod/web1-6fbb48567f-cw7kk 1/1 Running 0 80s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/web-mychart ClusterIP 10.96.75.107 80/TCP 8m53s service/web1 ClusterIP 10.96.166.252 80/TCP 63s [root@k8s-master templates]# ll total 8 -rw-r--r-- 1 root root 380 Apr 11 05:31 deployment.yaml -rw-r--r-- 1 root root 220 Apr 11 05:31 service.yaml [root@k8s-master templates]#catdeployment.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web1 name: web1 spec: replicas: 1 selector: matchLabels: app: web1 strategy: {} template: metadata: creationTimestamp: null labels: app: web1 spec: containers: - image: nginx name: nginx resources: {} status: {} [root@k8s-master templates]#
調試
Helm 也提供了--dry-run --debug 調試參數,幫助你驗證模板正確性。在執行 helm install 時候帶上這兩個參數就可以把對應的 values 值和渲染的資源清單打印出來,而不會真正的去部署一個 release。
[root@k8s-master ~]# helm install --dry-run web1 nginx/ NAME: web1 LAST DEPLOYED: Fri Apr 11 0540 2025 NAMESPACE: tekton-pipelines STATUS: pending-install REVISION: 1 TEST SUITE: None HOOKS: MANIFEST: --- # Source: nginx/templates/service.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: web1 name: web1 spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web1 status: loadBalancer: {} --- # Source: nginx/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web1 name: web1 spec: replicas: 1 selector: matchLabels: app: web1 strategy: {} template: metadata: creationTimestamp: null labels: app: web1 spec: containers: - image: nginx name: nginx resources: {} status: {}
內置對象
字段 | 描述 |
release.name | release的名稱,標識特定的release |
release.namespace | release的名稱空間,release所在的k8s名稱空間 |
release.service | release服務的名稱,通常用于服務發現和通信 |
release.revision | release的修訂版本號,cong1開始累加,表示release的更新次數 |
Values
? chart包中的values.yaml文件:是chart自帶的默認配置文件,它為模板提高了初始的配置值
? 父chart包的values.yaml文件:如果當前chart作為依賴被其他chart包含,那么父chart中的values.yaml文件中的值可以傳遞到子chart中,允許父chart覆蓋子chart的默認配置
? 通過helm install或者helm upgrade的-f或者--values參數傳入的自定義yaml文件:用戶可以通過命令行參數指定一個或多個仔頂喲的額yaml配置文件,這些文件中的值可以覆蓋chart包中的values.yaml文件中的相應值
[root@k8s-master helm]# helm install web1 nginx/ NAME: web1 LAST DEPLOYED: Mon Apr 14 1536 2025 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None [root@k8s-master helm]# helm list | grep web1 web1 default 1 2025-04-14 1536.185071275 +0800 CST deployed nginx-0.1.0 1.16.0 [root@k8s-master helm]# kubectl get pod,svc | grep web1 pod/web1-6fbb48567f-97m5v 1/1 Running 0 3m45s service/web1 ClusterIP 10.96.144.7180/TCP 3m45s #更新副本數 [root@k8s-master helm]# helm install web2 ./nginx --setreplicas=3 NAME: web2 LAST DEPLOYED: Mon Apr 14 1649 2025 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None [root@k8s-master helm]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION web default 1 2025-04-14 1009.27834645 +0800 CST deployed mychart-0.1.0 1.16.0 web2 default 1 2025-04-14 1649.529691316 +0800 CST deployed nginx-0.1.0 1.16.0 [root@k8s-master helm]# kubectl get pod NAME READY STATUS RESTARTS AGE web-mychart-59769b9b9c-xqvjt 1/1 Running 0 6h13m web2-7d78cf6476-2bqth 1/1 Running 0 13s web2-7d78cf6476-7qsvb 1/1 Running 0 13s web2-7d78cf6476-npljb 1/1 Running 0 13s #查看歷史記錄 [root@k8s-master helm]# helmhistoryweb2 REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Mon Apr 14 1649 2025 deployed nginx-0.1.0 1.16.0 Install complete
Helm部署Nginx
[root@k8s-master nginx]# tree . ├── 1ms-helper ├── nginx-hw │ ├── charts │ ├── Chart.yaml │ ├── templates │ │ ├── deployment.yaml │ │ └── service.yaml │ └── values.yaml ├── nginx-values.yaml ├── README.md └── templates [root@k8s-master nginx-hw]#catChart.yaml apiVersion: v1 appVersion: v2.2 description: first helm keywords: - hw-nginx name: nginx-hw version: v1.0.0 [root@k8s-master nginx-hw]#catvalues.yaml deployname: hw-nginx replicaCount: 2 images: repository: hub.17usoft.com/middleware/nginx:1.22.1 tag: v1 [root@k8s-master nginx-hw]#cattemplates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Values.deployname }} labels: app: hw-nginx spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: hw-nginx template: metadata: labels: app: hw-nginx spec: containers: - name: hw-nginx image: {{ .Values.images.repository }} imagePullPolicy: IfNotPresent ports: - containerPort: 80 [root@k8s-master nginx-hw]#cattemplates/service.yaml apiVersion: v1 kind: Service metadata: name: hw-nginx namespace: {{ .Release.Namespace }} spec: type: NodePort selector: app: hw-nginx ports: - name: http port: 80 targetPort: 80 nodePort: 30001 protocol: TCP [root@k8s-master nginx]# helm install hw-nginx ./nginx-hw -n nginx NAME: hw-nginx LAST DEPLOYED: Wed Apr 16 2036 2025 NAMESPACE: nginx STATUS: deployed REVISION: 1 TEST SUITE: None [root@k8s-master nginx]# kubectl get pod,svc -n nginx NAME READY STATUS RESTARTS AGE pod/hw-nginx-794b584dc6-nqh99 0/1 ContainerCreating 0 7s pod/hw-nginx-794b584dc6-t9tdk 0/1 ContainerCreating 0 7s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/hw-nginx NodePort 10.96.209.7380:30001/TCP 8s [root@k8s-master nginx]# kubectl get pod -n nginx -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hw-nginx-794b584dc6-nqh99 1/1 Running 0 23s 10.244.169.140 k8s-node2 hw-nginx-794b584dc6-t9tdk 1/1 Running 0 23s 10.244.36.79 k8s-node1 [root@k8s-master nginx]# curl 192.168.9.170:30001 Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank youforusing nginx.
鏈接:https://blog.csdn.net/m0_66011019/article/details/147297766
-
Linux
+關注
關注
87文章
11496瀏覽量
213255 -
應用程序
+關注
關注
38文章
3329瀏覽量
58883 -
kubernetes
+關注
關注
0文章
243瀏覽量
9022
原文標題:Helm模板語法終極指南:從Values.yaml到Release的完整生命周期
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
Kubernetes Helm入門指南

使用Helm 在容器服務k8s集群一鍵部署wordpress
ESP8266使用詳細介紹
Helm的一些概念及用法

請問如何使用Helm在K8s上集成Prometheus呢?

評論