Interview Prep/Real-World Interview/Write-a-Kubernetes-manifest tasks

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.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Kubernetes-manifest tasks