其他分享
首页 > 其他分享> > Kubernetes——利用环境变量(env.value or env.valueFrom)配置容器应用

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

作者:互联网

利用环境变量(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)字段构成:

  valueFrom 字段可引用的值有多种来源,包括当前 Pod 资源的属性值,容器相关的系统资源配置、ConfigMap 对象中的 Key 以及 Secret 对象中的 Key,它们应分别使用不同的嵌套字段进行定义:

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