Write a StatefulSet for a 3-node Redis or Postgres with per-pod storage and a headless Service.
AdvancedHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: v1
kind: Service
metadata: { name: pg }
spec:
clusterIP: None
selector: { app: pg }
ports: [{ port: 5432 }]
---
apiVersion: apps/v1
kind: StatefulSet
metadata: { name: pg }
spec:
serviceName: pg
replicas: 3
selector: { matchLabels: { app: pg } }
template:
metadata: { labels: { app: pg } }
spec:
containers:
- name: postgres
image: postgres:16
ports: [{ containerPort: 5432 }]
env:
- { name: PGDATA, value: /var/lib/postgresql/data/pgdata }
volumeMounts:
- { name: data, mountPath: /var/lib/postgresql/data }
volumeClaimTemplates:
- metadata: { name: data }
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: gp3
resources: { requests: { storage: 20Gi } }
Key trick → The headless Service (clusterIP: None) gives stable DNS names like pg-0.pg.<ns>.svc.cluster.local, and volumeClaimTemplates gives each pod its own PVC that survives rescheduling. The StatefulSet alone doesn't do replication; that needs an operator (CloudNativePG, Zalando) or app config.
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 a ConfigMap and Secret and inject them as environment variables and as a mounted file.IntermediateWrite an Ingress that routes `/api` to the api service and `/` to the web service with TLS.IntermediateWrite a pod spec with a hardened securityContext.Intermediate