欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kubernetes(k8s):包管理工具Helm

程序员文章站 2022-07-10 10:39:28
...

1.Helm简介

  • Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts类似Linux系统的yum
  • Helm Chart 是用来封装 Kubernetes 原生应用程序的一系列 YAML 文件。可以在你部署应用的时候自定义应用程序的一些Metadata,以便于应用程序的分发。
  • 对于应用发布者而言,可以通过 Helm 打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。
  • 对于使用者而言,使用 Helm 后不用需要编写复杂的应用部署文件,可以以简单的方式在 Kubernetes上查找、安装、升级、回滚、卸载应用程序。

Helm当前最新版本 v3.2.0 官网:https://helm.sh/docs/intro/
kubernetes(k8s):包管理工具HelmHelm V3 与 V2 最大的区别在于去掉了tiller,见下图:
kubernetes(k8s):包管理工具Helm本节使用的是 Helm V3

2. Helm的部署

Helm当前最新版本 v3.1.0 官网:https://helm.sh/docs/intro/

Helm安装:

下载软件包:helm-v3.1.1-linux-amd64.tar.gz
[aaa@qq.com ~]# tar zxf helm-v3.1.1-linux-amd64.tar.gz
[aaa@qq.com ~]# cd linux-amd64/
[aaa@qq.com linux-amd64]# ls
helm  LICENSE  README.md
[aaa@qq.com ~]# cp helm /usr/local/bin/

设置helm命令补齐:

[aaa@qq.com ~]$ echo "source <(helm completion bash)" >> ~/.bashrc

重新登陆生效

[aaa@qq.com ~]# su - kubeadm
Last login: Mon May 11 09:16:14 CST 2020 on pts/1
Last failed login: Mon May 11 11:38:46 CST 2020 on pts/1
There was 1 failed login attempt since the last successful login.
[aaa@qq.com ~]$ helm          //helm命令
completion  dependency  get         install     list        plugin      repo        search      status      test        upgrade     version
create      env         history     lint        package     pull        rollback    show        template    uninstall   verify  

kubernetes(k8s):包管理工具Helm
Helm 添加第三方 Chart 库:

[aaa@qq.com ~]$ helm repo add stable http://mirror.azure.cn/kubernetes/charts/
"stable" has been added to your repositories
[aaa@qq.com ~]$ helm repo list
NAME  	URL                                      
stable	http://mirror.azure.cn/kubernetes/charts/

[aaa@qq.com ~]$ helm search repo redis
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                       
stable/prometheus-redis-exporter	3.4.0        	1.3.4      	Prometheus exporter for Redis metrics             
stable/redis                    	10.5.7       	5.0.7      	DEPRECATED Open source, advanced key-value stor...
stable/redis-ha                 	4.4.4        	5.0.6      	Highly available Kubernetes implementation of R...
stable/sensu                    	0.2.3        	0.28       	Sensu monitoring framework backed by the Redis ...

3. Helm 部署应用(拉取仓库的redis-ha并部署)

[aaa@qq.com ~]$ helm search repo redis-ha -l  // 可以查看redis-ha的所有版本
NAME           	CHART VERSION	APP VERSION	DESCRIPTION                                       
stable/redis-ha	4.4.4        	5.0.6      	Highly available Kubernetes implementation of R...
stable/redis-ha	4.4.3        	5.0.6      	Highly available Kubernetes implementation of R...
stable/redis-ha	4.4.2        	5.0.6      	Highly available Kubernetes implementation of R...
stable/redis-ha	4.4.1        	5.0.6      	Highly available Kubernetes implementation of R...
stable/redis-ha	4.4.0        	5.0.6      	Highly available Kubernetes implementation of R...
stable/redis-ha	4.3.4        	5.0.6      	Highly available Kubernetes implementation of R...
[aaa@qq.com ~]$ helm pull stable/redis-ha // 拉取应用到本地,默认拉取最新版本

