How to leverage kubectl patch deployment to update an environment variable?

8/22/2019

I'm trying to patch a deployment, but I keep hitting deployment.extensions/velero not patched.

I've tried a few different variations of the following:

kubectl patch deployment velero -n velero -p '{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}]}]}}'

My initial deployment.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: velero
  labels:
    app.kubernetes.io/name: velero
    app.kubernetes.io/instance: velero
    app.kubernetes.io/managed-by: Tiller
    helm.sh/chart: velero-2.1.1
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/instance: velero
      app.kubernetes.io/name: velero
  template:
    metadata:
      labels:
        app.kubernetes.io/name: velero
        app.kubernetes.io/instance: velero
        app.kubernetes.io/managed-by: Tiller
        helm.sh/chart: velero-2.1.1
    spec:
      restartPolicy: Always
      serviceAccountName: velero-server
      containers:
        - name: velero
          image: "gcr.io/heptio-images/velero:v1.0.0"
          imagePullPolicy: IfNotPresent
          command:
            - /velero
          args:
            - server
          volumeMounts:
            - name: plugins
              mountPath: /plugins
            - name: cloud-credentials
              mountPath: /credentials
            - name: scratch
              mountPath: /scratch
          env:
            - name: AWS_SHARED_CREDENTIALS_FILE
              value: /credentials/cloud
            - name: VELERO_SCRATCH_DIR
              value: /scratch
      volumes:
        - name: cloud-credentials
          secret:
            secretName: cloud-credentials
        - name: plugins
          emptyDir: {}
        - name: scratch
          emptyDir: {}

I'm a bit stuck right now and fear I may be going about this the wrong way. Any suggestions would be much appreciated.

-- user2305321
kubectl
kubernetes
yaml

4 Answers

8/22/2019

There is also another option to change value in existing deployment except patch.

You can also execute edit command and add/edit value in YAML format. Then you have to save it.

$ kubectl edit deployment velero -o yaml

Or if you don't like vi you can do it in nano

$ KUBE_EDITOR="nano" kubectl edit deployment/velero
-- PjoterS
Source: StackOverflow

8/22/2019

As you are patching the deployment, the JSON for patch in your command is not accurate. You may want to try the following:

kubectl patch deployment velero -p '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}]}]}}}}'

-- Chun Liu
Source: StackOverflow

8/22/2019

You have deployed your velera deployment in default namespace and you are trying to patch in velera namespace.

Moreover, your patch string is not valid. Try the following one:

$ kubectl patch deployment velero -p '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}],"name":"velero"}]}}}}'
deployment.extensions/velero patched

Note: My client and server versions are:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
-- Shudipta Sharma
Source: StackOverflow

8/22/2019

Apart from kubectl patch command, you can also make use of kubectl set env to update environment variable of k8s deployment.

kubectl set env deployment/velero AWS_CLUSTER_NAME=test-cluster

Hope this helps.

-- mchawre
Source: StackOverflow