Kubernetes Pods - Inject environment variable from host node

3/17/2020

I have a container with the following spec:

containers:
      - name: test-container
        image: REDACTED
        env:
        - name: DISPLAY
          value: ":10.0"   <---------------------
        - name: NODE_ENV
          value: "production"
        - name: ENDPOINT
          value: REDACTED
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: x11
          mountPath: /tmp/.X11-unix
      volumes:
      - name: x11
        hostPath:
          path: /tmp/.X11-unix

sidenote: What this is doing is it's allowing me to render components on the host node, but perform the computation inside my container.

Right now I have DISPLAY hardcoded to ":10.0", but I want to do something like:

env:
        - name: DISPLAY
          value: ":$HOST_DISPLAY"

Which will mount the host node's DISPLAY variable (rather than hardcoding and guessing what it will be), so that if a node has ":0" or any other variable set it will forward/inject it into the container dynamically.

Any way to accomplish this? If it helps I can use an ansible operator

Edit to further explain:

Imagine a pod that was a simple http server that when you hit it with a browser, showed you the operating system of the node it is being run on. Lets say the node had an environment variable called OPERATING_SYSTEM. Node A has OPERATING_SYSTEM=Windows Node B has OPERATING_SYSTEM=Linux. How could I configure this pod to display the node's operating system. NOT THE CONTAINER OPERATING SYSTEM

Another example:

Imagine a pod that was a simple http server that when you hit it with a browser, it gave you back the ID of the node that it is running on. Lets say Node 1 has an environment variable called NODE_ID=123 and Node 2 has an environment variable called NODE_ID=456. When any pod gets scheduled on one of these two arbitrary nodes, it can access these NODE environment variables to correctly display which node it's running on

I almost want to do this, except with node.fields: https://v1-15.docs.kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

-- user2896438
google-kubernetes-engine
kubernetes
kubernetes-pod

2 Answers

3/18/2020

You can use ConfigMap. create a key=value in configure map and reference in your env

https://medium.com/google-cloud/kubernetes-configmaps-and-secrets-68d061f7ab5b

-- Siva
Source: StackOverflow

3/19/2020

There are few ways to pass environment variables.

  1. Define env variables in container, for example like in this doc.
  2. Define in Pod/Deployment config like here.
  3. From Secrets from this example.

To pass new environment variables to container you need to use ConfigMap or Secret.

Unfortunately, that what you want to achieve is impossible to obtain in pure Kubernetes due to Kubernetes concepts. As per doc:

A Pod encapsulates an application’s container (or, in some cases, multiple containers), storage resources, a unique network IP, and options that govern how the container(s) should run. A Pod represents a unit of deployment: a single instance of an application in Kubernetes, which might consist of either a single container or a small number of containers that are tightly coupled and that share resources.

If you will kubectl describe <pod-name> you will have only name of host, because that's all what Kubernetes needs. Kubernetes make sure to run whole deployment application, no mather on what node (unitl you specify nodeSelector). It's how Kubernetes was designed. For detailed information you should reat this docs.

You can add some tags with ansible, python, etc to question. Maybe if you would use some scripts/libraries it could be done, but you will not achieve this using pure Kubernetes.

-- PjoterS
Source: StackOverflow