本文介绍了如何在Docker运行期间将k8s的ENV变量提供给python应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker文件,在构建过程中将其注入应用程序时,我现在正在对该环境变量进行硬编码。现在,我想在运行时在应用程序在k8s pod中运行时注入它们。我尝试了,但是它不起作用。以下是我的docker文件。这是我第一次使用严肃的python,并且不确定如何修复它。

I have a docker file in which I am hardcoding the env variables for now as it gets injected in the app during the build process. Now, I want to inject those during the runtime when the application gets ran in the k8s pod. I tried this but its not working. Below is my docker file. Its my first time using serious python and am not sure how to fix it.

FROM python:3.7-slim AS build

WORKDIR /app

COPY . .

RUN python3 setup.py bdist_wheel

#ENV USE_DB="True" \
#   DB_USERNAME= \
#   DB_HOST= \
#   DB_PASSWORD= \
#   DB_DB=sth
RUN pip3 install dist/app_search*.whl && \
    semanticsearch-preprocess

FROM python:3.7-slim

WORKDIR /opt/srv

COPY --from=build /app/dist/app_search*.whl /opt/srv/

COPY --from=build /tmp/projects* /opt/srv/

# set environment variables to /opt/srv
ENV DICT_FILE="/opt/srv/projects.dict" \
    MODEL_FILE="/opt/srv/projects.model.cpickle" \
    INDEX_FILE="/opt/srv/projects.index" \
    EXTERNAL_INDEX_FILE="/opt/srv/projects.mm.metadata.cpickle"


RUN pip3 install waitress && \
    pip3 install app_search*.whl

EXPOSE 5000

ENTRYPOINT [ "waitress-serve" ]

CMD [ "--call", "app_search.app:main" ]


推荐答案

您可以使用pod将env变量传递给k8s pod规范字段: env

You can pass env variables to k8s pod with pod spec field: env.

从k8s文档中查找以下示例:

Look a the following example from k8s docs:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

也可以查看k8s文档以获取更多信息:

Also take a look at k8s documentaion for more information:





  • k8s api spec reference
  • defining environment variables
  • using secrets as environment variables

这篇关于如何在Docker运行期间将k8s的ENV变量提供给python应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:01