【容器应用系列教程】Kubernetes插件helm包管理器

一、关于helm介绍

1.作用

  • Helmkubernetes的包管理工具,相当于linux环境下的yum/apg-get命令
  • helm也是go语言开发的
  • 简化在kubernets集群中部署应用的流程

2.基本概念

  • helm
    • 命令行客户端工具,主要用于Kubernetes应用中的chart的创建、打包、发布和管理。
  • Chart
    • helm程序包 ,一系列用于描述k8s资源相关文件的集合 ,比方说我们部署nginx,需要deploymentyaml,需要serviceyaml,这两个清单文件就是一个helm程序包,在k8s中把这些yaml清单文件叫做chart图表。
  • values.yaml
    • 文件为模板中的文件赋值,可以实现我们自定义安装
  • repository
    • 存放chart图表的仓库
  • Release
    • 基于Chart的部署实体,一个chartHelm运行后将会生成对应的一个release

3.版本兼容性

官方文档:https://helm.sh/zh/docs/topics/version_skew/

kubernetes42

二、helm3安装

1.安装helm

由于我这使用的Kubernetes版本是1.20.7所以我使用helm3.8.2版本

[root@k8s-master ~]# wget https://get.helm.sh/helm-v3.8.2-linux-amd64.tar.gz
[root@k8s-master ~]# tar xf helm-v3.8.2-linux-amd64.tar.gz
[root@k8s-master ~]# cp /root/linux-amd64/helm /usr/local/bin/

2.编辑环境变量

[root@k8s-master ~]# vim /etc/profile
#在文件末尾添加内容
source <(helm completion bash)
[root@k8s-master ~]# source /etc/profile

3.测试

[root@k8s-master ~]# helm version
version.BuildInfo{Version:"v3.8.2", GitCommit:"6e3701edea09e5d55a8ca2aae03a68917630e91b", GitTreeState:"clean", GoVersion:"go1.17.5"}

4.添加国内仓库地址

[root@k8s-master ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@k8s-master ~]# helm repo add msoft http://mirror.azure.cn/kubernetes/charts/

5.更新仓库

[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 "msoft" chart repository
Update Complete. ⎈Happy Helming!⎈

6.查看仓库

[root@k8s-master ~]# helm repo list
NAME  	URL                                                   
aliyun	https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
msoft 	http://mirror.azure.cn/kubernetes/charts/

7.搜索指定chart

# helm search repo 镜像名
[root@k8s-master ~]# helm search repo mysql
NAME                           	CHART VERSION	APP VERSION	DESCRIPTION                                       
aliyun/mysql                   	0.3.5        	           	Fast, reliable, scalable, and easy to use open-...
msoft/mysql                    	1.6.9        	5.7.30     	DEPRECATED - Fast, reliable, scalable, and easy...
msoft/mysqldump                	2.6.2        	2.4.1      	DEPRECATED! - A Helm chart to help backup MySQL...
msoft/prometheus-mysql-exporter	0.7.1        	v0.11.0    	DEPRECATED A Helm chart for prometheus mysql ex...
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...
msoft/percona                  	1.2.3        	5.7.26     	DEPRECATED - free, fully compatible, enhanced, ...
msoft/percona-xtradb-cluster   	1.0.8        	5.7.19     	DEPRECATED - free, fully compatible, enhanced, ...
msoft/phpmyadmin               	4.3.5        	5.0.1      	DEPRECATED phpMyAdmin is an mysql administratio...
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-...
msoft/gcloud-sqlproxy          	0.6.1        	1.11       	DEPRECATED Google Cloud SQL Proxy                 
msoft/mariadb                  	7.3.14       	10.3.22    	DEPRECATED Fast, reliable, scalable, and easy t...

三、helm3的使用

1.查看某个chart的详细信息

[root@k8s-master ~]# helm show chart msoft/mysql

apiVersion: v1
appVersion: 5.7.30
deprecated: true
description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relational
  database system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.9

2.获取对应chart的配置文件

[root@k8s-master ~]# helm inspect values msoft/mysql > config.yaml

3.自定义配置

[root@k8s-master ~]# vim config.yaml
#配置文件并不完整,仅展示修改部分
mysqlRootPassword: redhat	#自定义数据库密码
mysqlDatabase: jiaowu	#创建库
persistence:
  enabled: false	#关闭PV数据卷

4.创建release

[root@k8s-master ~]# helm install -f config.yaml  msoft/mysql --generate-name
[root@k8s-master ~]# kubectl get pod
NAME                                READY   STATUS              RESTARTS   AGE
mysql-1687177709-5657f94b76-pg77b   0/1     Init:0/1            0          23s

5.查看

[root@k8s-master ~]# helm ls

NAME            	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql-1687177709	default  	1       	2023-06-19 20:28:30.942114444 +0800 CST	deployed	mysql-1.6.9	5.7.30

6.升级

[root@k8s-master ~]# helm upgrade -f config.yaml mysql-1687177709 msoft/mysql

7.版本回滚

查看历史版本

[root@k8s-master ~]# helm history mysql-1669203259 
REVISION	UPDATED                 	STATUS    	CHART      	APP VERSION	DESCRIPTION     
1       	Wed Nov 23 19:34:21 2022	superseded	mysql-1.6.9	5.7.30     	Install complete
2       	Wed Nov 23 19:38:38 2022	deployed  	mysql-1.6.9	5.7.30     	Upgrade complete

版本回退

# helm rollback 镜像 版本ID
[root@k8s-master ~]# helm rollback mysql-1669203259 1 
Rollback was a success! Happy Helming!