ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Kubernetes——利用环境变量(env.value or env.valueFrom)配置容器应用

2022-06-25 16:33:17  阅读:190  来源: 互联网

标签:environment container NAME Kubernetes valueFrom value will env VAR


利用环境变量(env.value or env.valueFrom)配置容器应用

  在 Kubernetes 中使用此类镜像启动容器时,也可以在 Pod 资源或 Pod 模板资源的定义中,为容器配置段使用 env 参数来定义所使用的环境变量列表。

  环境变量配置容器化应用时,需要在容器配置段中嵌套使用 env 字段,它的值是一个由环境变量构建的列表。

  pod.spec.containers 中嵌套字段 env 和 envFrom 解释如下:

[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 字段定义如下:

[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 字段定义如下:

[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。

标签:environment,container,NAME,Kubernetes,valueFrom,value,will,env,VAR
来源: https://www.cnblogs.com/zuoyang/p/16411839.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有