如何解决使用 patch_namespaced_deployment 从部署中删除卷不起作用
我正在尝试使用带有以下参数的 patch_namespaced_deployment
(https://github.com/kubernetes-client/python) 修补部署并删除其卷,但它不起作用。
patch_namespaced_deployment(
name=deployment_name,namespace='default',body={"spec": {"template": {
"spec": {"volumes": None,"containers": [{'name': container_name,'volumeMounts': None}]
}
}
}
},pretty='true'
)
如何重现它:
创建这个文件 app.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/bound-by-controller: "yes"
finalizers:
- kubernetes.io/pv-protection
labels:
volume: pv0001
name: pv0001
resourceVersion: "227035"
selfLink: /api/v1/persistentvolumes/pv0001
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: myclaim
namespace: default
resourceVersion: "227033"
hostPath:
path: /mnt/pv-data/pv0001
type: ""
persistentVolumeReclaimPolicy: Recycle
volumeMode: Filesystem
status:
phase: Bound
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pv-deploy
spec:
replicas: 1
selector:
matchLabels:
app: mypv
template:
metadata:
labels:
app: mypv
spec:
containers:
- name: shell
image: centos:7
command:
- "bin/bash"
- "-c"
- "sleep 10000"
volumeMounts:
- name: mypd
mountPath: "/tmp/persistent"
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myclaim
- kubectl apply -f app.yaml
- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)
- kubectl patch deployment.apps/pv-deploy --patch '{"spec": {"template": {"spec": {"volumes": null,"containers": [{"name": "shell","volumeMounts": null}]}}}}'
- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)
- Delete the application now: kubectl delete -f app.yaml
- kubectl create -f app.yaml
- Patch the deployment using the python library function as stated above. The *VolumeMounts* section is removed but the Volumes still exist.
** 编辑 **
运行 kubectl patch
命令按预期工作。但
在执行 Python 脚本并运行 describe deployment
命令后,persistentVolumeClaim 被替换为像这样的 emptyDir
Volumes:
mypd:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: <unset>
解决方法
您要做的就是所谓的战略合并补丁 (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)。正如您在文档中看到的那样,对于战略合并补丁,列表会根据其补丁策略被替换或合并,因此这可能就是您看到这种行为的原因。
我认为您应该使用 replace https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_replace/,而不是尝试管理部署对象的一部分,而是将其替换为新的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。