简体   繁体   English

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

[英]How can i remove an element in Deployment volumeMounts with kubectl Patch command?

I have a Deployment like this:我有这样的部署:

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

I want to change the Deloyment with the kubectl patch command, so it has the following volumeMounts in the PodTemplate instead:我想使用kubectl patch命令更改 Deloyment,因此它在PodTemplate中有以下volumeMounts

target.yaml:目标.yaml:

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

I used the below command, but it didn't work.我使用了下面的命令,但是没有用。

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

Can anyone give me some advice?谁能给我一些建议?

you can't do this with kubectl patch .你不能用kubectl patch做到这一点。 The patch you did in your problem is called a strategic merge patch .您在问题中所做的补丁称为strategic merge patch the patch can't replace things, instead with this patch you can only add things.补丁不能替换东西,而这个补丁只能添加东西。

like if you have intially one container in your podspec but you need to add another container .就像您的podspec中最初有一个container ,但您需要添加另一个container you can use patch here to add another container .您可以在此处使用patch添加另一个container but if you have two container and need to remove one you can't do this with this kind of patch.但是,如果您有两个container并且需要删除一个容器,则无法使用这种补丁执行此操作。

if you want to this with patch you need to use retainKeys .如果你想用patch做到这一点,你需要使用retainKeys Ref 参考

let me explain how you can do this in another simple way.让我解释一下如何以另一种简单的方式做到这一点。 lets assume you have applied below test.yaml with kubectl apply -f test.yaml假设您已应用以下 test.yaml 和kubectl apply -f test.yaml

test.yaml测试.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: {}

now you need update this one.现在你需要更新这个。 and the updated one target.yaml will remove one of volume.和更新的一个目标。yaml将删除其中一个卷。

target.yaml目标.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: {}

you can just use:你可以使用:

 kubectl apply -f target.yaml

this one will update your deployment with new configuration这将使用新配置更新您的部署

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM