update config map doc

This commit is contained in:
2025-07-28 21:24:14 +03:30
parent e2f00da11f
commit c92308fd54

View File

@@ -1,18 +1,22 @@
# 🧾 Kubernetes ConfigMap # 🧾 Kubernetes ConfigMap Guide
## 📘 What is a ConfigMap? Manage configuration without hardcoding! `ConfigMaps` let you store external configurations as key-value pairs and inject them into Kubernetes workloads.
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
--- ---
## ⚙️ 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 ```bash
kubectl -n <namespace> create configmap <configmap-name> --from-file=<file-or-directory> kubectl -n <namespace> create configmap <configmap-name> --from-file=<file-or-directory>
@@ -21,28 +25,33 @@ kubectl -n <namespace> create configmap <configmap-name> --from-file=<file-or-di
### ✅ Examples ### ✅ Examples
```bash ```bash
# Create from a single file # From a single file
kubectl -n <ns> create configmap nginx-conf --from-file=./nginx.conf kubectl -n <ns> create configmap nginx-conf --from-file=./nginx.conf
# Create from multiple files # From multiple files
kubectl -n <ns> create configmap nginx-conf --from-file=./nginx.conf --from-file=./site.conf kubectl -n <ns> create configmap nginx-conf \
--from-file=./nginx.conf \
--from-file=./site.conf
``` ```
--- ---
## 📂 Viewing and Editing ConfigMaps ## 📂 Viewing & Editing ConfigMaps
```bash ```bash
# List ConfigMaps in a namespace # List all ConfigMaps in a namespace
kubectl get cm -n <namespace> kubectl get cm -n <namespace>
# Edit a ConfigMap # View detailed ConfigMap info
kubectl describe configmap <name> -n <namespace>
# Edit in-place
kubectl -n <namespace> edit configmap <configmap-name> kubectl -n <namespace> edit configmap <configmap-name>
``` ```
--- ---
## 📄 ConfigMap YAML Example ## 📄 YAML: ConfigMap Example
```yaml ```yaml
apiVersion: v1 apiVersion: v1
@@ -50,14 +59,13 @@ kind: ConfigMap
metadata: metadata:
name: game-demo name: game-demo
data: data:
# Key-value style
player_initial_lives: "3" player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties" ui_properties_file_name: "user-interface.properties"
# Multi-line file-style entries
game.properties: | game.properties: |
enemy.types=aliens,monsters enemy.types=aliens,monsters
player.maximum-lives=5 player.maximum-lives=5
user-interface.properties: | user-interface.properties: |
color.good=purple color.good=purple
color.bad=yellow color.bad=yellow
@@ -66,39 +74,115 @@ data:
--- ---
## 🚀 Deployment Example: Using ConfigMap as Volume ## 💡 Usage Examples
### 📌 ConfigMap as Environment Variables
**ConfigMap:**
```yaml ```yaml
apiVersion: apps/v1 apiVersion: v1
kind: Deployment kind: ConfigMap
metadata: metadata:
name: app-1 name: app-config
namespace: dev namespace: default
labels: data:
app.kubernetes.io/name: nginx APP_MODE: "production"
spec: LOG_LEVEL: "info"
replicas: 3 FEATURE_FLAG_X: "enabled"
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
``` ```
**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
```