我什么时候应该使用 envFrom 作为配置映射?

hum*_*uin 18 environment-variables kubernetes configmap

根据 v1.6 及更高版本的 Kubernetes 文档K8s Docs,我们可以使用:

envFrom:
  - configMapRef:
      name: <config-file>
Run Code Online (Sandbox Code Playgroud)

将所有 configMaps 数据定义为容器环境变量。使用它与将其设置为具有名称和键的环境变量的用例是什么:

env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
Run Code Online (Sandbox Code Playgroud)

如果文件中有多个变量但只想提取单个键=值对,您会使用第二个示例吗?我试图了解我在 CKAD 考试中可能遇到的场景的不同用例。

ITC*_*hap 36

我已经使用了这两种环境,并且我的一些部署使用了数十个环境变量。这在很大程度上取决于您的具体情况以及您管理设置的方式。

一般来说,如果您对特定应用程序有特定的配置映射,其中您的应用程序使用配置映射中的所有或大部分键,那么envFrom显然更容易使用和维护。例如,当您的一位队友需要添加新功能标志时,只需将其添加到配置映射中就足以在您的所有部署上启用它。

另一方面,如果您更多地按主题组织配置映射,或者多个应用程序需要同一配置映射中的特定键,则configMapKeyRef更好。您将仅获取应用程序中所需的密钥,并确保不会意外覆盖任何内容。缺点是您的队友为了添加相同的功能标志,现在必须编辑配置映射和部署。

请记住,这两个选项并不排斥,您可能最终会混合使用两者。例如,这样的事情可能是有意义的:

envFrom:
    # This might contain environment-wide settings. Like the domain name that your application uses or a production only feature flag.
  - configMapRef:
      name: production-settings
    # Here you could store all the settings of this specific application.
  - configMapRef:
      name: my-app-settings
env:
  # This might be a bucket shared by multiple applications. So you might want to keep it a different configmap and let each aplication pick the keys they need.
  - name: S3_BUCKET
    valueFrom:
      configMapKeyRef:
        name: s3-settings
        key: bucket
Run Code Online (Sandbox Code Playgroud)