helm简介

helm的整体工作逻辑就类似于yum,而对应yum的rpm,helm提供的为chart。

但是与yum的rpm所不同的是,chart仅提供的是描述性文件,chart还需要依赖于image才能工作。

chart中只提供了各应用的部署清单,还需要连入到docker hub上去获取镜像。

helm相关术语

Chart:即一个Helm程序包,它包含了运行一个Kubernetes应用所需要的镜像、依赖关系和资源定义等,它类似于APT的dpkg文件或者yum的rpm文件。

Repository:集中存储和分发Chart的仓库,类似于Perl的CPAN,或者Python的PyPI等。

Config:Chart实例化安装运行时使用的配置信息。

Release:Chart实例化配置后运行于Kubernetes集群中的一个应用实例;在同一个集群上,一个Chart可以使用不同的Config重复安装多次,每次安装都会创建一个新的“发布(Release)”。

helm版本

helm到目前位置有两个维护的版本:

  • helm v2: v2版本在部署时需要先在k8s之上部署出一个工作组件tiller。helm可以工作在远程类似于kubectl,当需要部署某个应用时,helm先到远程的chart hub上下载对应的chart,然后在本地使用helm命令将其渲染成各种清单,然后将清单提交给API Server,让API Server进行应用。在提交给API Server之后需要先将其交给tiller,由tiller来维护各种应用的元数据信息,tiller要追踪各Release。在v2版中,helm客户端需要先联系到tiller,由tiller提交给API Server,然后再部署到本地集群中。部署、跟踪和管理这些功能是由tiller来完成的。tiller把每一个release存储到tiller自身中以供helm查询使用。
  • helm v3: v3版本中不再有tiller这个组件,其工作逻辑类似于flannel和calico。其通过向API Server提交chart和config来进行配置的。而chart和config生成的release信息直接存储到API Server的etcd中去。所以以后要删除应用时先向API Server中进行查询,将对应的清单进行删除。

helm配置信息定制

helm定制的配置信息可以有2种方式进行提供:

  1. 使用helm命令行进行提供,使用命令行选项传递参数。
  2. 通过值文件values.yaml传递参数,值文件就是传给Chart模板当中所定义的每一个模板指令的值。

helm安装

官方文档:https://helm.sh/zh/docs/intro/install/

在工作中基本不建议安装在Master节点上进行操作,建议在自己笔记本上进行部署,并连入集群。

helm使用

此处以redis和prometheus为例:

添加仓库

在官方制品库https://artifacthub.io/中找到promethus

添加promethus的helm仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@k8s-master01:~/linux-amd64# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories
root@k8s-master01:~/linux-amd64# helm repo add kube-state-metrics https://kubernetes.github.io/kube-state-metrics
"kube-state-metrics" has been added to your repositories
root@k8s-master01:~/linux-amd64# helm repo add bitnami https://charts.bitnami.com/bitnami # bitnami也是常用的仓库之一
"bitnami" has been added to your repositories

# 更新仓库抓取索引信息
root@k8s-master01:~/linux-amd64# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "kube-state-metrics" chart repository
...Successfully got an update from the "prometheus-community" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

查看仓库

1
2
3
4
5
6
7
root@k8s-master01:~/linux-amd64# helm repo list
NAME URL
prometheus-community https://prometheus-community.github.io/helm-charts
kube-state-metrics https://kubernetes.github.io/kube-state-metrics
bitnami https://charts.bitnami.com/bitnami

# 仓库已经添加

查找应用redis

1
2
3
4
5
root@k8s-master01:~/linux-amd64# helm search repo redis
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/redis 14.8.11 6.2.5 Open source, advanced key-value store. It is of...
bitnami/redis-cluster 6.3.4 6.2.5 Open source, advanced key-value store. It is of...
prometheus-community/prometheus-redis-exporter 4.2.0 1.11.1 Prometheus exporter for Redis metrics

如果期望能够使用某个chart来完成管理和安装,那就尝试将chart下载到本地或使用helm show来查看

1
2
3
4
5
# 使用show all会下载所有信息并显示
root@k8s-master01:~/linux-amd64# helm show all bitnami/redis

# 如果只需要查看readme,show readme即可
root@k8s-master01:~/linux-amd64# helm show readme bitnami/redis

helm部署redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 执行此步骤后会将redis chart中的默认信息渲染成资源清单后部署redis
root@k8s-master01:~/linux-amd64# helm install db bitnami/redis # instll 后需要添加release名
NAME: db # release名DB
LAST DEPLOYED: Wed Aug 25 05:48:50 2021 # 最近一次部署时间
NAMESPACE: default # 所部署的名称空间
STATUS: deployed # 当前状态
REVISION: 1 # 历史版本
TEST SUITE: None # 是否带有测试套件
NOTES:
** Please be patient while the chart is being deployed **

Redis™ can be accessed on the following DNS names from within your cluster:
# 部署了2个svc,一个读写,一个只读
db-redis-master.default.svc.cluster.local for read/write operations (port 6379)
db-redis-replicas.default.svc.cluster.local for read-only operations (port 6379)



