作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的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会检查环境变量。
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应用程序并在构建配置中提供环境变量时,该应用程序便可以工作。
最佳答案
我的猜测是,这些是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/
我是一名优秀的程序员,十分优秀!