Azure kubernetes - python 读取配置映射?

Kar*_*mar 5 python azure kubernetes configmap

我正在尝试对 python 应用程序进行 Dockerize,并希望从 configmap 中读取配置设置。我如何在python中读取configmap?

Kam*_*san 5

使用配置文件创建一个 configMap:

$ kubectl create configmap my-config --from-file my-config.file
Run Code Online (Sandbox Code Playgroud)

将 configMap 挂载到 Pod 的容器中,并在您的应用程序中使用它:

$ kubectl create configmap my-config --from-file my-config.file
Run Code Online (Sandbox Code Playgroud)

现在,您的配置文件将在/config-directory/my-config.file. 您可以从 Python 代码中读取它,如下所示:

config = open("/config-directory/my-config.file", "r")
Run Code Online (Sandbox Code Playgroud)

您还可以使用 configMap 的数据作为容器的 env -使用 configMap 数据定义容器环境变量

config = os.environ['MY_CONFIG']
Run Code Online (Sandbox Code Playgroud)