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,230 points
1,891 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
• 59,230 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,300 points
2,496 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,890 points
2,897 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
• 52,350 points
2,469 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
• 7,060 points
2,806 views
+1 vote
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
15,573 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,061 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
• 59,230 points
3,896 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
• 59,230 points
1,356 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP