Hello, I am having issues with mock exam 2 and the node affinity question: api . . .

John:
Hello,

I am having issues with mock exam 2 and the node affinity question:

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: beta-apps
name: beta-apps
spec:
replicas: 3
selector:
matchLabels:
app: beta-apps
strategy: {}
template:
metadata:
labels:
app: beta-apps
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app_type
operator: Exists
value:
- beta
containers:
- image: nginx
name: nginx
nodeSelector:
~

Error

error: error parsing label.yaml: error converting YAML to JSON: yaml: line 26: did not find expected key

line26 is

spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app_type
operator: Exists
value:
- beta Line 26

Can someone point out my mistake please?

Mayur Sharma:
Hi @John, The correct keyword is values.
Also, why do you need the values section for operator: Exists
Reference: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity

John:
@Mayur Sharma I have tried with values as well and that also throws the same error. I am trying to have the pod run on a node which has the label app_type=beta – what is the correct syntax for this with node affinity?

Mayur Sharma:
@John I think, you should use ‘In’ instead of Exists for the scenario you mentioned…
For e.g.
<Please correct the alignment below>
{code}
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app_type
operator: In
values:
- beta
{code}

John:
– I spotted an indent issue wit containers – once I fixed that I got a better error :

The Deployment “beta-apps” is invalid: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].values: Forbidden: may not be specified when operator is ‘Exists’ or ‘DoesNotExist’

This confirmed that I needed to use In as the operator

Mayur Sharma:
Cool :slightly_smiling_face:

John:
or else use Exists and remove values – thanks @Mayur Sharma

Shwetha Shenoy V:
Exists Operator doesn’t need a value as it just checks for existence of the label irrespective of the value. If you are planning to assign a pod to nodes that have certain values on the label, then ‘In’ operator is better suited.