首页 > 解决方案 > 如何使用 envconsul 在 pod 中设置 ENV 变量

问题描述

我正在尝试使用 envconsul 将环境变量推送到运行 openliberty 的 docker 容器。变量是从保险库中提取的。Consul、Vault 和 Openliberty 应用程序 pod 在 minikube 上运行。pod 还从 configmap 中提取非秘密变量。

根据日志,变量被拉出,但是当我运行 env 命令时,我无法在 pod 中查看它们。

此外,configmap 中的变量在服务器启动时不可用,但是一旦 pod 启动,一个 env 命令会显示它们。

应用程序 docker 文件有 CMD[./startup.sh]。

startup.sh 有这个命令

"/usr/local/bin/envconsul -log-level debug -config "/etc/envconsul/envconsul-config.hcl" /opt/ol/wlp/bin/server run defaultServer"

k 日志 pod/mp

2019/09/11 19:05:38.723258 [INFO] envconsul v0.9.0 (fd1ee3c7)
2019/09/11 19:05:38.723278 [INFO] (runner) creating new runner (once: false)
2019/09/11 19:05:38.723492 [DEBUG] (runner) final config: 9/09/11 19:05:38.723536 [INFO] (runner) creating watcher
2019/09/11 19:05:38.723615 [INFO] looking at vault secret/myapp/config
2019/09/11 19:05:38.724356 [INFO] (runner) starting
2019/09/11 19:05:38.724379 [DEBUG] (watcher) adding vault.read(secret/myapp/config)
2019/09/11 19:05:38.739500 [DEBUG] (runner) receiving dependency vault.read(secret/myapp/config)
2019/09/11 19:05:38.739527 [INFO] (runner) running
2019/09/11 19:05:38.739562 [DEBUG] (runner) setting USERNAME="appuser" from vault.read(secret/myapp/config)
2019/09/11 19:05:38.739659 [INFO] (child) spawning: /opt/ol/wlp/bin/server run defaultServer

Launching defaultServer (Open Liberty 19.0.0.8/wlp-1.0.31.cl190820190813-1136) on Eclipse OpenJ9 VM, version 1.8.0_222-b10 (en_US)

[AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT   ] CWWKG0093A: Processing configuration drop-ins resource: /opt/ol/wlp/usr/servers/defaultServer/configDropins/defaults/keystore.xml
[AUDIT   ] CWWKG0093A: Processing configuration drop-ins resource: /opt/ol/wlp/usr/servers/defaultServer/configDropins/defaults/open-default-port.xml
[WARNING ] CWWKG0011W: The configuration validation did not succeed. Value "${env.DB_MF_PORT}" is not a number.
[WARNING ] CWWKG0083W: A validation failure occurred while processing the [portNumber] property, value = [${env.DB_MF_PORT}]. Default value in use: [50000].
[WARNING ] CWWKG0033W: The value [myKeyStore] specified for the reference attribute [keyStoreRef] was not found in the configuration.
[AUDIT   ] CWWKS4104A: LTPA keys created in 1.419 seconds. LTPA key file: /opt/ol/wlp/output/defaultServer/resources/security/ltpa.keys
[AUDIT   ] CWPKI0803A: SSL certificate created in 2.867 seconds. SSL key file: /opt/ol/wlp/output/defaultServer/resources/security/key.p12
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/ibm/api/
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/metrics/
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/openapi/ui/
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/health/
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/openapi/
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://mp:9080/${app_context_root}/
[AUDIT   ] CWWKZ0001I: Application microprofile started in 13.021 seconds.
[AUDIT   ] CWWKF0012I: The server installed the following features: [appSecurity-2.0, cdi-2.0, distributedMap-1.0, jaxb-2.2, jaxrs-2.1, jaxrsClient-2.1, jaxws-2.2, jdbc-4.2, jndi-1.0, jpa-2.2, jpaContainer-2.2, json-1.0, jsonp-1.1, monitor-1.0, mpConfig-1.3, mpHealth-2.0, mpMetrics-2.0, mpOpenAPI-1.1, mpRestClient-1.3, servlet-4.0, ssl-1.0].
[AUDIT   ] CWWKF0011I: The defaultServer server is ready to run a smarter planet. The defaultServer server started in 23.484 seconds.

标签: minikubeconsulopen-liberty

解决方案


我更改了 dockerfile 中的 CMD 和 envconsul 的配置文件,现在它可以工作了

CMD ["/usr/local/bin/envconsul", "-log-level","debug", "-config","/etc/envconsul/envconsul-config.hcl"]

配置文件

# This block defines the configuration the the child process to execute and manage.
exec{
  # This is the command to execute as a child process. There can be only one command per process.
  command = "/opt/ol/wlp/bin/server run defaultServer"
  }

推荐阅读