目录
一、helm简介
-
Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts,类似Linux系统的yum。
-
Helm Chart是用来封装Kubernetes原生应用程序的一系列YAML文件。可以在你部署应用的时候自定义应用程序的一些 Metadata,以便于应用程序的分发。
-
对于应用发布者而言
-
通过Helm打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。
-
对于使用者而言
-
使用Helm后可以以简单的方式在Kubernetes上查找、安装、升级、回滚、卸载应用程序
二、部署helm
2.1 官网与资源
官网: Helm | 快速入门指南
软件资源: Releases · helm/helm · GitHub
2.2 安装helm
[root@k8s-master helm]# tar zxf helm-v3.15.4-linux-amd64.tar.gz
[root@k8s-master helm]# ls
helm-push_0.10.4_linux_amd64.tar.gz linux-amd64
helm-v3.15.4-linux-amd64.tar.gz
[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/
[root@k8s-master linux-amd64]#
2.3 配置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 客户端版本 |
3.1 查询官方应用中心
[root@k8s-master helm]# helm search hub nginx #在官方仓库中搜索
[root@k8s-master helm]# helm search repo nginx #在本地仓库中搜索
3.2 管理第三方repo源
-
阿里云仓库:https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
-
bitnami仓库: https://charts.bitnami.com/bitnami
添加阿里云仓库:
[root@k8s-master helm]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories
#查看仓库信息:
[root@k8s-master helm]# helm repo list
NAME URL
microsoft http://mirror.azure.cn/kubernetes/charts/
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@k8s-master helm]#
#查看仓库存储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
3.3 helm的使用方法
1 查找chart
[root@k8s-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
2 查看chart信息
[root@k8s-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.com
name: jackzampolin
- email: mgoodness@gmail.com
name: mgoodness
- email: chance.zibolski@coreos.com
name: chancez
name: nginx-ingress
sources:
- https://github.com/kubernetes/ingress-nginx
version: 0.9.5
[root@k8s-master helm]#
3 安装chart 包
[root@k8s-master helm]# helm install timingding nginx-18.1.11.tgz
[root@k8s-master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
timingding-nginx-7bbb95c4f-dpb4m 1/1 Running 0 86s
[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 2d8h
timingding-nginx LoadBalancer 10.109.97.99 172.25.254.50 80:30909/TCP,443:31980/TCP 2m2s
[root@k8s-master helm]# curl 172.25.254.50
<!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 helm]#
四、构建helm中的chart包
4.1 Helm Chart目录结构
[root@k8s-master helm]# helm create timingding
Creating timingding
[root@k8s-master helm]# tree timingding/
timingding/
├── 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
[root@k8s-master helm]#
4.2 构建方法
[root@k8s-master timingding]# ls
charts Chart.yaml templates values.yaml
[root@k8s-master timingding]# vim Chart.yaml
[root@k8s-master timingding]#
查看ingress的类型:
[root@k8s-master timingding]# kubectl -n ingress-nginx get ingressclasses.networking.k8s.io
NAME CONTROLLER PARAMETERS AGE
nginx k8s.io/ingress-nginx <none> 2d
[root@k8s-master timingding]#
[root@k8s-master timingding]# vim values.yaml
ingress: #要确保你有ingress
enabled: true
className: "nginx"
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: myapp.timingding.org
检测一下语法:
[root@k8s-master timingding]# helm lint /root/helm/timingding/
==> Linting /root/helm/timingding/
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
[root@k8s-master timingding]#
项目打包:
[root@k8s-master helm]# helm package timingding/
Successfully packaged chart and saved it to: /root/helm/timingding-0.1.0.tgz
[root@k8s-master helm]# ls
helm-push_0.10.4_linux_amd64.tar.gz linux-amd64 nginx nginx-1.27.1-debian-12-r2.tar nginx-18.1.11.tgz timingding timingding-0.1.0.tgz
[root@k8s-master helm]#
项目可以通过各种分享方式发方为任何人后部署即可
[root@k8s-master helm]# helm install timingding timingding-0.1.0.tgz
NAME: timingding
LAST DEPLOYED: Wed Sep 11 07:46:47 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
http://myapp.timingding.org/
[root@k8s-master helm]#
[root@k8s-master helm]# kubectl get pods
NAME READY STATUS RESTARTS AGE
timingding-66c9bb988c-z7kbt 1/1 Running 0 44s
[root@k8s-master helm]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
timingding nginx myapp.timingding.org 80 53s
[root@k8s-master helm]# curl myapp.timingding.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master helm]#
[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 2d9h
timingding ClusterIP 10.105.146.21 <none> 80/TCP 7m57s
[root@k8s-master helm]# curl 10.105.146.21
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master helm]#
版本更新:
把timingding目录中的版本号更新为v2,再重新打包
helm package timingding/
再重新下载:
helm install <打包出来的包名>
五、构建helm仓库
5.1 安装helm push插件
官方网址:GitHub - chartmuseum/helm-push: Helm plugin to push chart package to ChartMuseum
在harbor仓库新建一个公开的项目:
5.2 在线安装
如果网络没问题情况下直接安装即可
[root@k8s-master helm]# dnf install git -y
[root@k8s-master helm]# helm plugin install https://github.com/chartmuseum/helm-push
5.3 离线安装
[root@k8s-master helm]# dnf install git -y
[root@k8s-master helm]# mkdir ~/.local/share/helm/plugins/helm-push/ -p
解压到这个路径下,-C是指定文件路径
[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/
bin LICENSE plugin.yaml
查看是否OK:
[root@k8s-master helm]# helm cm-push help ## 能看到帮助就代表ok了
5.4 上传项目到仓库中
5.4.1 添加仓库
[root@k8s-master helm]# helm repo add timinglee https://reg.timinglee.org/chartrepo/timinglee
#添加仓库时报错,因为我们用的是加密访问
Error: looks like "https://reg.timinglee.org/chartrepo/timinglee" is not a valid chart repository or cannot be reached: Get "https://reg.timinglee.org/chartrepo/timinglee/index.yaml": tls: failed to verify certificate: x509: certificate signed by unknown authority
#为helm添加证书
[root@k8s-master helm]# cp /etc/docker/certs.d/reg.timingding.org/ca.crt /etc/pki/ca-trust/source/anchors/
#更新本地ca认证库
[root@k8s-master helm]# update-ca-trust
[root@k8s-master helm]# helm repo add timingding https://reg.timingding.org/chartrepo/timingding
"timingding" has been added to your repositories
[root@k8s-master helm]#
查看是否添加成功:
[root@k8s-master helm]# helm repo list
NAME URL
microsoft http://mirror.azure.cn/kubernetes/charts/
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
timingding https://reg.timingding.org/chartrepo/timingding
[root@k8s-master helm]#
5.4.2 上传本地项目
[root@k8s-master helm]# helm cm-push timingding-0.1.0.tgz timingding -u admin -p 123456
Pushing timingding-0.1.0.tgz to timingding...
Done.
[root@k8s-master helm]#
六、helm的版本迭代
6.1 重新构建新版本项目
[root@k8s-master helm]# vim timingding/Chart.yaml
version: 0.2.0
appVersion: "v2"
[root@k8s-master helm]# vim timingding/values.yaml
tag: "v2"
[root@k8s-master helm]# helm package timingding
Successfully packaged chart and saved it to: /root/helm/timingding-0.2.0.tgz
[root@k8s-master helm]#
6.2 上传项目到helm仓库中
[root@k8s-master helm]# helm cm-push timingding-0.2.0.tgz timingding -u admin -p 123456
Pushing timingding-0.2.0.tgz to timingding...
Done.
[root@k8s-master helm]#
查看镜像:
[root@k8s-master helm]# helm search repo timingding -l
6.2 更新应用
[root@k8s-master helm]# helm upgrade timingding timingding/timingding
Release "timingding" has been upgraded. Happy Helming!
NAME: timingding
LAST DEPLOYED: Wed Sep 11 09:00:43 2024
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
http://myapp.timingding.org/
[root@k8s-master helm]# curl myapp.timingding.org
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@k8s-master helm]#
应用回滚:
[root@k8s-master helm]# helm history timingding
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Wed Sep 11 08:28:48 2024 superseded timingding-0.1.0 v1 Install complete
2 Wed Sep 11 09:00:43 2024 superseded timingding-0.2.0 v2 Upgrade complete
3 Wed Sep 11 09:01:46 2024 deployed timingding-0.1.0 v1 Rollback to 1
[root@k8s-master helm]#
[root@k8s-master helm]# curl myapp.timingding.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master helm]#