一、什么是HELM
Helm包含两个组件:Helm客户端和Tiller服务器,如图所示:
1、在没使用helm之前,向k8s部署应用,我们要依次部署deployment、svc等,步骤繁琐。况且随着很多项目微服务化,复杂的应用在容器中部署以及管理显得较为复杂,helm通过打包的方式,支持发布的版本管理和控制,很大程度上简化了k8s应用的部署和管理
2、helm本质就是让k8s的应用管理(deployment、service等)可配置,能动态生成。通过动态生成k8s资源清单文件(deployment.yaml,service.yaml)。然后kubectl自动执行k8s资源部署
3、Helm是官方提供的类似于yum的包管理器,是部署环境的流程封装。helm有两个重要额概念:chart和release
1)chart:是创建一个应用的信息集合,包括各种k8s对象的配置模板、参数定义、依赖关系、文件说明等。chart是应用部署的自包含逻辑单元。可以将chart想象成apt、yum中的软件安装包
2)release是chart的运行实例,代表了一个正在运行的应用。当chart被安装到k8s集群,就生成一个release。chart能够多次安装到同一个集群。每次安装都是一个release
4、helm包含两个组件:Helm客户端和Tiller服务器
1)Helm客户端负载chart和release的创建和管理以及和Tiller的交互。Tiller服务器运行在k8s集群中,他会处理Helm客户端的请求,与k8s api server交互
二、Helm部署
1、部署helm(主节点运行)
helm:https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz
[root@k8s-master1 linux-amd64]# chmod +x helm
[root@k8s-master1 linux-amd64]# cp helm /usr/local/bin/
2、授权
[root@k8s-master1 helm]# vim rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
[root@k8s-master1 helm]# kubectl create -f rbac.yaml
[root@k8s-node1 ~]# yum install -y socat
报错时安装:E0514 18:59:32.423110 1859 portforward.go:391] an error occurred forwarding 40514 -> 44134: error forwarding port 44134 to pod 21880df51cca7a0256522bbcf9306da6daaa4d309e2f94903b09c28cdd4a2f03, uid : unable to do port forwarding: socat not found.
E0514 18:59:33.428408 1859 portforward.go:391] an error occurred forwarding 40514 -> 44134: error forwarding port 44134 to pod 21880df51cca7a0256522bbcf9306da6daaa4d309e2f94903b09c28cdd4a2f03, uid : unable to do port forwarding: socat not found.
1)安装tiller
[root@k8s-master1 helm]# helm init --service-account tiller --skip-refresh
Happy Helming!
tiller默认会被部署到k8s集群的kube-system中
[root@k8s-master1 helm]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
tiller-deploy-58565b5464-mvnrp 1/1 Running 0 2m13s
[root@k8s-master1 .helm]# helm version
Client: &version.Version{SemVer:"v2.13.1", GitCommit:"618447cbf203d147601b4b9bd7f8c37a5d39fbb4", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.13.1", GitCommit:"618447cbf203d147601b4b9bd7f8c37a5d39fbb4", GitTreeState:"clean"}
3、helm的基本使用
[root@k8s-master1 helm]# mkdir hello-word
1)创建自描述文件Chart.yaml,这个文件必须有name和version(Chart.yaml,templates不能改名字)
[root@k8s-master1 hello-word]# vim Chart.yaml
name: nginx
version: v1
2)创建模板文件,用于生成k8s资源清单(manifests)
[root@k8s-master1 hello-word]# mkdir templates/
[root@k8s-master1 hello-word]# vim templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: hub.benet.com/xitong/nginx
imagePullPolicy: IfNotPresent
post:
- containerPort: 80
protocol: TCP
[root@k8s-master1 hello-word]# vim templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
3)使用helm创建服务(不使用变量值)
[root@k8s-master1 hello-word]# helm install . (可用--name指定名字)
NAME: wistful-cricket
LAST DEPLOYED: Thu May 14 21:48:55 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deployment-58f4f857b-c4nr7 0/1 ContainerCreating 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.211 <none> 80:46275/TCP 0s
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 0/1 0 0 0s
[root@k8s-master1 hello-word]# helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
wistful-cricket 1 Thu May 14 21:48:55 2020 DEPLOYED nginx-v1 default
[root@k8s-master1 hello-word]# helm status wistful-cricket
LAST DEPLOYED: Thu May 14 21:48:55 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deployment-58f4f857b-c4nr7 1/1 Running 0 2m18s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.211 <none> 80:46275/TCP 2m18s
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 2m18s
4)使用helm创建服务(使用变量值)
[root@k8s-master1 helm]# mkdir -p nginx/templates
[root@k8s-master1 nginx]# kubectl run deployment nginx --image=nginx:v2 --dry-run -o yaml > templates/deployment.yaml
#编辑Chart.yaml文件
[root@k8s-master1 nginx]# vim Chart.yaml
name: nginx
version: v2
#编辑deployment.yaml
[root@k8s-master1 nginx]# vim templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deploy
spec:
replicas: 1
template:
metadata:
labels:
run: nginx
spec:
nodeName: 192.168.100.30
containers:
- image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
imagePullPolicy: IfNotPresent
name: nginx
port:
- containerPort: 80
protocol: TCP
#编辑services.yaml
[root@k8s-master1 nginx]# vim templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
run: nginx
#编辑values.yaml文件
[root@k8s-master1 nginx]# vim values.yaml
image:
repository: nginx
tag: "v2"
#安装
[root@k8s-master1 nginx]# helm install --name=nginx ./
NAME: nginx
LAST DEPLOYED: Fri May 22 01:17:31 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deploy-6d7bd6648b-mbf82 0/1 ContainerCreating 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.187 <none> 80:46922/TCP 0s
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 0/1 1 0 0s
#查看运行的pod
[root@k8s-master1 nginx]# helm ls
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
nginx 1 Fri May 22 01:17:31 2020 DEPLOYED nginx-v2 default
#查看pod状态
[root@k8s-master1 nginx]# helm status nginx
LAST DEPLOYED: Fri May 22 01:17:31 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deploy-6d7bd6648b-mbf82 1/1 Running 0 3m27s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.187 <none> 80:46922/TCP 3m27s
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 3m27s
#测试pod是否能正常使用
[root@k8s-node1 ~]# curl 10.0.0.137
2
4、helm的使用最多的命令
1)更新(1:通过修改镜像的方式)
#修改yaml文件
[root@k8s-master1 hello-word]# vim templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
nodeName: 192.168.100.30
containers:
- name: nginx
image: nginx:v1 #修改镜像
imagePullPolicy: IfNotPresent
post:
- containerPort: 80
protocol: TCP
#更新
[root@k8s-master1 hello-word]# helm upgrade wistful-cricket .
Release "wistful-cricket" has been upgraded. Happy Helming!
LAST DEPLOYED: Thu May 21 20:35:36 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deployment-58f4f857b-c4nr7 1/1 Terminating 1 6d22h
nginx-deployment-699787c785-rgd6f 0/1 ContainerCreating 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.211 <none> 80:46275/TCP 6d22h
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 0/1 1 0 6d22h
[root@k8s-master1 hello-word]# helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
wistful-cricket 2 Thu May 21 20:35:36 2020 DEPLOYED nginx-v1 default
#查看是否正常运行
[root@k8s-master1 hello-word]# helm status wistful-cricket
LAST DEPLOYED: Thu May 21 20:35:36 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deployment-699787c785-rgd6f 1/1 Running 0 3m18s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.211 <none> 80:46275/TCP 6d22h
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 6d22h
#查看网页内容是否为1
[root@k8s-node1 ~]# curl 10.0.0.211
1
#查看历史版本
[root@k8s-master1 hello-word]# helm history wistful-cricket
REVISION UPDATED STATUS CHART DESCRIPTION
1 Thu May 14 21:48:55 2020 SUPERSEDED nginx-v1 Install complete
2 Thu May 21 20:35:36 2020 DEPLOYED nginx-v1 Upgrade complete
2)更新(2:直接更新镜像)
#查看之前存在的nginx
[root@k8s-master1 nginx]# helm ls nginx
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
nginx 1 Fri May 22 01:42:30 2020 DEPLOYED nginx-v2 default
#使用更新
[root@k8s-master1 nginx]# helm upgrade nginx --set image.tag="v3" .
Release "nginx" has been upgraded. Happy Helming!
LAST DEPLOYED: Fri May 22 01:56:09 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
nginx-deploy-58bd5bd4f7-7g9dw 0/1 ContainerCreating 0 0s
nginx-deploy-6d7bd6648b-5mtcf 1/1 Terminating 0 13m
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc NodePort 10.0.0.137 <none> 80:43898/TCP 13m
==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 0 1 13m
#查看历史版本
[root@k8s-master1 nginx]# helm history nginx
REVISION UPDATED STATUS CHART DESCRIPTION
1 Fri May 22 01:42:30 2020 SUPERSEDED nginx-v2 Install complete
2 Fri May 22 01:56:09 2020 DEPLOYED nginx-v2 Upgrade complete
#查看是否成功
[root@k8s-node1 ~]# curl 10.0.0.137
3
3)回滚
#查看存在的nginx
[root@k8s-master1 nginx]# helm history nginx
REVISION UPDATED STATUS CHART DESCRIPTION
1 Fri May 22 01:42:30 2020 SUPERSEDED nginx-v2 Install complete
2 Fri May 22 01:56:09 2020 DEPLOYED nginx-v2 Upgrade complete
#回滚到1版本(3就是1版本)
[root@k8s-master1 nginx]# helm rollback nginx 1
[root@k8s-master1 nginx]# helm history nginx
REVISION UPDATED STATUS CHART DESCRIPTION
1 Fri May 22 01:42:30 2020 SUPERSEDED nginx-v2 Install complete
2 Fri May 22 01:56:09 2020 SUPERSEDED nginx-v2 Upgrade complete
3 Fri May 22 02:03:55 2020 DEPLOYED nginx-v2 Rollback to 1
#查看是否为2
[root@k8s-node1 ~]# curl 10.0.0.137
2
4)删除
#只删除服务(存在历史保留,但无法访问服务)
[root@k8s-master1 nginx]# helm ls
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
nginx 3 Fri May 22 02:03:55 2020 DEPLOYED nginx-v2 default
[root@k8s-master1 nginx]# helm delete nginx
[root@k8s-master1 nginx]# helm history nginx
REVISION UPDATED STATUS CHART DESCRIPTION
1 Fri May 22 01:42:30 2020 SUPERSEDED nginx-v2 Install complete
2 Fri May 22 01:56:09 2020 SUPERSEDED nginx-v2 Upgrade complete
3 Fri May 22 02:03:55 2020 DELETED nginx-v2 Deletion complete
#在重新启动服务
[root@k8s-master1 nginx]# helm rollback nginx 3
[root@k8s-master1 nginx]# helm ls
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
nginx 4 Fri May 22 02:09:54 2020 DEPLOYED nginx-v2 default
5)完全删除
[root@k8s-master1 nginx]# helm history nginx
REVISION UPDATED STATUS CHART DESCRIPTION
1 Fri May 22 01:42:30 2020 SUPERSEDED nginx-v2 Install complete
2 Fri May 22 01:56:09 2020 SUPERSEDED nginx-v2 Upgrade complete
3 Fri May 22 02:03:55 2020 SUPERSEDED nginx-v2 Deletion complete
4 Fri May 22 02:09:54 2020 DEPLOYED nginx-v2 Rollback to 3
[root@k8s-master1 nginx]# helm del --purge nginx
[root@k8s-master1 nginx]# helm history nginx
Error: release: "nginx" not found