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 in Kubernetes by Namik
• 1,200 points
10 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 by Sirajul
• 16,880 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
287 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,810 points
59 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
• 39,170 points
79 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,900 points
229 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 in Others by anonymous
1,578 views
+3 votes
1 answer
0 votes
1 answer
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 in Kubernetes by Sirajul
• 16,880 points
17 views