【问题标题】:How can I set JNDI configuration in a Docker overrides.yaml file?如何在 Docker overrides.yaml 文件中设置 JNDI 配置?
【发布时间】:2022-01-24 18:24:04
【问题描述】:

如果我有一个 java 配置 bean,说:

package com.mycompany.app.configuration;

// whatever imports

public class MyConfiguration {

  private String someConfigurationValue = "defaultValue"; 

  // getters and setters etc
}

如果我设置使用码头进行本地测试,我可以使用config.xml 文件,格式如下:

  <myConfiguration class="com.mycompany.app.configuration.MyConfiguration" context="SomeContextAttribute">
    <someConfigurationValue>http://localhost:8080</someConfigurationValue>
  </myConfiguration>

但是在我需要测试的部署环境中,我需要使用docker来设置这些配置值,我们使用jboss。

有没有办法直接设置这些 JNDI 值?我一直在寻找示例,但找不到任何示例。这将在用于配置 k8 集群的 yaml 文件的上下文中。为伪代码道歉,我会发布真实代码,但它都是专有的,所以我不能。

到目前为止,overrides.yaml sn-p 的格式如下:

env:
    'MyConfig.SomeContextAttribute':
      class_name: 'com.mycompany.app.configuration.MyConfiguration'
      someConfigurationValue: 'http://localhost:8080'

然而,这是一个完整的猜测。

【问题讨论】:

  • 应用程序是直接读取env_vars还是应该从pod中的其他文件读取变量?
  • 应用程序直接读取环境变量。

标签: docker kubernetes configuration yaml jndi


【解决方案1】:

你可以使用ConfigMap来实现它。

ConfigMap 是一个 API 对象,用于在键值对中存储非机密数据。 Pod 可以将 ConfigMap 用作环境变量、命令行参数或卷中的配置文件。

首先,您需要使用以下命令从文件中创建ConfigMap

kubectl create configmap <map-name> <data-source>

其中&lt;map-name&gt; 是您要分配给ConfigMap 的名称,&lt;data-source&gt; 是从中提取数据的目录、文件或文字值。你可以阅读更多关于它的信息here

这是一个例子:

  1. 下载示例文件:
wget https://kubernetes.io/examples/configmap/game.properties

您可以使用cat 命令检查此文件中的内容:

cat game.properties 

你会看到这个文件中有一些变量:

enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30r
  1. 从此文件创建ConfigMap
kubectl create configmap game-config --from-file=game.properties

您应该会看到 ConfigMap 已创建的输出:

configmap/game-config created

您可以使用以下命令显示ConfigMap 的详细信息:

kubectl describe configmaps game-config

你会看到如下输出:

Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

您还可以使用以下方法查看 yamlConfigMap 的外观:

kubectl get configmaps game-config -o yaml

输出将是相似的:

apiVersion: v1
data:
  game.properties: |-
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
kind: ConfigMap
metadata:
  creationTimestamp: "2022-01-28T12:33:33Z"
  name: game-config
  namespace: default
  resourceVersion: "2692045"
  uid: 5eed4d9d-0d38-42af-bde2-5c7079a48518

下一个目标是将ConfigMap 连接到Pod。可以添加到Podconfiguration的yaml文件中。
正如您在containers 下看到的,有envFrom 部分。因为name 是我在上一步中创建的ConfigMap 的名称。你可以阅读envFromhere

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: nginx
      envFrom:
      - configMapRef:
          name: game-config

使用以下方法从 yaml 文件创建Pod

kubectl apply -f <name-of-your-file>.yaml

最后一步是使用以下命令检查 Pod 中的环境变量:

kubectl exec -it test-pod -- env

如下所示,我在第一步下载的简单文件中有环境变量:

game.properties=enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

【讨论】:

    【解决方案2】:

    方法如下:

    如果您尝试设置一个完全限定名称如下所示的值:

    com.mycompany.app.configuration.MyConfiguration#someConfigurationValue 
    

    然后在 yaml 文件中将如下所示:

    com_mycompany_app_configuration_MyConfiguration_someConfigurationValue: 'blahValue'
    

    真的就是这么简单。它确实需要在 yaml 中设置为环境变量,但我不确定它是否需要env: 下,或者这是否是我们特有的。

    但是,我不认为有一种方法可以在 YAML 中设置 XML 中的属性。我已经尝试弄清楚那部分,但我没能做到。

    【讨论】:

    • 答案不适用于此,请更新您的问题
    猜你喜欢
    • 2018-03-02
    • 2020-08-23
    • 2015-06-06
    • 2020-08-07
    • 1970-01-01
    • 2013-07-06
    • 2020-10-17
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多