helm入門教學
引言
helm是k8s的包管理工具,使用helm,可以使用更為簡化和系統化的方式對k8s應用進行部署、升級。
helm是CNCF已畢業的專案,社群也是相當活躍的,在
https://
artifacthub。io/
上,能找到很多現成的helm chart,稍作修改就能用到生產環境中,非常方便。
本文會介紹helm的核心概念,並用一個例子幫助大家更直觀的認識helm,大家可以跟著這個例子操作一遍,相信對理解helm會有非常大的作用。
(教程地址:cloud-native-tutorial)
什麼是helm
初識helm
helm在希臘語中的意思是:舵;駕駛盤。據說是helm創始人mutt butcher翻遍了航海手冊找出來的,目的是為了找一個和kubernetes主題相匹配的詞語。
官網地址:
https://
helm。sh/
官方給出的解釋是:Helm is the best way to find, share, and use software built for Kubernetes。 意思是helm是kubernetes中查詢、分享、構建應用的最佳方式。
這種說法當然不算誇張,Helm其實是一個Kubernetes應用的包管理工具,用來管理chart(一種預先配置好的安裝包資源),有點類似於Ubuntu 的APT和CentOS中的YUM。因此,helm的出現解決了k8s應用管理能力缺失的問題。
另外helm也是dev和ops的橋樑,運維人員在使用helm的時候,一方面不需要理解大量在chart中的各種k8s元素,只需要配置少量的環境變數即可安裝;另一方面,helm也給初級運維人員提供了學習的機會,他們可以在chart中學習並理解各種k8s元素,從而能夠更快的掌握k8s。
下面列舉了helm的一些關鍵資訊
2019 年11 月13 日,Helm 3 釋出;
2020 年4 月30 日,從CNCF 中畢業;
目前github上接近有1。9w個Star;
最新版本:3。5。0;
主要作者:Mutt Butcher,目前在Microsoft,主要關注DevOps領域。
helm與apt對比
在我們接觸一個新事物的時候,最好的學習方式就是和我們熟悉的事務來做類比,能夠幫助我們快速的掌握其中的核心概念。
由於Mutt Butcher在設計helm的時候,大量參考了apt和homebrew的設計,這裡我們用apt來做對比,幫助大家更好的理解helm。
從下表中可以看到,helm和apt之間概念的對比,相信熟悉apt的同學能夠很快的對helm有一個初步的認識。
helm總覽
對helm有了一個初步的印象之後,我們來對helm做一個總覽,包括helm的的核心概念、執行流程、常用命令、chart做一個概要性的瞭解,這樣的話,對我們後續深入helm的學習和實踐起一個綱領的作業,避免只見樹木、不見森林。
helm三大概念
chart
:chart就是helm package,包含了一個k8s app應用執行起來的所有要素,比如service, deployment, configmap, serviceaccount, rbac, 等,這些要素都是以template檔案的形式存在,再結合values檔案,最終渲染出能夠被k8s執行的yaml檔案;
repository
:倉庫是charts的集合,方便進行分享和分發。下面是官網倉庫和阿里雲倉庫的地址,大家可以進去看看,感受一下;
https://
artifacthub。io/
https://
developer。aliyun。com/hu
b
release
:release是helm chart在kubernetes的一個執行例項,你可以用不同的release name多次安裝同一個chart,比如:當叢集中需要多個redis例項,你可以使用不同的配置檔案安裝redis chart。
helm執行流程
下面這張圖我建議大家多看幾遍,可以說掌握了這張圖,就掌握了helm的核心。
從下圖可以看到,helm的核心執行流程分為以下幾步:
從chart倉庫中獲取chart;
使用者配置自己的values檔案,根據自己的執行環境對values進行修改;
預設values檔案和使用者values檔案會進行一個merge,形成最終的values檔案;
使用最終的values檔案,渲染chart的template,形成可以被kubernetes執行的yaml;
呼叫kube apply提交yaml到kubernetes
在這裡,需要注意chart開發者和使用者的界限,正是由於在跨越這個界限的時候,從需要理解大量的配置到只需要理解少量的配置,使得ops的工作變得簡便,這也是helm核心的設計哲學。看完整個文章,即使你什麼都沒記住,只記住了這張圖,你也會有很大的收穫了。
helm常用命令
在helm的時候,你可以使用第三方開發的chart,也可以自己開發chart,以下是兩種情況下使用的常見命令。更為詳細的命令,可以安裝好helm之後,使用helm help來檢視,或檢視官方文件。
使用第三方開發的chart
部署前
repo: add, list, remove, update, and index chart repositories
search: search for a keyword in charts
部署後
install: install a chart
list: list releases
status: display the status of the named release
upgrade: upgrade a release
rollback: roll back a release to a previous revision
uninstall: uninstall a release
自己開發chart
lint: examine a chart for possible issues
package: package a chart directory into a chart archive
push: push helm chart to chartmuseum
chart push: push helm chart to OCI repository
chart
chart可以說是helm裡面最重要的概念了,關於chart也有很多內容需要掌握,在這裡做一個列舉。
chart開發
:主要是指利用模板技術開發一個chart,會在後面做詳細介紹;
chart hooks
:在chart的生命週期中,提供一些hooks,方便進行一些前置或後置操作
在chart安裝前,建立應用需要的Secret
在chart安裝前,備份資料庫
在chart解除安裝後,做一些清理工作
chart test
:當你install了一個chart後,如何知道這個release是否執行正常呢?chart test提供了一種測試的方式,來驗證你的應用是否正常執行,比如:
校驗mysql應用能夠正常連線並接受請求
校驗services能夠正常做load balance
library chart
:一種以library形式存在的chart,可以在application chart之間進行共享避免重複邏輯;類似於程式語言中的public library;
chart校驗
:基於PKI、GnuPG等技術,對helm package進行簽名,保證傳輸或釋出過程中的安全性;
OCI(Open Container Initiative,容器髮型規範)支援
:helm 3引入(EXPERIMENTAL),能夠將chart推送到支援OCI的倉庫中,比如harbor, nexus,等,比如,將chart儲存到harbor中:
儲存chart:helm chart save kubeedge/ some-harbor-repo/kubeedge-cloud-chart:1。0。0
登入repo:helm registry login
https://
some-harbor-repo
推送chart:helm chart push some-harbor-repo/kubeedge-cloud-chart:1。0。0
高階特性
post rendering: 提供在helm install之前對manifests進行操作、配置的一種機制;一般結合kustomize使用。比如:
在install時插入sidecar,從而為deployment增加功能
不修改原chart的情況下,更改manifests的配置
這可能不是非常好理解,這裡舉一個例子,假設我需要對一個第三方的chart做少量的修改,以滿足我的部署要求,那麼我可以fork一份這個chart,然後在上面做修改,但是這樣我需要一些額外的維護工作,當這個chart升級之後,我還需要做merge的工作,十分不便。
因此post rendering提供了一種機制,你只需要使用post rendering+kustomize編寫的一個指令碼,就可以在install前,對原始chart的內容做一些定製化的修改,避免了額外的維護工作。如下圖所示。(kustomize是一個開源的配置工具,可以方便的對現有的k8s app配置做一些定製化的修改,官網地址:
https://
kustomize。io/
)
go sdk:helm提供了golang sdk,方便在go語言中使用;
storage backend:指定release資訊的儲存型別,預設是secret;可以指定configmap、secret和postgreSQL;下圖顯示helm的版本資訊存放在secret中的情況。
helm plugin:支援以外掛的形式拓展helm的功能。
helm demo
下面我們一起用一個demo來對helm有一個更為直觀的瞭解。在這個demo中,我們的目標是部署一個redis叢集,包括以下幾個步驟:
安裝helm
使用helm install單節點redis
使用helm upgrade升級到master-slave
使用helm rollback,回滾到單節點模式
步驟1:安裝helm(透過apt)
以下是透過apt安裝helm的命令,其他的安裝方式可參考官網:
curl https://baltocdn。com/helm/signing。asc
|
sudo apt-key add –
sudo apt-get install apt-transport-https ——yes
echo
“deb https://baltocdn。com/helm/stable/debian/ all main”
|
sudo tee /etc/apt/sources。list。d/helm-stable-debian。list
sudo apt-get update
sudo apt-get install
helm
=
3
。4。2-1
步驟2:新增倉庫並找到redis chart
新增倉庫:helm repo add stable
https://
charts。helm。sh/stable
檢視已經新增的倉庫:helm repo list
搜尋倉庫有哪些chart:helm search repo stable
更新倉庫列表到本地:helm repo update
搜尋redis:helm search repo redis
檢視redis chart詳情:helm show chart stable/redis
檢視redis values(values:相當於chart的配置檔案):helm show values stable/redis
步驟3:install/upgrade/rollback/uninstall
建立單master的配置檔案,名為only-master。values
## Cluster settings
cluster
:
enabled
:
false
## Redis pod Security Context
securityContext
:
enabled
:
false
## Use password authentication
usePassword
:
true
## Redis password (both master and slave)
password
:
“admin”
## Redis Master parameters
master
:
persistence
:
enabled
:
false
然後——dry-run一下,看看生成出來的yaml檔案是否存在問題:
helm install redis-demo stable/redis -f 。/only-master。values ——dry-run
如果沒有問題,則進行實際的安裝
helm install redis-demo stable/redis -f 。/only-master。values
安裝成功之後,可以登入redis進行操作,做進一步的校驗:
redis-cli -h
`
kubectl get svc redis-demo-master -o
=
jsonpath
=
“{。spec。clusterIP}”
`
-a admin
set
name zhangsan
get name
info replication
# 可以看到這時候沒有slave連線
建立master-slave配置檔案,名為master-slave。values
## Cluster settings
cluster
:
enabled
:
true
slaveCount
:
1
securityContext
:
enabled
:
false
## Use password authentication
usePassword
:
true
password
:
“admin”
## Mount secrets as files instead of environment variables
usePasswordFile
:
false
## Redis Master parameters
master
:
persistence
:
enabled
:
false
## Redis Slave properties
slave
:
persistence
:
enabled
:
false
——dry-run一下,看看生成出來的yaml檔案是否存在問題;由於在系統中已經有redis-demo的release,因此使用upgrade來進行升級:
helm upgrade redis-demo stable/redis -f 。/master-slave。values ——dry-run helm upgrade redis-demo stable/redis -f 。/master-slave。values
檢查slave是否安裝成功,以及是否同步成功
redis-cli -h `kubectl get svc redis-demo-slave -o=jsonpath=“{。spec。clusterIP}”` -a admin
get name
最後,回滾至單master模式
檢視部署歷史:helm history redis-demo
回滾到對應的單master版本:helm rollback redis-demo REVISION
再連線slave已經不成功了:
redis-cli -h `kubectl get svc redis-demo-slave -o=jsonpath=“{。spec。clusterIP}”` -a admin
只有master能連線:
redis-cli -hkubectl get svc redis-demo-master -o=jsonpath=“{。spec。clusterIP}”` -a admin
# 輸入命令測試一下
get age
解除安裝redis-demo
helm uninstall redis-demo
chart詳解
總體結構
下圖是wordpress的helm chart目錄結構,每個元素的解釋如下:
Chart.yaml
: chart的資訊,包括chart的版本資訊、描述資訊、依賴關係,等
LICENSE
: (可選)chart的LICENSE資訊
README.md
: (可選)chart的說明檔案
values.yaml
: chart的預設配置資訊
values.schema.json
: (可選) values配置資訊的元資訊(欄位型別、欄位描述、欄位之間的依賴等),格式json
charts
: 依賴的其他chart
crds
: Custom Resource Definitions
templates
: 部署模板,結合values。yaml會渲染出kubernetes yaml檔案
templates/NOTES.txt
: (可選)安裝說明
Chart。yaml
Chart。yaml相當於chart的說明書,說明了這個chart的功能、版本、依賴關係、關鍵字等資訊。
下圖是redis的Chart。yaml
apiVersion: chart api version, 目前helm 3固定為v2
name: chart名字
description: chart描述
type:
application: 可部署的chart
library: 不可部署,可在不同chart間共享公用邏輯
version: chart的版本
appVersion: chart內應用的版本,比如redis的版本
kubeVersion: 相容的k8s版本
keywords: 關鍵詞,在helm search中用到
dependencies: 依賴的chart
mantainers: chart的開發人員
icon: chart圖示,會在hub中展示
template
template存放chart中的主要內容,主要以模板的形式存在,template中可以使用變數、控制語句、函式等,功能非常豐富。
chart hub & chart repo
chart hub
:可以理解為chart倉庫的彙總,官方地址:
https://
artifacthub。io/
chart repo
:chart倉庫,存放具體的chart
示例:
https://
charts。bitnami。com/bitn
ami
開源的倉庫:harbor (
https://
goharbor。io/
)(利用helm OCI的支援)
helm官方提供的倉庫:chartmuseum -
https://
github。com/helm/chartmu
seum
chart repo UI
:
https://
github。com/helm/monocul
ar
helm CICD
可以使用helm來構建CICD pipeline,下面是一個CICD的示例:
可以看到,整個CICD分為3個步驟,當然這只是一個例子,開發者可以根據實際的需要對這個步驟進行調整:
stage1
:開發人員編碼、自測通過後,提交feature分支,並觸發pipeline構建,並分別產生具有相同tag的docker image和helm chart,並部署helm chart
stage2
:reviewer review程式碼,測試通過後merge到release分支,修改版本號,併產生new tag的docker image和helm chart,並部署到叢集
stage3
:測試、產品、客戶,在staging上驗證通過後,直接把helm chart部署到生產環境
從helm v2 到 v3 都有哪些變化
從下圖中看到,最大的變化就是移出了tiller,主要原因是k8s早已支援了RBAC,不再需要tiller這麼一個“多餘”的元件來做許可權管理了,另外的幾個修改如下圖所示,要了解細節,請參考官方的文件。
附錄
helm入門教程地址:
https://
github。com/zkf1317/clou
d-native-tutorial
下一篇:孩子親奶奶會不會大過親媽媽?