Node affinity for k8 cluster task failed

@Tej-Singh-Rana I created a deployments and labeled the nodes as per the task. Pods were running on node01. Task failed.

kubectl label nodes node01 color=blue
kubectl get nodes --show-labels

apiVersion: apps/v1
kind: Deployment
metadata:
name: blue
spec:
selector:
matchLabels:
color: blue
replicas: 3
template:
metadata:
labels:
color: blue
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: color
operator: In
values:
- blue
topologyKey: “kubernetes.io/hostname
containers:
- name: nginx-container
image: nginx:latest

Hello, @pratikshag
I think we have to use nodeAffinity instead of podAffinity.


Hi @pratikshag 

I found some syntax  error(s) in your  Deployment file.

Here is the corrected file:

vi deployment.yml

 ---
 apiVersion: apps/v1   
    kind: Deployment 
    metadata:    
      labels:        
        app: blue  
      name: blue      
    spec:               
      replicas: 3        
      selector:      
        matchLabels: 
          app: blue 
      template:      
        metadata:                   
          labels:    
            app: blue 
        spec:   
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: color
                    operator: In
                    values:
                    - blue     
          containers:
           - image: nginx:latest                      
             name: nginx-container

save &  exit

create deployment :
------------------------

kubectl apply -f deploy.yml

deployment.apps/blue  created

check if pods  are running:
----------------------------------

controlplane $ kubectl get pods

NAME                   READY   STATUS    RESTARTS   AGE:
blue-f7c59c79f-5dncq   1/1     Running   0          6m29s
blue-f7c59c79f-8h6p7   1/1     Running   0          6m29s
blue-f7c59c79f-kv9jb   1/1     Running   0          6m29s

Good Luck !

@samer looks like my yaml file was incorrect. Thanks a lot.

@Tej-Singh-Rana Gotcha . Thanks a lot.

Hi @pratikshag

You’re welcome …