Kubernetes安装Helm和使用

部署Helm

获取Helm

Github项目地址

二进制安装

1
2
3
wget -O - https://get.helm.sh/helm-v2.14.1-linux-amd64.tar.gz | tar xz linux-amd64/helm
mv linux-amd64/helm /usr/local/bin/helm
rm -rf linux-amd64

通过官方脚本安装

1
curl -L https://git.io/get_helm.sh | bash

创建RBAC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat << EOF | kubectl apply -f -
# 创建名为tiller的ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
# 给tiller绑定cluster-admin权限
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller-cluster-rule
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
EOF

安装Helm服务端

1
2
3
helm init --tiller-image dockerhub.azk8s.cn/gcrxio/tiller:v2.14.1 \
--service-account tiller \
--stable-repo-url http://mirror.azure.cn/kubernetes/charts/

检查部署结果

查看Pod状态

1
kubectl -n kube-system get pod -l app=helm,name=tiller

输出示例

1
2
NAME                             READY     STATUS    RESTARTS   AGE
tiller-deploy-84fc6cd5f9-nz4m7 1/1 Running 0 1m

查看Helm版本信息

1
helm version

输出示例

1
2
Client: &version.Version{SemVer:"v2.14.1", GitCommit:"d325d2a9c179b33af1a024cdb5a4472b6288016a", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.1", GitCommit:"d325d2a9c179b33af1a024cdb5a4472b6288016a", GitTreeState:"clean"}

使用Helm

命令自动补齐

临时生效

1
source <$(helm compeltion bash)

永久生效

1
2
helm compeltion bash > /etc/bash_completions./helm
source /etc/profile.d/bash_completion.sh

查看Chart

https://hub.helm.sh/charts

查看Repo

1
helm repo list

输出示例

1
2
3
NAME   	URL                                              
local http://127.0.0.1:8879/charts
stable https://kubernetes-charts.storage.googleapis.com/

添加Repo

1
helm repo add pingcap <Chart-URL>

更新Repo缓存

1
helm repo update

查看Repo里的Chart

1
helm search <Chart-Name> -l

输出示例

1
2
3
4
helm search stable -l
NAME CHART VERSION APP VERSION DESCRIPTION
stable/acs-engine-autoscaler 2.2.2 2.1.1 DEPRECATED Scales worker nodes within agent pools
stable/acs-engine-autoscaler 2.2.1 2.1.1 Scales worker nodes within agent pools

查找Chart

这里以redis作为关键字

1
helm search redis

输出示例

1
2
3
4
5
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                                 
stable/prometheus-redis-exporter 3.0.0 1.0.3 Prometheus exporter for Redis metrics
stable/redis 9.0.1 5.0.5 Open source, advanced key-value store. It is often referr...
stable/redis-ha 3.6.2 5.0.5 Highly available Kubernetes implementation of Redis
stable/sensu 0.2.3 0.28 Sensu monitoring framework backed by the Redis transpor

下载Chart

1
helm fetch stable/redis --version=9.0.1

查看Chart文件结构

解压Chart

1
tar xzf redis-9.0.1.tgz

查看目录结构

1
tree 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
redis
├── Chart.yaml
├── ci
│   ├── default-values.yaml
│   ├── dev-values.yaml
│   ├── extra-flags-values.yaml
│   ├── production-sentinel-values.yaml
│   ├── production-values.yaml
│   ├── redisgraph-module-values.yaml
│   └── redis-lib-values.yaml
├── README.md
├── templates
│   ├── configmap.yaml
│   ├── headless-svc.yaml
│   ├── health-configmap.yaml
│   ├── _helpers.tpl
│   ├── metrics-prometheus.yaml
│   ├── metrics-svc.yaml
│   ├── networkpolicy.yaml
│   ├── NOTES.txt
│   ├── redis-master-statefulset.yaml
│   ├── redis-master-svc.yaml
│   ├── redis-rolebinding.yaml
│   ├── redis-role.yaml
│   ├── redis-serviceaccount.yaml
│   ├── redis-slave-statefulset.yaml
│   ├── redis-slave-svc.yaml
│   ├── redis-with-sentinel-svc.yaml
│   └── secret.yaml
├── values-production.yaml
└── values.yaml

使用Helm安装Chart

帮助文档里面是这么描述的

1
2
3
4
5
1. By chart reference: helm install stable/mariadb
2. By path to a packaged chart: helm install ./nginx-1.2.3.tgz
3. By path to an unpacked chart directory: helm install ./nginx
4. By absolute URL: helm install https://example.com/charts/nginx-1.2.3.tgz
5. By chart reference and repo url: helm install --repo https://example.com/charts/ nginx

直接安装

1
helm install stable/redis --name=helm-redis --namespace=default --version=9.0.1

安装时指定变量

具体的变量定义在Chart目录下的values.yaml文件里面有

1
2
3
4
5
6
7
helm install stable/redis \
--name=helm-redis \
--namesapce=default \
--version=9.0.1 \
--set image.registry=docker.io \
--set image.repository=bitnami/redis \
--set image.tag=5.0.5-debian-9-r36

安装时指定变量文件

按需修改Chart目录下values.yaml文件

1
2
3
4
5
helm install stable/redis \
--name=helm-redis \
--namesapce=default \
--version=9.0.1 \
-f redis/values.yaml

列出Release

1
helm list --all

输出示例

1
2
NAME                UPDATED                     CHART
helm-redis Mon May 9 16:07:08 2019 redis-9.0.1

更新Release

1
helm upgrade helm-redis stable/redis -f redis/values.yaml

查看Release历史

1
2
3
4
helm history helm-redis
REVISION UPDATED STATUS CHART DESCRIPTION
1 Mon Apr 16 10:21:44 2019 SUPERSEDED stable/redis-9.0.1 Install complete
2 Mon Apr 16 10:43:10 2019 DEPLOYED stable/redis-9.0.1 Upgrade complete

回滚Release

1
helm rollback helm-redis 1

删除Release

1
helm delete helm-redis
  • 彻底删除
1
helm delete --purge helm-redis

通过模板功能生成YAML文件

1
2
3
4
5
helm template stable/redis \
--name=helm-redis \
--namesapce=default \
--version=9.0.1 \
-f redis/values.yaml > helm_redis_generated.yaml

此方法生成的YAML文件可以通过kubectl命令进行部署