Kubectl patch命令使用

kubectl patch

使用(patch)修補程式修改、更新資源的欄位。

支援JSON和YAML格式。

請參閱//htmlpreview.github.io/?//github.com/kubernetes/kubernetes/blob/HEAD/docs/api-reference/v1/definitions.html中說明,查找資源欄位是否為可變的。

語法

$ patch (-f FILENAME | TYPE NAME) -p PATCH

示例

使用patch更新Node節點。

kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'

使用patch更新由「node.json」文件中指定的類型和名稱標識的節點

kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
更新容器的鏡像
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

 更改service類型為NodePort形式

kubectl patch  svc schedule -n uat --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"},{"op":"add","path":"/spec/ports/0/nodePort","value":30930}]'

kubectl patch svc schedule -n uat --type='json' -p '[{"op":"remove","path":"/spec/ports/0/nodePort"},{"op":"replace","path":"/spec/type","value":"ClusterIP"}]'

 給pod手動添加dns解析

kubectl -n monitoring patch  deployments prometheus-grafana --patch '{
    "spec": {
        "template": {
            "spec": {
                "hostAliases": [
                    {
                        "hostnames":
                        [
                            "prometheus-prometheus-oper-prometheus"
                        ],
                            "ip": "10.233.10.10"
                    }
                ]
            }
        }
    }
}'