Deploy pods in different nodes

8/12/2019

I have a namespace called airflow that has 2 pods: webserver and scheduler. I want to deploy scheduler on node A and webserver on node B.

nodes & pods

And here you can see deployment files:

scheduler:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: airflow
  name: airflow-scheduler
  labels:
    name: airflow-scheduler
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: airflow-scheduler
    spec:
      terminationGracePeriodSeconds: 60
      containers:        
        - name: scheduler
          image: 123423.dkr.ecr.us-east-1.amazonaws.com/airflow:$COMMIT_SHA1          
          volumeMounts:
          - name: logs
            mountPath: /logs          
          command: ["airflow"]
          args: ["scheduler"]
          imagePullPolicy: Always
          resources:
            limits:
              memory: "3072Mi"
            requests:
              cpu: "500m"
              memory: "2048Mi"
      volumes:
          - name: logs
            persistentVolumeClaim:
              claimName: logs

webserver:

apiVersion: v1
kind: Service
metadata:
  name: airflow-webserver
  namespace: airflow
  labels:
    run: airflow-webserver
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    run: airflow-webserver
---    
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: airflow-webserver
  namespace: airflow
  annotations:
    kubernetes.io/ingress.class: nginx
    certmanager.k8s.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - airflow.awesome.com.br
      secretName: airflow-crt
  rules:
    - host: airflow.awesome.com.br
      http:
        paths:
          - path: /
            backend:
              serviceName: airflow-webserver
              servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: airflow
  name: airflow-webserver
  labels:
    run: airflow-webserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: airflow-webserver
    spec:
      terminationGracePeriodSeconds: 60
      containers:
        - name: webserver
          image: 123423.dkr.ecr.us-east-1.amazonaws.com/airflow:$COMMIT_SHA1
          volumeMounts:
          - name: logs
            mountPath: /logs          
          ports:
            - containerPort: 8080
          command: ["airflow"]
          args: ["webserver"]
          imagePullPolicy: Always
          resources:
            limits:
              cpu: "200m"
              memory: "3072Mi"
            requests:
              cpu: "100m"
              memory: "2048Mi"              
      volumes:
          - name: logs
            persistentVolumeClaim:
              claimName: logs

What's the proper way to ensure that pods will be deployed on different nodes?

edit1:

antiaffinity is not working:

I've tried to set podAntiAffinity on scheduler but it's not working:

  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: name
            operator: In
            values:
            - airflow-webserver
        topologyKey: "kubernetes.io/hostname"
-- placplacboom
kubernetes

1 Answer

8/12/2019

If you want to have these pods run on different nodes but you don't care about which nodes exactly, you can use the Pod anti-affinity feature. It basically defines that the pod X should not run on the same node (it can be also used with failure domain / zones etc., not just with nodes) as pod Y and uses labels to specify the pods. So you will need to add some labels and specify them in the spec sections. More info about it is in Kube docs.

If in addition you want to also specify on which node it should run, you can use the Node affinity feature. See Kube docs for more details.

-- Jakub
Source: StackOverflow