Helm3的使用與開發

Helm是Kubernetes的軟件包(或資源)管理工具,最近發佈了Helm的3.0版本,提供了一些新特性,在使用上相比之前的版本更加簡單、方便,比如:

  • 移除Tiller,安裝chart前無需執行init命令(包括安裝tiller和初始化本地倉庫),相對地也不再需要與Tiller交互,而是直接通過ApiServer進行安裝操作
  • 支持使用JSONSchema校驗values
  • 兼容v2版本

使用

現在介紹一下v3版本的簡單使用。

首先我們可以從github上下載3.x.x版本的相應二進制文件,將helm程序放到PATH目錄下即可。

執行以下命令,簡單驗證一下程序的可用性:

$helm --help
The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

Environment variables:

+------------------+-----------------------------------------------------------------------------+
| Name             | Description                                                                 |
+------------------+-----------------------------------------------------------------------------+
| $XDG_CACHE_HOME  | set an alternative location for storing cached files.                       |
| $XDG_CONFIG_HOME | set an alternative location for storing Helm configuration.                 |
| $XDG_DATA_HOME   | set an alternative location for storing Helm data.                          |
| $HELM_DRIVER     | set the backend storage driver. Values are: configmap, secret, memory       |
| $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                  |
| $KUBECONFIG      | set an alternative Kubernetes configuration file (default "~/.kube/config") |
+------------------+-----------------------------------------------------------------------------+

......

可以看到helm的命令參數以及配置變量。

上文已經提到v3版本不再需要Tiller,而是通過ApiServer與k8s交互,可以設置環境變量KUBECONFIG來指定存有ApiServre的地址與token的配置文件地址,默認爲~/.kube/config,網上已有很多教程講解如何配置,這裏不再贅述。

在配置完環境變量KUBECONFIG及配置文件後,即可正常使用:

測試安裝:

# 生成chart文件
$helm create foo
Creating foo

# 打包
$helm package foo
Successfully packaged chart and saved it to: /home/test/helm/foo-0.1.0.tgz

# 安裝
$helm install foo ./foo-0.1.0.tgz
NAME: foo
LAST DEPLOYED: Sat Dec  7 21:05:33 2019
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=foo,app.kubernetes.io/instance=foo" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

# 查詢release
$helm ls
NAME	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART    	APP VERSION
foo 	default  	1       	2019-12-07 21:05:33.355624435 +0800 CST	deployed	foo-0.1.0	1.16.0     

# 刪除release
$helm delete foo
release "foo" uninstalled

repo相關操作:

# 添加倉庫
$helm repo add {倉庫名字} {倉庫地址}
"{倉庫名字}" has been added to your repositories

# 查詢倉庫列表
$helm repo list
NAME  	    URL                                                            
{倉庫名字}	{倉庫地址}

# 查詢chart包
$helm search repo

# 刪除倉庫
$helm repo remove {倉庫名字}
"{倉庫名字}" has been removed from your repositories

helm的其他命令可以通過help中的介紹進行查看,網上也有很多相關介紹,接下來介紹一下如何基於helm api進行二次開發

基於Helm開發

helm同其他k8s組件一樣使用golang編寫,如果你是個golang的初學者,並且有着k8s的運維經驗,helm源碼完全可以作爲你深入理解k8s設計理念的途徑之一。

因爲helm是個客戶端類的程序,代碼條理、層次分明,很少使用golang的特性(比如goroutine),所以源碼閱讀起來不會太繞。

在看了helm部分源碼後,我總結了以下幾點:

  • 對於golang初學者,通過閱讀helm源碼學習go語言,以及如何像helm一樣優雅的使用golang開發命令交互類應用程序
  • 對於k8s運維人員,可以基於helm開發一些針對k8s的運維工具,提高運維效率
  • 瞭解helm、chart設計思路,學習其中的設計理念

用料

長話短說,第一步我們當然要準備環境,需要安裝以下程序:

  • golang version >= 1.11
  • git bash
  • go ide (goland/vs code)

helm使用go mod作爲包管理工具,建議安裝golang的版本大於1.11,因爲此版以後已內置go mod,類似於java中的maven,方便下載、管理依賴包。

配置go mod代理

由於國內環境下載依賴相對較慢,需要設置go mod代理進行加速,這裏我使用的是https://goproxy.io/

配置以下環境變量:

# Enable the go modules feature
export GO111MODULE=on
# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io

創建項目

可以在{gopath}/src下直接git clone我在github上的示例項目

或者自己新建項目,並在go.mod文件中引入helm依賴即可:

require (
    ...
	helm.sh/helm/v3 v3.0.0
	...
)

replace (
	// github.com/Azure/go-autorest/autorest has different versions for the Go
	// modules than it does for releases on the repository. Note the correct
	// version when updating.
	github.com/Azure/go-autorest/autorest => github.com/Azure/go-autorest/autorest v0.9.0
	github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309

	// Kubernetes imports github.com/miekg/dns at a newer version but it is used
	// by a package Helm does not need. Go modules resolves all packages rather
	// than just those in use (like Glide and dep do). This sets the version
	// to the one oras needs. If oras is updated the version should be updated
	// as well.
	github.com/miekg/dns => github.com/miekg/dns v0.0.0-20181005163659-0d29b283ac0f
	gopkg.in/inf.v0 v0.9.1 => github.com/go-inf/inf v0.9.1
	gopkg.in/square/go-jose.v2 v2.3.0 => github.com/square/go-jose v2.3.0+incompatible

	rsc.io/letsencrypt => github.com/dmcgowan/letsencrypt v0.0.0-20160928181947-1847a81d2087
)

其中replace配置必不可少,它可以替換掉一些找不到的依賴,例如

docker項目已經改名成moby,所以通過replace配置將github.com/docker/docker替換成github.com/moby/moby

下載依賴

執行以下命令,將依賴包放置於項目下的vendor目錄中:

go mod vendor

待下載成功後,即可開始我們的開發之旅了

helm源碼結構及開發思路

├── cmd/         // helm的cli代碼
├── pkg/         // helm的api代碼,我們真正需要關注的
├── script/      // 一些腳本
......

helm使用cobra開發命令交互功能(形如helm list [flags]),相關代碼均處於cmd目錄下。

比如安裝命令install定義在cmd/helm/install.go文件中,它會調用pkg/action/install.go中的Run()方法執行安裝操作。

helm的每條命令都可以在pkg目錄下找到對應的api方法,弄明白這些方法的參數意義,即可完全使用到helm的能力了。

是不是挺簡單的,隨後我們就可以使用helm的原生能力來靈活地開發自己的程序,包括:

  • 打包
  • 校驗格式
  • 重建索引
  • 安裝chart
  • 查詢release

用這些能力,你就可以像helm/monocular一樣開發一個自己風格的chart可視化UI了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章