Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extend environment variable for a container in Kubernetes

Tags:

kubernetes

Kubernetes documentation on setting environment variables of a container only include examples of new environment variables.

This approach does not work when I try to extend an existing environment variable PATH:

kind: Pod
apiVersion: v1
spec:
  containers:
    - name: blah
      image: blah
      env:
        - name: PATH
          value: "$PATH:/usr/local/nvidia/bin"

The created pod keeps crashing with

BackOff       Back-off restarting failed container
FailedSync    Error syncing pod

Any recommendations as to how I could extend the PATH environment variable?

like image 841
Lana Nova Avatar asked Jul 14 '17 00:07

Lana Nova


People also ask

How do I update environment variables in Kubernetes?

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. In your shell, run the printenv command to list the environment variables. To exit the shell, enter exit .

How do I set an environment variable in Kubernetes deployment?

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 override any environment variables specified in the container image.

How do I set an environment variable on an existing container?

How to set the Environment variable in the container? To set an environment variable you should use flag -e while using docker run or docker-compose command. Environment file - If the environment variable is not overridden by docker-compose.

Do containers in a pod share environment variables?

Short answer is No, they can't.


2 Answers

If you only need this path declaration for the command you are running with, you can add it to containers section, under args

Example:

spec:
  containers:
  - name: blah
    image: blah
    args:
    - PATH="$PATH:/usr/local/nvidia/bin" blah

If you do not have args specified in your yaml, you probably have a CMD specified in your Dockerfile that will just run your container with the command automatically. Thus you can add the following to your Dockerfile.

CMD ["PATH=$PATH:/usr/local/nvidia/bin", "blah"]

If you want this to be in your container in general, you would have to add to the .profile or .bashrc file of the user within the container you are using. This will probably involve creating a new image with these new files baked in.

like image 195
Lindsay Landry Avatar answered Oct 25 '22 19:10

Lindsay Landry


If you expect the environment variable PATH to be updated with additional directory both for your container startup program and even when you want to get into the container using kubectl exec, you can update your manifest command argument something like below

spec:
  containers:
  - name: mysvc-daemon
    command:
    - /bin/bash
    - -c
    args: 
    - |
      echo 'export PATH=$PATH:$HOME/bin:$HOME/local/bin' >> $HOME/.bashrc

And then you can ensure to put the binary files in the directory $HOME/bin or $HOME/local/bin. If you get into the bash environment and check you should be able to see the directories $HOME/bin or $HOME/local/bin are now part of PATH environment variable

like image 38
Saga Avatar answered Oct 25 '22 20:10

Saga