本文介绍了我应该为每个环境变量使用configMap吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用头盔.我的项目是这样的:

I am using helm right now. My project is like that:

values.yaml:

environmentVariables:
  KEY1: VALUE1
  KEY2: VALUE2

configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "myproject.fullname" . }}
data:
{{- range $k, $v := .Values.environmentVariables }}
  {{ $k }}: {{ $v | quote }}
{{- end }}

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "myproject.fullname" . }}
spec:
  template:
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
{{- range $k, $v := .Values.environmentVariables }}
            - name: {{ $k }}
              valueFrom:
                configMapKeyRef:
                  name: {{ template "myproject.fullname" $ }}
                  key: {{ $k }}
{{- end }}
...

但是现在,我真的很困惑.我真的需要这个configmap吗?将configmap用于环境变量有什么好处?

But right now, I'm really confused. Am I really need this configmap? Is there any benefit to use configmap for environment variables?

推荐答案

除了将config与pod分开的要点外,ConfigMap的一个优点是它使您可以让其他Pod或应用访问变量的值不一定是您图表的一部分.

Aside from the points about separation of config from pods, one advantage of a ConfigMap is it lets you make the values of the variables accessible to other Pods or apps that are not necessarily part of your chart.

它确实增加了一些额外的复杂性,并且在何时使用ConfigMap方面可能会有很大的偏好.由于您的ConfigMap键是环境变量的名称,因此您可以通过使用'envFrom'来简化部署.

It does add a little extra complexity though and there can be a large element of preference about when to use a ConfigMap. Since your ConfigMap keys are the names of the environment variables you could simplify your Deployment a little by using 'envFrom'

这篇关于我应该为每个环境变量使用configMap吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 22:39