1. 程式人生 > >Kubernetes 編排神器之 Helm

Kubernetes 編排神器之 Helm

什麼是Kubernetes Helm?為什麼要使用Helm?

前言

編寫一堆Kubernetes配置檔案是一件很麻煩的事情。對於一些容器,我們可能需要10多個yaml檔案。維護它們是一個問題,而且在不同的環境中執行或使用相同的檔案更是是一個噩夢。
我們可以使用一些 bash 技巧來替換某些值,但這是一個不好的做法。
這就是我們為什麼要使用helm。
我應該提到,還有另一個很好的工具ksonnet,它以自己的方式進行“相同”操作。
在這篇文章中,我將介紹為什麼Helm是Kubernetes應用程式必不可少的要素,將Kubernetes應用程式與Helm打包的過程,以及如何使用Helm部署可能具有的某些複雜應用程式。

為什麼要使用helm

我最近在部署的微服務很複雜,我的釋出檔案目錄中有65個以上的Kubernetes配置檔案 ... o(^▽^)┛)。
主要問題是,我要如何把這個服務部署到多個環境中?
或者如何使用Kubernetes製作CI/CD?
當然做一些shell指令碼是一個選擇,但是我不是很喜歡這樣做。
然後,我開始使用Kubernetes研究CI/CD pipline,發現一些團隊正在將Helm整合到該過程中。

我們可以將理解為為像應用程式包那樣的應用程式,在其中我們可以進行依賴管理,不同型別的鉤子(安裝前,升級前,安裝後等),並且可以輕鬆升級或回滾。

安裝

  • 選擇一個你需要安裝的版本 https://github.com/helm/helm/releases
  • 解壓 tar -zxvf helm-v3.0.0-linux-amd64.tar.gz
  • 找到解壓目錄中的二進位制檔案,把它移動到你的系統變數目錄中 例如 mv linux-amd64/helm /usr/local/bin/helm
[root@localhost helm-test]# helm init
Creating /root/.helm
Creating /root/.helm/repository
Creating /root/.helm/repository/cache
Creating /root/.helm/repository/local
Creating /root/.helm/plugins
Creating /root/.helm/starters
Creating /root/.helm/cache/archive
Creating /root/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /root/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
[root@localhost helm-test]#

檢視kubernetes kube-system的namespace下的pods

[root@localhost test-app]# kubectl get pods --namespace=kube-system
NAME                                            READY   STATUS             RESTARTS   AGE
coredns-58cc8c89f4-q7lgg                        1/1     Running            4          40d
coredns-58cc8c89f4-wdqqx                        1/1     Running            4          40d
etcd-localhost.localdomain                      1/1     Running            4          40d
kube-apiserver-localhost.localdomain            1/1     Running            4          40d
kube-controller-manager-localhost.localdomain   1/1     Running            4          40d
kube-proxy-gt72b                                1/1     Running            4          40d
kube-scheduler-localhost.localdomain            1/1     Running            4          40d
tiller-deploy-58f57c5787-t2b7w                  0/1     ImagePullBackOff   0          22m
weave-net-qdr2l                                 2/2     Running            8          40d
[root@localhost test-app]#

會發現tiller-deploy-*正在啟動.

建立示例

[root@localhost helm-test]# helm create test-app
Creating test-app
  • 建立完成後目錄結構如下
[root@localhost helm-test]# tree test-app/
test-app/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 9 files
[root@localhost helm-test]#
  • Chart.yaml:這是包含對圖表的描述的主檔案
  • values.yaml:這是包含圖表預設值的檔案
  • templates: 這是Kubernetes資源定義為模板的目錄
  • charts:這是一個可選目錄,可能包含子圖表
%重點%

正像我們看到的一樣,所有templates 資料夾中Kubernetes配置檔案都是模板。
你可以使用 Chart.yaml 檔案來描述當前的專案,並且可以對它進行版本控制。
我們只需要一個檔案,用於配置應用程式,並在values.yaml中儲存所有值。

執行 :

⚡ helm install --name test test-app/

這樣我們第一個helm的demo就執行成功了.

另,出現以下錯誤,代表podtiller-deploy-*未啟動成功:

Error: could not find a ready tiller pod

詳細資料參見 https://helm.sh/docs/ 官方文