Kubernetes YAML Workloads/Basic StatefulSet YAML

Run stateful replicas with stable ordinals.

Section: StatefulSets

Basic StatefulSet YAML

yaml
yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres
  replicas: 3
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:17
          ports:
            - containerPort: 5432
              name: postgres
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing StatefulSets
Set rolling update partition
Control StatefulSet ordinal rollout.
OpenIn sheetyamlsame section
Basic DaemonSet YAML
Deploy a log collector or node agent everywhere.
Basic Job YAML
Run a one-time batch task with retries.
HorizontalPodAutoscaler YAML
Scale Deployment based on CPU utilization.
Restart a DaemonSet rollout
Trigger rollout after a config change.
Basic CronJob YAML
Schedule recurring batch work.