Kubernetes包管理:Helm2 升级 Helm3

Helm是一个非常常用的K8s应用包管理工具,负责云原生应用的安装部署和生命周期管理。

Helm2

Helm2有两个主要的组件:

  • Tiller: helm的服务端,部署在k8s里面的一个pod,通常在kube-system这个系统空间里。主要负责部署helm charts,管理release,跟k8s API通信。
  • Helm Client: 主要负责从共有或者私有helm charts仓库拉取chart包,修改变量值,然后直接扔给tiller。

Helm2的问题

Helm2的一个主要问题是需要在k8s集群里面运行一个服务端,而这就需要把tiller的端口暴露给外界,会产生安全隐患。

在helm 2中引入的tiller主要是当时k8s还没有RBAC机制,所以就引入了服务端tiller。

而后来k8s的功能相应完善,加入了RBAC和CRD等,都使得tiller这个东西显得多余。

Helm3

helm3只有一个客户端,没有服务端,所以安装起来很方便,把相应的程序下下来即可,不需要helm init安装了。

相对于helm2,helm3有几大特性:

  • 移除了tiller
  • 支持分布式helm hub, 有了它就可以在很多时候不需要手动添加非官方repo了,例如helm3 search hub <package name>
  • 为chart输入值进行json schema验证。
  • 可以给helm charts添加test了,通过helm test <release>就能针对部署的应用跑一些tests。
  • 部署的时候release name必须指定了,helm2的时候不指定会自动生成一个。
  • 删除的时候不需要--purge了,删了就是删了。

Helm2到Helm3的迁移

helm官方提供了一个小工具,帮助我们把已经部署的helm2应用迁移到helm3上。

  • 安装插件
helm3 plugin install https://github.com/helm/helm-2to3

备注:github如果被墙了的话可以选择源码安装:

$ mkdir -p ${GOPATH}/src/github.com/helm
$ cd $_
$ git clone git@github.com:helm/helm-2to3.git
$ cd helm-2to3
$ make build
$ export HELM_LINTER_PLUGIN_NO_INSTALL_HOOK=true
$ helm plugin install <your_path>/helm-2to3
  • 迁移helm2的配置,例如仓库
helm3 2to3 move config
  • 迁移helm2部署的应用(确保helm2和helm3同时安装在同一台机器上)

现在我们准备好,开始迁移releases了。

让我们首先来检查可用的选项:

$ helm3 2to3 convert -h
migrate Helm v2 release in-place to Helm v3

Usage:
    2to3 convert [flags] RELEASE

Flags:
        --delete-v2-releases    v2 releases are deleted after migration. By default, the v2 releases are retained
        --dry-run               simulate a convert
    -h, --help                  help for convert
    -l, --label                 string label to select tiller resources by (default "OWNER=TILLER")
    -s, --release-storage       string v2 release storage type/object. It can be 'secrets' or 'configmaps'. This is only used with the 'tiller-out-cluster' flag (default "secrets")
    -t, --tiller-ns             string namespace of Tiller (default "kube-system")
        --tiller-out-cluster    when Tiller is not running in the cluster e.g. Tillerless

让我们列出Helm v2的releases,并从中挑选一个用于我们的迁移测试:

$ helm list

NAME      REVISION    UPDATED                  STATUS   CHART             APP VERSION   NAMESPACE
postgres  1           Wed Sep 11 14:52:32 2019 DEPLOYED postgresql-6.3.5  11.5.0        postgres
redis     1           Wed Sep 11 14:52:57 2019 DEPLOYED redis-9.1.7       5.0.5         redis

为了保证安全,我们先使用–dry-run 标志:

$ helm3 2to3 convert --dry-run postgres
NOTE: This is in dry-run mode, the following actions will not be executed.
Run without --dry-run to take the actions described below:

Release "postgres" will be converted from Helm 2 to Helm 3.
[Helm 3] Release "postgres" will be created.
[Helm 3] ReleaseVersion "postgres.v1" will be created.

现在荣我们执行真正的迁移工作:

$ helm3 2to3 convert postgres
Release "postgres" will be converted from Helm 2 to Helm 3.
[Helm 3] Release "postgres" will be created.
[Helm 3] ReleaseVersion "postgres.v1" will be created.
[Helm 3] ReleaseVersion "postgres.v1" created.
[Helm 3] Release "postgres" created.
Release "postgres" was converted successfully from Helm 2 to Helm 3. Note: the v2 releases still remain and should be removed to avoid conflicts with the migrated v3 releases.

检查是否迁移成功:

$ helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
postgres 1 Wed Sep 11 14:52:32 2019 DEPLOYED postgresql-6.3.5 11.5.0 postgres
redis 1 Wed Sep 11 14:52:57 2019 DEPLOYED redis-9.1.7 5.0.5 redis

$ helm3 list
NAME NAMESPACE REVISION UPDATED STATUS CHART
postgres postgres 1 2019-09-11 12:52:32.529413 +0000 UTC deployed postgresql-6.3.5

注意:由于我们并没有指定 --delete-v2-releases 标志,所以Helm v2 的postgres release仍然保留了下来,以后我们可以使用kubectl命令来删除它。

迁移时直接删除helm2 release。

helm3 2to3 convert <release-name> --delete-v2-releases

当你已经准备好了迁移你所有的releases时,你可以在一个循环中自动运行它,以应用helm3 2to3 转换每一个Helm v2对应的release。

最后一步是清除helm2的数据,这一步不是必须的,但是是强烈建议的。helm3 2to3 cleanup

参考:https://zhuanlan.zhihu.com/p/105903477

参考:https://www.jianshu.com/p/08f2a82f0756

参考:https://blog.csdn.net/weixin_44738411/article/details/100868358

posted @ 2020-11-17 11:49  人艰不拆_zmc  阅读(1246)  评论(0编辑  收藏  举报