将数组添加为 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 部署,使其保持这样吗?

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

您可以将任何 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"

最新更新