# ๐Ÿงพ Kubernetes ConfigMap Guide Manage configuration without hardcoding! `ConfigMaps` let you store external configurations as key-value pairs and inject them into Kubernetes workloads. --- ## ๐Ÿ“˜ What is a ConfigMap? A **ConfigMap** is a Kubernetes object used to store **non-confidential configuration data** in **key-value** format. You can: * ๐Ÿ“„ Mount it as files inside a Pod * ๐ŸŒฟ Use it as environment variables * ๐Ÿงฉ Keep your container images decoupled from configuration --- ## โš™๏ธ Creating a ConfigMap You can create a ConfigMap from files or directories: ```bash kubectl -n create configmap --from-file= ``` ### โœ… Examples ```bash # From a single file kubectl -n create configmap nginx-conf --from-file=./nginx.conf # From multiple files kubectl -n create configmap nginx-conf \ --from-file=./nginx.conf \ --from-file=./site.conf ``` --- ## ๐Ÿ“‚ Viewing & Editing ConfigMaps ```bash # List all ConfigMaps in a namespace kubectl get cm -n # View detailed ConfigMap info kubectl describe configmap -n # Edit in-place kubectl -n edit configmap ``` --- ## ๐Ÿ“„ YAML: ConfigMap Example ```yaml apiVersion: v1 kind: ConfigMap metadata: name: game-demo data: player_initial_lives: "3" ui_properties_file_name: "user-interface.properties" game.properties: | enemy.types=aliens,monsters player.maximum-lives=5 user-interface.properties: | color.good=purple color.bad=yellow allow.textmode=true ``` --- ## ๐Ÿ’ก Usage Examples ### ๐Ÿ“Œ ConfigMap as Environment Variables **ConfigMap:** ```yaml apiVersion: v1 kind: ConfigMap metadata: name: app-config namespace: default data: APP_MODE: "production" LOG_LEVEL: "info" FEATURE_FLAG_X: "enabled" ``` **Pod:** ```yaml apiVersion: v1 kind: Pod metadata: name: my-app spec: containers: - name: my-container image: busybox command: ["/bin/sh", "-c", "env && sleep 3600"] envFrom: - configMapRef: name: app-config ``` --- ### ๐Ÿงพ Mounting ConfigMap as a File (nginx.conf example) **ConfigMap:** ```yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-config namespace: default data: nginx.conf: | worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } ``` **Pod:** ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx image: nginx:latest volumeMounts: - name: nginx-config-volume mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx-config-volume configMap: name: nginx-config ``` --- ## ๐Ÿ›  Pro Tips * ๐Ÿ” For **sensitive data**, use **Secrets** instead of ConfigMaps. * ๐Ÿงช Combine ConfigMaps with **Deployment** objects for flexible, versioned config management. * ๐Ÿ“ฆ Use `--from-env-file` to load multiple key-value pairs from a `.env` style file: ```bash kubectl create configmap my-config --from-env-file=env.list ```