How does nodeAffinity work in DaemonSets

kubernetes

I'm trying to create a DaemonSet with a specific affinity, I want it only to create Pods on nodes with type=prod. I use the following test code:

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: test0
  namespace: kube-system
spec:
  template:
    metadata:
      labels:
        app: test0
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: type
                operator: In
                values:
                - prod
      containers:
      - name: test0
        image: gcr.io/google_containers/pause:2.0

However, the kubectl exits with the following error:

error: error validating "test.yaml": error validating data: found invalid field affinity for v1.PodSpec; if you choose to ignore these errors, turn validation off with --validate=false

I'm at a loss as to waht I'm doing wrong here. I tried placing the affinity block under the template block as well, same error.

I should note that the cluster is still Kubernetes 1.4.8, though. Affinity is part of Kubernetes since 1.2, so I don't think that would be the problem?

Best Answer

Found the solution myself, apparently I need to use an annotation instead:

...
spec:
  template:
    metadata:
      labels:
        app: test0
      annotations:
          scheduler.alpha.kubernetes.io/affinity: >
            {
              "nodeAffinity": {
                "requiredDuringSchedulingIgnoredDuringExecution": {
                  "nodeSelectorTerms": [
                    {
                      "matchExpressions": [
                        {
                          "key": "type",
                          "operator": "In",
                          "values": [ "prod" ]
                        }
                      ]
                    }
                  ]
                }
              }
            }
...
Related Topic