diff --git a/Containerization & Orchestration/Kubernetes/workloads/11-Config-map.md b/Containerization & Orchestration/Kubernetes/workloads/11-Config-map.md index 778a5c7..6dce7d1 100644 --- a/Containerization & Orchestration/Kubernetes/workloads/11-Config-map.md +++ b/Containerization & Orchestration/Kubernetes/workloads/11-Config-map.md @@ -1,18 +1,22 @@ -# ๐Ÿงพ Kubernetes ConfigMap +# ๐Ÿงพ Kubernetes ConfigMap Guide -## ๐Ÿ“˜ What is a ConfigMap? - -A **ConfigMap** allows you to **store configuration data** in key-value pairs that can be: - -* Mounted as files in a Pod -* Exposed as environment variables -* Consumed by Kubernetes workloads without hardcoding settings in container images +Manage configuration without hardcoding! `ConfigMaps` let you store external configurations as key-value pairs and inject them into Kubernetes workloads. --- -## โš™๏ธ Creating a ConfigMap from Files +## ๐Ÿ“˜ What is a ConfigMap? -Create a ConfigMap from one or more files: +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= @@ -21,28 +25,33 @@ kubectl -n create configmap --from-file= create configmap nginx-conf --from-file=./nginx.conf -# Create from multiple files -kubectl -n create configmap nginx-conf --from-file=./nginx.conf --from-file=./site.conf +# From multiple files +kubectl -n create configmap nginx-conf \ + --from-file=./nginx.conf \ + --from-file=./site.conf ``` --- -## ๐Ÿ“‚ Viewing and Editing ConfigMaps +## ๐Ÿ“‚ Viewing & Editing ConfigMaps ```bash -# List ConfigMaps in a namespace +# List all ConfigMaps in a namespace kubectl get cm -n -# Edit a ConfigMap +# View detailed ConfigMap info +kubectl describe configmap -n + +# Edit in-place kubectl -n edit configmap ``` --- -## ๐Ÿ“„ ConfigMap YAML Example +## ๐Ÿ“„ YAML: ConfigMap Example ```yaml apiVersion: v1 @@ -50,14 +59,13 @@ kind: ConfigMap metadata: name: game-demo data: - # Key-value style player_initial_lives: "3" ui_properties_file_name: "user-interface.properties" - # Multi-line file-style entries game.properties: | enemy.types=aliens,monsters player.maximum-lives=5 + user-interface.properties: | color.good=purple color.bad=yellow @@ -66,39 +74,115 @@ data: --- -## ๐Ÿš€ Deployment Example: Using ConfigMap as Volume +## ๐Ÿ’ก Usage Examples + +### ๐Ÿ“Œ ConfigMap as Environment Variables + +**ConfigMap:** ```yaml -apiVersion: apps/v1 -kind: Deployment +apiVersion: v1 +kind: ConfigMap metadata: - name: app-1 - namespace: dev - labels: - app.kubernetes.io/name: nginx -spec: - replicas: 3 - selector: - matchLabels: - app.kubernetes.io/name: nginx - template: - metadata: - labels: - app.kubernetes.io/name: nginx - os: linux - spec: - containers: - - name: nginx - image: nginx - ports: - - containerPort: 80 - volumeMounts: - - name: configfile - mountPath: /etc/nginx/nginx.conf - subPath: nginx.conf # Mount specific file from ConfigMap - volumes: - - name: configfile - configMap: - name: nginx-conf + 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 + ``` +