4

K8s:通过 kubectl 插件 Kubepug 实现升级检查(废弃API资源检查)

 1 year ago
source link: https://liruilongs.github.io/2023/01/09/%E5%BE%85%E5%8F%91%E5%B8%83/%E5%85%B3%E4%BA%8EKubernetes-%E4%B8%AD-%E9%80%9A%E8%BF%87-kubectl-%E6%8F%92%E4%BB%B6-Kubepug-%E5%AE%9E%E7%8E%B0%E5%8D%87%E7%BA%A7%E6%A3%80%E6%9F%A5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

K8s:通过 kubectl 插件 Kubepug 实现升级检查(废弃API资源检查)

我所渴求的,無非是將心中脫穎語出的本性付諸生活,為何竟如此艱難呢 ——赫尔曼·黑塞《德米安》


  • 分享一个小工具,可用于 版本升级的 废弃 API 对象检查
  • 博文内容涉及:
    • kubepug 离线安装,配置 kubectl 插件
    • kubepug 两种方式使用 Demo
  • 理解不足小伙伴帮忙指正

我所渴求的,無非是將心中脫穎語出的本性付諸生活,為何竟如此艱難呢 ——赫尔曼·黑塞《德米安》


k8s 的版本迭代很快,虽然主要版本一直没有变化,但是次要版本一直在迭代,2022年一年就发布了三个次要版本,同时不同的次要版本之间 API 资源一直在变化,有新加入的,也有废弃删除的。不同版本的 api 资源版本也有不同,往往不是向下兼容的,比如在低版本中 API 资源版本为 v1beta1,而高版本可能升级为 v1。但是在高版本中不能运行低版本的API资源。

所以在 k8s 版本升级的时候,需要对之前废弃的和删除的 API 资源 做出清理,需要升级的做升级,或者替换为其他的 API 资源。 在这之前,需要一个工具来检查 API 资源对象,那些事已经废弃的,那些将要废弃。

Kubepug 即是这样一个工具,一个升级前检查器,可帮助在迁移到新的主要版本之前在 Kubernetes 资源中找到已弃用和已删除的 API

在这里插入图片描述

在这里插入图片描述

KubePug/Deprecations 作为一个 kubectl 插件,他可以实现下面的功能:

  • 从特定的 Kubernetes 版本下载 swagger.json
  • 解析此 Json 发现弃用通知
  • 验证当前的 kubernetes 集群或输入文件,检查此已弃用的 API 版本中是否存在对象,允许用户在迁移前进行检查

Kubepug 安装

如果可以科学上网,并且安装了 krew,可以使用下面的方式。

kubectl krew install deprecations

如果是内网环境,可以浏览器下载二进制文件

┌──[[email protected]]-[~/ansible/krew]
└─$wget https://github.com/rikatz/kubepug/releases/download/v1.4.0/kubepug_linux_amd64.tar.gz

然后配置为 kubectl 插件

┌──[[email protected]]-[~/ansible/krew]
└─$tar -zxvf kubepug_linux_amd64.tar.gz
LICENSE
README.md
kubepug
┌──[[email protected]]-[~/ansible/krew]
└─$mv kubepug kubectl-kubepug
┌──[[email protected]]-[~/ansible/krew]
└─$mv ./kubectl-kubepug /usr/local/bin/
┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug

查看版本测试

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug version
__ ___ __ __ .______ _______ ______ .___________. __ __ ___ __ __ .______ _______ .______ __ __ _______
| |/ / | | | | | _ \ | ____| / || || | | |/ / | | | | | _ \ | ____|| _ \ | | | | / _____|
| ' / | | | | | |_) | | |__ | ,----'`---| |----`| | ______ | ' / | | | | | |_) | | |__ | |_) | | | | | | | __
| < | | | | | _ < | __| | | | | | | |______|| < | | | | | _ < | __| | ___/ | | | | | | |_ |
| . \ | `--' | | |_) | | |____ | `----. | | | `----. | . \ | `--' | | |_) | | |____ | | | `--' | | |__| |
|__|\__\ \______/ |______/ |_______| \______| |__| |_______| |__|\__\ \______/ |______/ |_______|| _| \______/ \______|
kubectl-kubepug: Shows all the deprecated objects in a Kubernetes cluster allowing the operator to verify them before upgrading the cluster.
It uses the swagger.json version available in master branch of Kubernetes repository (github.com/kubernetes/kubernetes) as a reference.

GitVersion: v1.4.0
GitCommit: 4de32d695b27c52c16d4a801b613b78e45e28ca9
GitTreeState: clean
BuildDate: 2022-08-21T18:25:40
GoVersion: go1.18.5
Compiler: gc
Platform: linux/amd64

获取集群当前 API 状态

可以使用以下命令检查正在运行的集群的状态

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug --k8s-version=v1.22.2
Error: Get "https://raw.githubusercontent.com/kubernetes/kubernetes/v1.22.2/api/openapi-spec/swagger.json": dial tcp 0.0.0.0:443: connect: connection refused
time="2023-01-08T23:23:59+08:00" level=error msg="An error has occurred: Get \"https://raw.githubusercontent.com/kubernetes/kubernetes/v1.22.2/api/openapi-spec/swagger.json\": dial tcp 0.0.0.0:443: connect: connection refused"

如果没有科学上网,会报上面的错误,可以把 对应的 swagger 文件下载下来上传。然后在检查的时候指定 swagger 文件目录

