helm

helm

helm 通过打包的方式,支持发布的版本管理和控制,很大程度上简化了 Kubernetes 应用的部署和管理.

Helm 本质就是让 K8s 的应用管理(Deployment,Service 等 ) 可配置,能动态生成。通过动态生成 K8s 资源清单文件(deployment.yaml,service.yaml 等)。然后调用 Kubectl 自动执行 K8s 资源部署

Helm 是官方提供的类似于 YUM 的包管理器,是部署环境的流程封装。Helm 有两个重要的概念:chartrelease

  1. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板、参数定义、依赖关系、文档说明等。chart 是应用部署的自包含逻辑单元。可以将 chart 想象成 apt、yum 中的软件安装包
  2. releasechart 的运行实例,代表了一个正在运行的应用。当 chart 被安装到 Kubernetes 集群,就生成一个 releasechart 能够多次安装到同一个集群,每次安装都是一个 release

Helm 包含两个组件:Helm 客户端和 Tiller 服务器 (Tillerhelm 3.0 以上版本被遗弃),有关内容已删除。

helm 部署

1
2
3
4
ntpdate ntp1.aliyun.com
wget https://get.helm.sh/helm-v3.3.1-linux-amd64.tar.gz # 当前最新版 v3.3.1
tar -zxvf helm-*-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/
1
2
3
4
5
6
7
8
9
10
11
12
13
## helm 自定义模板
$ mkdir ./hello-world
$ cd ./hello-world


# 创建自描述文件 Chart.yaml , 这个文件必须有 name 和 version 定义

$ cat <<'EOF' > ./Chart.yaml
name: hello-world
version: 1.0.0
EOF

# helm create mychart

当前文件夹下,创建模板文件,用于生成 Kubernetes 资源清单(manifests
./templates/service.yaml

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: hello-world

./templates/deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: 80
protocol: TCP

配置体现在配置文件 values.yaml

./values.yaml

1
2
3
image:
repository: imwl/myapp
tag: 'v2'

目录结构

1
2
3
4
5
6
7
8
9
10
[root@k8s01 ~]# cd hello-world/
[root@k8s01 hello-world]# tree
.
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   └── service.yaml
└── values.yaml

1 directory, 4 files

打包

1
2
helm package /root/hello-world/
Successfully packaged chart and saved it to: /root/hello-world/hello-world-1.0.0.tgz

使用 hello-world

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
46
47
48
49
50
51
52
53
54
55
56
 使用命令 helm install RELATIVE_PATH_TO_CHART 创建一次Release

[root@k8s01 hello-world]# helm install --name-template hello .

[root@k8s01 hello-world]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-world-6768f995d8-tzw8x 1/1 Running 0 45s 172.18.235.154 k8s03 <none> <none>
[root@k8s01 hello-world]# curl 172.18.235.154
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

# 在 values.yaml 中的值可以被部署 release 时用到的参数 --values YAML_FILE_PATH 或 --setkey1=value1, key2=value2 覆盖掉
[root@k8s01 hello-world]# helm install --set image.tag='v1' --name-template hello .
NAME: hello
LAST DEPLOYED: Mon Aug 31 21:44:42 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s01 hello-world]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-world-7cb76c9f4d-lhbw8 1/1 Running 0 19s 172.18.235.167 k8s03 <none> <none>
[root@k8s01 hello-world]# curl 172.18.235.167
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

# 升级版本
[root@k8s01 hello-world]# vi values.yaml # 改为v3
[root@k8s01 hello-world]# helm upgrade -f values.yaml hello .
Release "hello" has been upgraded. Happy Helming!
NAME: hello
LAST DEPLOYED: Mon Aug 31 21:47:00 2020
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
[root@k8s01 hello-world]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-world-7d845997d-fc86w 1/1 Running 0 20s 172.18.235.164 k8s03 <none> <none>
[root@k8s01 hello-world]# curl 172.18.235.164
Hello MyApp | Version: v3 | <a href="hostname.html">Pod Name</a>


# 回滚
[root@k8s01 hello-world]# helm history hello
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Mon Aug 31 21:44:42 2020 superseded hello-world-1.0.0 Install complete
2 Mon Aug 31 21:47:00 2020 deployed hello-world-1.0.0 Upgrade complete
[root@k8s01 hello-world]# helm rollback hello 1
Rollback was a success! Happy Helming!
[root@k8s01 hello-world]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-world-7cb76c9f4d-6zgcb 1/1 Running 0 16s 172.18.235.170 k8s03 <none> <none>
[root@k8s01 hello-world]# curl 172.18.235.170
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s01 hello-world]#

## 扩容

helm upgraded hello –set replicas=3 ./ #扩容副本3个

1
2
3
4
5
6
7


## 删除
[root@k8s01 hello-world]# helm uninstall hello (--keep-history 保存)
release "hello" uninstalled

[root@k8s01 hello-world]# helm del --purge hello # 完整删除

Debug

使用模板动态生成 K8s 资源清单,非常需要能提前预览生成的结果。
使用 --dry-run --debug 选项来打印出生成的清单文件内容,而不执行部署

1
helm install  --generate-name  --dry-run --debug --set  image.tag=latest .

配置 helm 源地址

1
2
helm  repo  add  stable  http://mirror.azure.cn/kubernetes/charts/
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

检查配置源地址,更新

1
2
helm repo list
helm repo update

通过helm安装软件

1
2
3
4
5
6
7
8
9
10
11
12
helm  search  repo  weave   #去仓库中查找weave的程序包

kubectl create namespace airflow
helm repo add apache-airflow https://airflow.apache.org
helm install airflow apache-airflow/airflow --namespace airflow # 安装

helm repo update


helm fetch apache-airflow/airflow --version 2.1.1 # 获取 chart 文件 tgz 格式,需要解压

helm install airflow . --namespace airflow # 从 文件中安装