我有一个这样的部署:
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
命令改变部署,所以它在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)"
谁能给我一些建议?可以使用JSON补丁http://jsonpatch.com/
删除特定卷挂载
kubectl patch deployment <NAME> --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/volumeMounts/0"}]'
用需要的内容替换卷挂载
kubectl patch deployment <NAME> --type json -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/volumeMounts", "value": [{"mountPath": "/home", "name": "john-webos-vol", "subPath": "home"}]}]'
Kubectl sheet获取更多信息:https://kubernetes.io/docs/reference/kubectl/cheatsheet/#patching-resources
您不能对kubectl patch
这样做。你在问题中所做的补丁称为strategic merge patch
。这个补丁不能替换东西,你只能添加东西。
就像你在podspec
中最初有一个container
,但你需要添加另一个container
。您可以在这里使用patch
添加另一个container
。但如果你有两个container
,需要删除一个,你不能用这种补丁。
如果你想使用patch
,你需要使用retainKeys
。Ref
让我用另一种简单的方法来解释如何做到这一点。假设您已经应用了下面的测试。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
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
这个将用新的配置更新你的部署
您可以利用应用命令,通过获取JSON格式的部署定义,修改(在您的情况下删除)此部分
- mountPath: /pkg
name: john-vol
readOnly: true
subPath: school
使用sed或类似的实用程序,然后应用它:
kubectl get deployment <myDeployment> -n <myNamespace> | sed -z -s -E -b -e 's/REGEX_TO_MATCH_PART_OF_DEPLOYMENT_TO_REMOVE//g' | kubectl apply -f -