目录
一、helm简介
二、部署helm
1、官网与资源
2、部署helm
(1)安装helm
(2)配置helm命令补齐
三、helm常用操作
(1)查询官方应用中心
(2)管理第三方repo源
(3)helm的使用方法
(4)安装项目前预定义项目选项
四、构建helm中的chart包
1、Helm Chart目录结构
2、构建方法
五、构建helm仓库
1、在harbor仓库中构建一个公开的项目
2、安装helm push插件
(1)在线安装
(2)离线安装
3、上传项目到仓库
(1)添加仓库
(2)上传本地项目
六、helm的版本迭代
1、重新构建新版本项目
2、上传项目到helm仓库中
3、更新应用
4、应用回滚
一、helm简介
-
Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts,类似Linux系统的yum。
-
Helm Chart是用来封装Kubernetes原生应用程序的一系列YAML文件。可以在你部署应用的时候自定义应用程序的一些 Metadata,以便于应用程序的分发。
-
对于应用发布者而言
-
通过Helm打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。
-
-
对于使用者而言
-
使用Helm后可以以简单的方式在Kubernetes上查找、安装、升级、回滚、卸载应用程序
-
二、部署helm
1、官网与资源
官网: Helm | 快速入门指南
软件资源: Releases · helm/helm · GitHub
2、部署helm
(1)安装helm
[root@k8s-master ~]# mkdir helm
[root@k8s-master ~]# cd helm/
[root@k8s-master helm]# tar zxf helm-v3.15.4-linux-amd64.tar.gz
[root@k8s-master helm]# ls
helm-v3.15.4-linux-amd64.tar.gz linux-amd64
[root@k8s-master helm]# cd linux-amd64/
[root@k8s-master linux-amd64]# ls
helm LICENSE README.md
[root@k8s-master linux-amd64]# cp -p helm /usr/local/bin/
cp: overwrite '/usr/local/bin/helm'? y
(2)配置helm命令补齐
[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.15.4", GitCommit:"fa9efb07d9d8debbb4306d72af76a383895aa8c4", GitTreeState:"clean", GoVersion:"go1.22.6"}
三、helm常用操作
命令 | 描述 |
---|---|
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 客户端版本 |
(1)查询官方应用中心
[root@k8s-master helm]# helm search hub nginx #在官方仓库中搜索
[root@k8s-master helm]# helm search repo nginx #在本地仓库中搜索
(2)管理第三方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 helm]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories
#添加bitnami仓库
[root@k8s-master helm]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
#查看仓库信息
[root@k8s-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
#删除第三方存储库
[root@k8s-master helm]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami
[root@k8s-master helm]# helm repo remove aliyun
"aliyun" has been removed from your repositories
[root@k8s-master helm]# helm repo list
NAME URL
bitnami https://charts.bitnami.com/bitnami
(3)helm的使用方法
# 1、查找chart
[root@k8s-master helm]# helm search repo nginx
# 2、查看chart信息
[root@k8s-master helm]# helm show chart bitnami/nginx
# 3.安装chart包
[root@k8s-master helm]# helm install zx bitnami/nginx
[root@k8s-master helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
zx default 1 2024-09-20 11:17:39.302881065 +0800 CST deployed nginx-18.1.15 1.27.1
[root@k8s-master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
zx-nginx-77f58d964f-9s4cl 0/1 Init:ImagePullBackOff 0 81s
#查看项目的发布状态
[root@k8s-master helm]# helm status zx
NAME: zx
LAST DEPLOYED: Fri Sep 20 11:17:39 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 18.1.15
APP VERSION: 1.27.1
#卸载项目
[root@k8s-master helm]# helm uninstall zx
release "zx" uninstalled
[root@k8s-master helm]# kubectl get pods
No resources found in default namespace.
[root@k8s-master helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
(4)安装项目前预定义项目选项
#拉取项目
[root@k8s-master helm]# helm pull bitnami/nginx
[root@k8s-master helm]# ls
helm-v3.15.4-linux-amd64.tar.gz linux-amd64
[root@k8s-master helm]#
[root@k8s-master helm]# helm pull bitnami/nginx
[root@k8s-master helm]# ls
helm-v3.15.4-linux-amd64.tar.gz linux-amd64 nginx-18.1.15.tgz
[root@k8s-master helm]# tar zxf nginx-18.1.15.tgz
[root@k8s-master helm]# ls
helm-v3.15.4-linux-amd64.tar.gz linux-amd64 nginx nginx-18.1.15.tgz
[root@k8s-master helm]# cd nginx/
[root@k8s-master nginx]# ls
Chart.lock charts Chart.yaml README.md templates values.schema.json values.yaml
[root@k8s-master nginx]# ls templates/ #项目模板
deployment.yaml hpa.yaml NOTES.txt serviceaccount.yaml
extra-list.yaml ingress-tls-secret.yaml pdb.yaml servicemonitor.yaml
health-ingress.yaml ingress.yaml prometheusrules.yaml svc.yaml
_helpers.tpl networkpolicy.yaml server-block-configmap.yaml tls-secret.yaml
[root@k8s-master nginx]# vim values.yaml #项目变量文件
#上传项目所需要镜像到仓库
[root@k8s-master helm]# docker load -i nginx-1.27.1-debian-12-r2.tar
[root@k8s-master helm]# docker tag bitnami/nginx:1.27.1-debian-12-r2 reg.zx.org/bitnami/nginx:1.27.1-debian-12-r2
[root@k8s-master helm]# docker push reg.zx.org/bitnami/nginx:1.27.1-debian-12-r2
#安装本地项目
[root@k8s-master helm]# helm upgrade zx /root/helm/nginx --set image.tag=1.27.1-debian-12-r2
[root@k8s-master helm]# helm install zx /root/helm/nginx
[root@k8s-master nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
zx-nginx-6c7ffc66f7-26lpp 1/1 Running 0 40s
[root@k8s-master nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d1h
myapp1 ClusterIP 10.99.26.240 <none> 80/TCP 3d3h
zx-nginx LoadBalancer 10.97.55.101 172.25.254.50 80:32536/TCP,443:32763/TCP 105s
#更新项目
[root@k8s-master nginx]# vim values.yaml
623 type: ClusterIP
761 hostname: myapp.zx.org
781 ingressClassName: "nginx"
[root@k8s-master nginx]# helm upgrade zx .
[root@k8s-master nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d1h
myapp1 ClusterIP 10.99.26.240 <none> 80/TCP 3d3h
zx-nginx ClusterIP 10.102.14.124 <none> 80/TCP,443/TCP 13m
[root@k8s-master nginx]# curl 10.102.14.124
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#查看历史
[root@k8s-master nginx]# helm history zx
#删除项目
[root@k8s-master nginx]# helm uninstall zx
四、构建helm中的chart包
1、Helm Chart目录结构
# 建立chart项目
[root@k8s-master helm]# helm create zx
Creating zx
[root@k8s-master helm]# tree zx/
zx/
├── 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 目录中模板文件中用到变量的值
3 directories, 10 files
2、构建方法
[root@k8s-master helm]# cd zx/
[root@k8s-master zx]# vim Chart.yaml
apiVersion: v2
name: zx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0 #项目版本
appVersion: "v1" #软件版本
[root@k8s-master zx]# vim values.yaml
image:
repository: reg.zx.org/library/myapp
pullPolicy: IfNotPresent
tag: "v1"
ingress:
enabled: true
className: "nginx"
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: www.zx.org
paths:
- path: /
pathType: ImplementationSpecific
#语法检测
[root@k8s-master zx]# helm lint .
==> Linting .
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
#项目打包
[root@k8s-master zx]# cd ..
[root@k8s-master helm]# helm package zx/
Successfully packaged chart and saved it to: /root/helm/zx-0.1.0.tgz
[root@k8s-master helm]# ls
#项目可以通过各种分享方式发方为任何人后部署即可
[root@k8s-master helm]# helm install zx zx-0.1.0.tgz
NAME: zx
LAST DEPLOYED: Fri Sep 20 12:43:20 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
http://www.zx.org/
[root@k8s-master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
zx-69745f4966-fq79r 1/1 Running 0 7s
[root@k8s-master helm]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
myapp nginx myapp-tls.zx.org 172.25.254.10 80, 443 3d23h
zx nginx www.zx.org 172.25.254.10 80 72s
[root@k8s-master helm]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d2h
myapp1 ClusterIP 10.99.26.240 <none> 80/TCP 3d4h
zx ClusterIP 10.100.153.131 <none> 80/TCP 4m11s
[root@k8s-master helm]# curl 10.100.153.131
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
五、构建helm仓库
1、在harbor仓库中构建一个公开的项目
2、安装helm push插件
官方网址:GitHub - chartmuseum/helm-push: Helm plugin to push chart package to ChartMuseum
(1)在线安装
[root@k8s-master helm]# dnf install git -y
[root@k8s-master helm]# helm plugin install https://github.com/chartmuseum/helm-push
(2)离线安装
#创建helm plugin的存放目录
[root@k8s-master helm]# mkdir ~/.local/share/helm/plugins/helm-push -p
#解压push插件包到指定目录
[root@k8s-master helm]# tar zxf helm-push_0.10.4_linux_amd64.tar.gz -C ~/.local/share/helm/plugins/helm-push
[root@k8s-master helm]# ls ~/.local/share/helm/plugins/helm-push
acceptance_tests BUILDING.md go.mod LICENSE pkg README.md scripts
bin cmd go.sum Makefile plugin.yaml releases testdata
#查看helm调用命令是否成功
[root@k8s-master helm]# helm cm-push --help
Helm plugin to push chart package to ChartMuseum
3、上传项目到仓库
(1)添加仓库
[root@k8s-master helm]# helm repo add zx-new https://reg.zx.org/chartrepo/zx-new
"zx-new" has been added to your repositories
#添加仓库时报错,因为用的是加密访问,需添加证书
#为helm添加证书
[root@k8s-master helm]# cp /etc/docker/certs.d/reg.zx.org/ca.crt /etc/pki/ca-trust/source/anchors/
#更新本地ca认证库
[root@k8s-master helm]# update-ca-trust
(2)上传本地项目
#命令执行格式
helm cm-push <项目名称> <仓库名称> -u admin -p 123
[root@k8s-master helm]# helm cm-push zx-0.1.0.tgz zx-new -u admin -p123
Pushing zx-0.1.0.tgz to zx-new...
Done.
#查看项目上传情况
#上传后数据未更新
[root@k8s-master helm]# helm search repo zx-new
No results found
#更新仓库
[root@k8s-master helm]# helm repo update zx-new
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "zx-new" chart repository
Update Complete. ⎈Happy Helming!⎈
#再次查看
[root@k8s-master helm]# helm search repo zx-new
NAME CHART VERSION APP VERSION DESCRIPTION
zx-new/zx 0.1.0 v1 A Helm chart for Kubernetes
#安装项目
[root@k8s-master helm]# helm install zx zx-new/zx
NAME: zx
LAST DEPLOYED: Fri Sep 20 13:11:11 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
http://www.zx.org/
[root@k8s-master helm]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
zx-69745f4966-zw7tm 1/1 Running 0 5s 10.244.101.132 k8s-node1.zx.org <none> <none>
[root@k8s-master helm]# curl 10.244.101.132
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
六、helm的版本迭代
1、重新构建新版本项目
[root@k8s-master helm]# vim zx/Chart.yaml
version: 0.2.0
appVersion: "v2"
[root@k8s-master helm]# vim zx/values.yaml
tag: "v2"
[root@k8s-master helm]# helm package zx/
Successfully packaged chart and saved it to: /root/helm/zx-0.2.0.tgz
2、上传项目到helm仓库中
[root@k8s-master helm]# helm cm-push zx-0.2.0.tgz zx-new -u admin -p 123
Pushing zx-0.2.0.tgz to zx-new...
Done.
[root@k8s-master helm]# helm repo update zx-new
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "zx-new" chart repository
Update Complete. ⎈Happy Helming!⎈
[root@k8s-master helm]# helm search repo zx-new -l
NAME CHART VERSION APP VERSION DESCRIPTION
zx-new/zx 0.2.0 v2 A Helm chart for Kubernetes
zx-new/zx 0.1.0 v1 A Helm chart for Kubernetes
3、更新应用
[root@k8s-master helm]# helm upgrade zx zx-new/zx
Release "zx" has been upgraded. Happy Helming!
NAME: zx
LAST DEPLOYED: Fri Sep 20 13:19:42 2024
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
http://www.zx.org/
[root@k8s-master helm]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
zx-568f564d69-m7vgn 1/1 Running 0 35s 10.244.101.133 k8s-node1.zx.org <none> <none>
[root@k8s-master helm]# curl 10.244.101.133
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@k8s-master helm]# helm history zx
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Fri Sep 20 13:13:28 2024 superseded zx-0.1.0 v1 Install complete
2 Fri Sep 20 13:19:42 2024 deployed zx-0.2.0 v2 Upgrade complete
4、应用回滚
[root@k8s-master helm]# helm rollback zx
Rollback was a success! Happy Helming!
[root@k8s-master helm]# helm history zx
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Fri Sep 20 13:13:28 2024 superseded zx-0.1.0 v1 Install complete
2 Fri Sep 20 13:19:42 2024 superseded zx-0.2.0 v2 Upgrade complete
3 Fri Sep 20 13:21:40 2024 deployed zx-0.1.0 v1 Rollback to 1
[root@k8s-master helm]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
zx-69745f4966-44n5m 1/1 Running 0 14s 10.244.101.134 k8s-node1.zx.org <none> <none>
[root@k8s-master helm]# curl 10.244.101.134
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>