Kubernetes Node Affinity and statefulsets

Nestor Camino picture Nestor Camino · Oct 3, 2017 · Viewed 8.9k times · Source

I'm looking to create a statefulset using affinity. I have added a label to my 3 nodes. Two of them have area=area1 and one node has area=area2. I'm looking to run my statefulset pods only on the nodes with area=area1. It's not working. I'm getting an error from the scheduler that no nodes were matched. I'm running Kubernetes v1.7.4

Yaml:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: stateful-bcs
spec:
  serviceName: mybcs
  replicas: 2
  template:
    metadata:
      labels:
       app: simplecount
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: area
                operator: In
                values:
                - area1
      containers:
        - name: test1
          image: XXXX.azurecr.io/simple
          env:
            - name: SIMPLE_SERVICE_NAME
              valueFrom:
                fieldRef:
              fieldPath: metadata.name
         command:
            - ./simplecount
            - "$(SIMPLE_SERVICE_NAME)"
      imagePullSecrets:
       - name: XXXXXXX
      restartPolicy: Always

Answer

user1181554 picture user1181554 · Nov 29, 2017

You should be using nodeAffinity not podAffinity, podAffinity is "based on labels on pods that are already running on the node rather than based on labels on nodes" (https://kubernetes.io/docs/concepts/configuration/assign-pod-node/).