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了

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值