将数组添加为Kubernetes环境变量

时间:2018-05-23 13:27:19

标签: java playframework redis kubernetes

我正在开发一个Java Play项目,在我的application.conf文件中,我有一个Redis集群设置,它接收一组Redis服务器节点。

现在,我想在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部署,以便它保持这样吗?

2 个答案:

答案 0 :(得分:4)

您可以使用ConfigMaps机制注入整个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。

答案 1 :(得分:1)

您可以将任何env变量定义到pod中。为此,您需要在部署中更新规范部分:

以下是一个例子:

spec:
  containers:
  - name: container-name
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: VAR_NAME
      value: "any var value"
    - name: VAR_NAME2
      value: "another value"