我正在尝试对 python 应用程序进行 Dockerize,并希望从 configmap 中读取配置设置。如何在Python中读取configmap?
使用配置文件创建 configMap:
$ kubectl create configmap my-config --from-file my-config.file
将 configMap 挂载到 Pod 的容器中并在应用程序中使用它:
volumeMounts:
- name: config
mountPath: "/config-directory/my-config.file"
subPath: "my-config.file"
volumes:
- name: config
configMap:
name: my-config
现在,您的配置文件将在/config-directory/my-config.file
。您可以从 Python 代码中读取它,如下所示:
config = open("/config-directory/my-config.file", "r")
您还可以使用 configMap 的数据作为容器的 env -使用configMap数据定义容器环境变量 https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data
config = os.environ['MY_CONFIG']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)