Review StatefulSet replica placement

Check placement policies to avoid concentrating replicas in one failure domain.

Description

StatefulSet replicas on the same node can fail together. Plan separately for node placement, zone failure and the application’s quorum.

Potential impact

Requiring distinct nodes can leave some Pods Pending when too few nodes qualify.

Remediation

Configure podAntiAffinity or a topology spread constraint using the Pod labels and topologyKey. Before requiring separation, check node count, capacity and where volumes can attach. Use a preference when sharing a host is acceptable. Verify placement and quorum after deployment.

Examples

Use a supported ZooKeeper image and provide the zk-hs headless Service. The second configuration needs at least three eligible nodes to run all three replicas.

Before

yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zk
spec:
  serviceName: zk-hs
  replicas: 3
  selector:
    matchLabels:
      app: zk
  template:
    metadata:
      labels:
        app: zk
    spec:
      containers:
        - name: zookeeper
          image: k8s.gcr.io/kubernetes-zookeeper:1.0-3.4.10

zk requests three replicas without specifying how to spread them.

After

yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zk
spec:
  serviceName: zk-hs
  replicas: 3
  selector:
    matchLabels:
      app: zk
  template:
    metadata:
      labels:
        app: zk
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: zk
              topologyKey: kubernetes.io/hostname
      containers:
        - name: zookeeper
          image: k8s.gcr.io/kubernetes-zookeeper:1.0-3.4.10

New Pods labeled app=zk in the same namespace must use distinct hostnames. The rule does not relocate running Pods.

References