一、 helm 是什麼
0.問題引入
1.概念
- chart: 包含了一組Kubernetes應用創建的必要信息(應用yaml畫像)
- value:包含了應用發佈配置信息 (應用配置)
- release:一組 chart 及其配置的一個運行實例 (應用實例)
2.作用:
- Kubernetes生態系統中的軟件包管理工具
相當於Kubernetes環境下的yum包管理工具
3.用途–類比和架構
- 創建新的chart
- 構建chart tgz包
- 上傳chart到helm倉庫;從helm倉庫下載chart
- 在kubernetes集羣中安裝或卸載chart
- 管理用helm安裝應用的發佈週期
4.組成
- Helm Client
- Tiller Server
client客戶端管理 charts,Tiller服務端負責管理release
二、安裝helm
0.前提條件
- 本地kubectl 能正常訪問kube-api-server
helm client 默認會讀 ~/.kube/config
1.安裝helm客戶端
wget https://get.helm.sh/helm-v2.16.1-linux-amd64.tar.gz
tar -zxvf helm-v2.0.0-linux-amd64.tgz
mv linux-amd64/helm /usr/local/bin/helm
- 需要其他版本可查看:Helm Realese
2.安裝Tiller Server
helm init --upgrade --tiller-image sapcc/tiller:v2.16.1
卸載Tiller Server執行:
helm reset
驗證:等tiller pod running 執行 helm version
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-u1nMPv5u-1593951368142)(/download/attachments/134426261/image.png?version=1&modificationDate=1587545627887&api=v2 ‘image.png’)]
**注意:**儘量helm client和tiller server 版本一致,不然可能會遇到神奇的問題
3.給Tiller 授權
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
驗證:部署一組標準helm應用
helm install --name redis --set rbac.create=true stable/redis #部署一組redis服務
helm list #查看部署列表
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-mgs5YM2E-1593951368144)(/download/attachments/134426261/image.png?version=3&modificationDate=1587546992761&api=v2 ‘image.png’)]
刪除驗證應用:
helm delete --purge redis
三、寫一個簡單的helm應用
1.helm create 項目名
git clone https://github.com/zhaohuabing/testapi.git && cd testapi
helm create testapi-chart
testapi-chart
├── charts
├── Chart.yaml # 這個chart的描述
├── .helmignore # 指定不想包含在 helm chart 中的文件列表
├── templates # 項目的Go模板文件
│ ├── deployment.yaml ## deployment的基本manifest
│ ├── _helpers.tpl ## 模板助手(子模板)
│ ├── ingress.yaml ## ingress的基本manifest
│ ├── NOTES.txt ## 幫助文本(helm install 顯示給用戶的部署信息)
│ ├── serviceaccount.yaml ## serviceaccount的基本manifest
│ ├── service.yaml ## service的基本manifest
│ └── tests ## test套件(檢測chart應用是否符合預期)
│ └── test-connection.yaml ### 簡單的鏈接測試
└── values.yaml # 模板用到的配置變量
2.上傳chart至helm倉庫(基於騰訊雲)
<1>. 創建和添加helm倉庫
- 創建一個私有的tcr, 並開啓公網訪問
- 給本地添加helm chart 倉庫
helm repo add mychartrepo https://attlee.cloudcr.com/chartrepo/$namesapce --username 101010 --password [實例臨時密碼]
helm repo update
helm repo list ## 查看是否成功
<2>. 上傳chart應用
- 下載helm push 插件
helm plugin install https://github.com/chartmuseum/helm-push
- 上傳chart 文件
helm push ./mychart mychartrepo
- 上傳chart tgz 包
helm package ${chart-name} --version ${version} -d ./mychart
helm push mychart-1.2.0.tgz mychartrepo
<3>. 下載和安裝chart應用
helm fetch mychartrepo/mychart --version 1.2.0
helm install -f values.yaml mychartrepo/mychart --version 1.2.0
<4>. 升級和回滾chart應用
helm upgrade mychart mychartrepo/mychart --version 1.2.0 ## 升級chart到指定版本
helm history testapi ## 查看chart發佈歷史
helm rollback testapi 2 ## 回滾chart應用到指定歷史