为什么需要Helm
由于Kubernetes缺少对发布的应用版本管理和控制,使得部署的应用维护和更新等面临诸多的挑战,主要面临以下问题:
001 如何将这些服务作为一个整体管理?
002 这些资源文件如何高效复用?
003 不支持应用级别的版本管理
Helm介绍
Helm是一个Kubernetes的包管理工具,就像Linux下的包管理器,如yum/apt等,可以很方便的将之前
打包好的yaml文件部署到kubernetes上。
helm
一个命令行客户端工具,主要用于Kubernetes应用chart的创建、打包、发布和管理
Chart
应用描述,一系列用于描述 k8s 资源相关文件的集合
Release
基于Chart的部署实体,一个 chart 被 Helm 运行后将会生成对应的一个 release;将在k8s中创建出真实运行的资源对象
Helm工作流程
1637808148300.png
Helm客户端
[root@k8smaster helm]# tar -xzvf helm-v3.2.4-linux-amd64.tar.gz
[root@k8smaster helm]# mv linux-amd64/helm /usr/bin/
[root@k8smaster helm]# helm -- help
制作Chart
[root@k8smaster helm]# helm create mychart
Creating mychart
-----------进入mychart的相关目录--------------------------
[root@k8smaster templates]# rm -rf tests/
[root@k8smaster templates]# rm *yaml -rf
[root@k8smaster templates]# vi NOTES.txt
Helm Test!!!
Helm基本结构
001 Chart.yaml:用于描述这个 Chart的基本信息,包括名字、描述信息以及版本等。
002 values.yaml:用于存储 templates 目录中模板文件中用到变量的值。
003 Templates: 目录里面存放所有yaml模板文件。
004 charts:目录里存放这个chart依赖的所有子chart。
005 NOTES.txt :用于介绍Chart帮助信息, helm install 部署后展示给用户。例如:如何使用这个 Chart、列出缺省的设置等。
006 _helpers.tpl:放置模板的地方,可以在整个 chart 中重复使用
配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
namespace: default
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: web
image: {{ .Values.image }}:{{ .Values.imageTag }}
---------------------------------------------------------------------------------
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-service
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
------------------------------vaules.yaml--------------------------------------
replicas: 3
image: "nginx"
imageTag: "1.17"
部署chart
[root@k8smaster helm]# ls
helm-v3.2.4-linux-amd64.tar.gz linux-amd64 mychart
[root@k8smaster helm]# helm install web mychart
NAME: web
LAST DEPLOYED: Thu Nov 25 10:13:03 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Helm Test!!!
--------------------------------------------------------------
查看Release
[root@k8smaster helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web default 1 2021-11-25 10:13:03.66216703 +0800 CST deployed mychart-0.1.0 1.16.0
[root@k8smaster helm]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nfs-client-provisioner-5f98b5cdfb-rm7dd 1/1 Running 3 27h
pod/web-deployment-76f5f6d7f5-bfptg 1/1 Running 0 3m55s
pod/web-deployment-76f5f6d7f5-lxxpf 1/1 Running 0 3m55s
pod/web-deployment-76f5f6d7f5-wljbc 1/1 Running 0 3m55s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/web-service NodePort 10.96.217.231 <none> 80:32589/TCP 3m55s
#查看相关版本
[root@k8smaster helm]# curl -I 10.96.217.231
HTTP/1.1 200 OK
Server: nginx/1.17.10
------------------------------------------------------------------------------------
http://192.168.153.22:32589/
同一chart多个部署
[root@k8smaster helm]# helm install web2 mychart
NAME: web2
LAST DEPLOYED: Thu Nov 25 10:20:46 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Helm Test!!!
--------------------------------------------
[root@k8smaster helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web default 1 2021-11-25 10:13:03.66216703 +0800 CST deployed mychart-0.1.0 1.16.0
web2 default 1 2021-11-25 10:20:46.514193903 +0800 CST deployed mychart-0.1.0 1.16.0
[root@k8smaster helm]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nfs-client-provisioner-5f98b5cdfb-rm7dd 1/1 Running 3 27h
pod/web-deployment-76f5f6d7f5-bfptg 1/1 Running 0 8m55s
pod/web-deployment-76f5f6d7f5-lxxpf 1/1 Running 0 8m55s
pod/web-deployment-76f5f6d7f5-wljbc 1/1 Running 0 8m55s
pod/web2-deployment-76f5f6d7f5-2mg5p 1/1 Running 0 72s
pod/web2-deployment-76f5f6d7f5-lddnn 1/1 Running 0 72s
pod/web2-deployment-76f5f6d7f5-pxzbb 1/1 Running 0 72s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/web-service NodePort 10.96.217.231 <none> 80:32589/TCP 8m55s
service/web2-service NodePort 10.107.108.29 <none> 80:31477/TCP 72s
卸载chart
[root@k8smaster helm]# helm uninstall web2
release "web2" uninstalled
升级chart
通过set升级
[root@k8smaster helm]# helm upgrade --set imageTag=1.18,replicas=2 web mychart/
Release "web" has been upgraded. Happy Helming!
NAME: web
LAST DEPLOYED: Thu Nov 25 10:28:20 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
Helm Test!!!
[root@k8smaster helm]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nfs-client-provisioner-5f98b5cdfb-rm7dd 1/1 Running 3 27h
pod/web-deployment-7d49f6fccd-rmf2n 1/1 Running 0 54s
pod/web-deployment-7d49f6fccd-xnwtw 1/1 Running 0 52s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/web-service NodePort 10.96.217.231 <none> 80:32589/TCP 16m
[root@k8smaster helm]# curl -I 10.96.217.231
HTTP/1.1 200 OK
Server: nginx/1.18.0
通过修改values.yaml 升级
[root@k8smaster mychart]# vi values.yaml
replicas: 3
image: "nginx"
imageTag: "1.16"
[root@k8smaster helm]# helm upgrade -f mychart/values.yaml web mychart/
Release "web" has been upgraded. Happy Helming!
NAME: web
LAST DEPLOYED: Thu Nov 25 10:35:59 2021
NAMESPACE: default
STATUS: deployed
REVISION: 4
TEST SUITE: None
NOTES:
Helm Test!!!
[root@k8smaster helm]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nfs-client-provisioner-5f98b5cdfb-rm7dd 1/1 Running 3 28h
pod/web-deployment-6fbb9b87dd-6ct7m 1/1 Running 0 15s
pod/web-deployment-6fbb9b87dd-9zw7x 1/1 Running 0 14s
pod/web-deployment-6fbb9b87dd-qkb8x 1/1 Running 0 16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/web-service NodePort 10.96.217.231 <none> 80:32589/TCP 23m
[root@k8smaster helm]# curl -I 10.96.217.231
HTTP/1.1 200 OK
Server: nginx/1.16.1
回滚指定版本
[root@k8smaster helm]# helm history web
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Thu Nov 25 10:13:03 2021 superseded mychart-0.1.0 1.16.0 Install complete
2 Thu Nov 25 10:28:20 2021 superseded mychart-0.1.0 1.16.0 Upgrade complete
3 Thu Nov 25 10:31:27 2021 superseded mychart-0.1.0 1.16.0 Upgrade complete
4 Thu Nov 25 10:35:59 2021 deployed mychart-0.1.0 1.16.0 Upgrade complete
#回滚到上一版本
[root@k8smaster helm]# helm rollback web
Rollback was a success! Happy Helming!
#回滚到指定版本
[root@k8smaster helm]# helm rollback web 2
Rollback was a success! Happy Helming!
查看helm的所有yaml
[root@k8smaster helm]# helm get manifest web
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: web-service
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: web
image: nginx:1.18
将chart打包
[root@k8smaster helm]# helm package mychart
Successfully packaged chart and saved it to: /root/k8s/helm/mychart-0.1.0.tgz
[root@k8smaster helm]# ls
mychart-0.1.0.tgz
公共Chart仓库
添加并查看chart仓库
[root@k8smaster helm]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories
[root@k8smaster helm]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "aliyun" chart repository
Update Complete. ⎈ Happy Helming!⎈
[root@k8smaster helm]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
查看chart仓库的安装包
[root@k8smaster helm]# helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
aliyun/mysql 0.3.5 Fast, reliable, scalable, and easy to use open-...
aliyun/percona 0.3.0 free, fully compatible, enhanced, open source d...
aliyun/percona-xtradb-cluster 0.0.2 5.7.19 free, fully compatible, enhanced, open source d...
aliyun/gcloud-sqlproxy 0.2.3 Google Cloud SQL Proxy
aliyun/mariadb 2.1.6 10.1.31 Fast, reliable, scalable, and easy to use open-...
拉取相关安装包
[root@k8smaster helm]# helm pull aliyun/mysql --untar
[root@k8smaster helm]# ls
mysql
网友评论