Skip to content

Kubernetes ConfigMap

A ConfigMap is an API object used in Kubernetes to decouple configuration artifacts from image content to keep containerized applications portable.^[400-devops-06-kubernetes-k8s-ithelp-day16-readme.md] It allows users to store configuration data as key-value pairs or files, which can then be injected into Pods.

Purpose

The primary function of a ConfigMap is to store non-sensitive configuration data, such as environment variables, initialization scripts, or configuration files.^[400-devops-06-kubernetes-k8s-ithelp-day16-readme.md] By externalizing configuration, ConfigMaps help reduce code coupling; the same application code can be deployed across different environments (e.g., Development, Production) simply by switching the associated ConfigMap.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]

Key Characteristics

  • Data Storage: A single ConfigMap object can hold one or multiple configuration files or key-value pairs.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]
  • Unified Management: ConfigMaps provide a centralized way to manage and view configuration files within the Kubernetes cluster.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]
  • Volume Type: ConfigMap is treated as a type of Volume in Kubernetes.^[400-devops-06-kubernetes-k8s-ithelp-day16-readme.md]

Creating a ConfigMap

There are several methods to create a ConfigMap:

  • From a file: Use kubectl create [ConfigMap](<./configmap.md>) <name> --from-file=<path> to import an entire file's content into the ConfigMap.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]
  • From literals: Use kubectl create [ConfigMap](<./configmap.md>) <name> --from-literal=<key>=<value> to create specific key-value pairs directly via the command line.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]
  • From YAML: Define the ConfigMap structure in a YAML manifest using apiVersion: v1, kind: ConfigMap, and specifying the data under the data field.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]

Usage in Pods

ConfigMaps can be consumed by Pods in two main ways:

  1. Environment Variables: Data from the ConfigMap can be injected into the container as environment variables using the valueFrom.configMapKeyRef field in the Pod specification.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md]
  2. Mounted Files: The ConfigMap can be mounted as a Volume. Files defined in the ConfigMap will be created in the specified mountPath inside the container.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md, 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md] This is commonly used for initialization scripts (e.g., placing an SQL script in /docker-entrypoint-initdb.d).
  • Kubernetes Secrets: Similar to ConfigMaps but designed for storing sensitive data (e.g., passwords, tokens), often with base64 encoding.^[400-devops-06-kubernetes-k8s-ithelp-day16-readme.md]
  • Kubernetes Volume: ConfigMaps are one of many volume types available for data management in Pods.
  • Kubernetes Pod: The unit where ConfigMaps are consumed.
  • Kubernetes Deployment

Sources

  • 400-devops-06-kubernetes-k8s-ithelp-day16-readme.md
  • 400-devops-06-kubernetes-k8s-ithelp-day18-readme.md
  • 400-devops__06-Kubernetes__k8s-ithelp__Day18__README.md