我正在研究Java Play项目,在application.conf文件中,我有一个Redis集群设置,该设置可接收Redis服务器节点的阵列。

现在,我想将该值作为环境变量注入(inject)Kubernetes部署中,并且找不到合适的语法来实现。

我当前的application.conf看起来像这样:

play.cache.redis {
  # enable cluster mode
  source: cluster

  # nodes are defined as a sequence of objects:
  cluster:  [
    {
      # required string, defining a host the node is running on
      host:        localhost
      # required integer, defining a port the node is running on
      port:        6379
      # optional string, defines a password to use
      password:    null
    }
  ]
}

有人可以告诉我如何将play.cache.redis.cluster变量传递给Kubernetes部署,使其保持这种状态吗?

最佳答案

您可以使用ConfigMaps机制注入(inject)整个application.conf:

apiVersion: v1
kind: ConfigMap
metatada:
  name: app-config
data:
  application.conf: |
    play.cache.redis {
      # enable cluster mode
      source: cluster
      # nodes are defined as a sequence of objects:
      cluster:  [
        {
          # required string, defining a host the node is running on
          host:        localhost
          # required integer, defining a port the node is running on
          port:        6379
          # optional string, defines a password to use
          password:    null
        }
      ]
    }

然后将其直接安装到您的容器中:
apiVersion: v1
kind: Pod
metadata:
  name: ....
spec:
  containers:
    - name: ...
      image: ...
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config

然后,该应用程序可以在/etc/config/application.conf中对其进行访问。

关于java - 将数组添加为Kubernetes环境变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50489692/

10-16 10:42