← Về danh sách bài học Bài 7/25

⚙️ Bài 7: ConfigMap - Quản Lý Cấu Hình

⏱️ Thời gian: 20 phút | 📚 Độ khó: Trung bình

🎯 Sau bài học này, bạn sẽ:

1. ConfigMap Là Gì?

ConfigMap lưu trữ dữ liệu cấu hình dạng key-value. Giúp tách config khỏi container image.

💡 Tại sao cần ConfigMap?
• Không hardcode config trong image
• Dùng chung config cho nhiều pods
• Thay đổi config không cần rebuild image
• Tách biệt config giữa các môi trường
⚠️ Lưu ý: ConfigMap cho dữ liệu KHÔNG nhạy cảm. Dữ liệu nhạy cảm (passwords, tokens) dùng Secret (bài sau).

2. Tạo ConfigMap

Cách 1: Từ literal (key=value)

kubectl create configmap app-config \
    --from-literal=APP_ENV=production \
    --from-literal=LOG_LEVEL=info \
    --from-literal=MAX_CONNECTIONS=100

# Xem
kubectl get configmap app-config -o yaml

Cách 2: Từ file

# Tạo file config
echo "DATABASE_HOST=mysql.example.com
DATABASE_PORT=3306
DATABASE_NAME=myapp" > app.properties

# Tạo configmap từ file
kubectl create configmap app-config --from-file=app.properties

# Hoặc từ nhiều files
kubectl create configmap app-config \
    --from-file=app.properties \
    --from-file=nginx.conf

Cách 3: Từ YAML (khuyên dùng)

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  # Key-value đơn giản
  APP_ENV: "production"
  LOG_LEVEL: "info"
  MAX_CONNECTIONS: "100"
  
  # File config (multiline)
  nginx.conf: |
    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_pass http://backend:8080;
        }
    }
  
  app.properties: |
    database.host=mysql.example.com
    database.port=3306
    database.name=myapp
kubectl apply -f configmap.yaml
kubectl describe configmap app-config

3. Sử Dụng ConfigMap Trong Pod

Cách 1: Environment Variables (một số keys)

apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
    - name: app
      image: myapp:1.0
      env:
        - name: APP_ENVIRONMENT    # Tên env trong container
          valueFrom:
            configMapKeyRef:
              name: app-config     # Tên ConfigMap
              key: APP_ENV         # Key trong ConfigMap

Cách 2: Tất cả keys thành env (envFrom)

apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
    - name: app
      image: myapp:1.0
      envFrom:
        - configMapRef:
            name: app-config
        # Tất cả keys trong configmap → env vars

Cách 3: Mount như Volume (files)

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.24
      volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d
          # nginx.conf sẽ tạo ở /etc/nginx/conf.d/nginx.conf
  volumes:
    - name: config-volume
      configMap:
        name: app-config
        items:
          - key: nginx.conf       # Key trong ConfigMap
            path: default.conf    # Tên file trong container
💡 Khi nào dùng gì?
env: Config đơn giản, ít keys
envFrom: Inject tất cả keys
volume: File config phức tạp (nginx.conf, app.properties)

4. Ví Dụ Hoàn Chỉnh

# app-with-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: webapp-config
data:
  APP_NAME: "MyWebApp"
  APP_ENV: "production"
  LOG_LEVEL: "info"
  config.json: |
    {
      "database": {
        "host": "mysql.default.svc.cluster.local",
        "port": 3306
      },
      "cache": {
        "enabled": true,
        "ttl": 3600
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
        - name: webapp
          image: nginx:1.24
          ports:
            - containerPort: 80
          env:
            - name: APP_NAME
              valueFrom:
                configMapKeyRef:
                  name: webapp-config
                  key: APP_NAME
          envFrom:
            - configMapRef:
                name: webapp-config
          volumeMounts:
            - name: config-file
              mountPath: /app/config
      volumes:
        - name: config-file
          configMap:
            name: webapp-config
            items:
              - key: config.json
                path: config.json

5. Update ConfigMap

# Edit trực tiếp
kubectl edit configmap app-config

# Apply file mới
kubectl apply -f configmap.yaml
⚠️ Quan trọng về Update:
Volume mount: Tự động update sau ~1 phút
Env vars: KHÔNG tự update, phải restart pod
kubectl rollout restart deployment/webapp

📝 Tóm Tắt Bài Học