Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use configMap for every environment variable?

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 }}
...

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

like image 654
yozel Avatar asked Nov 27 '18 09:11

yozel


2 Answers

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.

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'

like image 176
Ryan Dawson Avatar answered Nov 08 '22 11:11

Ryan Dawson


It would work even if you don't use a configmap, but it has some advantages:

  • You can update the values at runtime, without updating a deployment. Which means you might not need to restart your application (pods). If you don't use a config map, everytime you update the value, your application (or pod) will be recreated.
  • Separation of concerns, i.e. deployment configuration and external values separated
like image 25
Hazim Avatar answered Nov 08 '22 12:11

Hazim