温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Helm3的实际使用与开发

发布时间:2021-09-01 12:39:00 来源:亿速云 阅读:170 作者:chen 栏目:云计算

这篇文章主要介绍“Helm3的实际使用与开发”,在日常操作中,相信很多人在Helm3的实际使用与开发问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Helm3的实际使用与开发”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用

现在介绍一下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了

到此,关于“Helm3的实际使用与开发”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI