NodeSelector
What is nodeSelector?
nodeSelector is a field in a Pod spec that selects nodes based on their labels. Unlike nodeName which bypasses the scheduler, nodeSelector works with the scheduler to match pods to nodes with specific labels.
How It Works
yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
nodeSelector: # ← This is the key field
disktype: ssd # Node must have label disktype=ssd
region: us-east # Node must have label region=us-east
containers:
- name: main
image: nginxThe scheduler will only place this pod on nodes that have ALL the labels specified in nodeSelector.
Key Differences: nodeSelector vs nodeName
| Feature | nodeName | nodeSelector |
|---|---|---|
| Scheduler | Bypasses scheduler | Works with scheduler |
| Flexibility | Hardcoded node name | Uses labels (more flexible) |
| Failover | No failover | Can reschedule to other matching nodes |
| Validation | No resource checking | Normal scheduler validation |
| Production Use | Rarely used | Commonly used |
Step-by-Step Usage
1. Label Your Nodes
bash
# Add labels to nodes
kubectl label nodes node-01 disktype=ssd
kubectl label nodes node-01 region=us-east
kubectl label nodes node-02 disktype=hdd
kubectl label nodes node-02 region=us-west
# Verify labels
kubectl get nodes --show-labels2. Create Pod with nodeSelector
yaml
apiVersion: v1
kind: Pod
metadata:
name: web-app
spec:
nodeSelector:
disktype: ssd
region: us-east
containers:
- name: app
image: nginx:alpine3. Verify Scheduling
bash
# Check which node the pod landed on
kubectl get pod web-app -o wide
# Describe the pod to see scheduling details
kubectl describe pod web-appCommon Use Cases
- Hardware Requirements
yaml
nodeSelector:
gpu: "true"
memory: "high"- Environment/Region
yaml
nodeSelector:
environment: production
zone: zone-a- Team/Project Isolation
yaml
nodeSelector:
team: data-science
project: ml-trainingLimitations
- AND Logic Only: All labels must match (cannot do OR logic)
- No Preferences: Cannot say "prefer SSD, but HDD is okay"
- No Complex Rules: Cannot specify "not equal to" or "exists" operators
Best Practices
- Use Meaningful Labels:
disktype:ssdinstead oftype:1 - Document Labels: Keep a list of node labels your team uses
- Combine with Resources: Use with resource requests/limits
- Test Label Matching: Verify labels exist before deploying
Troubleshooting
Pod Stays Pending?
bash
# Check if any nodes have the required labels
kubectl get nodes -l disktype=ssd,region=us-east
# Check pod events
kubectl describe pod [pod-name]
# Check node capacity
kubectl describe node [node-name]Remove Labels:
bash
kubectl label node node-01 disktype-Practice Question 2
You have a Kubernetes cluster with the following nodes and labels:
Node: worker-1
Labels: environment=production, storage=ssd, zone=us-east-1a
Node: worker-2
Labels: environment=staging, storage=hdd, zone=us-east-1b
Node: worker-3
Labels: environment=production, storage=ssd, zone=us-east-1bCreate a Pod YAML for a database application that:
- Pod name:
production-db - Image:
postgres:14 - Label the pod:
app: database - Environment variable:
POSTGRES_PASSWORD: secret123 - Must run only on nodes with:
environment=productionstorage=ssd
- Container port:
5432
Write your YAML solution below: