kubectl 指定 -o yaml 输出的 yaml 文件包含一些默认字段,这些字段我们是不需要的,可以使用 yq 来进行去除这些字段,生成的 yaml 文件可以用于迁移/部署服务等。

安装 yq

1
2
curl -L https://file.babudiu.com/f/gRIG/yq_linux_amd64 -o /bin/yq
chmod +x /bin/yq

导出 deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
kubectl get deployment nginx -o yaml | yq eval '
del(
.metadata.annotations,
.metadata.creationTimestamp,
.metadata.generation,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid,
.spec.progressDeadlineSeconds,
.spec.revisionHistoryLimit,
.spec.template.metadata.creationTimestamp,
.status
)
'

导出 service

1
2
3
4
5
6
7
8
9
10
11
12
kubectl get svc nginx -o yaml | yq eval '
del(
.metadata.annotations,
.metadata.creationTimestamp,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid,
.spec.clusterIP,
.spec.externalTrafficPolicy,
.status
)
'

导出 Configmap

1
2
3
4
5
6
7
8
9
kubectl get configmap nginx -o yaml | yq eval '
del(
.metadata.annotations,
.metadata.creationTimestamp,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid
)
'

如果导出的 configmap 格式错乱,换行符变成 \n ,可以使用 yq 再次进行格式化

1
cat prometheus_config.yaml | yq -r '.data."prometheus.yml"'

导出 secret

1
2
3
4
5
6
7
8
kubectl get configmap nginx -o yaml | yq eval '
del(
.metadata.creationTimestamp,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid
)
'

导出 ingress

1
2
3
4
5
6
7
8
9
10
11
12
kubectl get ingress example.com -o yaml | yq eval '
del(
.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",
.metadata.creationTimestamp,
.metadata.finalizers,
.metadata.generation,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid,
.status
)
'