How do you force a specific unequal pod distribution pattern (e.g., 3,3,1) across nodes in Kubernetes?
Quick Answer
Kubernetes cannot natively enforce unequal distribution. Use multiple deployments with nodeSelector targeting specific nodes. TopologySpreadConstraints only achieve approximately even distribution.
Detailed Answer
Kubernetes schedulers optimize for even distribution, not specific patterns like 3,3,1.
1. TopologySpreadConstraints (maxSkew=1): Spreads as evenly as possible (e.g., 3,2,2 for 7 pods). Cannot force 3,3,1.
2. Multiple Deployments (recommended for exact control): Create 3 deployments pinned to specific nodes with nodeSelector. deployment-a: replicas=3 on node1, deployment-b: replicas=3 on node2, deployment-c: replicas=1 on node3.
3. StatefulSet with podAntiAffinity: Can spread but cannot enforce unequal distribution.
The real question to ask: does the business need exactly 3,3,1, or just well-distributed with fault tolerance? TopologySpreadConstraints with maxSkew=1 handles the latter much more cleanly. Push back on the exact requirement.
Code Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 7
template:
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: myappInterview Tip
Push back on the requirement. Ask WHY 3,3,1 specifically. Usually the real need is even distribution with fault tolerance. This shows senior-level thinking.