Description
If Deployment replicas share a node, one node failure can interrupt several Pods. The replicas value alone does not tell you where they run.
Potential impact
A placement preference allows Pods to share a node. A strict requirement can leave new Pods unscheduled when too few nodes qualify.
Remediation
Set pod_anti_affinity or a topology spread constraint using the Pod labels and node topology labels. Before requiring separation, check eligible node count and capacity. Verify which nodes run the replicas after deployment.
Examples
Supply the omitted Deployment selector and containers. Match the selector to the Pod template labels.
Before
resource "kubernetes_deployment" "example" {
metadata {
name = "terraform-example"
labels = {
k8s-app = "prometheus"
}
}
spec {
replicas = 3
}
}
After
resource "kubernetes_deployment" "example" {
metadata {
name = "terraform-example"
labels = {
k8s-app = "prometheus"
}
}
spec {
replicas = 3
template {
metadata {
labels = {
k8s-app = "prometheus"
}
}
spec {
affinity {
pod_anti_affinity {
preferred_during_scheduling_ignored_during_execution {
weight = 100
pod_affinity_term {
label_selector {
match_labels = {
k8s-app = "prometheus"
}
}
topology_key = "kubernetes.io/hostname"
}
}
}
}
}
}
}
}
Both configurations request three replicas. With weight = 100, the second prefers different hostnames for Pods labeled k8s-app=prometheus. They can still share a host, and running Pods stay in place.