CKA labs node affinity question

in your CKA labs of node affinity there is question of Set Node Affinity to the deployment to place the pods on node01 only.

Name: blue
Replicas: 3
Image: nginx
NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
Key: color
values: blue 

but i tried to do with ```
preferredDuringSchedulingIgnoredDuringExecution and i use this yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: blue
name: blue
spec:
replicas: 3
selector:
matchLabels:
app: blue
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: blue
spec:
containers:
- image: nginx
name: nginx

  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: color
            operator: In
            values:
            - blue

    resources: {}

status: {}

but i ran this command : kubectl create -f blue-deploy.yaml
error: error validating “blue-deploy.yaml”: error validating data: [ValidationError(Deployment.spec.template.spec.affinity.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution): invalid type for io.k8s.api.core.v1.NodeAffinity.preferredDuringSchedulingIgnoredDuringExecution: got “map”, expected “array”, ValidationError(Deployment.spec.template.spec.affinity): unknown field “resources” in io.k8s.api.core.v1.Affinity]; if you choose to ignore these errors, turn validation off with --validate=false

It give above error

Hello, @anu
You can check the available Solution for this.

yes i have check but still not work

hmm, any different error? or facing same error?

same error i got but i ran this

actually i not set label color=blue i want to check this that with ```
preferredDuringSchedulingIgnoredDuringExecution

it will take  or not

iam waiting for the solution

Please, check the steps in the attached gif
Aff

the gif that you share with requiredDuringSchedulingIgnoredDuringExecution it’s already working as you told before i want with not set label color=blue i want to check this that with ```
preferredDuringSchedulingIgnoredDuringExecution instead of required
could you please share this

That nodeSelectorTerms field is not under the preferredDuringSchedulingIgnoredDuringExecution, it’s only available under the requiredDuringSchedulingIgnoredDuringExecution.
Please find the attached screenshot.

Under the preferredDuringSchedulingIgnoredDuringExecution, there are only two fields:

  1. preference
  2. weight

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: blue
  name: blue
spec:
  replicas: 3
  selector:
    matchLabels:
      app: blue
  template:
    metadata:
      labels:
        app: blue
    spec:
      containers:
      - image: nginx
        name: nginx
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              preference:
                matchExpressions:
                - key: color
                  operator: In
                  values:
                  - blue