gpt4 book ai didi

kubernetes - Play 应用程序未从Kubernetes yaml文件中选择环境变量

转载 作者:行者123 更新时间:2023-12-02 12:11:21 25 4
gpt4 key购买 nike

我的YAML文件定义了以下环境变量。

apiVersion: apps/v1
kind: Deployment
metadata:
name: codingjediweb
spec:
replicas: 2
selector:
matchLabels:
app: codingjediweb
template:
metadata:
labels:
app: codingjediweb
spec:
volumes:
- name: shared-logs
emptyDir: {}
containers:
- name: codingjediweb
image: docker.io/manuchadha25/codingjediweb:06072020
volumeMounts:
- name: shared-logs
mountPath: /deploy/codingjediweb-1.0/logs/
env:
- name: db.cassandraUri
value: cassandra://xx.yy.zzz.ppp:9042
- name: db.password
value: 9__
- name: db.keyspaceName
value: dbname
- name: db.username
value: dbname2
ports:
- containerPort: 9000
- name: logging
image: busybox
volumeMounts:
- name: shared-logs
mountPath: /deploy/codingjediweb-1.0/logs/
command: ['sh', '-c', "while true; do sleep 86400; done"]
我的Play应用程序使用变量。如果未在配置文件中定义变量,则Play会检查环境变量。
https://www.playframework.com/documentation/2.6.x/ConfigFile
我的应用程序选择如下配置:
 val dbUsernameOption = configuration.getOptional[String]("db.username")

val dbUsername = dbUsernameOption.map(name => name).getOrElse({
cassandraRepositoryComponentsLogger.error("unable to pick db username from configuration.")
""
})

val dbPasswordOption = configuration.getOptional[String]("db.password")
val dbPassword = dbPasswordOption.map(name=>name).getOrElse({
cassandraRepositoryComponentsLogger.error("unable to pick db password from configuration.")
""
})
当我通过应用Kubernetes YAML文件启动应用程序时,容器停止并且出现错误
unable to pick db username from configuration.
unable to pick db password from configuration.
我可以看到环境变量是在容器中定义的。
# printenv
db.keyspaceName=somename <-- here
KUBERNETES_PORT=tcp://10.15.240.1:443
KUBERNETES_SERVICE_PORT=443
CODINGJEDIWEB_SERVICE_PORT_9000_TCP_ADDR=10.15.249.26
HOSTNAME=codingjediweb-649db4fcb9-xxhwm
CODINGJEDIWEB_SERVICE_PORT_9000_TCP_PORT=9000
SHLVL=1
CODINGJEDIWEB_SERVICE_PORT_9000_TCP_PROTO=tcp
HOME=/root
CODINGJEDIWEB_SERVICE_SERVICE_HOST=10.15.249.26
db.cassandraUri=cassandra://xxx <-- here
CODINGJEDIWEB_SERVICE_PORT_9000_TCP=tcp://10.15.249.26:9000
CODINGJEDIWEB_SERVICE_SERVICE_PORT=9000
CODINGJEDIWEB_SERVICE_PORT=tcp://10.15.249.26:9000
db.username=something <-- here
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.15.240.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.15.240.1:443
KUBERNETES_SERVICE_HOST=10.15.240.1
PWD=/
db.password=9__ <-- here
/ #
如果配置文件中缺少配置,Play是否会检查环境变量,这是我不对吗?我不确定这是Play问题还是Kubernetes问题,因为当我在IntelliJ中运行Play应用程序并在构建配置中提供环境变量时,该应用程序便可以工作。
IntelliJ

最佳答案

我的猜测是,这些是JVM环境变量和bash / shell环境变量,因此不会被使用。
从K8s Angular 来看,一种选择是将整个配置文件设置为Secret并将其安装在conf/application.conf

apiVersion: v1
kind: Secret
metadata:
name: appconf-secret
data:
application.conf: |
...
...
...
然后:
apiVersion: apps/v1
kind: Deployment
metadata:
name: codingjediweb
spec:
replicas: 2
selector:
matchLabels:
app: codingjediweb
template:
metadata:
labels:
app: codingjediweb
spec:
volumes:
- name: shared-logs
emptyDir: {}
- name: appconf
secret:
secretName: appconf-secret
containers:
- name: codingjediweb
image: docker.io/manuchadha25/codingjediweb:06072020
volumeMounts:
- name: shared-logs
mountPath: /deploy/codingjediweb-1.0/logs/
- name: appconf
mountPath: /deploy/codingjediweb-1.0/conf/application.conf
...
另一种方法是使用原始方法,并在Play配置文件中使用 Substitutions

关于kubernetes - Play 应用程序未从Kubernetes yaml文件中选择环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62782930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com