Environment Variables

You can set environment variables for containers running in a pod. Additionally, Kubernetes automatically exposes certain runtime information via environment variables.

Let's launch a pod that we pass an environment variable SIMPLE_SERVICE_VERSION with the value 1.0:

kubectl apply -f https://github.com/openshift-evangelists/kbe/raw/main/specs/envs/pod.yaml

Now, let's verify from within the cluster if the application running in the pod has picked up the environment variable:

kubectl exec envs -t -- curl -s

The output reflects the value that was set for the environment variable (the default, unless overridden by the variable, is 0.5.0):

{"host": "", "version": "1.0", "from": ""}

You can check what environment variables Kubernetes itself provides automatically using a REST endpoint in the sample application:

kubectl exec envs -t -- curl -s

Your results will vary slightly depending on you cluster configuration, but an example output is included below:

  "version": "1.0",
    'HOSTNAME': 'envs',
    'HOME': '/root',
    'LANG': 'C.UTF-8',
    'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
    'GPG_KEY': 'C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF',
    'REFRESHED_AT': '2017-04-24T13:50',
    'PYTHON_PIP_VERSION': '9.0.1',
    'PYTHON_VERSION': '2.7.13',
    'ROUTER_PORT': 'tcp://',
    'ROUTER_PORT_1936_TCP': 'tcp://',
    'ROUTER_PORT_1936_TCP_ADDR': '',
    'ROUTER_PORT_1936_TCP_PORT': '1936',
    'ROUTER_PORT_1936_TCP_PROTO': 'tcp',
    'ROUTER_PORT_443_TCP': 'tcp://',
    'ROUTER_PORT_443_TCP_ADDR': '',
    'ROUTER_PORT_443_TCP_PORT': '443',
    'ROUTER_PORT_443_TCP_PROTO': 'tcp',
    'ROUTER_PORT_80_TCP': 'tcp://',
    'ROUTER_PORT_80_TCP_ADDR': '',
    'ROUTER_PORT_80_TCP_PORT': '80',
    'ROUTER_PORT_80_TCP_PROTO': 'tcp',
    'ROUTER_SERVICE_PORT_1936_TCP': '1936',
    'ROUTER_SERVICE_PORT_443_TCP': '443',
    'DOCKER_REGISTRY_PORT': 'tcp://',
    'DOCKER_REGISTRY_PORT_5000_TCP': 'tcp://',
    'KUBERNETES_PORT': 'tcp://',
    'KUBERNETES_PORT_443_TCP': 'tcp://',
    'KUBERNETES_PORT_443_TCP_PORT': '443',
    'KUBERNETES_PORT_53_TCP': 'tcp://',
    'KUBERNETES_PORT_53_UDP': 'udp://',

Alternatively, you can also use the exec subcommand to display the environment variables within the pod:

kubectl exec envs -- printenv

Remove the sample pod with:

kubectl delete pod/envs

In addition to the above examples, you can also use secrets, volumes, or the downward API to inject additional information into your container environments.