CKA [Installation] – 更改已有资源的yaml配置

官方文档:https://kubernetes.io/docs/reference/kubectl/generated/kubectl_patch/

对于已有的Kubernetes资源你可以进行更改,你可以使用edit或patch来更改。

kubectl edit 是编辑整个yaml , 如果你只是想要局部更改的话可以用kubectl patch。kubectl patch就类似打补丁的方式来更改。

通常kubectl patch 是用户脚本自动化使用的,所以在写之前需要反复执行确保没有问题即可。

不是每个东西都可以更改的,以上提到的immutable field是无法更改的,如果你依然想要更改的话,那么就至少删掉,然后重新redeploy才能。

✅ Kubectl patch 实践步骤

1. 创建pod

kubectl run nginx --image=nginx

2. 输出json以便查看label

kubectl get pod nginx -o json

3. 使用patch更改原有的label

kubectl patch pod nginx -p '{"metadata":{"labels":{"run":"dev-pod"}}}'

4. 查回nginx pod 的json是否已经更改

kubectl get pod nginx -o json

Loading

Facebook评论