覆盖容器规范中定义的环境值 [英] Override env values defined in container spec

查看:109
本文介绍了覆盖容器规范中定义的环境值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a configmap where I have defined the following key-value mapping in the data section:

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

然后在我的容器的定义(在Deployment/statefulset清单中)中,我具有以下内容:

then in the definition of my container (in the deployment / statefulset manifest) I have the following:

        env:
        - name: TEST
          value: "ANOTHER_VALUE"
        envFrom:
        - configMapRef:
            name: test-config

执行此操作时,我期望configmap中的值(TEST ="CONFIGMAP_VALUE")将覆盖容器规范(TEST ="ANOTHER_VALUE")中指定的(默认)值,但事实并非如此(TEST总是从容器规范中获取值).我找不到与此相关的任何文档-是否可以实现这样的env变量值替代?

When doing this I was expecting that the value from the configmap (TEST="CONFIGMAP_VALUE") will override the (default) value specified in the container spec (TEST="ANOTHER_VALUE"), but this is not the case (TEST always gets the value from the container spec). I couldn't find any relevant documentation about this - is it possible to achieve such env variable value overriding?

推荐答案

来自Kubernetes api引用:

From Kubernetes api refernece : https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#container-v1-core

envFrom:在容器中填充环境变量的源列表.在源中定义的密钥必须是C_IDENTIFIER.容器启动时,所有无效键将被报告为一个事件.当多个源中都存在键时,与最后一个源相关联的值将优先.由具有重复键的Env定义的值将优先.无法更新.

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.

因此,上面明确指出 env 将优先于 envFrom .

So above clearly states the env will take precedence than envFrom.

当一个键存在于多个源中时,与最后一个源相关联的值将优先.

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

因此,有关覆盖的信息,请参见以下内容:

So, for overriding see below:

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

这篇关于覆盖容器规范中定义的环境值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