A ConfigMap stores non-sensitive configuration as a Kubernetes resource, keeping values separate from the containers that consume them. This is useful when a cluster spans multiple machines and server-level environment variables or configuration files are impractical. section 10.7
Provide a database URL
Create a ConfigMap named hello-api with database_url set to redis-cluster. Applying k8s/hello-api/config.yml with kubectl creates the resource, which a Deployment can expose to its container as DATABASE_URL through configMapKeyRef. section 10.7
Listing 10.8 config.yml
apiVersion: v1 kind: ConfigMap metadata: name: hello-api data: database_url: "redis-cluster" #1
The YAML shows the minimal ConfigMap structure: apiVersion, kind, metadata, and a data entry for database_url. section 10.7
Listing 10.9 deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-api
spec:
replicas: 1
selector:
matchLabels:
app: hello-api
template:
metadata:
name: hello-api
labels:
app: hello-api
spec:
containers:
- name: hello-api
imagePullPolicy: Never
image: holmes89/hello-api:v0.3
ports:
- containerPort: 8080
name: hello-api-svc
env:
- name: DATABASE_URL #1
valueFrom:
configMapKeyRef:
name: hello-api
key: database_url
- name: DATABASE_PASSWORD #2
valueFrom:
secretKeyRef:
name: redis-cluster
key: redis-password
optional: false
The Deployment connects the container's DATABASE_URL environment variable to the ConfigMap while sourcing DATABASE_PASSWORD from a Secret. section 10.7







