gpt4 book ai didi

deployment - 覆盖容器规范中定义的 env 值

转载 作者:行者123 更新时间:2023-12-03 08:25:35 27 4
gpt4 key购买 nike

我有一个 configmap,我在 data 中定义了以下键值映射部分:

apiVersion: v1
kind: ConfigMap
metadata:
namespace: test
name: test-config
data:
TEST: "CONFIGMAP_VALUE"

然后在我的容器的定义中(在部署/状态集 list 中)我有以下内容:
        env:
- name: TEST
value: "ANOTHER_VALUE"
envFrom:
- configMapRef:
name: test-config

这样做时,我期望来自 configmap 的值 (TEST="CONFIGMAP_VALUE") 将覆盖容器规范中指定的(默认)值(TEST="ANOTHER_VALUE"),但情况并非如此(TEST 总是得到值来自容器规范)。我找不到任何关于此的相关文档 - 是否有可能实现这种 env 变量值覆盖?

最佳答案

来自 Kubernetes API reference :

envFrom : List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.


所以上面清楚地说明了 环境 将优先于 envFrom .

When a key exists in multiple sources, the value associated with the last source will take precedence.


因此,对于覆盖,不要使用 envFrom ,但在 env 内定义该值两次, 见下文:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: default
name: test-config
data:
TEST: "CONFIGMAP_VALUE"
---
apiVersion: v1
kind: Pod
metadata:
name: busy
namespace: default
spec:
containers:
- name: busybox
image: busybox
env:
- name: TEST
value: "DEFAULT_VAULT"
- name: TEST
valueFrom:
configMapKeyRef:
name: test-config
key: TEST
command:
- "sh"
- "-c"
- >
while true; do
echo "$(TEST)";
sleep 3600;
done
查看:
kubectl logs busy -n default
CONFIGMAP_VALUE

关于deployment - 覆盖容器规范中定义的 env 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54398272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com