Set container environment variables while creating a deployment with kubectl create

1/17/2020

We can create a deployment with:

kubectl create deployment nginx-deployment --image=nginx

How can we pass an environment variable, say, key=value, for container while creating a deployment using kubectl?

Additionally, can we also use configmap or secret values as environment variables?

-- moazzem
kubectl
kubernetes

4 Answers

1/17/2020

kubectl create deployment command does not have option to pass environment variable as a flag on the imperative command .. possible available flags on create deployment command are as below (listed by autocomplete on on kubectl cli)

   $ kubectl create deployment nginx --image=nginx --
--add-dir-header               --client-certificate=          --insecure-skip-tls-verify     --log-flush-frequency=         --profile-output=              --token
--allow-missing-template-keys  --client-key                   --kubeconfig                   --logtostderr                  --request-timeout              --token=
--alsologtostderr              --client-key=                  --kubeconfig=                  --match-server-version         --request-timeout=             --user
--as                           --cluster                      --log-backtrace-at             --namespace                    --save-config                  --user=
--as=                          --cluster=                     --log-backtrace-at=            --namespace=                   --server                       --username
--as-group                     --context                      --log-dir                      --output                       --server=                      --username=
--as-group=                    --context=                     --log-dir=                     --output=                      --skip-headers                 --v
--cache-dir                    --dry-run                      --log-file                     --password                     --skip-log-headers             --v=
--cache-dir=                   --generator                    --log-file=                    --password=                    --stderrthreshold              --validate
--certificate-authority        --generator=                   --log-file-max-size            --profile                      --stderrthreshold=             --vmodule
--certificate-authority=       --image                        --log-file-max-size=           --profile=                     --template                     --vmodule=
--client-certificate           --image=                       --log-flush-frequency          --profile-output               --template=

Alternately kubectl run command can be used to create a deployment which will allow you to pass env flag on the imperative command , refer below example

 $ kubectl run nginx --image=nginx --env="TEST"="/var/tmp"
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created

Now to check the env variable has been correctly set you can connect to the POD and display the env variables to verify it

Connect

$ kubectl exec -it nginx /bin/bash

List env variables on the pod

root@nginx:/# env | grep -i test
TEST=/var/tmp

Refer official doc example for second part of your question link

-- DT.
Source: StackOverflow

1/17/2020

When creating a pod you can specify the environment variables using the --env option eg.

kubectl run nginx-pod --restart Never --image=nginx --env=key1=value1,key2=value2 

Checkout kubectl run documentation

However, you cannot do this with kubectl create deployment. I'll recommend you use a declarative manifest instead.

-- Shawlz
Source: StackOverflow

1/20/2020

There is a possibility as other community members pointed to pass a variable to a pod but I would advise you to use a declarative approach to creating objects in Kubernetes. Why would I?

There is a nice comic explaining differences between imperative and declarative approach.

Below are examples to:

  • Create a basic NGINX deployment
  • Create a Configmap and Secret in a declarative approach
  • Apply above Configmap and Secret to already created NGINX deployment

Create a basic NGINX deployment

Create a YAML definition of NGINX similar to this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Apply it by running $ kubectl apply -f FILE_NAME.yaml

Create a basic ConfigMap

Create a YAML definition of ConfigMap similar to this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-for-nginx
data:
  port: "12345"

Apply it by running $ kubectl apply -f FILE_NAME.yaml

Create a basic Secret

Create a YAML definition of Secret similar to this:

apiVersion: v1
kind: Secret
metadata:
  name: password-for-nginx
type: Opaque
data:
  password: c3VwZXJoYXJkcGFzc3dvcmQK

Take a specific look at:

  password: c3VwZXJoYXJkcGFzc3dvcmQK

This password is base64 encoded.

To create this password invoke command from your terminal: $ echo "YOUR_PASSWORD" | base64

Paste the output to the YAML definition and apply it with: $ kubectl apply -f FILE_NAME.

Apply above Configmap and Secret to already created NGINX deployment

You can edit your previously created NGINX deployment and add the part responsible for adding the ConfigMap and Secret to be available as environmental variables:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        env:
        - name: NGINX_PASSWORD
          valueFrom:
            secretKeyRef:
              name: password-for-nginx
              key: password
        - name: NGINX_PORT
          valueFrom:
            configMapKeyRef:
              name: config-for-nginx
              key: port
        ports:
        - containerPort: 80

Please take a specific look on below part which will add the environmental variables to created pods as NGINX_PASSWORD and NGINX_PORT:

        env:
        - name: NGINX_PASSWORD
          valueFrom:
            secretKeyRef:
              name: password-for-nginx
              key: password
        - name: NGINX_PORT
          valueFrom:
            configMapKeyRef:
              name: config-for-nginx
              key: port

The secretKeyRef is reference to created Secret and the configMapKeyRef is the reference for created ConfigMap.

Apply it by running $ kubectl apply -f FILE_NAME.yaml once more.

It will terminate old pods and create new one with new configuration.

To check if environmental variables are configured correctly invoke below commands:

  • $ kubectl get pods
  • $ kubectl exec -it NAME_OF_THE_POD -- /bin/bash
  • $ echo $NGINX_PORT
  • $ echo $NGINX_PASSWORD

You should see the variables from ConfigMap and Secret accordingly.

-- Dawid Kruk
Source: StackOverflow

1/17/2020

kubectl run nginx-pod --generator=run-pod/v1 --image=nginx --env="key1=value1" --env="key2=value2"...

Reference - run.

-- gears
Source: StackOverflow