bashintermediate

Kubernetes ConfigMap and Secrets

Create and use Kubernetes ConfigMaps and Secrets for application configuration management.

bash
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: production
  LOG_LEVEL: info
  MAX_CONNECTIONS: "100"
  config.json: |
    {
      "feature_flags": {
        "new_ui": true,
        "beta_api": false
      }
    }
---
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: app-secrets
type: Opaque
stringData:
  DATABASE_URL: postgres://user:pass@db:5432/app
  API_KEY: sk-your-api-key-here
---
# deployment using configmap and secrets
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  template:
    spec:
      containers:
        - name: app
          image: myapp: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

# kubectl create configmap app-config --from-file=config.properties
# kubectl create secret generic app-secrets --from-literal=API_KEY=value

Use Cases

  • Externalizing application configuration in K8s
  • Securely managing sensitive credentials
  • Sharing configuration across multiple pods

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.