溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Helm3的實際使用與開發(fā)

發(fā)布時間:2021-09-01 12:39:00 來源:億速云 閱讀:185 作者:chen 欄目:云計算

這篇文章主要介紹“Helm3的實際使用與開發(fā)”,在日常操作中,相信很多人在Helm3的實際使用與開發(fā)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Helm3的實際使用與開發(fā)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

使用

現(xiàn)在介紹一下v3版本的簡單使用。

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

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

$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的命令參數(shù)以及配置變量。

上文已經(jīng)提到v3版本不再需要Tiller,而是通過ApiServer與k8s交互,可以設(shè)置環(huán)境變量KUBECONFIG來指定存有ApiServre的地址與token的配置文件地址,默認(rèn)為~/.kube/config,網(wǎng)上已有很多教程講解如何配置,這里不再贅述。

在配置完環(huán)境變量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相關(guān)操作:

# 添加倉庫
$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中的介紹進(jìn)行查看,網(wǎng)上也有很多相關(guān)介紹,接下來介紹一下如何基于helm api進(jìn)行二次開發(fā)

基于Helm開發(fā)

helm同其他k8s組件一樣使用golang編寫,如果你是個golang的初學(xué)者,并且有著k8s的運(yùn)維經(jīng)驗,helm源碼完全可以作為你深入理解k8s設(shè)計理念的途徑之一。

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

在看了helm部分源碼后,我總結(jié)了以下幾點:

  • 對于golang初學(xué)者,通過閱讀helm源碼學(xué)習(xí)go語言,以及如何像helm一樣優(yōu)雅的使用golang開發(fā)命令交互類應(yīng)用程序

  • 對于k8s運(yùn)維人員,可以基于helm開發(fā)一些針對k8s的運(yùn)維工具,提高運(yùn)維效率

  • 了解helm、chart設(shè)計思路,學(xué)習(xí)其中的設(shè)計理念

用料

長話短說,第一步我們當(dāng)然要準(zhǔn)備環(huán)境,需要安裝以下程序:

  • golang version >= 1.11

  • git bash

  • go ide (goland/vs code)

helm使用go mod作為包管理工具,建議安裝golang的版本大于1.11,因為此版以后已內(nèi)置go mod,類似于java中的maven,方便下載、管理依賴包。

配置go mod代理

由于國內(nèi)環(huán)境下載依賴相對較慢,需要設(shè)置go mod代理進(jìn)行加速,這里我使用的是https://goproxy.io/

配置以下環(huán)境變量:

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

創(chuàng)建項目

可以在{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項目已經(jīng)改名成moby,所以通過replace配置將github.com/docker/docker替換成github.com/moby/moby

下載依賴

執(zhí)行以下命令,將依賴包放置于項目下的vendor目錄中:

go mod vendor

待下載成功后,即可開始我們的開發(fā)之旅了

helm源碼結(jié)構(gòu)及開發(fā)思路

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

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

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

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

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

  • 打包

  • 校驗格式

  • 重建索引

  • 安裝chart

  • 查詢release

  • ......

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

到此,關(guān)于“Helm3的實際使用與開發(fā)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI