K8s - Helm的使用
安装Helm
- https://helm.sh/zh/docs/
- https://github.com/helm/helm/releases
- https://get.helm.sh/helm-v3.16.2-linux-amd64.tar.gz
- 在 master 节点安装Helm
[root@k8s-master ~]# tar -xvzf helm-v3.16.2-linux-amd64.tar.gz
linux-amd64/
linux-amd64/LICENSE
linux-amd64/helm
linux-amd64/README.md
[root@k8s-master ~]# mv linux-amd64/helm /usr/local/bin
[root@k8s-master ~]#
[root@k8s-master ~]# helm version
version.BuildInfo{Version:"v3.16.2", GitCommit:"13654a52f7c70a143b1dd51416d633e1071faffb", GitTreeState:"clean", GoVersion:"go1.22.7"}
[root@k8s-master ~]#
Helm国内镜像源
# 移除原先的stable源并增加国内镜像源
[root@k8s-master ~]# helm repo remove stable
"stable" has been removed from your repositories
[root@k8s-master ~]#
[root@k8s-master ~]# helm repo add azure http://mirror.azure.cn/kubernetes/charts
"azure" has been added to your repositories
[root@k8s-master ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories
[root@k8s-master ~]# helm repo add offical https://charts.helm.sh/incubator
"offical" has been added to your repositories
[root@k8s-master ~]#
# 列出已配置的Helm库
[root@k8s-master ~]# helm repo list
NAME URL
azure http://mirror.azure.cn/kubernetes/charts
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
offical https://charts.helm.sh/incubator
[root@k8s-master ~]#
# 搜索测试
[root@k8s-master ~]# 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
azure/nginx-ingress 1.41.3 v0.34.1 DEPRECATED! An nginx Ingress controller that us...
azure/nginx-ldapauth-proxy 0.1.6 1.13.5 DEPRECATED - nginx proxy with ldapauth
azure/nginx-lego 0.3.1 Chart for nginx-ingress-controller and kube-lego
aliyun/gcloud-endpoints 0.1.0 Develop, deploy, protect and monitor your APIs ...
azure/gcloud-endpoints 0.1.2 1 DEPRECATED Develop, deploy, protect and monitor...
[root@k8s-master ~]#
[root@k8s-master ~]# helm search hub nginx |head
URL CHART VERSION APP VERSION DESCRIPTION
https://artifacthub.io/packages/helm/krakazyabr... 1.0.0 1.19.0 Nginx Helm chart for Kubernetes
https://artifacthub.io/packages/helm/bitnami/nginx 18.2.3 1.27.2 NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/dysnix/nginx 7.1.8 1.19.4 Chart for the nginx server
https://artifacthub.io/packages/helm/bitnami-ak... 13.2.12 1.23.2 NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/ashu-nginx... 0.1.0 1.16.0 A Helm chart for Kubernetes
https://artifacthub.io/packages/helm/test-nginx... 0.1.0 1.16.0 A Helm chart for Kubernetes
https://artifacthub.io/packages/helm/shubhamtat... 0.1.12 1.19.6 Nginx Helm chart for Kubernetes
https://artifacthub.io/packages/helm/dhinesh/nginx 18.2.2 1.27.2 NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/wiremind/n... 2.1.1 An NGINX HTTP server
[root@k8s-master ~]#
# 更新本地chart库信息
[root@k8s-master ~]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "offical" chart repository
...Successfully got an update from the "azure" chart repository
Update Complete. ⎈Happy Helming!⎈
[root@k8s-master ~]#
一些Helm命令
helm search repo <chart-name> -o yaml # 从repo查找Chart
helm search hub <chart-name> -o yaml # 从hub查找Chart
helm show all <chart-name> # 查看Chart详情
helm lint <chart-dir> # 检查依赖和模版配置
helm package <chart-dir> # 打包应用
helm install <release-name> ./<chart-dir> # 离线部署
helm install <release-name> ./<chart-dir> -f ./<file-dir>/<file>.yaml # 根据配置文件离线部署
制作Chart模版
创建Chart模版,作为制作新Chart的参考和基础。
[root@k8s-master ~]# helm create chart-demo
Creating chart-demo
[root@k8s-master ~]# tree chart-demo/
chart-demo/
├── 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
[root@k8s-master ~]#
Chart模版目录结构说明
├── charts # 可选目录,存放与当前Chart相关的子Chart
├── Chart.yaml # 用于描述 Chart 信息,如名称、版本、描述等
├── templates # 主要用于存放Kubernetes资源的模板文件
│ ├── deployment.yaml # Kubernetes资源文件
│ ├── _helpers.tpl # 存放 Helm 模板函数,可以在模板文件中重复使用,以简化模板文件的编写,并提高可维护性
│ ├── hpa.yaml
│ ├── ingress.yaml # Kubernetes资源文件
│ ├── NOTES.txt # 在安装或升级 Chart 后显示的提示和说明
│ ├── serviceaccount.yaml # Kubernetes资源文件
│ ├── service.yaml # Kubernetes资源文件
│ └── tests
│ └── test-connection.yaml
└── values.yaml # 用于定义Chart的默认值,通常在模板文件中引用,可以在安装 Chart时修改这些值,以灵活地改变应用程序的配置
一些配置字段说明
apiVersion: Chart API 版本
name: 名称
description: 简要描述
type: 类型
version: 版本号
appVersion: 应用程序的版本号
replicas: 定义默认的副本数
image: 镜像仓库地址和标签
service: Service类型、端口和目标端口
双花括号"{{}}" 用于定义和插入变量、表达式和函数
“.Release.Name” 内置变量,用于安装Chart时指定的Release名称
以".Values"开头的变量 用于引用在 values.yaml 文件中定义的变量
双花括号"{{}}"中的横杠“-” 用于去除表达式执行后遗留的空白行,使得渲染的结果更加紧凑和美观
语法说明
indent函数 缩进空格,可以指定空格数目
nindent函数 先添加一个换行再缩进空格,可以指定空格数目
toYaml函数 用于将values.yaml文件中的片段插入模版文件中的指定位置
range函数 用于遍历数组、对象等数据结构
include函数 用来引用_helpers.tpl文件中的模版代码
{{if}}...{{end}}
条件判断,用于根据不同的条件生成不同的内容,结合配置中的布尔型字段使用
{{with}}...{{end}}
用于创建一个局部作用域,在这个作用域中执行特定的模块代码块
在with块内不支持引用外部变量,但可以在with块之前定义一个变量,但并在内部引用该变量
如果with的变量未定义或者为空,with函数将不执行里面的模块代码块,
可以将一些不常用的配置设置为空,用户可以运行helm命令时,通过--set参数来自定义
内置对象".Files”,用于将文件内容插入模板中的指定位置处
“.Files.Get”方法读取文件内容并将其插入当前位置
“.Files.Glob”方法获取匹配指定通配符模式的文件列表,并通过range函数遍历这些文件以读取其内容
创建Chart
[root@k8s-master ~]# kubectl create namespace test-helm
namespace/test-helm created
[root@k8s-master ~]#
创建目录及文件
[root@k8s-master ~]# cd helm-chartdemo/
[root@k8s-master helm-chartdemo]# tree
.
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── NOTES.txt
│ └── service.yaml
└── values.yaml
1 directory, 5 files
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# cat Chart.yaml
apiVersion: v2
name: helm-chartdemo
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# cat values.yaml
replicas: 1
image:
repository: nginx
tag: latest
service:
type: ClusterIP
port: 80
targetPort: 80
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
release-name: {{ .Release.Name }}
template:
metadata:
labels:
release-name: {{ .Release.Name }}
spec:
containers:
- image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
name: web
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# cat templates/NOTES.txt
欢迎使用Chart!这是一个Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# cat templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}
spec:
type: {{ .Values.service.type }}
selector:
release-name: {{ .Release.Name }}
ports:
- protocol: TCP
port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
[root@k8s-master helm-chartdemo]#
安装Chart及查看Release
# 从本地安装
[root@k8s-master helm-chartdemo]# helm install chartdemo . -n test-helm
NAME: chartdemo
LAST DEPLOYED: Wed Oct 16 10:21:05 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
欢迎使用Chart!这是一个Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]#
# 查看Release等信息
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
chartdemo test-helm 1 2024-10-16 10:21:05.380344629 +0800 CST deployed helm-chartdemo-0.1.0 1.16.0
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 18m 10.244.235.205 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 1/1 1 1 18m web nginx:latest release-name=chartdemo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo ClusterIP 10.96.229.216 <none> 80/TCP 18m release-name=chartdemo
[root@k8s-master helm-chartdemo]#
动态更新values.yaml值
[root@k8s-master helm-chartdemo]# helm install chartdemo2 . --set service.type=NodePort -n test-helm
NAME: chartdemo2
LAST DEPLOYED: Wed Oct 16 10:50:40 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
欢迎使用Chart!这是一个Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
chartdemo test-helm 1 2024-10-16 10:21:05.380344629 +0800 CST deployed helm-chartdemo-0.1.0 1.16.0
chartdemo2 test-helm 1 2024-10-16 10:50:40.287432354 +0800 CST deployed helm-chartdemo-0.1.0 1.16.0
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 30m 10.244.235.205 k8s-master <none> <none>
pod/chartdemo2-7b65dfd898-z7775 1/1 Running 0 69s 10.244.235.206 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 1/1 1 1 30m web nginx:latest release-name=chartdemo
deployment.apps/chartdemo2 1/1 1 1 69s web nginx:latest release-name=chartdemo2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo ClusterIP 10.96.229.216 <none> 80/TCP 30m release-name=chartdemo
service/chartdemo2 NodePort 10.104.7.166 <none> 80:30456/TCP 69s release-name=chartdemo2
[root@k8s-master helm-chartdemo]#
更新、回滚和卸载 Release
# 当前状态
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
chartdemo test-helm 1 2024-10-16 10:21:05.380344629 +0800 CST deployed helm-chartdemo-0.1.0 1.16.0
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Wed Oct 16 10:21:05 2024 deployed helm-chartdemo-0.1.0 1.16.0 Install complete
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 44m 10.244.235.205 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 1/1 1 1 44m web nginx:latest release-name=chartdemo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo ClusterIP 10.96.229.216 <none> 80/TCP 44m release-name=chartdemo
[root@k8s-master helm-chartdemo]#
# 更新 Release
[root@k8s-master helm-chartdemo]# helm upgrade chartdemo . --set replicas=3,service.type=NodePort -n test-helm
Release "chartdemo" has been upgraded. Happy Helming!
NAME: chartdemo
LAST DEPLOYED: Wed Oct 16 11:07:49 2024
NAMESPACE: test-helm
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
欢迎使用Chart!这是一个Helm Chart 的 Demo !
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Wed Oct 16 10:21:05 2024 superseded helm-chartdemo-0.1.0 1.16.0 Install complete
2 Wed Oct 16 11:07:49 2024 deployed helm-chartdemo-0.1.0 1.16.0 Upgrade complete
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-2nvq2 1/1 Running 0 11s 10.244.235.209 k8s-master <none> <none>
pod/chartdemo-86697c564b-ghp7d 1/1 Running 0 11s 10.244.235.208 k8s-master <none> <none>
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 46m 10.244.235.205 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 3/3 3 3 46m web nginx:latest release-name=chartdemo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo NodePort 10.96.229.216 <none> 80:31710/TCP 46m release-name=chartdemo
[root@k8s-master helm-chartdemo]#
# 回滚 Release
[root@k8s-master helm-chartdemo]# helm rollback chartdemo -n test-helm
Rollback was a success! Happy Helming!
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 50m 10.244.235.205 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 1/1 1 1 50m web nginx:latest release-name=chartdemo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo ClusterIP 10.96.229.216 <none> 80/TCP 50m release-name=chartdemo
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Wed Oct 16 10:21:05 2024 superseded helm-chartdemo-0.1.0 1.16.0 Install complete
2 Wed Oct 16 11:07:49 2024 superseded helm-chartdemo-0.1.0 1.16.0 Upgrade complete
3 Wed Oct 16 11:11:05 2024 deployed helm-chartdemo-0.1.0 1.16.0 Rollback to 1
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm rollback chartdemo 2 -n test-helm
Rollback was a success! Happy Helming!
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm history chartdemo -n test-helm
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Wed Oct 16 10:21:05 2024 superseded helm-chartdemo-0.1.0 1.16.0 Install complete
2 Wed Oct 16 11:07:49 2024 superseded helm-chartdemo-0.1.0 1.16.0 Upgrade complete
3 Wed Oct 16 11:11:05 2024 superseded helm-chartdemo-0.1.0 1.16.0 Rollback to 1
4 Wed Oct 16 11:13:05 2024 deployed helm-chartdemo-0.1.0 1.16.0 Rollback to 2
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# kubectl get po,deploy,svc -n test-helm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/chartdemo-86697c564b-dtczs 1/1 Running 0 9s 10.244.235.210 k8s-master <none> <none>
pod/chartdemo-86697c564b-mfjzr 1/1 Running 0 52m 10.244.235.205 k8s-master <none> <none>
pod/chartdemo-86697c564b-vl8lk 1/1 Running 0 9s 10.244.235.211 k8s-master <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/chartdemo 3/3 3 3 52m web nginx:latest release-name=chartdemo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/chartdemo NodePort 10.96.229.216 <none> 80:31650/TCP 52m release-name=chartdemo
[root@k8s-master helm-chartdemo]#
# 删除 Release
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
chartdemo test-helm 4 2024-10-16 11:13:05.336732826 +0800 CST deployed helm-chartdemo-0.1.0 1.16.0
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm uninstall chartdemo -n test-helm
release "chartdemo" uninstalled
[root@k8s-master helm-chartdemo]#
[root@k8s-master helm-chartdemo]# helm list -n test-helm
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
[root@k8s-master helm-chartdemo]#
自建Chart仓库
在实际使用中通常使用Nexus3、Harbor,JFrog等制品库工具来自建Chart仓库。
如果想单独部署一个纯粹的Chart仓库,可以使用ChartMuseum(Helm开源的Chart仓库服务)。
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。