Should I use configMap for every environment variable?

kubernetes dynamic environment variables
kubernetes configmap environment variables
kubectl set environment variable
kubernetes init container environment variables
kubernetes update configmap
kubernetes dynamic configmap
minikube environment variables
google kubernetes engine environment variables

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?

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'

Kubernetes Patterns : Environment Variables Configuration Pattern, Each and every application out there needs external configuration at Like configMaps variables, Secret variables can be exposed to the  The Twelve Factor App guide advocates the use of environment variables because when using configuration files. it’s easy to mistakenly check in a config file to the repo; there is a tendency for config files to be scattered about in different places and different formats, making it hard to see and manage all the config in one place.

Configure a Pod to Use a ConfigMap, A ConfigMap allows you to decouple environment-specific configuration from You write the code to look in an environment variable named DATABASE_HOST . Each ConfigMap you want to use needs to be referred to in .spec.volumes . ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. This page provides a series of usage examples demonstrating how to create ConfigMaps and configure Pods using data stored in ConfigMaps. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your

I feel like this is largely a matter of taste; but I've generally been avoiding ConfigMaps for cases like these.

env:
{{- range $k, $v := .Values.environmentVariables }}
  - name: {{ quote $k }}
    value: {{ quote $v }}
{{- end }}

You generally want a single source of truth and Helm can be that: you don't want to be in a situation where someone has edited a ConfigMap outside of Helm and a redeployment breaks local changes. So there's not a lot of value in a ConfigMap being "more editable" than a Deployment spec.

In principle (as @Hazim notes) you can update a ConfigMap contents without restarting a container, but that intrinsically can't update environment variables in running containers, and restarting containers is so routine that doing it once shouldn't matter much.

ConfigMaps, to store fine-grained information like individual properties or coarse-grained information like entire config files or JSON blobs. Sure, I could set something on every deployment and daemonset and manual pod and . but that is overhead and is messy. Could use configmap or secret, same issue. The cluster administrator should be able to say, "define the following environment vars that are injected into every container in the cluster (or, at best, limited by namespace).

Define Environment Variables for a Container, as environment variables, command-line arguments, or as configuration files in a volumeA directory containing data, accessible to the containers in a pod. . Use ConfigMap-defined environment variables in Pod commands. You can use ConfigMap-defined environment variables in the command section of the Pod specification using the $(VAR_NAME) Kubernetes substitution syntax. For example, the following Pod specification

Configmap, To configure your apps in Kubernetes, you can use: Good old environment variables; ConfigMap; Secret — this will be covered in a subsequent blog post. You will need e.g. to install kubectl for Mac, all you need is curl -LO  A ConfigMap is an API object used to store non-confidential data in key-value pairs. PodsA Pod represents a set of running containers in your cluster. can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volumeA directory containing data, accessible to the containers in a pod. . A ConfigMap allows you to decouple environment-specific

Learn how to configure Kubernetes apps using ConfigMap, You can use the kubectl create configmap command to create configmaps easily command can be used to create a ConfigMap holding the content of each file in ConfigMaps can be used to populate individual environment variables or be​  The DB_PORT environment variable, the volumeMount and the podpreset.admission.kubernetes.io annotation of the Pod verify that the preset has been applied. Pod spec with ConfigMap example. This is an example to show how a Pod spec is modified by a Pod preset that references a ConfigMap containing environment variables.

Comments
  • I didn't know that envFrom exists. I use my ConfigMap with other pods and this is a great way to do that. Thank you.
  • I don't think I can update values at runtime. Environment variables in a pod can't be updated without restart. But separation of concern is a good point.