Can kubectl delete environment variable?

1/9/2020

Here I can update the envs through kubectl patch, then is there any method that can delete envs except re-deploy a deployment.yaml?

$ kubectl patch deployment demo-deployment -p '{"spec":{"template":{"spec":{"containers":[{"name": "demo-deployment","env":[{"name":"foo","value":"bar"}]}]}}}}'
deployment.extensions "demo-deployment" patched

Can I delete the env "foo" through command line not using a re-deploy on the whole deployment?

-- Kaii Chu
containers
kubectl
kubernetes

2 Answers

1/9/2020

Consider that containers is an array inside an object. Arrays can only be fetched by their index, as opposed to objects which can be fetched via key value pairs. See reference here. So there is a workaround for using index.

Here you have env that are placed into the container:

spec:
  containers:
  - env:
    - name: DEMO_GREETING
      value: Hello from the environment
    - name: DSADASD
      value: asdsad

Here you have a command to remove the anv using index:

kubectl patch deployments asd  --type=json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/env/1"}]

And the result:

spec:
  containers:
  - env:
    - name: DEMO_GREETING
      value: Hello from the environment

This will still however restart your pod. Hope that helps!

-- acid_fuji
Source: StackOverflow

1/9/2020

If you are fine with redeployment then follow the below steps

  1. Create configmap and include your environment variables
  2. Load env variables from configmap in the deployment
envFrom:
- configMapRef:
  name: app-config
  1. If you want to delete env variable then remove those key-value pairs from configmap
  2. It will cause redeployment. You can also delete the pod from corresponding deployment
-- P Ekambaram
Source: StackOverflow