[aaa@qq.com ~]$ ls  // 拉取了一个redis-ha的压缩包
components.yaml  dashrbac.yaml  kube-flannel.yml  manifest  recommended.yaml  redis-ha-4.4.4.tgz
[aaa@qq.com ~]$ mkdir helm
[aaa@qq.com ~]$ tar zxf redis-ha-4.4.4.tgz -C helm/  // 解压到helm目录
[aaa@qq.com redis-ha]$ sudo yum install tree -y
[aaa@qq.com redis-ha]$ tree .  // 查看目录结构
.
├── Chart.yaml
├── ci
│   └── haproxy-enabled-values.yaml
├── OWNERS
├── README.md
├── templates
│   ├── _configs.tpl
│   ├── _helpers.tpl
│   ├── NOTES.txt
│   ├── redis-auth-secret.yaml
│   ├── redis-ha-announce-service.yaml
│   ├── redis-ha-configmap.yaml
│   ├── redis-ha-exporter-script-configmap.yaml
│   ├── redis-ha-pdb.yaml
│   ├── redis-haproxy-deployment.yaml
│   ├── redis-haproxy-serviceaccount.yaml
│   ├── redis-haproxy-servicemonitor.yaml
│   ├── redis-haproxy-service.yaml
│   ├── redis-ha-rolebinding.yaml
│   ├── redis-ha-role.yaml
│   ├── redis-ha-serviceaccount.yaml
│   ├── redis-ha-servicemonitor.yaml
│   ├── redis-ha-service.yaml
│   ├── redis-ha-statefulset.yaml
│   └── tests
│       ├── test-redis-ha-configmap.yaml
│       └── test-redis-ha-pod.yaml
└── values.yaml

3 directories, 25 files

我们重点关注的是 values.yaml文件 进行修改和编辑

案例:部署redis-ha,实现高可用

(1)将镜像拉取到本地harbor之后

需要redis:5.0.6-alpine的镜像
[aaa@qq.com harbor]# docker pull redis:5.0.6-alpine
[aaa@qq.com harbor]# docker tag redis:5.0.6-alpine reg.westos.org/library/redis:5.0.6-alpine
[aaa@qq.com harbor]# docker push reg.westos.org/library/redis
[aaa@qq.com redis-ha]$ kubectl create namespace redis    //创建redis命名空间
[aaa@qq.com redis-ha]$ helm -n redis install redis    //在redisnamespace下安装redis
NAME: redis-ha
LAST DEPLOYED: Mon May 11 13:04:20 2020
NAMESPACE: redis
STATUS: deployed
REVISION: 1
NOTES:
Redis can be accessed via port 6379 and Sentinel can be accessed via port 26379 on the following DNS name from within your cluster:
redis-ha.redis.svc.cluster.local

To connect to your Redis server:
1. Run a Redis pod that you can use as a client:

   kubectl exec -it redis-ha-server-0 sh -n redis

2. Connect using the Redis CLI:

  redis-cli -h redis-ha.redis.svc.cluster.local

kubernetes(k8s):包管理工具Helm(2)创建后查看:

[aaa@qq.com redis-ha]$ kubectl -n redis get all  // redis-ha部署完成
NAME                    READY   STATUS    RESTARTS   AGE
pod/redis-ha-server-0   2/2     Running   0          2m16s
pod/redis-ha-server-1   2/2     Running   0          107s
pod/redis-ha-server-2   0/2     Pending   0          77s

NAME                          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)              AGE
service/redis-ha              ClusterIP   None             <none>        6379/TCP,26379/TCP   2m19s
service/redis-ha-announce-0   ClusterIP   10.100.4.107     <none>        6379/TCP,26379/TCP   2m18s
service/redis-ha-announce-1   ClusterIP   10.109.211.70    <none>        6379/TCP,26379/TCP   2m18s
service/redis-ha-announce-2   ClusterIP   10.105.156.107   <none>        6379/TCP,26379/TCP   2m18s

NAME                               READY   AGE
statefulset.apps/redis-ha-server   2/3     2m17s

[aaa@qq.com redis-ha]$ kubectl -n redis describe pod redis-ha-server-0  //pod中由两个容器redis和sentinel
Containers:
  redis:
  sentinel: // 负责主从切换

可以看到创建了两个pod,其中server-0为master,还有两个svc和一个statefulset控制器。

