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 ~]# cd linux-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 Open source, 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 open source, advanced key-value ... bitnami/redis-cluster 11.4.5 7.4.2 Redis(R) is an open source, 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: Open source, 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: containers@bitnami.com 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 ~]# helm history db 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]# cd mysql [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 connection id is 15 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. 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:
export POD_NAME=$(kubectl get pods --namespace tekton-pipelines -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=web" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_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_NAME 8080:$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 ~]# cd nginx/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]# cat 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: {} [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 --set replicas=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]# helm history web2 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]# cat Chart.yaml
apiVersion: v1
appVersion: v2.2
description: first helm
keywords:
- hw-nginx
name: nginx-hw
version: v1.0.0
[root@k8s-master nginx-hw]# cat values.yaml
deployname: hw-nginx
replicaCount: 2
images:
repository: hub.17usoft.com/middleware/nginx:1.22.1
tag: v1
[root@k8s-master nginx-hw]# cat templates/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]# cat templates/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.73 80: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 you for using nginx.
链接:https://blog.csdn.net/m0_66011019/article/details/147297766
全部0条评论
快来发表一下你的评论吧 !