gpt4 book ai didi

kubernetes - 如何使用 kubectl Patch 命令删除 Deployment volumeMounts 中的元素?

转载 作者:行者123 更新时间:2023-12-05 02:45:40 25 4
gpt4 key购买 nike

我有这样的部署:

apiVersion: apps/v1
kind: Deployment
spec:
template:
volumeMounts:
- mountPath: /home
name: john-webos-vol
subPath: home
- mountPath: /pkg
name: john-vol
readOnly: true
subPath: school

我想用 kubectl patch 命令更改 Deloyment,所以它在 PodTemplate 中有以下 volumeMounts:

target.yaml:

apiVersion: apps/v1
kind: Deployment
spec:
template:
volumeMounts:
- mountPath: /home
name: john-webos-vol
subPath: home

我使用了下面的命令,但是没有用。

kubectl patch deployment sample --patch "$(cat target.yaml)"

谁能给我一些建议?

最佳答案

你不能用 kubectl patch 做到这一点。您在问题中所做的补丁称为 strategic merge patch。补丁不能替换东西,而有了这个补丁你只能添加东西。

如果您的 podspec 中最初有一个 container,但您需要添加另一个 container。您可以在此处使用 patch 添加另一个 container。但是如果您有两个 container 并且需要删除一个,则不能使用这种补丁来完成此操作。

如果你想使用补丁,你需要使用retainKeysRef

让我解释一下如何以另一种简单的方式做到这一点。让我们假设你已经在 test.yaml 下面应用了kubectl apply -f test.yaml

test.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /home
name: john-webos-vol
subPath: home
- mountPath: /pkg
name: john-vol
readOnly: true
subPath: school
volumes:
- name: john-webos-vol
emptyDir: {}
- name: john-vol
emptyDir: {}

现在你需要更新这个。更新后的 target.yaml 将删除其中一个 volume 。

target.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /pkg
name: john-vol
readOnly: true
subPath: school
volumes:
- name: john-vol
emptyDir: {}

你可以使用:

 kubectl apply -f target.yaml

这将使用新配置更新您的部署

关于kubernetes - 如何使用 kubectl Patch 命令删除 Deployment volumeMounts 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com