(3)测试redis集群高可用

每个pod中有两个容器redis和sentinel,我们进入redis容器查看:

kubernetes(k8s):包管理工具Helm

[aaa@qq.com redis-ha]$ kubectl -n redis exec -it redis-ha-server-0 -c redis -- sh  // 进入redis-ha-server-0,-c 指定进入的容器时redis
/data $ redis-cli
127.0.0.1:6379> info // 查看master的信息
/data $ redis-cli -h 10.109.211.70  // 进入salve
10.109.211.70:6379> info

/data $ nslookup redis-ha.redis.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      redis-ha.redis.svc.cluster.local
Address 1: 10.244.1.147 redis-ha-server-0.redis-ha.redis.svc.cluster.local
Address 2: 10.244.2.131 redis-ha-server-1.redis-ha.redis.svc.cluster.local

/data $ redis-cli -h redis-ha-server-1.redis-ha.redis.svc.cluster.local
redis-ha-server-1.redis-ha.redis.svc.cluster.local:6379> info  // 1是slave
# Replication
role:slave

/data $ redis-cli -h redis-ha-server-0.redis-ha.redis.svc.cluster.local
redis-ha-server-0.redis-ha.redis.svc.cluster.local:6379> info // 0是master
# Replication
role:master

helm -n redis upgrade redis-ha  // 更新
helm status redis-ha  // 查看状态
helm -n redis uninstall redis-ha  // 删除卸载
kubectl delete namespaces redis // 删除namespace
helm -n redis history redis-ha  // 查看历史
[aaa@qq.com redis-ha]# kubectl -n redis exec -it redis-ha-server-0 -c redis -- sh
/data $ redis-cli
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=10.101.152.37,port=6379,state=online,offset=25236,lag=0
master_replid:4baa1ff233604d357f9a5b2d6b100f90192d309c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:25236
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:25236

使用info Replication来查看主机信息,可以看出redis-ha-server-0为master主机。
执行SHUTDOWN命令来使主机宕机:

127.0.0.1:6379> SHUTDOWN
command terminated with exit code 137

宕机后控制器会马上帮我们启动一个pod

[aaa@qq.com redis-ha]# kubectl get pod -n redis 
NAME                READY   STATUS    RESTARTS   AGE
redis-ha-server-0   2/2     Running   1          4m57s
redis-ha-server-1   2/2     Running   0          4m48s

等重建好了之后再次查看pod角色

[aaa@qq.com redis-ha]# kubectl -n redis exec -it redis-ha-server-0 -c redis -- sh
/data $ redis-cli
127.0.0.1:6379> info Replication
# Replication
role:slave            //宕机后重启切换为slave
master_host:10.101.152.37
master_port:6379
...

可以看出redis-ha-server-0已经变成了从机,而主机的ip为10.101.152.37,这时就实现了主从故障的切换。

主从故障切换依靠 sentinel容器完成。

(4)可以直接更改变量文件中的副本数量实现集群的动态拉伸
kubernetes(k8s):包管理工具Helm(5)使用upgrade选项更新集群

[aaa@qq.com redis-ha]# helm -n redis upgrade redis-ha .

更新后查看pod可以看出已经变成了3个

[aaa@qq.com redis-ha]# kubectl -n redis get pod
NAME                READY   STATUS    RESTARTS   AGE
redis-ha-server-0   2/2     Running   2          20m
redis-ha-server-1   2/2     Running   0          20m
redis-ha-server-2   0/2     Pending   0          12s

也可以使用以下命令查看更新历史以便回滚

[aaa@qq.com redis-ha]# helm -n redis history 
Error: "helm history" requires 1 argument

Usage:  helm history RELEASE_NAME [flags]
[aaa@qq.com redis-ha]# helm -n redis history redis-ha 
REVISION	UPDATED                 	STATUS    	CHART         	APP VERSION	DESCRIPTION     
1       	Sun May 10 23:15:35 2020	superseded	redis-ha-4.4.4	5.0.6      	Install complete
2       	Sun May 10 23:35:35 2020	deployed  	redis-ha-4.4.4	5.0.6      	Upgrade complete
相关标签: kubernetes helm