Define ConfigMaps (AKS)

AI-200: Azure AI Cloud Developer Associate · 39 of 112

Microsoft official learning paths

Azure Kubernetes Service (AKS)7
Deploy applications3
  1. Create K8s deployment manifest
  2. Como exponer apps
  3. Deploy apps to Azure Kubernete Service
Configure applications3
  1. Define ConfigMaps
  2. Define Secrets
  3. Attach persistent storage to an app
Monitor and troubleshoot applications1
  1. Monitor app logs and metrics
All 112 notes of the guide →
On this page (6)
  1. Define a ConfigMap
  2. Consumir ConfigMaps en un deployment
  3. Montar ficheros como ConfigMaps
  4. Usar ConfigMaps inmutables
  5. Integrar con Azure App Configuration
  6. Verificar con kubectl

Los ConfigMaps se usan para guardar config no sensible como feature flags o service endpoints. Estos ConfigMaps luego se inyectan en las imágenes y el source code.

Define a ConfigMap

Un ConfigMap es un recurso que guarda config data como valores clave-valor.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-settings
data:
  FEATURE_X_ENABLED: "true"
  SERVICE_ENDPOINT: "https://api.example.com"
  # File-like configuration can use multi-line values
  app.config: |
    log_level=info
    timeout_seconds=30

Consumir ConfigMaps en un deployment

Se inyecta la config en los pods a través de las variables de entorno. Podemos mapear valores específicos de un ConfigMap o inyectar directamente todos los valores.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-api
  template:
    metadata:
      labels:
        app: web-api
    spec:
      containers:
      - name: api
        image: myregistry.azurecr.io/web-api:v1
        env:
        - name: FEATURE_X_ENABLED
          valueFrom:
            configMapKeyRef:
              name: app-settings
              key: FEATURE_X_ENABLED
        - name: SERVICE_ENDPOINT
          valueFrom:
            configMapKeyRef:
              name: app-settings
              key: SERVICE_ENDPOINT
        # Alternative: load all keys as environment variables
        # envFrom:
        # - configMapRef:
        #     name: app-settings

Montar ficheros como ConfigMaps

Podemos montar un fichero como si fuera la config. Esto se apoya en el uso de un Volumen.

# Code fragment - focus on volume mount
volumes:
- name: config-volume
  configMap:
    name: app-settings
    # Optional: select specific keys to mount as files
    # items:
    # - key: app.config
    #   path: application.conf
containers:
- name: api
  volumeMounts:
  - name: config-volume
    mountPath: /app/config
    readOnly: true

Usar ConfigMaps inmutables

Un ConfigMap se puede marcar como inmutable para protegerlo contra cambios. Si quieres hacer cambios tienes que borrarlo y recrearlo. Esto funciona bien para configuración que no tiene que cambiar durante el runtime y para valores que cuando se cambian, hay que redesplegar.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-settings-v2
data:
  FEATURE_X_ENABLED: "true"
  SERVICE_ENDPOINT: "https://api.example.com"
immutable: true

Integrar con Azure App Configuration

Se puede integrar AKS con Azure App Configuration para centralizar la config a lo largo de distintas apps y entornos.

Verificar con kubectl

kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml
kubectl describe configmap app-settings
kubectl describe deployment web-api
# Check if Pod received configuration as environment variables
kubectl exec <pod-name> -- printenv | grep FEATURE
Found it useful? Share it
LinkedIn