3

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?

1
  • if you only want to create the pod in specific nodes, in this simple case is better use nodeSelector
    – c4f4t0r
    Sep 12, 2019 at 1:03

2 Answers 2

2

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" ]
                        }
                      ]
                    }
                  ]
                }
              }
            }
...
0

What you have looks perfect to me, if I were to guess why it didn't work it'd problably be due to one of these 2 reasons:

  1. nodeAffinity didn't work for daemonsets (different controller) back then / only explicit node selectors worked back then.
  2. or it was some alpha feature that needed feature flags enabled on the api server back then.

I'm using this (just fiddling with something as an experiment), which seems very similar to what you have an it works as expected for me.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ingress-node
  namespace: ingress
  labels:
    ingress-node: healthz
spec:
  selector:
    matchLabels:
      ingress-node: healthz
  template:
    metadata:
      labels:
        ingress-node: healthz
    spec:
      containers:
      - name: ingress-node-identifier
        image: gcr.io/google-containers/healthz-server:1.0 
      affinity:
        nodeAffinity: 
          requiredDuringSchedulingIgnoredDuringExecution: 
            nodeSelectorTerms: 
            - matchExpressions: 
              - key: tier
                operator: In
                values:
                - ingress-east-us-1a
                - ingress-east-us-1b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .