7

I want to set an environment variable (I'll just name it ENV_VAR_VALUE) to a container during deployment through Kubernetes.

I have the following in the pod yaml configuration:

...
...
    spec:
      containers:
      - name: appname-service
        image: path/to/registry/image-name
        ports:
        - containerPort: 1234
        env:
        - name: "ENV_VAR_VALUE"
          value: "some.important.value"
...
...

The container needs to use the ENV_VAR_VALUE's value.
But in the container's application logs, it's value always comes out empty.
So, I tried checking it's value from inside the container:

$ kubectl exec -it appname-service bash
root@appname-service:/# echo $ENV_VAR_VALUE
some.important.value
root@appname-service:/# 

So, the value was successfully set.

I imagine it's because the environment variables defined from Kubernetes are set after the container is already initialized.

So, I tried overriding the container's CMD from the pod yaml configuration:

...
...
    spec:
      containers:
      - name: appname-service
        image: path/to/registry/image-name
        ports:
        - containerPort: 1234
        env:
        - name: "ENV_VAR_VALUE"
          value: "some.important.value"
        command: ["/bin/bash"]
        args: ["-c", "application-command"]
...
...

Even still, the value of ENV_VAR_VALUE is still empty during the execution of the command.
Thankfully, the application has a restart function
-- because when I restart the app, ENV_VAR_VALUE get used successfully.
-- I can at least do some other tests in the mean time.

So, the question is...

How should I configure this in Kubernetes so it isn't a tad too late in setting the environment variables?

As requested, here is the Dockerfile.
I apologize for the abstraction...

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y some-dependencies

COPY application-script.sh application-script.sh

RUN ./application-script.sh

# ENV_VAR_VALUE is set in this file which is populated when application-command is executed
COPY app-config.conf /etc/app/app-config.conf

CMD ["/bin/bash", "-c", "application-command"]
2
  • You might need to share the Dockerfile for the container Commented Feb 28, 2019 at 6:24
  • I added the Dockerfile. Commented Feb 28, 2019 at 6:52

2 Answers 2

1

You can try also running two commands in Kubernetes POD spec:

  1. (read in env vars): "source /env/required_envs.env" (would come via secret mount in volume)
  2. (main command): "application-command"

Like this:


containers:
  - name: appname-service
    image: path/to/registry/image-name
    ports:
        - containerPort: 1234
    command: ["/bin/sh", "-c"]
    args:
      - source /env/db_cred.env;
        application-command;
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you move the

RUN ./application-script.sh

below

COPY app-config.conf /etc/app/app-config.conf

Looks like the app is running before the env conf is available for it.

4 Comments

Because the /etc/app directory is only available after the application-script.sh execution is completed. Yes, that's exactly my problem. application-command executes before ENV_VAR_VALUE is populated from Kubernetes.
Then it's matter of doing a RUN mkdir /etc/app or splitting the script appropriately.
But that won't change anything. The problem lies in the fact that ENV_VAR_VALUE is populated by the command in CMD. Kubernetes needs to run the container to be able to set the environment variable. When the container is initialized first, then the environment variable setting is too late. I guess this is a dead-end?
The environment variable is not just for the CMD. You can try doing RUN echo $ENV_VAR_VALUE to see. You can even try using the ENV command in dockerfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.