背景
笔者最近接到一则任务,大体目标是将线上某个k8s集群下掉(降低研发成本、运维成本,统一k8s集群版本),原集群中的服务需要在另一个集群中创建出来,主要涉及到三块内容:Deployment
、Service
、PV
和PVC
作为一名合格的”YAML工程师”,想到通过kubectl
从旧集群导出所有的YAML文件,往新集群一提交,是不是就完事了?实际操作下来,发现导出的文件中有很多由k8s控制器自动添加的字段,如metadata
下的resourceVersion
、selfLink
、uid
和status
等,尤其是resourceVersion
,如果不删除此字段,直接往新集群提交文件,会出现报错无法往下进行
工具
也有工程师发现了这个痛点,在Github上开源了这款工具:https://github.com/itaysk/kubectl-neat
使用方法
- 安装方式一:作为kubectl插件
kubectl krew install neat
- 安装方式二:下载二进制包,加入PATH路径下进行使用
笔者选择了下载二进制包的方式,下面演示下使用方式
步骤一:导出文件到本地
MacBook-Pro-WRJ:kubectl-neat wrj$ kubectl get deployment jibtest -o yaml > jibtest-deploy.yaml
MacBook-Pro-WRJ:kubectl-neat wrj$ cat jibtest-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
creationTimestamp: "2020-08-27T04:01:16Z"
generation: 4
labels:
app: jibtest
name: jibtest
namespace: default
resourceVersion: "140489087"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/jibtest
uid: f49d6beb-e819-11ea-b478-00163e121eee
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: jibtest
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: jibtest
spec:
containers:
- image: jibtest:v3.0
imagePullPolicy: IfNotPresent
name: jibtest
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: "2020-08-27T04:01:16Z"
lastUpdateTime: "2020-08-27T05:35:50Z"
message: ReplicaSet "jibtest-596d7b848" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
- lastTransitionTime: "2020-08-29T10:34:00Z"
lastUpdateTime: "2020-08-29T10:34:00Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 4
readyReplicas: 1
replicas: 1
updatedReplicas: 1
步骤二:通过工具删除多余字段,生成一份新文件
MacBook-Pro-WRJ:kubectl-neat wrj$ kubectl-neat -f jibtest-deploy.yaml > jibtest-deploy-neat.yaml
MacBook-Pro-WRJ:kubectl-neat wrj$ cat jibtest-deploy-neat.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
labels:
app: jibtest
name: jibtest
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: jibtest
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: jibtest
spec:
containers:
- image: harbor-test-k8s.dui88.com/library/jibtest:v3.0
imagePullPolicy: IfNotPresent
name: jibtest
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
步骤三:往新集群提交新部署文件
MacBook-Pro-WRJ:kubectl-neat wrj$ kubect apply -f jibtest-deploy-neat.yaml
经过以上步骤,完成一个deployment的迁移工作,其他service等资源类似