1.1helm概述
1)什么是helm?
helm有点类似于Linux的yum,apt工具,帮助我们管理K8S集群的资源清单。
Helm 帮助您管理 Kubernetes 应用—— Helm Chart,即使是最复杂的 Kubernetes 应用程序,都可以帮助您定义,安装和升级。
Helm Chart 易于创建、发版、分享和发布,所以停止复制粘贴,开始使用 Helm 吧。
Helm 是 CNCF 的毕业项目,由 Helm 社区维护。
2)helm的架构版本选择
2019年11月Helm团队发布V3版本,相比v2版本最大变化是将Tiller删除,并大部分代码重构。
helm v3相比helm v2还做了很多优化,比如不同命名空间资源同名的情况在v3版本是允许的,我们在生产环境中使用建议大家使用v3版本,不仅仅是因为它版本功能较强,而且相对来说也更加稳定了。
3)helm的相关术语
- Chart:
是用来管理k8s集群的资源清单,以及Chart应用信息(Chart.yaml),变量定义(values.yaml)文件的集合。
Chart分为两种类型,'application' 和 'library'。
- application: 可以直接基于该Chart进行部署,可以单独部署。
- library: 只能被其他Chart引用,作为一个库存在,无法单独部署。
值得注意的是,Chart只是一堆文件集合,可以对其进行修改,打包,传输等。
- Release:
我们可以基于Chart进行部署,部署后的应用我们称之为Release,即对外发布的版本。
我们可以对同一个Chart部署多个应用,也可以基于Chart进行升级。
我们也可以对Release单独进行回滚。
温馨提示:
Chart和Release的关系有点类似于程序文件和程序的关系,比如:"游戏exe"和"运行的游戏"的关系。
1.2安装helm
1.下载helm
wget https://get.helm.sh/helm-v3.19.4-linux-amd64.tar.gz
2.解压helm软件包
[root@master231 ~]# tar xf helm-v3.19.4-linux-amd64.tar.gz -C /usr/local/bin/ linux-amd64/helm --strip-components=1 #只解压linux-amd64/helm,踢除linux-amd64/这一层目录,剩helm
[root@master231 ~]#
[root@master231 ~]# ll /usr/local/bin/helm
-rwxr-xr-x 1 1001 1001 60182712 Dec 13 05:40 /usr/local/bin/helm*
[root@master231 ~]#
3.查看helm的版本
[root@master231 ~]# helm version
version.BuildInfo{Version:"v3.19.4", GitCommit:"7cfb6e486dac026202556836bb910c37d847793e", GitTreeState:"clean", GoVersion:"go1.24.11"}
[root@master231 ~]#
4.配置helm的自动补全功能
[root@master231 ~]# helm completion bash > /etc/bash_completion.d/helm
[root@master231 ~]#
[root@master231 ~]# echo 'source /etc/bash_completion.d/helm' >> ~/.bashrc
5.验证自动补全功能
[root@master231 ~]# source ~/.bashrc
[root@master231 ~]#
[root@master231 ~]# helm #连续按2次tab键即可
completion (generate autocompletion scripts for the specified shell)
create (create a new chart with the given name)
dependency (manage a chart's dependencies)
env (helm client environment information)
get (download extended information of a named release)
help (Help about any command)
history (fetch release history)
install (install a chart)
lint (examine a chart for possible issues)
list (list releases)
package (package a chart directory into a chart archive)
plugin (install, list, or uninstall Helm plugins)
pull (download a chart from a repository and (optionally) unpack it in local directory)
push (push a chart to remote)
registry (login to or logout from a registry)
repo (add, list, remove, update, and index chart repositories)
rollback (roll back a release to a previous revision)
search (search for a keyword in charts)
show (show information of a chart)
status (display the status of the named release)
template (locally render templates)
test (run tests for a release)
uninstall (uninstall a release)
upgrade (upgrade a release)
verify (verify that a chart at the given path has been signed and is valid)
version (print the client version information)
1.3helm的Chart基本管理
1.创建Chart
[root@master231 ~]# mkdir -p /oldboyedu/manifests/add-ons/helm-Chart
[root@master231 ~]#
[root@master231 ~]# cd /oldboyedu/manifests/add-ons/helm-Chart
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# apt -y install tree
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm create oldboyedu-linux
Creating oldboyedu-linux
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# ll
total 12
drwxr-xr-x 3 root root 4096 Dec 17 15:32 ./
drwxr-xr-x 7 root root 4096 Dec 17 15:31 ../
drwxr-xr-x 4 root root 4096 Dec 17 15:32 oldboyedu-linux/ #多了一个目录
[root@master231 helm-Chart]#
2.查看Chart结构
[root@master231 helm-Chart]# tree oldboyedu-linux
oldboyedu-linux/
├── charts # 包含chart依赖的其他chart
├── Chart.yaml # 包含了chart信息的YAML文件
├── templates # 模板目录, 当和values 结合时,可生成有效的Kubernetes manifest文件
│ ├── deployment.yaml # deployment资源清单模板。
│ ├── _helpers.tpl # 自定义模板
│ ├── hpa.yaml # hpa资源清单模板。
│ ├── ingress.yaml # Ingress资源清单模板。
│ ├── NOTES.txt # 可选: 包含简要使用说明的纯文本文件
│ ├── serviceaccount.yaml # sa资源清单模板。
│ ├── service.yaml # svc资源清单模板。
│ └── tests # 测试目录
│ └── test-connection.yaml
└── values.yaml # chart 默认的配置值
3 directories, 10 files
[root@master231 helm-Chart]#
3.修改默认的values.yaml
[root@master231 helm-Chart]# egrep "repository:|tag:" oldboyedu-linux/values.yaml
repository: nginx
tag: ""
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# sed -i "/repository\:/s#nginx#registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps#" oldboyedu-linux/values.yaml
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# sed -ri '/tag\:/s#tag: ""#tag: v1#' oldboyedu-linux/values.yaml
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# egrep "repository:|tag:" oldboyedu-linux/values.yaml
repository: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps
tag: v1
[root@master231 helm-Chart]#
4.基于Chart安装服务 发行Release
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME SECRETS AGE
serviceaccount/default 1 17d
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm -n kube-public install xiuxian oldboyedu-linux
NAME: xiuxian
LAST DEPLOYED: Wed Dec 17 15:38:55 2025
NAMESPACE: kube-public
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace kube-public -l "app.kubernetes.io/name=oldboyedu-linux,app.kubernetes.io/instance=xiuxian" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace kube-public $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace kube-public port-forward $POD_NAME 8080:$CONTAINER_PORT
[root@master231 helm-Chart]#
5.查看Release列表并查看K8S的资源变化
[root@master231 helm-Chart]# helm list -n kube-public
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
xiuxian kube-public 1 2025-12-17 15:38:55.471776112 +0800 CST deployed oldboyedu-linux-0.1.0 1.16.0
[root@master231 helm-Chart]#
#helm自己全都给创建了
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/xiuxian-oldboyedu-linux 1/1 1 1 2m4s oldboyedu-linux registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/xiuxian-oldboyedu-linux ClusterIP 10.200.136.76 <none> 80/TCP 2m4s app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME SECRETS AGE
serviceaccount/default 1 17d
serviceaccount/xiuxian-oldboyedu-linux 1 2m4s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/xiuxian-oldboyedu-linux-6847ff7955-2xtnm 1/1 Running 0 2m4s 10.100.1.226 worker232 <none> <none>
[root@master231 helm-Chart]#
6.测试访问
[root@master231 helm-Chart]# curl 10.100.1.226
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v1</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>
<body>
<h1 style="color: green">凡人修仙传 v1 </h1>
<div>
<img src="1.jpg">
<div>
</body>
</html>
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# curl 10.200.136.76
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v1</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>
<body>
<h1 style="color: green">凡人修仙传 v1 </h1>
<div>
<img src="1.jpg">
<div>
</body>
</html>
[root@master231 helm-Chart]#
7.卸载服务
[root@master231 helm-Chart]# helm list -n kube-public
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
xiuxian kube-public 1 2025-12-17 15:38:55.471776112 +0800 CST deployed oldboyedu-linux-0.1.0 1.16.0
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm -n kube-public uninstall xiuxian
release "xiuxian" uninstalled
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm list -n kube-public
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME SECRETS AGE
serviceaccount/default 1 17d
[root@master231 helm-Chart]#
1.4helm的两种升级方式案例
1.安装旧的服务
[root@master231 helm-Chart]# helm -n kube-public install xiuxian oldboyedu-linux
NAME: xiuxian
LAST DEPLOYED: Wed Dec 17 16:09:12 2025
NAMESPACE: kube-public
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace kube-public -l "app.kubernetes.io/name=oldboyedu-linux,app.kubernetes.io/instance=xiuxian" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace kube-public $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace kube-public port-forward $POD_NAME 8080:$CONTAINER_PORT
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/xiuxian-oldboyedu-linux 1/1 1 1 20s oldboyedu-linux registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/xiuxian-oldboyedu-linux ClusterIP 10.200.198.9 <none> 80/TCP 20s app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME SECRETS AGE
serviceaccount/default 1 17d
serviceaccount/xiuxian-oldboyedu-linux 1 20s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/xiuxian-oldboyedu-linux-6847ff7955-mkrv7 1/1 Running 0 20s 10.100.1.227 worker232 <none> <none>
[root@master231 helm-Chart]#
2.测试访问svc
[root@worker232 ~]# curl 10.200.198.9
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v1</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>
<body>
<h1 style="color: green">凡人修仙传 v1 </h1>
<div>
<img src="1.jpg">
<div>
</body>
</html>
[root@worker232 ~]#
3.修改要升级的相关参数
[root@master231 helm-Chart]# egrep "replicaCount|tag:" oldboyedu-linux/values.yaml
replicaCount: 1
tag: v1
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# sed -i '/replicaCount/s#1#3#' oldboyedu-linux/values.yaml
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# sed -i "/tag:/s#v1#v2#" oldboyedu-linux/values.yaml
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# egrep "replicaCount|tag:" oldboyedu-linux/values.yaml
replicaCount: 3
tag: v2
[root@master231 helm-Chart]#
4.基于文件方式升级
[root@master231 helm-Chart]# helm -n kube-public upgrade xiuxian -f oldboyedu-linux/values.yaml oldboyedu-linux
Release "xiuxian" has been upgraded. Happy Helming!
NAME: xiuxian
LAST DEPLOYED: Wed Dec 17 16:12:43 2025
NAMESPACE: kube-public
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace kube-public -l "app.kubernetes.io/name=oldboyedu-linux,app.kubernetes.io/instance=xiuxian" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace kube-public $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace kube-public port-forward $POD_NAME 8080:$CONTAINER_PORT
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm list -n kube-public
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
xiuxian kube-public 2 2025-12-17 16:12:43.388020459 +0800 CST deployed oldboyedu-linux-0.1.0 1.16.0
[root@master231 helm-Chart]#
5.验证升级效果
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/xiuxian-oldboyedu-linux 3/3 3 3 4m1s oldboyedu-linux registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2 app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/xiuxian-oldboyedu-linux ClusterIP 10.200.198.9 <none> 80/TCP 4m1s app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME SECRETS AGE
serviceaccount/default 1 17d
serviceaccount/xiuxian-oldboyedu-linux 1 4m1s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/xiuxian-oldboyedu-linux-5576db869d-4pp7b 1/1 Running 0 28s 10.100.2.161 worker233 <none> <none>
pod/xiuxian-oldboyedu-linux-5576db869d-8rbsf 1/1 Running 0 25s 10.100.0.82 master231 <none> <none>
pod/xiuxian-oldboyedu-linux-5576db869d-hzw6n 1/1 Running 0 30s 10.100.1.228 worker232 <none> <none>
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# curl 10.200.198.9
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v2</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>
<body>
<h1 style="color: red">凡人修仙传 v2 </h1>
<div>
<img src="2.jpg">
<div>
</body>
</html>
[root@master231 helm-Chart]#
6.基于环境变量方式升级
[root@master231 helm-Chart]# helm -n kube-public upgrade xiuxian --set replicaCount=5,image.tag=v3 oldboyedu-linux
Release "xiuxian" has been upgraded. Happy Helming!
NAME: xiuxian
LAST DEPLOYED: Wed Dec 17 16:15:29 2025
NAMESPACE: kube-public
STATUS: deployed
REVISION: 3
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace kube-public -l "app.kubernetes.io/name=oldboyedu-linux,app.kubernetes.io/instance=xiuxian" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace kube-public $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace kube-public port-forward $POD_NAME 8080:$CONTAINER_PORT
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# helm -n kube-public list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
xiuxian kube-public 3 2025-12-17 16:15:29.682751815 +0800 CST deployed oldboyedu-linux-0.1.0 1.16.0
[root@master231 helm-Chart]#
7.再次验证升级效果
[root@master231 helm-Chart]# kubectl get deploy,hpa,svc,sa,po -o wide -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/xiuxian-oldboyedu-linux 5/5 5 5 6m53s oldboyedu-linux registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3 app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/xiuxian-oldboyedu-linux ClusterIP 10.200.198.9 <none> 80/TCP 6m53s app.kubernetes.io/instance=xiuxian,app.kubernetes.io/name=oldboyedu-linux
NAME SECRETS AGE
serviceaccount/default 1 17d
serviceaccount/xiuxian-oldboyedu-linux 1 6m53s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/xiuxian-oldboyedu-linux-7d8557d5-czqgk 1/1 Running 0 35s 10.100.2.163 worker233 <none> <none>
pod/xiuxian-oldboyedu-linux-7d8557d5-fw5w8 1/1 Running 0 35s 10.100.0.83 master231 <none> <none>
pod/xiuxian-oldboyedu-linux-7d8557d5-pjcv7 1/1 Running 0 33s 10.100.1.231 worker232 <none> <none>
pod/xiuxian-oldboyedu-linux-7d8557d5-sfqck 1/1 Running 0 33s 10.100.2.164 worker233 <none> <none>
pod/xiuxian-oldboyedu-linux-7d8557d5-zls6c 1/1 Running 0 35s 10.100.1.229 worker232 <none> <none>
[root@master231 helm-Chart]#
[root@master231 helm-Chart]# curl 10.200.198.9
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v3</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>
<body>
<h1 style="color: pink">凡人修仙传 v3 </h1>
<div>
<img src="3.jpg">
<div>
</body>
</html>
[root@master231 helm-Chart]#