Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

486
Views
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:

target.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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

you can't do this with kubectl patch. The patch you did in your problem is called a 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. you can use patch here to add another container. but if you have two container and need to remove one you can't do this with this kind of patch.

if you want to this with patch you need to use 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

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 .

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: {}

you can just use:

 kubectl apply -f target.yaml

this one will update your deployment with new configuration

over 4 years ago · Santiago Trujillo Report

0

You can use JSON patch http://jsonpatch.com/

Remove specific volume mount

kubectl patch deployment <NAME> --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/volumeMounts/0"}]'

Replace volume mounts with what you need

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 cheet sheet for more info: https://kubernetes.io/docs/reference/kubectl/cheatsheet/#patching-resources

over 4 years ago · Santiago Trujillo Report

0

You can leverage the apply command, by getting the deployment definition in JSON format, modifying (in your case removing) this section

  - mountPath: /pkg
    name: john-vol
    readOnly: true
    subPath: school

with sed or a simmilar utility and then applying it back:

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 -
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!