Understanding Kubernetes environment variables

Filed Under: Random
Understanding Kubernetes Environment Variables

Hello, readers! This article talks about Understanding Kubernetes environment variables with descriptive examples.

So, let us begin! 馃檪

Use of environment variables in Kubernetes

In the usual programming scenario, we often come across scenarios where we need to have variables and values that are common to various environments and have a global definition.

This is when environmental variables come into the picture. We can have our values stored in these variables and then they can be referred across varied applications on the same underlying platform or infrastructure.

We can define these variables at a global scale and then use them by referring to the variable name in the pipelines or even the deployment code.

Even in Kubernetes, we can define these environment variables defined and being used within the deployments directly or even through some command line references.

In the upcoming sections, we will be defining the environment variables to be used within a Kubernetes Container.

1. Define and use an environment variable in a Kubernetes Container

In the Kubernetes world, we can have the environment variables set for the container within the Pod definition.

For us to set the environment variables, we need to include tag env or envFrom in the Pod YAML file.

Have a look at the below YAML file-

apiVersion: v1
kind: Pod
  name: demo
  - name: demo-container
    image: gcr.io/google-samples/node-hello:1.0
    - name: VAR1
      value: "Hello 1234"

This POD YAML file contains the details around the configuration of the container as mentioned below-

  1. name : demo-container
  2. It has made use of the sample hello world image from google’s public container registry.
  3. We have defined an environment variable VAR1 with the value Hello 1234

Once we create this pod, we should ideally be able to view the value of VAR1 within the container.

kubectl apply -f POD.YAML

2. Inspect the environment variable

Once the pod is up and running, it is now the time to check if the environment variable configuration gets reflected within the container-

kubectl exec -it demo-container -- printenv

The above command enables us to list the environment variables of the container.


VAR1=Hello 1234

3. Using environment variables in the Container CLI arguments

The environment variables that we define within the container take precedence over the variables defined within the Dockerfile.

We can expose the information about the pods specifically about a process within a container through the arguments within it. Having said that, we make use of the environment variables to expose and represent the information about the Pods through Command Line Arguments.

The other way of having the information about Pods through CLI is through container ephemeral volumes.

In the below example, we will be representing the pod information through the container CLI using environment variables.

Have a look at the below code!

apiVersion: v1
kind: Pod
  name: demo
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sh", "-c"]
      - while true; do
          echo -en '\n';
        - name: MY_POD_NAME
              fieldPath: metadata.name
        - name: MY_POD_SERVICE_ACCOUNT
              fieldPath: spec.serviceAccountName
  restartPolicy: Never

In the above example, we have made use of the environment variables to expose information about the Pod name as well as the Pod’s default service account. For the same, we expose the values through env variables and then use the commands and arguments to print these env variables that we trigger.


Image 2
Print env variables


By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Kubernetes and Docker, stay tuned with us.

Till then, Happy Learning!! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content