Workloads: Deployments, rollouts and jobs · 5 MIN READ

StatefulSets for stable identity and storage, DaemonSets for one Pod per node

Run a StatefulSet with a headless Service and volumeClaimTemplates, predict its ordering and storage behaviour, and run a DaemonSet that covers every node, tainted ones included.

Deployments treat Pods as interchangeable: random names, new IPs, no memory of earlier replicas. Databases, queues and consensus systems need the opposite: each member keeps its name, its DNS record and its disk across restarts. Node agents need a third shape, exactly one Pod on every node. StatefulSets and DaemonSets provide those two guarantees.

kubernetes · A MAP OF THE IDEA

  1. Create a headless Service
  2. StatefulSet with claim templates
  3. Ordered start, stable DNS
  4. DaemonSet for node agents

Useful when

  • Members keep names, DNS records and volumes across restarts.
  • DaemonSets follow nodes as they join and leave.

Watch out

  • Claims outlive the StatefulSet and keep costing storage.
  • A DaemonSet skips tainted nodes unless it tolerates them.

Implementation & operation cost: A persistent volume per replica with backups, an application that handles its own replication, and small DaemonSet requests on every node.

01

What a StatefulSet guarantees

A StatefulSet named db with three replicas creates Pods db-0, db-1 and db-2. Each name is stable: if db-1 is deleted or its node fails, the replacement is again db-1, with the same DNS name and the same volume, although usually with a new IP. With the default podManagementPolicy: OrderedReady, Pods are created one at a time in ordinal order, each waiting until the previous one is Running and Ready, and removed in reverse order when you scale down. Rolling updates also go from the highest ordinal down, and a partition lets you update only ordinals at or above a number, as a canary. Parallel management drops the ordering when members do not depend on each other.

02

Headless Service and volumeClaimTemplates

serviceName points at a headless Service, one with clusterIP: None. Instead of a virtual IP, its DNS name returns the addresses of the ready Pods, and each Pod gets its own record of the form <pod>.<service>.<namespace>.svc.cluster.local. volumeClaimTemplates make the controller create one PersistentVolumeClaim per ordinal, named <template>-<pod>, here data-redis-0 to data-redis-2, and mount it into that Pod only. The StatefulSet does not create the Service; it must exist for the per-Pod DNS records to work.

yaml
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  clusterIP: None
  selector:
    app: redis
  ports:
  - name: redis
    port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7
        ports:
        - name: redis
          containerPort: 6379
        volumeMounts:
        - name: data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi
Read the code

The headless Service selects app: redis and names its port. The StatefulSet links to it with serviceName: redis. Each replica gets its own 1Gi ReadWriteOnce claim from the default StorageClass, standard in kind, mounted at /data where Redis writes its snapshots. Replication between the three Redis processes is not configured here: that is the application's job, not Kubernetes'.

03

Volumes that outlive Pods

A claim created from volumeClaimTemplates belongs to the ordinal, not to a Pod. When redis-1 is deleted, the new redis-1 mounts data-redis-1 again, so whatever it wrote survives. With a node-local provisioner such as kind's local-path, the volume is pinned to one node and the Pod must return to it. Scaling down or deleting the StatefulSet keeps the claims by default, so scaling up again reattaches the old data; persistentVolumeClaimRetentionPolicy with whenScaled or whenDeleted set to Delete changes that. Deleting a claim releases its volume according to the reclaim policy, which is Delete for dynamically provisioned volumes: the data is gone. Keep backups, because a StatefulSet provides identity, not durability or replication.

04

DaemonSets: one Pod per eligible node

A DaemonSet runs one copy of its Pod on every node that matches its node selector and affinity and whose taints it tolerates. When a node joins it gets a Pod; when a node leaves, the Pod goes with it. kube-proxy and kind's kindnet network agent are DaemonSets, as are log shippers, node exporters and storage drivers. The controller adds tolerations for conditions such as node.kubernetes.io/not-ready and unreachable automatically, but not for the control-plane taint, so add that toleration when an agent must run there; kube-proxy simply tolerates every taint with operator: Exists. Updates roll node by node, one unavailable at a time by default. Keep requests small, since a DaemonSet's requests are taken from every node.

yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-agent
spec:
  selector:
    matchLabels:
      app: node-agent
  template:
    metadata:
      labels:
        app: node-agent
    spec:
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      containers:
      - name: agent
        image: busybox
        command: ["sh", "-c", "while true; do sleep 3600; done"]
        resources:
          requests:
            cpu: 10m
            memory: 16Mi
Read the code

The toleration lets the agent run on the tainted control-plane node as well as on the workers. The busybox loop keeps the container running, standing in for a real agent. The small requests matter because every node in the cluster pays them.

05

Try it in the lab terminal

The lab uses a cluster with two workers. Apply the Service and StatefulSet together, watch the ordered start, resolve a member by its stable DNS name from another member, delete one Pod and scale down. Then list the DaemonSets that kind itself runs in kube-system and add your own node agent that tolerates the control-plane taint, so that it covers all three nodes.

bash
cluster create --workers 2
cat > redis.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  clusterIP: None
  selector:
    app: redis
  ports:
  - name: redis
    port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7
        ports:
        - name: redis
          containerPort: 6379
        volumeMounts:
        - name: data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi
EOF
kubectl apply -f redis.yaml
kubectl get pods -l app=redis
kubectl rollout status sts/redis
kubectl get pvc
kubectl exec redis-0 -- nslookup redis-2.redis
kubectl delete pod redis-1
kubectl wait --for=condition=Ready pod/redis-1 --timeout=60s
kubectl scale sts redis --replicas=1
kubectl get pods,pvc
kubectl get ds -n kube-system
cat > agent.yaml <<'EOF'
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-agent
spec:
  selector:
    matchLabels:
      app: node-agent
  template:
    metadata:
      labels:
        app: node-agent
    spec:
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      containers:
      - name: agent
        image: busybox
        command: ["sh", "-c", "while true; do sleep 3600; done"]
        resources:
          requests:
            cpu: 10m
            memory: 16Mi
EOF
kubectl apply -f agent.yaml
kubectl wait --for=condition=Ready pod -l app=node-agent --timeout=60s
kubectl get pods -l app=node-agent -o wide
Read the code

Right after apply only redis-0 exists. rollout status waits for 3, 2, then 1 Pods and prints partitioned roll out complete: 3 new pods have been updated..., kubectl's usual wording for StatefulSets. Claims data-redis-0 to data-redis-2 are Bound, and nslookup redis-2.redis returns redis-2's Pod IP. The deleted redis-1 returns under the same name. After scaling to 1, redis-2 is Terminating first while redis-1 waits its turn, and all three claims stay Bound. kube-system runs kindnet and kube-proxy with DESIRED 3, and node-agent ends with one Running Pod on each of kind-control-plane, kind-worker and kind-worker2.

TAKE THIS WITH YOU

StatefulSets give each replica a stable name, DNS record and volume, start and stop replicas in order, and keep their claims after a scale-down. DaemonSets run one Pod per eligible node and must tolerate any taint on the nodes they are meant to cover.

Check your understanding

You scale a StatefulSet with volumeClaimTemplates from 3 replicas to 1. By default, what happens to the claims of db-1 and db-2?

Go to the source

Progress stays in this browser. No account needed.