https://drive.google.com/file/d/1w8N_GUG-1O1KfFnklYIprM3DNGnZ9p4i/view?usp=sharing
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.
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:
- Portable: Works on any cluster with labeled nodes
- Resilient: If node fails, Pod reschedules to another
sku=smallnode- Scalable: Add more
sku=smallnodes โ scheduler uses them automatically
nodeSelector to Target Labeled Nodes๐ก Weโll use Minikube or Kind โ just label one of your existing nodes.
# 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
# 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
# Delete Pod
kubectl delete pod nnappone -n learning
# Remove label (optional)
kubectl label node minikube sku-
# Delete namespace
kubectl delete namespace learning
If no node has sku=small: