Write a ConfigMap and Secret and inject them as environment variables and as a mounted file.
IntermediateHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: v1
kind: ConfigMap
metadata: { name: app-config }
data:
LOG_LEVEL: info
app.yaml: |
featureX: true
---
apiVersion: v1
kind: Secret
metadata: { name: app-secret }
type: Opaque
stringData:
DB_PASSWORD: s3cr3t
---
# in the pod spec
containers:
- name: app
image: myapp:1.0
envFrom:
- configMapRef: { name: app-config }
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef: { name: app-secret, key: DB_PASSWORD }
volumeMounts:
- { name: cfg, mountPath: /etc/app, readOnly: true }
volumes:
- name: cfg
configMap:
name: app-config
items: [{ key: app.yaml, path: app.yaml }]
Key trick → stringData lets you write plain text and the API server base64-encodes it. Env vars don't update when the ConfigMap changes, but mounted files do (eventually, and not when mounted with subPath).
More on Write-a-Kubernetes-manifest tasks
Write a Deployment and Service for an Nginx app with 3 replicas, resources and probes.BeginnerAdd an HPA that scales the Deployment between 3 and 15 pods at 70% CPU.BeginnerFix this broken Deployment YAML.BeginnerWrite an Ingress that routes `/api` to the api service and `/` to the web service with TLS.IntermediateWrite a pod spec with a hardened securityContext.IntermediateWrite a CronJob that runs a backup every night at 02:00 and keeps limited history.Intermediate