我想在通过 Kubernetes 部署期间为容器设置一个环境变量(我将其命名为 ENV_VAR_VALUE
)。
我在 pod yaml 配置中有以下内容:
...
...
spec:
containers:
- name: appname-service
image: path/to/registry/image-name
ports:
- containerPort: 1234
env:
- name: "ENV_VAR_VALUE"
value: "some.important.value"
...
...
容器需要使用
ENV_VAR_VALUE
的值(value)。但是在容器的应用程序日志中,它的值总是为空。
所以,我尝试从容器内部检查它的值:
$ kubectl exec -it appname-service bash
root@appname-service:/# echo $ENV_VAR_VALUE
some.important.value
root@appname-service:/#
因此,该值已成功设置。
我想是因为设置了从 Kubernetes 定义的环境变量 后 容器已经初始化。
因此,我尝试从 pod yaml 配置中覆盖容器的 CMD:
...
...
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"]
...
...
即便如此,
ENV_VAR_VALUE
的值在命令执行期间仍然为空。值得庆幸的是,该应用程序具有重启功能
-- 因为当我重新启动应用程序时,
ENV_VAR_VALUE
成功使用。——我至少可以同时做一些其他的测试。
所以,问题是...
How should I configure this in Kubernetes so it isn't a tad too late in setting the environment variables?
根据要求,这里是 Dockerfile。
我为抽象道歉......
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"]
最佳答案
您还可以尝试在 Kubernetes POD 规范中运行两个命令:
像这样:
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;
关于kubernetes - 如何在 Kubernetes 的容器中设置环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918710/