Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

487
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda