容器编排大王Kubernetes——helm包管理工具(8)
概念:
helm简介

- Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts,类似Linux系统的yum/dnf,charts就是rpm。
- Helm Chart是用来封装Kubernetes原生应用程序的一系列YAML文件。可以在你部署应用的时候自定义应用程序的一些 Metadata,以便于应用程序的分发。
- 对于应用发布者而言
- 通过Helm打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。
- 对于使用者而言
- 使用Helm后可以以简单的方式在Kubernetes上查找、安装、升级、回滚、卸载应用程序
官网与资源
官网: https://helm.sh/zh/docs/intro/quickstart/
软件资源: https://github.com/helm/helm/releases

实验操作:
一 部署helm
1.安装helm
[root@master mnt]# tar zxf helm-v3.19.0-linux-amd64.tar.gz
[root@master mnt]# ls
helm-v3.19.0-linux-amd64.tar.gz
[root@master helm]# cd linux-amd64/
[root@master linux-amd64]# ls
helm LICENSE README.md
[root@master linux-amd64]# cp -p helm /usr/local/bin/
2.配置helm命令补齐
[root@master linux-amd64]# echo "source <(helm completion bash)" >> ~/.bashrc
root@master linux-amd64]# source ~/.bashrc
[root@master linux-amd64]# helm version
version.BuildInfo{Version:"v3.19.0", GitCommit:"3d8990f0836691f0229297773f3524598f46bda6", GitTreeState:"clean", GoVersion:"go1.24.7"}
二 helm常用操作
2.1 常用命令
| 命令 | 描述 |
|---|---|
| create | 创建一个 chart 并指定名字 |
| dependency | 管理 chart 依赖 |
| get | 下载一个 release。可用子命令:all、hooks、manifest、notes、values |
| history | 获取 release 历史 |
| install | 安装一个 chart |
| list | 列出 release |
| package | 将 chart 目录打包到 chart 存档文件中 |
| pull | 从远程仓库中下载 chart 并解压到本地 # helm pull stable/mysql – untar |
| repo | 添加,列出,移除,更新和索引 chart 仓库。可用子命令:add、index、 list、remove、update |
| rollback | 从之前版本回滚 |
| search | 根据关键字搜索 chart。可用子命令:hub、repo |
| show | 查看 chart 详细信息。可用子命令:all、chart、readme、values |
| status | 显示已命名版本的状态 |
| template | 本地呈现模板 |
| uninstall | 卸载一个 release |
| upgrade | 更新一个 release |
| version | 查看 helm 客户端版本 |
2.2 helm仓库的管理
以下为常用的仓库源
- 阿里云仓库:https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
- bitnami仓库: https://charts.bitnami.com/bitnami
- 微软仓库:http://mirror.azure.cn/kubernetes/charts/
- 官方仓库: https://hub.kubeapps.com/charts/incubator
[root@k8s-master helm]# helm search hub nginx #在官方仓库中搜索
[root@k8s-master helm]# helm search repo nginx #在本地仓库中搜索
添加第三方repo源
#添加阿里云仓库
[root@master helm]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories#添加bitnami仓库
[root@master helm]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories#添加微软仓库
[root@master helm]# helm repo add azure http://mirror.azure.cn/kubernetes/charts/
"azure" has been added to your repositories#查看仓库信息
[root@master helm]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
azure http://mirror.azure.cn/kubernetes/charts/
bitnami https://charts.bitnami.com/bitnami#查询某个源的包并过滤nginx的包
[root@master helm]# helm search repo aliyun | grep nginx#删除第三方源
[root@master helm]# helm repo remove azure
[root@master helm]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami#查看仓库存储helm清单
[root@k8s-master helm]# 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
aliyun/aerospike 0.1.7 v3.14.1.2 A Helm chart for Aerospike in Kubernetes
......
2.3 helm的使用方法
1.查找chart包
#repo为从添加的第三方源中查找,hub为从官方源查找
[root@master helm]# 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
bitnami/nginx 22.2.1 1.29.2 NGINX Open Source is a web server that can be a...
bitnami/nginx-ingress-controller 12.0.7 1.13.1 NGINX Ingress Controller is an Ingress controll...
bitnami/nginx-intel 2.1.15 0.4.9 DEPRECATED NGINX Open Source for Intel is a lig...
aliyun/gcloud-endpoints 0.1.0 Develop, deploy, protect and moni
2.查看chart包信息
#查找出来后进行信息的查看
[root@master helm]# helm show chart aliyun/nginx-ingress
apiVersion: v1
appVersion: 0.10.2
description: An nginx Ingress controller that uses ConfigMap to store the nginx configuration.
icon: https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Nginx_logo.svg/500px-Nginx_logo.svg.png
keywords:
- ingress
- nginx
maintainers:
- email: jack.zampolin@gmail.comname: jackzampolin
- email: mgoodness@gmail.comname: mgoodness
- email: chance.zibolski@coreos.comname: chancez
name: nginx-ingress
sources:
- https://github.com/kubernetes/ingress-nginx
version: 0.9.5
3 安装chart包
[root@master ~]# helm install fjwyyy bitnami/nginx
[root@master ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
fjwyyy default 1 2024-09-10 12:08:12.3165525 +0800 CST deployed nginx-18.1.11 1.27.1[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
fjwyyy-nginx-8598578bcf-qjwwq 1/1 Running 0 41s#查看项目的发布状态
[root@master ~]# helm status fjwyyy
NAME: fjwyyy
LAST DEPLOYED: Tue Sep 10 12:08:12 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 18.1.11
APP VERSION: 1.27.1#卸载项目
[root@master nginx]# helm uninstall fjwyyy
release "fjwyyy" uninstalled[root@master nginx]# kubectl get pods
No resources found in default namespace.[root@master nginx]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
2.5 安装项目前预定义项目选项
[root@master helm]# docker load -i nginx-1.27.1-debian-12-r2.tar
[root@master nginx]# docker tag bitnami/nginx:1.27.1-debian-12-r2 reg.fy.org/bitnami/nginx:1.27.1-debian-12-r2
[root@master nginx]# docker push reg.fy.org/bitnami/nginx:1.27.1-debian-12-r2[root@master nginx]# helm install fjw /root/helm/nginx

三 构建helm中的chart包
3.1 Helm Chart目录结构
#建立chart项目
[root@master helm]# helm create fjwyyy
Creating fjwyyy[root@master helm]# tree fjwyyy/
fjwyyy/
├── charts #目录里存放这个chart依赖的所有子chart。
├── Chart.yaml #用于描述这个 Chart 的基本信息,包括名字、描述信息以及版本等。
├── templates #目录里面存放所有 yaml 模板文件。
│ ├── deployment.yaml
│ ├── _helpers.tpl #放置模板助手的地方,可以在整个 chart 中重复使用
│ ├── hpa.yaml
│ ├── httproute.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml #用于存储 templates 目录中模板文件中用到变量的值。
3.2 构建方法
编写chart文件
[root@master fjwyyy]# vim Chart.yaml
apiVersion: v2
name: fjwyyy
description: A Helm chart for Kubernetes
type: application
version: 0.1.0 #项目版本
appVersion: "v1" #软件版本[root@master fjwyyy]# vim values.yaml
replicaCount: 2 #运行pod的数量
image:repository: myapp #运行的镜像pullPolicy: IfNotPresenttag: "v1"
ingress:enabled: true #使用ingress暴露端口className: "nginx"annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: www.fjwyyy.orgpaths:- path: /pathType: ImplementationSpecific#语法检测
[root@master fjwyyy]# helm lint .
==> Linting .
[INFO] Chart.yaml: icon is recommended1 chart(s) linted, 0 chart(s) failed#运行项目
[root@master helm]# helm install myapp fjwyyy
NAME: myapp
LAST DEPLOYED: Fri Oct 24 23:31:15 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:http://www.fjwyyy.org/[root@master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-fjwyyy-9fd96499-tvhb9 1/1 Running 0 31s
myapp-fjwyyy-9fd96499-xgx4c 1/1 Running 0 31s#删除项目
[root@master helm]# helm uninstall myapp
release "myapp" uninstalled
项目打包并运行
#项目打包
[root@master helm]# helm package fjwyyy/
Successfully packaged chart and saved it to: /root/helm/fjwyyy-0.1.0.tgz
[root@master helm]# ls
fjwyyy fjwyyy-0.1.0.tgz#项目可以通过各种分享方式发方为任何人后部署即可
[root@master helm]# helm install testhelm fjwyyy-0.1.0.tgz
NAME: testhelm
LAST DEPLOYED: Fri Oct 24 23:36:47 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:http://www.fjwyyy.org/
[root@master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testhelm-fjwyyy-86dfcf84f4-6vkm2 1/1 Running 0 5s
testhelm-fjwyyy-86dfcf84f4-pw2xr 1/1 Running 0 5s
[root@master helm]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
testhelm-fjwyyy nginx www.fjwyyy.org 80 8s
四 helm的版本迭代
1.更新应用
[root@master helm]# helm upgrade fjw fjwyyy/fjw[root@master helm]# curl www.fy.org
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
2.显示项目版本
#可以看到更新跟回滚过的版本
[root@master helm]# helm history fjw

3.应用回滚
#查看项目版本后,回滚想要回去的版本号
[root@master helm]# helm rollback fjw 8[root@master helm]# curl www.fy.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
