生产环境中helm v2升级v3版本遇到的疑难杂症

前言

Helm V3V2 版本架构变化较大,数据迁移也比较麻烦,官方为了解决数据迁移问题,提供一个 helm-2to3 工具,本文基于 helm-2to3 工具来迁移 V2 版本中的数据。

Helm V3V2 变化,请参考 Helm v3 新的功能

注意:Helm V2 升级 V3 版本,Kubernetes 集群中 Deployment、Service、Pod等都不会重新创建,所以迁移过程是不会影响线上在跑的服务。

安装 Helm V3 命令

下载 helm 最新 v3.2.3 版本

$ wget https://get.helm.sh/helm-v3.2.3-linux-amd64.tar.gz -O /tmp/helm-v3.2.3-linux-amd64.tar.gz

解压并移动到 /usr/local/bin/ 目录下

# 解压
$ tar xf /tmp/helm-v3.2.3-linux-amd64.tar.gz

# 移动
$ cd /tmp/linux-amd64
$ mv helm /usr/local/bin/helm3

安装 2to3 插件

一键安装

$ helm3 plugin install https://github.com/helm/helm-2to3

检查 2to3 插件是否安装成功

$ helm3 plugin list

NAME    VERSION DESCRIPTION
2to3    0.5.1   migrate and cleanup Helm v2 configuration and releases in-place to Helm v3

迁移 Helm V2 配置

下面操作主要迁移:

  • Helm 插件
  • Chart 仓库
  • Chart starters
$ helm3 2to3 move config

检查 repoplugin

# 检查 repo
$ helm3 repo list

NAME        URL
stable      https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

# 更新 repo
$ helm3 repo update

Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository

# 检查 plugin
$ helm3 plugin list

NAME    VERSION DESCRIPTION
2to3    0.5.1   migrate and cleanup Helm v2 configuration and releases in-place to Helm v3

迁移 Heml V2 Release

查看 2to3 插件中 convert 子命令选项

$ helm3 2to3 convert --help

migrate Helm v2 release in-place to Helm v3

Usage:
  2to3 convert [flags] RELEASE

Flags:
      --delete-v2-releases         v2 release versions are deleted after migration. By default, the v2 release versions are retained
      --dry-run                    simulate a command
  -h, --help                       help for convert
      --kube-context string        name of the kubeconfig context to use
      --kubeconfig string          path to the kubeconfig file
  -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")
      --release-versions-max int   limit the maximum number of versions converted per release. Use 0 for no limit (default 10)
  -t, --tiller-ns string           namespace of Tiller (default "kube-system")
      --tiller-out-cluster         when  Tiller is not running in the cluster e.g. Tillerless
  • --dry-run:模拟迁移但不做真实迁移操作,建议每次迁移都先带上这个参数测试下效果,没问题的话再去掉这个参数做真实迁移
  • --tiller-ns:通常 tiller 部署在k8s集群中,但不在 kube-system 命名空间才指定
  • --tiller-out-cluster:如果你的 Helm V2 是 tiller 在集群外面 (tillerless) 的安装方式,请带上这个参数

迁移 helm v2 数据

查看 helm v2 的 release

$ helm ls

NAME     REVISION    UPDATED                     STATUS      CHART          APP VERSION    NAMESPACE
redis    1           Mon Sep 16 19:46:58 2020    DEPLOYED    redis-9.1.3    5.0.5          default

使用 --dry-run 预演效果

$ helm3 2to3 convert redis --dry-run

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 "redis" will be converted from Helm 2 to Helm 3.
[Helm 3] Release "redis" will be created.
[Helm 3] ReleaseVersion "redis.v1" will be created.

没有报错,去掉 --dry-run 开始迁移

$ helm3 2to3 convert redis

Release "redis" will be converted from Helm 2 to Helm 3.
[Helm 3] Release "redis" will be created.
[Helm 3] ReleaseVersion "redis.v1" will be created.
[Helm 3] ReleaseVersion "redis.v1" created.
[Helm 3] Release "redis" created.
Release "redis" 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 v2 release
$ helm ls

NAME     REVISION    UPDATED                     STATUS      CHART          APP VERSION    NAMESPACE
redis    1           Mon Sep 16 19:46:58 2020    DEPLOYED    redis-9.1.3    5.0.5          default

# 检查 helm v3 release
$ helm3 list -A

NAME    NAMESPACE   REVISION    UPDATED       STATUS    CHART      APP VERSION
redis   default     1           2020-06-15 18:19:12.409578018 +0800 CST deployed    redis-9.1.3 1

helm v3 release 区分命名空间,需要带上 -A 参数,显示所有命名空间

更新 helm charts

通过 lint 检查 chart 语法

helm v2 chart 声明:

  {{- if .Values.route.tls }}
  tls:
  {{ toYaml .Values.route.tls | indent 2 }}
  {{- end -}}

在 helm v2 版本中,lint 是没有问题的,但是使用 helm v3 版本 lint 报:mapping values are not allowed in this context 错误

上面 chart 需要调整,下面给出 helm v3 正确 chart 模板

  {{- if .Values.route.tls }}
  tls:
  {{ toYaml .Values.route.tls | indent 2 }}
  {{- end }}

参考链接:https://github.com/helm/helm/issues/6251

Chart v3 变动

  • 需要把 requirements.yaml 文件配置合并到 Chart.yaml 配置中
  • 需要把 Chart.yaml 配置中 apiVersion: v1 修改成 v2

清理 Helm V2 Release

使用 --dry-run 参数,helm v2 清理预演,不会清理 Release 数据

$ helm3 2to3 cleanup --dry-run

2019/11/14 15:06:59 NOTE: This is in dry-run mode, the following actions will not be executed.
2019/11/14 15:06:59 Run without --dry-run to take the actions described below:
2019/11/14 15:06:59
WARNING: "Helm v2 Configuration" "Release Data" "Release Data" will be removed.
This will clean up all releases managed by Helm v2. It will not be possible to restore them if you haven't made a backup of the releases.
Helm v2 may not be usable afterwards.

[Cleanup/confirm] Are you sure you want to cleanup Helm v2 data? [y/N]: y
2019/11/14 15:07:01
Helm v2 data will be cleaned up.
2019/11/14 15:07:01 [Helm 2] Releases will be deleted.
2019/11/14 15:07:01 [Helm 2] ReleaseVersion "postgres.v1" will be deleted.
2019/11/14 15:07:01 [Helm 2] ReleaseVersion "redis.v1" will be deleted.
2019/11/14 15:07:01 [Helm 2] Home folder "/Users/rimasm/.helm" will be deleted.

如果上面命令执行完没有问题,这次清理 V2 Release 数据

$ helm3 2to3 cleanup

执行完后,Tiller Pod 会被删除,并且 kube-system 命名空间中 configmaps 历史版本信息也会被清理。

参考链接

本文由 YP小站 发布!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354