什么是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/ 官方文檔