Kubernetes CKA sample exam question 114 with answer

Question
Create necessary configuration so that all pods with label app=nginx can only be deployed on node node01.
Use nginx image for pods. Label the node with label env=dev.
These pods should be preferred to be deployed on node node01

Answer
Label the node:

kubectl label node node01 env=dev
Generate pod manifest:
kubectl run nginx --image=nginx -l=app=nginx --dry-run=client -o yaml
Add following snippet to the manifest:
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: env
            operator: In
            values:
            - dev
Node Affinity, Labels & Selectors are used to define how and where pods should be scheduled. Apply and deploy the manifest.