0

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?

nickgryg
  • 25,567
  • 5
  • 77
  • 79
Rosse
  • 1
  • 1
  • 1
  • Seems like your validation errors don't really match your provided yaml snippets. Are you sure you provided up-to-date ones? – Nebril Jan 12 '18 at 10:30
  • The above snippets are off course only a small part of the actual statefulset yaml, but the validation errors match the provided snippets afaik – Rosse Jan 12 '18 at 10:41
  • Hi, did you get this working? – NumeroUno Jul 25 '19 at 18:16

2 Answers2

0

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

- nodeSelectorTerms:
  - matchExpressions:
...
Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48
  • I tried but this results in the same error. I guess downward API can only be used in env and volumes? – Rosse Jan 12 '18 at 12:16
0

For future reference, the YAML needs to be as follows;

spec: 
 affinity:
   nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
            - key: <key>
              operator: <operator>
              values:
              - <values>

Notice the placement of dash (-) characters.

Ege Kaan Gürkan
  • 2,923
  • 2
  • 13
  • 24