Define an environment variable for a container

0 votes
I want to create a kubernetes pod and set the environment variables for the containers that run in the Pod  in a way that override any environment variables specified in the container image. How can i do this?
Jul 23, 2019 in Kubernetes by Namik
• 1,210 points
198 views

1 answer to this question.

0 votes

When you create a Pod, you can set environment variables for the containers that run in the Pod. 

To set environment variables, include the env or envFrom field in the configuration file.

Note: The environment variables set using the env or envFrom field will override any environment variables specified in the container image.

Example:  

Create a Pod that runs one container. 

The configuration file for the Pod defines an environment variable with name DEMO_GREETING and value "Hello from the environment". 

Here is the configuration file for the Pod:

pods/inject/envars.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"
  1. Create a Pod based on the YAML configuration file: 

    kubectl apply -f https://k8s.io/examples/pods/inject/envars.yaml
  2. List the running Pods:

    kubectl get pods -l purpose=demonstrate-envars
    
    

    The output is similar to this:

    NAME            READY     STATUS    RESTARTS   AGE
    envar-demo      1/1       Running   0          9s
    
    
  3. Get a shell to the container running in your Pod:

    kubectl exec -it envar-demo -- /bin/bash
    
  4. In your shell, run the printenv command to list the environment variables.

    root@envar-demo:/# printenv
    
    

    The output is similar to this:

    NODE_VERSION=4.4.2
    EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237
    HOSTNAME=envar-demo
    ...
    DEMO_GREETING=Hello from the environment
    DEMO_FAREWELL=Such a sweet sorrow
  5. To exit the shell, enter exit.

answered Jul 23, 2019 by Sirajul
• 50,480 points

Related Questions In Kubernetes

0 votes
1 answer

How to use gravitational teleport in a container/kubernetes environment?

You can use teleport to augment kubernetes ...READ MORE

answered Jun 28, 2018 in Kubernetes by ajs3033
• 7,280 points
613 views
0 votes
1 answer

Getting the pod's ip address for the container inside of it

You can make sure that the replication ...READ MORE

answered Aug 24, 2018 in Kubernetes by DareDev
• 6,850 points
299 views
0 votes
1 answer

Pods IP address from inside a container in the pod

Make sure that your pod yaml file ...READ MORE

answered Aug 29, 2018 in Kubernetes by Kalgi
• 51,830 points
378 views
0 votes
2 answers

Access Kubernetes api from within a pod container

wget version: KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token) wget -vO- ...READ MORE

answered Aug 29, 2018 in Kubernetes by Nilesh
• 6,960 points
662 views
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
5,622 views
+4 votes
1 answer

Installing Web UI (Dashboard):kubernetes-dashboard on main Ubuntu 16.04.6 LTS (Xenial Xerus) server

Follow these steps: $ kubeadm reset $ kubeadm init ...READ MORE

answered Apr 11, 2019 in Kubernetes by Kalgi
• 51,830 points

reshown Apr 12, 2019 by Kalgi 1,533 views
0 votes
1 answer

Is it possible for containers within a pod to communicate with each other?

Yes, it is possible for containers to ...READ MORE

answered Jul 12, 2019 in Kubernetes by Sirajul
• 50,480 points
496 views
0 votes
1 answer

Can I configure my kubernetes job to run for a specified time?

When we create a job spec, we ...READ MORE

answered Jul 17, 2019 in Kubernetes by Sirajul
• 50,480 points
44 views