yamlbeginner

Kubernetes ConfigMap and Secret Management

Create and use ConfigMaps and Secrets for application configuration with env vars and volume mounts.

yaml
# ConfigMap for non-sensitive configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: production
data:
  APP_ENV: production
  LOG_LEVEL: info
  MAX_CONNECTIONS: '100'
  config.json: |
    {
      "features": {
        "darkMode": true,
        "betaAccess": false
      }
    }
---
# Secret for sensitive data (base64 encoded)
apiVersion: v1
kind: Secret
metadata:
  name: app-secrets
  namespace: production
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQxMjM=
  API_KEY: c2stbGl2ZS1hYmMxMjM=
stringData:
  JWT_SECRET: my-super-secret-jwt-key
---
# Pod using both ConfigMap and Secret
apiVersion: v1
kind: Pod
metadata:
  name: api-pod
spec:
  containers:
    - name: api
      image: api-server:latest
      envFrom:
        - configMapRef:
            name: app-config
        - secretRef:
            name: app-secrets
      volumeMounts:
        - name: config-volume
          mountPath: /app/config
          readOnly: true
  volumes:
    - name: config-volume
      configMap:
        name: app-config
        items:
          - key: config.json
            path: config.json

Sponsored

DigitalOcean

Use Cases

  • Managing application configuration in Kubernetes
  • Injecting secrets without hardcoding
  • Mounting configuration files into containers

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.