我在一个容器中有一个应用程序,它可以从configMap中读取某些数据,就像这样
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
application.yaml: |
server:
port: 8080
host: 0.0.0.0
##
## UCP configuration.
## If skipped, it will default to looking inside of the connections.xml file.
database:
ApplicationDB:
username: username
password: hello123
现在,我为密码创建了一个 secret ,并在启动容器时将其安装为env变量。
apiVersion: v1
kind: Secret
metadata:
name: appdbpassword
type: Opaque
stringData:
password: hello123
我的 pods 看起来像:
apiVersion: v1
kind: Pod
metadata:
name: {{ .Values.pod.name }}
spec:
containers:
- name: {{ .Values.container.name }}
image: {{ .Values.image }}
command: [ "/bin/sh", "-c", "--" ]
args: [ "while true; do sleep 30; done;"]
env:
- name: password
valueFrom:
secretKeyRef:
name: appdbpassword
key: password
volumeMounts:
- name: config-volume
mountPath: /app/app-config/application.yaml
subPath: application.yaml
volumes:
- name: config-volume
configMap:
name: app-config
我尝试在configMap中使用此env变量:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
application.yaml: |
server:
port: 8080
host: 0.0.0.0
##
## UCP configuration.
## If skipped, it will default to looking inside of the connections.xml file.
database:
ApplicationDB:
username: username
**password: ${password}**
但是我的应用程序无法读取此密码。我在这里想念什么吗?
编辑:
我无法将application.yaml更改为任何其他形式,因为我的服务器在源路径中查找application.yaml。我们是否有任何方法可以在values.yaml(helm)文件中使用该环境变量,并在configmap中使用它?
最佳答案
您的$ {password}变量不会被其值替换,因为application.yaml是静态文件。如果在某些配置中使用此yaml文件,则很可能会将其替换为其值。
考虑一种方案,而不是application.yaml传递此文件
application.sh: |
echo "${password}"
现在进入/ app / app-config,您将看到application.sh文件。现在执行
sh application.sh
,您将看到环境变量的值。我希望这可以澄清您的观点。
关于kubernetes - 使用Kubernetes secret 作为配置映射中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59082646/