https://drive.google.com/file/d/1w8N_GUG-1O1KfFnklYIprM3DNGnZ9p4i/view?usp=sharing

๐Ÿ” YAML Breakdown

apiVersion: v1
kind: Pod
meta
  name: nnappone
  namespace: learning
  labels:
    app: nnappone
spec:
  containers:
    - name: crackone-app
      image: nginx
      resources:
        requests:
          memory: "300Mi"
        limits:
          memory: "500Mi"
  nodeSelector:
    sku: small   # โ† Only run on nodes with label: sku=small

๐Ÿ”‘ Key Field: spec.nodeSelector: { sku: small }

โ†’ Scheduler will only consider nodes that have the label sku=small.


๐Ÿ“Œ How nodeSelector Works

Step What Happens
1. You label nodes kubectl label node <name> sku=small
2. You deploy Pod With nodeSelector: { sku: small }
3. Scheduler filters nodes Only nodes with sku=small are candidates
4. Scheduler scores & picks Best node (based on resources, etc.)
5. Pod runs On a matching node โ€” no hardcoding!

โœ… Advantages over nodeName:


๐Ÿงช Lab: Use nodeSelector to Target Labeled Nodes

๐Ÿ’ก Weโ€™ll use Minikube or Kind โ€” just label one of your existing nodes.

๐Ÿ”ง Step 1: Check & Label a Node

# 1. See current nodes
kubectl get nodes

# Example output:
# NAME       STATUS   ROLES           AGE   VERSION
# minikube   Ready    control-plane   10m   v1.28.0

# 2. Label your node as "small"
kubectl label node minikube sku=small

# 3. Verify label
kubectl get nodes --show-labels | grep sku
# Should show: minikube ... sku=small

๐Ÿ”ง Step 2: Deploy the Pod

# 1. Create namespace
kubectl create namespace learning

# 2. Apply Pod
kubectl apply -f pod-for-specific-node-selector.yml

# 3. Check where it runs
kubectl get pods -n learning -o wide

# โœ… Expected:
# NAME       READY   STATUS    NODE       ...
# nnappone   1/1     Running   minikube   ...

# 4. Describe Pod โ†’ confirm nodeSelector
kubectl describe pod nnappone -n learning | grep -A 2 "Node-Selectors"
# Output:
# Node-Selectors:  sku=small

๐Ÿ”ง Step 3: Clean Up

# Delete Pod
kubectl delete pod nnappone -n learning

# Remove label (optional)
kubectl label node minikube sku-

# Delete namespace
kubectl delete namespace learning


โ“ What If No Node Has the Label?

If no node has sku=small: