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

11/5/2018

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"
-- user797963
kubernetes
kubernetes-helm

1 Answer

11/5/2018

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"
-- Kun Li
Source: StackOverflow