Running two kubernetes pods on differents nodes

7/4/2019

Is there a way to tell Kubernetes to never run two pods on the same node, an example I have two pods replicas, I want them to be always distributed over zone1/zone2 and never in the same zone together.

apiVersion: app/v1
kind: Deployment
metadata:
  name: testApp
  labels:
    app: testApp-front
  namespace: 
spec:
  replicas: 2
  selector:
    matchLabels:
      app: testApp-front
  template:
    metadata:
      labels:
        app: testApp-front
    spec:      
      nodeSelector:
        failure-domain.beta.kubernetes.io/zone: zone1
-- Dady
docker
high-availability
kubernetes

4 Answers

7/5/2019

The k8s scheduler is a smart piece of software.

  1. The kubernetes scheduler will first determine all possible nodes where a pod can be deployed based on your affinity/anti-affinity/resource limits/etc.

  2. Afterward, the scheduler will find the best node where the pod can be deployed. The scheduler will automatically schedule the pods to be on separate availability zones and on separate nodes if this is possible of course.

P.S. If you never want 2 replicas of a pod to be on the same node, define an anti-affinity rule.

-- cecunami
Source: StackOverflow

7/4/2019

very simple you can use deamon set to run every pod in diffrent node or as the others said you can use pod anti-affinity

-- yasin lachini
Source: StackOverflow

7/4/2019

Seems like it can be done with Interpod Affinity you can see :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
spec:
  selector:
    matchLabels:
      app: testApp-front
  replicas: 3
  template:
    metadata:
      labels:
        app: testApp-front
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - testApp-front
            topologyKey: "kubernetes.io/hostname"
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - store
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: web-testApp-front
        image: nginx:1.12-alpine

you can see the full example here

-- Eran Chetzroni
Source: StackOverflow

7/4/2019

I think you need the concept of pod anti-affinity. This is within one cluster to take care that pods do not reside on one worker-node. https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity

-- Matthias Rich
Source: StackOverflow