To get your password run:
# 部署时生成了密码,使用以下命令来获取
export REDIS_PASSWORD=$(kubectl get secret --namespace default db-redis -o jsonpath="{.data.redis-password}" | base64 --decode)

To connect to your Redis™ server:
# 以下为连接redis的方法
1. Run a Redis™ pod that you can use as a client:

kubectl run --namespace default redis-client --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:6.2.5-debian-10-r11 --command -- sleep infinity

Use the following command to attach to the pod:

kubectl exec --tty -i redis-client \
--namespace default -- bash

2. Connect using the Redis™ CLI:
redis-cli -h db-redis-master -a $REDIS_PASSWORD
redis-cli -h db-redis-replicas -a $REDIS_PASSWORD

To connect to your database from outside the cluster execute the following commands:

kubectl port-forward --namespace default svc/db-redis-master 6379:6379 &
redis-cli -h 127.0.0.1 -p 6379 -a $REDIS_PASSWORD

查看部署结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@k8s-master01:~# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
db default 1 2021-08-25 05:48:50.961139929 +0000 UTC deployed redis-14.8.11 6.2.5

root@k8s-master01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
db-redis-master-0 0/1 Pending 0 6m56s
db-redis-replicas-0 0/1 Pending 0 6m56s
# 处于pending状态

# 查看原因
root@k8s-master01:~# kubectl describe pod db-redis-master-0 | grep -A5 Events
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 8m12s default-scheduler 0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
Warning FailedScheduling 8m11s default-scheduler 0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
# 卷挂载问题,redis chart中默认绑定的存储类系统中没有导致的。
# 下面解决此问题

要修改chart的默认配置可以去修改其值文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 一旦执行过helm命令后会在本地家目录下生成一个.cache的隐藏目录
root@k8s-master01:~/.cache/helm/repository# ls
bitnami-charts.txt bitnami-index.yaml kube-state-metrics-charts.txt kube-state-metrics-index.yaml prometheus-community-charts.txt prometheus-community-index.yaml redis-14.8.11.tgz

# 在此目录下可以看到一个redis-14.8.11.tgz的文件,将其展开
root@k8s-master01:~/.cache/helm/repository# tar xf redis-14.8.11.tgz -C /tmp

# 此为chart文件的组成格式
root@k8s-master01:~/.cache/helm/repository# ls /tmp/redis/
Chart.lock charts Chart.yaml ci img README.md templates values.schema.json values.yaml

# values.yaml就是所需要的值文件
# 可以查看改文件中的内容,每一级以.隔开在命令行中进行对应的设置。此处可以将master和replica下的persistence.enabled进行禁用

重新部署redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 先删除已经部署的db
root@k8s-master01:~# helm uninstall db
release "db" uninstalled

# 重新部署redis
root@k8s-master01:~# helm install db bitnami/redis --set master.persistence.enabled=false --set replica.persistence.enabled=false
NAME: db
LAST DEPLOYED: Wed Aug 25 06:44:40 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

Redis™ can be accessed on the following DNS names from within your cluster:

db-redis-master.default.svc.cluster.local for read/write operations (port 6379)
db-redis-replicas.default.svc.cluster.local for read-only operations (port 6379)



To get your password run:

export REDIS_PASSWORD=$(kubectl get secret --namespace default db-redis -o jsonpath="{.data.redis-password}" | base64 --decode)

To connect to your Redis™ server:

1. Run a Redis™ pod that you can use as a client:

kubectl run --namespace default redis-client --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:6.2.5-debian-10-r11 --command -- sleep infinity

Use the following command to attach to the pod:

kubectl exec --tty -i redis-client \
--namespace default -- bash

2. Connect using the Redis™ CLI:
redis-cli -h db-redis-master -a $REDIS_PASSWORD
redis-cli -h db-redis-replicas -a $REDIS_PASSWORD

To connect to your database from outside the cluster execute the following commands:

kubectl port-forward --namespace default svc/db-redis-master 6379:6379 &
redis-cli -h 127.0.0.1 -p 6379 -a $REDIS_PASSWORD

再次查看redis

1
2
3
4
5
6
7
root@k8s-master01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
db-redis-master-0 1/1 Running 0 2m9s
db-redis-replicas-0 1/1 Running 0 2m9s
db-redis-replicas-1 1/1 Running 0 89s
db-redis-replicas-2 1/1 Running 0 38s
# 成功启动了服务。

此处有个问题,这种配置需要执行时添加一系列参数,相当不便,我们可以将其values.yaml取出进行修改,部署时直接使用值文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 对值文件进行修改后,将其改为单主进行运行
root@k8s-master01:~# vim /tmp/redis/values.yaml

architecture: standalone
auth:
enabled: false
sentinel: false
master:
persistence:
enabled: true
path: /data
subPath: ""
storageClass: "longhorn"

再次使用values.yaml进行部署

1
2
3
4
5
6
# 先将旧版本删除
root@k8s-master01:~# helm uninstall db
release "db" uninstalled

# 使用values.yaml值文件重新部署
root@k8s-master01:~# helm install db bitnami/redis -f /tmp/redis/values.yaml

验证部署

1
2
3
4
# 单主节点已经创建。
root@k8s-master01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
db-redis-master-0 1/1 Running 0 70s