kubenetes: patch更新和替换、删除资源内容

目录

使用(patch)补丁修改、更新资源的字段。

kubectl命令操作

替换:replace

新增:add

删除:remove

多个资源内容的修改、删除、新增

疑惑:

kubernetes-client/java操作Patch


使用(patch)补丁修改、更新资源的字段。

支持JSON和YAML格式。

kubectl命令操作

k8s版本1.13

替换:replace

kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

新增:add

kubectl patch pod valid-pod --type='json' -p='[{"op": "add", "path": "/metadata/labels/MixedDeploy_Status", "value":"true"}]'

删除:remove

kubectl patch pod valid-pod --type='json' -p='[{"op": "remove", "path": "/metadata/labels/MixedDeploy_Status"}]'

 

多个资源内容的修改、删除、新增

参数:JSON数组

疑惑:

kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

在K8s版本为1.13上,使用这种方式修改更新资源,却提示unpatch

kubernetes-client/java操作Patch

CoreV1Api apiInstance = new CoreV1Api();
String name = "主机名称"; // String | name of the Node
// 新增
V1Patch body = new V1Patch("[{\"op\": \"add\", \"path\": \"/metadata/labels/MixedDeploy_Status\", \"value\":\"true\"}]");
// 替换更新
V1Patch body = new V1Patch("[{\"op\": \"replace\", \"path\": \"/metadata/labels/MixedDeploy_Status\", \"value\":\"false\"}]");
String pretty = "true";
String dryRun = null;
Boolean force = true; // Boolean | Force is going to \"force\" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests.
try {
    V1Node result = apiInstance.patchNode(name, body, pretty, dryRun, null, force);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling CoreV1Api#patchNode");
    e.printStackTrace();
}

 

你可能感兴趣的:(kubenetes,kubectl,patch,更新资源,替换)