Review Kubernetes Deployment Pod placement

Spread replicas across the required failure domains and verify actual placement.

Description

A Deployment’s replica count sets how many Pods to run. If several share a node, one node failure can take them down together.

Potential impact

Requiring separate hosts can leave new Pods Pending if too few nodes qualify.

Remediation

Set affinity.podAntiAffinity or a topology spread constraint. Match the selector to Pod labels and check node topology labels and capacity. Require separation if it is essential, or prefer it if sharing a node is acceptable. Check actual placement after rollout.

Examples

Use a verified, maintained image. The second configuration needs at least three eligible hostnames for three replicas. Allow room for extra Pods during a rollout.

Before

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-store
  template:
    metadata:
      labels:
        app: web-store
    spec:
      containers:
        - name: web-app
          image: nginx:1.16-alpine

web-server requests three replicas with no rule specifying their hosts.

After

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-store
  template:
    metadata:
      labels:
        app: web-store
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: web-store
              topologyKey: kubernetes.io/hostname
      containers:
        - name: web-app
          image: nginx:1.16-alpine

A new Pod cannot use a hostname already occupied by a Pod labeled app=web-store in the same namespace. The rule does not move running Pods.

References