PS C:\Users\山河已无恙\Downloads> curl -o swagger-v1.22.2.json https://raw.githubusercontent.com/kubernetes/kubernetes/v1.22.2/api/openapi-spec/swagger.json
PS C:\Users\山河已无恙\Downloads> scp .\swagger-v1.22.2.json [email protected]:/root/ansible/krew/
[email protected]''s password:
swagger-v1.22.2.json 100% 4400KB 86.4MB/s 00:00
PS C:\Users\山河已无恙\Downloads>

通过 --swagger-dir= 指定 对应的 swagger 文件位置,--k8s-version=v1.22.2 指定要检测的版本

┌──[[email protected]]-[~/ansible/krew]
└─$mkdir -p swagger/folder
┌──[[email protected]]-[~/ansible/krew]
└─$mv swagger-v1.22.2.json swagger/folder/

通过输出我们可以看到当前集群的一些 api 变化

  • v1 版本的 ComponentStatus(ComponentStatusList)v1.19+ 中已弃用
  • policy/v1beta1a 版本 PodSecurityPolicyv1.21+ 版本已弃用,v1.25+ 版本不可用
┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug --k8s-version=v1.22.2 --swagger-dir=./swagger/folder
W0108 23:32:02.991528 30270 warnings.go:70] v1 ComponentStatus is deprecated in v1.19+
W0108 23:32:02.995308 30270 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
RESULTS:
Deprecated APIs:

ComponentStatus found in /v1
├─ ComponentStatus (and ComponentStatusList) holds the cluster validation info. Deprecated: This API is deprecated in v1.19+
-> GLOBAL: scheduler
-> GLOBAL: controller-manager
-> GLOBAL: etcd-0

PodSecurityPolicy found in policy/v1beta1
├─ PodSecurityPolicy governs the ability to make requests that affect the Security Context that will be applied to a pod and container. Deprecated in 1.21.
-> GLOBAL: controller
-> GLOBAL: speaker


Deleted APIs:

┌──[[email protected]]-[~/ansible/krew]
└─$

通过 kubectl 也可以看到当前集群存在的对应 API 资源。

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl get psp
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES
controller false RunAsAny MustRunAs MustRunAs MustRunAs true configMap,secret,emptyDir
speaker true NET_RAW RunAsAny RunAsAny RunAsAny RunAsAny true configMap,secret,emptyDir
┌──[[email protected]]-[~/ansible/krew]
└─$
┌──[[email protected]]-[~/ansible/krew]
└─$kubectl get componentstatuses
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
scheduler Unhealthy Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused
controller-manager Healthy ok
etcd-0 Healthy {"health":"true","reason":""}

假如我们希望升级到 v1.25.2 ,可以下载 1.25.2 版本的 swagger json 文件

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug --k8s-version=v1.25.2 --swagger-dir=./swagger/folder
W0108 23:43:37.187999 41352 warnings.go:70] v1 ComponentStatus is deprecated in v1.19+
W0108 23:43:37.339503 41352 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
RESULTS:
Deprecated APIs:

ComponentStatus found in /v1
├─ ComponentStatus (and ComponentStatusList) holds the cluster validation info. Deprecated: This API is deprecated in v1.19+
-> GLOBAL: scheduler
-> GLOBAL: controller-manager
-> GLOBAL: etcd-0


Deleted APIs:

PodSecurityPolicy found in policy/v1beta1
├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!!
-> GLOBAL: controller
-> GLOBAL: speaker

通过检查可以看到 ,1.25 之后的废弃和已经删除的 API 资源。如果要升级到 1.25 需要把删除的 API 处理掉。

在这里插入图片描述

在这里插入图片描述

查看指定文件资源 API 状态

Kubepug 可以放入 CI / 检查输入文件:

  • 来自 master 分支的 swagger.json 将被使用
  • 将验证所有 YAML 文件(不包括子目录)
  • 如果发现已弃用或已删除的对象,程序将退出并出错。

这里指定为当前的版本。

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug --input-file=/root/ansible/ --error-on-deleted --error-on-deprecated --k8s-version=v1.22.2 --swagger-dir=./swagger/folder
........
RESULTS:
Deprecated APIs:


Deleted APIs:

calicoApiConfig found in /v1
├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!!
-> OBJECT: namespace: default location: /root/ansible//calicoctl.j2

Error: found 1 Deleted APIs and 0 Deprecated APIs
time="2023-01-08T23:50:39+08:00" level=error msg="An error has occurred: found 1 Deleted APIs and 0 Deprecated APIs"

直接指出删除的 API 资源和对应的文件。上面两个参数的意思:

  • --error-on-deleted 如果发现一个被删除的对象,程序将以返回代码 1 而不是 0 退出,默认为 false
  • --error-on-deprecated 如果发现一个被废弃的对象,程序将以返回代码 1 而不是 0 退出。

当然也可以检查要升级的版本。

┌──[[email protected]]-[~/ansible/krew]
└─$kubectl kubepug --input-file=/root/ansible/ --error-on-deleted --error-on-deprecated --k8s-version=v1.25.2 --swagger-dir=./swagger/folder
........
RESULTS:
Deprecated APIs:


Deleted APIs:

PodDisruptionBudget found in policy/v1beta1
├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!!
-> OBJECT: calico-kube-controllers namespace: kube-system location: /root/ansible//calico.yaml

calicoApiConfig found in /v1
├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!!
-> OBJECT: namespace: default location: /root/ansible//calicoctl.j2

Error: found 2 Deleted APIs and 0 Deprecated APIs
time="2023-01-08T23:52:37+08:00" level=error msg="An error has occurred: found 2 Deleted APIs and 0 Deprecated APIs"

https://github.com/rikatz/kubepug


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK