Junior — Middle
How to ensure the launch of two container replicas so that they are not placed on the same worker node?
sobes.tech AI
Answer from AI
To run two container replicas so that they are not placed on the same worker node, in Kubernetes, you can use annotations or specifications that ensure pod distribution across different nodes.
One way is to use podAntiAffinity in the Deployment or StatefulSet manifest. Example:
spec:
replicas: 2
template:
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- my-app
topologyKey: "kubernetes.io/hostname"
This guarantees that pods with the label app: my-app will not be placed on the same node (host) by the key kubernetes.io/hostname.
If not about Kubernetes but another orchestrator, the principle is similar: you need to set up distribution rules or placement constraints so that replicas do not end up on the same machine.