How to create statefulset with nodeAffinity

1/12/2018

I'm trying to create a statefulset in kubernetes 1.9 with nodeAffinity. I found some examples with a simple nodeselector, but that is not really what I would like to accomplish. I want to make sure that statefulset instances always start on the same node, like this:

  • statefulpod-0 on node-0
  • statefulpod-1 on node-1
  • statefulpod-2 on node-2

I tried labelling the corresponding nodes with the statefulpod-name, and using downward api in nodeselector or nodeaffinity, but I cannot produce a working yaml to do this.

The example:

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - nodeSelectorTerms:
        matchExpressions:
        - key: statefulpodname
          operator: In
          values:
          - valueFrom:
              fieldRef:
                fieldPath: metadata.name

The error:

ValidationError(StatefulSet.spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution): invalid type for io.k8s.api.core.v1.NodeSelector: got "map", expected "array";

The example:

nodeSelector:
  statefulpodname:
  - valueFrom:
        fieldRef:
          fieldPath: metadata.name

The error:

invalid type for io.k8s.api.core.v1.PodSpec.nodeSelector: got "array", expected "string"

Any ideas?

-- Rosse
kubernetes
statefulset

1 Answer

1/12/2018

as error states got "map", expected "array";, try with :

- nodeSelectorTerms:
  - matchExpressions:
...
-- Radek 'Goblin' Pieczonka
Source: StackOverflow