Search code examples
kuberneteskubernetes-helm

Node AntiAffinity in StatefulSets, how to rely solely on node name vs. finding a specific label?


I'm having trouble using nodeAntiAffinity... in my use case I need to prevent the instances of a StatefulSet running on the same node, and that's it. I don't have labels for my nodes, which the doc looks to list as a requirement. Is it possible to purely rely on unique values of the built in label "kubernetes.io/hostname"?

What I am trying to do in my StatefulSet:

spec:
  podManagementPolicy: OrderedReady
  affinity:
    nodeAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        topologyKey: "kubernetes.io/hostname"

What the examples in the doc say I have to do:

spec:
  podManagementPolicy: OrderedReady
  affinity:
    nodeAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
            - key: <some key>
              operator: In
              values:
                - <some value>
        topologyKey: "kubernetes.io/hostname"

Solution

  • To prevent the instances of a StatefulSet running on the same node, you need a podAntiAffinity, excerpt from zookeeper tutorial of Kubernetes document :

      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - zk
              topologyKey: "kubernetes.io/hostname"