什么是 Helm
在沒使用 helm 之前,向 kubernetes 部署應用,我們要依次部署 deployment、svc 等,步驟較繁瑣。況且隨
着很多項目微服務化,復雜的應用在容器中部署以及管理顯得較為復雜,helm 通過打包的方式,支持發布的版本
管理和控制,很大程度上簡化了 Kubernetes 應用的部署和管理
Helm 本質就是讓 K8s 的應用管理(Deployment,Service 等 ) 可配置,能動態生成。通過動態生成 K8s 資源清
單文件(deployment.yaml,service.yaml)。然后調用 Kubectl 自動執行 K8s 資源部署
Helm 是官方提供的類似於 YUM 的包管理器,是部署環境的流程封裝。Helm 有兩個重要的概念:chart 和
release
chart 是創建一個應用的信息集合,包括各種 Kubernetes 對象的配置模板、參數定義、依賴關系、文檔說
明等。chart 是應用部署的自包含邏輯單元。可以將 chart 想象成 apt、yum 中的軟件安裝包
release 是 chart 的運行實例,代表了一個正在運行的應用。當 chart 被安裝到 Kubernetes 集群,就生成
一個 release。chart 能夠多次安裝到同一個集群,每次安裝都是一個 release
Helm 包含兩個組件:Helm 客戶端和 Tiller 服務器,如下圖所示
Helm 客戶端負責 chart 和 release 的創建和管理以及和 Tiller 的交互。Tiller 服務器運行在 Kubernetes 集群
中,它會處理 Helm 客戶端的請求,與 Kubernetes API Server 交互
Helm 部署
越來越多的公司和團隊開始使用 Helm 這個 Kubernetes 的包管理器,我們也將使用 Helm 安裝 Kubernetes 的常用
組件。 Helm 由客戶端命 helm 令行工具和服務端 tiller 組成,Helm 的安裝十分簡單。 下載 helm 命令行工具到
master 節點 node1 的 /usr/local/bin 下,這里下載的 2.15. 2版本:
[root@k8s-master helm]# ll 總用量 58188 -rw-r--r-- 1 root root 22949819 1月 5 20:58 helm-v2.13.1-linux-amd64.tar.gz -rw-r--r-- 1 root root 24525846 1月 5 22:46 helm-v2.15.2-linux-amd64.tar.gz -rw-r--r-- 1 root root 12101232 1月 5 22:10 helm-v3.0.2-linux-amd64.tar.gz drwxr-xr-x 2 root root 64 10月 30 04:53 linux-amd64 -rw-r--r-- 1 root root 354 1月 5 22:39 rbac.yaml drwxr-xr-x 3 root root 60 1月 6 17:44 test [root@k8s-master helm]#
tar -zxvf helm-v2.15.2-linux-amd64.tar.gz cd linux-amd64/ cp helm /usr/local/bin/
[root@k8s-master helm]# cat 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-master helm]#
為了安裝服務端 tiller,還需要在這台機器上配置好 kubectl 工具和 kubeconfig 文件,確保 kubectl 工具可以
在這台機器上訪問 apiserver 且正常使用。 這里的 node1 節點以及配置好了 kubectl
因為 Kubernetes APIServer 開啟了 RBAC 訪問控制,所以需要創建 tiller 使用的 service account: tiller 並分
配合適的角色給它。 詳細內容可以查看helm文檔中的 Role-based Access Control。 這里簡單起見直接分配
cluster- admin 這個集群內置的 ClusterRole 給它。創建 rbac.yaml 文件.
kubectl create -f rbac.yaml helm init --service-account tiller --skip-refresh
https://www.cnblogs.com/dalianpai/p/12154410.html,初始化遇到問題,參照我這篇博客。
tiller 默認被部署在 k8s 集群中的 kube-system 這個namespace 下
[root@k8s-master helm]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-58cc8c89f4-9gn5g 1/1 Running 12 17d coredns-58cc8c89f4-xxzx7 1/1 Running 12 17d etcd-k8s-master 1/1 Running 14 17d kube-apiserver-k8s-master 1/1 Running 14 17d kube-controller-manager-k8s-master 1/1 Running 24 17d kube-flannel-ds-amd64-4bc88 1/1 Running 16 17d kube-flannel-ds-amd64-lzwd6 1/1 Running 18 17d kube-flannel-ds-amd64-vw4vn 1/1 Running 16 17d kube-proxy-bs8sd 1/1 Running 13 17d kube-proxy-nfvtt 1/1 Running 12 17d kube-proxy-rn98b 1/1 Running 14 17d kube-scheduler-k8s-master 1/1 Running 21 17d tiller-deploy-7476769959-pns9q 1/1 Running 1 21h [root@k8s-master helm]# helm version Client: &version.Version{SemVer:"v2.15.2", GitCommit:"8dce272473e5f2a7bf58ce79bb5c3691db54c96b", GitTreeState:"clean"} Server: &version.Version{SemVer:"v2.15.2", GitCommit:"8dce272473e5f2a7bf58ce79bb5c3691db54c96b", GitTreeState:"clean"} [root@k8s-master helm]#
Helm 自定義模板
[root@k8s-master helm]# mkdir test [root@k8s-master helm]# cd test/ [root@k8s-master test]# ll 總用量 0 [root@k8s-master test]# cat <<'EOF' > ./Chart.yaml > name: hello-world > version: 1.0.0 > EOF [root@k8s-master test]# ll 總用量 4 -rw-r--r-- 1 root root 33 1月 6 16:43 Chart.yaml [root@k8s-master test]# mkdir ./templates [root@k8s-master test]# ll 總用量 4 -rw-r--r-- 1 root root 33 1月 6 16:43 Chart.yaml drwxr-xr-x 2 root root 6 1月 6 16:44 templates [root@k8s-master helm]# cd test [root@k8s-master test]# ll 總用量 8 -rw-r--r-- 1 root root 33 1月 6 16:43 Chart.yaml drwxr-xr-x 2 root root 49 1月 6 17:44 templates -rw-r--r-- 1 root root 53 1月 6 17:29 values.yaml [root@k8s-master test]# cd templates [root@k8s-master templates]# ll 總用量 8 -rw-r--r-- 1 root root 407 1月 6 17:42 deployment.yaml -rw-r--r-- 1 root root 174 1月 6 16:53 service.yaml [root@k8s-master templates]# cat deployment.yaml 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: wangyanglinux/myapp:v1 ports: - containerPort: 80 protocol: TCP [root@k8s-master templates]# cat service.yaml apiVersion: v1 kind: Service metadata: name: hello-world spec: type: NodePort ports: - port: 80 targetPort: 80 protocol: TCP selector: app: hello-world [root@k8s-master templates]#
root@k8s-master test]# helm install . NAME: callous-duck LAST DEPLOYED: Mon Jan 6 16:57:05 2020 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE hello-world 0/1 1 0 0s ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE hello-world-64f7589d8c-jthnw 0/1 ContainerCreating 0 0s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world NodePort 10.104.65.50 <none> 80:30644/TCP 0s [root@k8s-master test]# helm list NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE callous-duck 1 Mon Jan 6 16:57:05 2020 DEPLOYED hello-world-1.0.0 default [root@k8s-master test]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-world-64f7589d8c-jthnw 1/1 Running 0 97s [root@k8s-master test]# helm list NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE callous-duck 1 Mon Jan 6 16:57:05 2020 DEPLOYED hello-world-1.0.0 default
# 列出已經部署的 Release $ helm ls # 查詢一個特定的 Release 的狀態 $ helm status RELEASE_NAME # 移除所有與這個 Release 相關的 Kubernetes 資源 $ helm delete cautious-shrimp # helm rollback RELEASE_NAME REVISION_NUMBER $ helm rollback cautious-shrimp 1 # 使用 helm delete --purge RELEASE_NAME 移除所有與指定 Release 相關的 Kubernetes 資源和所有這個 Release 的記錄 $ helm delete --purge cautious-shrimp $ helm ls --deleted
[root@k8s-master test]# cd templates/ [root@k8s-master templates]# ll 總用量 12 -rw-r--r-- 1 root root 432 1月 6 17:25 deployment.yaml -rw-r--r-- 1 root root 174 1月 6 16:53 service.yaml -rw-r--r-- 1 root root 54 1月 6 17:26 values.yaml [root@k8s-master templates]# vim values.yaml [root@k8s-master templates]# vim deployment.yaml [root@k8s-master templates]# cd .. [root@k8s-master test]# ll 總用量 4 -rw-r--r-- 1 root root 33 1月 6 16:43 Chart.yaml drwxr-xr-x 2 root root 68 1月 6 17:31 templates [root@k8s-master test]# helm upgrade callous-duck . UPGRADE FAILED Error: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository Error: UPGRADE FAILED: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository [root@k8s-master test]# cat templates/values.yaml image: repository: wangyanglinux/myapp tag: 'v2' [root@k8s-master test]# cat templates/deployment.yaml 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 [root@k8s-master test]# helm lint ==> Linting . [ERROR] Chart.yaml: directory name (test) and chart name (hello-world) must be the same [ERROR] Chart.yaml: apiVersion is required [INFO] Chart.yaml: icon is recommended [INFO] values.yaml: file does not exist [ERROR] templates/: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository Error: 1 chart(s) linted, 1 chart(s) failed [root@k8s-master test]# cd templates/ [root@k8s-master templates]# ll 總用量 12 -rw-r--r-- 1 root root 407 1月 6 17:31 deployment.yaml -rw-r--r-- 1 root root 174 1月 6 16:53 service.yaml -rw-r--r-- 1 root root 53 1月 6 17:29 values.yaml [root@k8s-master templates]# vim deployment.yaml [root@k8s-master templates]# mv values.yaml ../ [root@k8s-master templates]# ll 總用量 8 -rw-r--r-- 1 root root 407 1月 6 17:42 deployment.yaml -rw-r--r-- 1 root root 174 1月 6 16:53 service.yaml [root@k8s-master templates]# cd .. [root@k8s-master test]# ll 總用量 8 -rw-r--r-- 1 root root 33 1月 6 16:43 Chart.yaml drwxr-xr-x 2 root root 49 1月 6 17:44 templates -rw-r--r-- 1 root root 53 1月 6 17:29 values.yaml [root@k8s-master test]# history [root@k8s-master test]# helm upgrade callous-duck . Release "callous-duck" has been upgraded. LAST DEPLOYED: Mon Jan 6 17:53:41 2020 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE hello-world 1/1 0 1 56m ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE hello-world-64f7589d8c-jthnw 1/1 Running 0 56m hello-world-77cbb5cd7d-zljd7 0/1 ContainerCreating 0 1s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world NodePort 10.104.65.50 <none> 80:30644/TCP 56m [root@k8s-master test]# helm upgrade callous-duck --set image.tag='v3' . Release "callous-duck" has been upgraded. LAST DEPLOYED: Mon Jan 6 17:54:56 2020 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE hello-world 1/1 1 1 57m ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE hello-world-76d75cfc8f-7hq2j 0/1 ContainerCreating 0 0s hello-world-77cbb5cd7d-zljd7 1/1 Running 0 75s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world NodePort 10.104.65.50 <none> 80:30644/TCP 57m