Kubernetes——利用环境变量(env.value or env.valueFrom)配置容器应用
利用环境变量(env.value or env.valueFrom)配置容器应用
在 Kubernetes 中使用此类镜像启动容器时,也可以在 Pod 资源或 Pod 模板资源的定义中,为容器配置段使用 env 参数来定义所使用的环境变量列表。
环境变量配置容器化应用时,需要在容器配置段中嵌套使用 env 字段,它的值是一个由环境变量构建的列表。
pod.spec.containers 中嵌套字段 env 和 envFrom 解释如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | [root@mh-k8s-master-247-10 ~] # kubectl explain pod.spec.containers KIND: Pod VERSION: v1 RESOURCE: containers <[]Object> DESCRIPTION: List of containers belonging to the pod. Containers cannot currently be added or removed. There must be at least one container in a Pod. Cannot be updated. A single application container that you want to run within a pod. FIELDS: args <[]string> Arguments to the entrypoint. The docker image's CMD is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container's environment. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. Cannot be updated. More info: https: //kubernetes .io /docs/tasks/inject-data-application/define-command-argument-container/ #running-a-command-in-a-shell command <[]string> Entrypoint array. Not executed within a shell. The docker image's ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container's environment. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. Cannot be updated. More info: https: //kubernetes .io /docs/tasks/inject-data-application/define-command-argument-container/ #running-a-command-in-a-shell env <[]Object> List of environment variables to set in the container. Cannot be updated. envFrom <[]Object> 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. …… |
pod.spec.containers.env 字段定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@mh-k8s-master-247-10 ~] # kubectl explain pod.spec.containers.env KIND: Pod VERSION: v1 RESOURCE: env <[]Object> DESCRIPTION: List of environment variables to set in the container. Cannot be updated. EnvVar represents an environment variable present in a Container. FIELDS: name <string> -required- Name of the environment variable. Must be a C_IDENTIFIER. value <string> Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "" . valueFrom <Object> Source for the environment variable's value. Cannot be used if value is not empty. [root@mh-k8s-master-247-10 ~] # |
pod.spec.containers.envFrom 字段定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@mh-k8s-master-247-10 ~] # kubectl explain pod.spec.containers.envFrom KIND: Pod VERSION: v1 RESOURCE: envFrom <[]Object> DESCRIPTION: 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. EnvFromSource represents the source of a set of ConfigMaps FIELDS: configMapRef <Object> The ConfigMap to select from prefix <string> An optional identifier to prepend to each key in the ConfigMap. Must be a C_IDENTIFIER. secretRef <Object> The Secret to select from [root@mh-k8s-master-247-10 ~] # |
环境变量通常由 name 和 value(或 valueFrom)字段构成:
-
- name <string>: 环境变量的名称,必选字段。
- value <string>: 环境变量的值,通过 $(VAR_NAME)引用,逃逸格式为 "$$(VAR_NAME)",默认值为空。
- valueFrom <Object>: 环境变量值的引用源。例如,当前 Pod 资源的名称、名称空间、标签等,不能与非空值的 value 字段同时使用,即环境变量的值要么源于 value 字段,要么源于 valueFrom 字段,二者不可同时提供服数据。
valueFrom 字段可引用的值有多种来源,包括当前 Pod 资源的属性值,容器相关的系统资源配置、ConfigMap 对象中的 Key 以及 Secret 对象中的 Key,它们应分别使用不同的嵌套字段进行定义:
- configMapRef <Object>:ConfigMap 对象中的特定 Key。
- secretKeyRef <Object>: Secret 对象中的特定 Key。
分类:
Kubernetes专题精讲
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)