k8s集群patch方式的使用

238 阅读1分钟

patch是什么

使用策略合并补丁、JSON 合并补丁或 JSON 补丁来更新某资源的字段。
接受 JSON 和 YAML 格式。
官网: kubernetes.io/zh-cn/docs/…

pathc的几种方式

kubectl 提供了多种方式来打patch,包括JSON Patch、Merge Patch 和 Strategic Patch 如下kubectl patch --help显示:

image.png

patch的使用

官网的使用示例

# 使用策略合并补丁部分更新节点,指定补丁为 JSON 格式
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
  
# 使用策略合并补丁部分更新节点,指定补丁为 YAML 格式
kubectl patch node k8s-node-1 -p $'spec:\n unschedulable: true'
  
# 使用策略合并补丁部分更新以在 "node.json" 中所指定类别和名称标识的节点
kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
  
# 更新容器的镜像;spec.containers[*].name 是必需的,因为它是合并键
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

# 使用带有位置数组的 JSON 补丁更新容器的镜像
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
  
# 使用合并补丁通过 “scale” 子资源更新 Deployment 的副本
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'

JSON 、Merge 和 Strategic 的实际使用案例

接下来我们就从修改 deployment 的镜像来演示patch的使用

如下为待修改的deploy配置,deploy name为test:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test
  labels:
    app: test
    namespace: nstest
spec:
  template:
    spec:
      containers:
        - name: test
          image: 'nginx-19-el6:20230104'
          resources:
            limits:
              cpu: '1'
              memory: 2Gi
            requests:
              cpu: '0.33'
              memory: 2Gi

我们更新如下:

  1. 更新镜像为 nginx-19-el6:latest
  2. 修改cpu资源值

Strategic Merge Patch

Strategic Merge Patch是Kubernetes特有的patch方式,它允许你根据Kubernetes的特定策略来合并资源。这种方式在处理列表和结构化数据时非常有用。
如果没有指定 --type 参数,Kubernetes 会默认使用 Strategic Merge Patch 类型。

使用文件 patch.yaml

spec:
  template:
    spec:
      containers:
        - name: test
          image: 'nginx-19-el6:latest'
          resources:
            requests:
              cpu: '1'

执行patch

kubectl patch deployment test -n nstest --patch-file patch.yaml

或者

kubectl patch deployment test -n nstest --patch "$(cat patch.yaml)"

查看修改后的deploytment

# kubectl get deployment  -n nstest test -oyaml

输出如下:
...
spec:
  template:
    spec:
      containers:
        - name: test
          image: 'nginx-19-el6:latest'
          resources:
            limits:
              cpu: '1'
              memory: 2Gi
            requests:
              cpu: '1'
              memory: 2Gi
...

JSON Patch方式

JSON Patch使用RFC 6902标准来描述对资源的修改。它通过一系列操作(如addremovereplace等)来修改资源。

如下是修改deployment的指令:

kubectl patch deployment -n nstest test --type='json' -p='[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"nginx-19-el6:latest3"},{"op":"replace","path":"/spec/template/spec/containers/0/resources/requests/cpu","value":"0.2"}]'

Merge Patch

Merge Patch是一种更简单的patch方式,它直接指定要合并到现有资源中的新值。如果字段存在,则更新该字段;如果字段不存在,则添加该字段。

如下是修改deployment的指令:

kubectl patch deployment -n nstest test  -p '{"spec":{"template":{"spec":{"containers":[{"name":"test","image":"nginx-19-el6:latest"},{"name":"test","resources":{"requests":{"cpu":"1"}}}]}}}}'