https://drive.google.com/file/d/1uLx1C0J4Kul69fUwDoBwNXvcFP7eQU2L/view?usp=sharing
apiVersion: v1
kind: Pod
metadata:
name: nnappone
namespace: learning
labels:
app: nnappone
spec:
containers:
- name: crackone-app
image: nginx
resources:
requests:
memory: "300Mi"
limits:
memory: "500Mi"
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: size
operator: NotIn # β EXCLUDE these values
values:
- small
π Key Rule:
"Do NOT schedule this Pod on any node where label
sizeissmall."
β Important:
This does NOT require the
sizelabel to exist!
- If a node has no
sizelabel β it matches (becausesizeis notsmall)- Only nodes with
size=smallare excluded
NotIn Works| Node Label | Matches NotIn [small]? |
Why? |
|---|---|---|
size=small |
β No | Explicitly excluded |
size=large |
β Yes | Not in exclusion list |
size=medium |
β Yes | Not in exclusion list |
No size label |
β Yes | Key doesnβt exist β canβt be small |
π‘ This is different from In:
In [small]β requires the label to exist and matchNotIn [small]β only excludes if label exists and matches
small Nodesβ Assumption: You have 2+ worker nodes in your k3s cluster.
small, One as large)# 1. List nodes
kubectl get nodes
# 2. Label nodes
kubectl label node k3s-node1 size=small
kubectl label node k3s-node2 size=large
# 3. Verify
kubectl get nodes --show-labels | grep size
# 1. Create namespace
kubectl create namespace learning
# 2. Apply Pod
kubectl apply -f pod-with-node-affinity-cannot.yml
# 3. Check placement
kubectl get pods -n learning -o wide
# β
Expected: Runs on k3s-node2 (size=large)
# β Will NOT run on k3s-node1 (size=small)
smallπ‘ What if every node is labeled size=small?
# 1. Relabel k3s-node2 as "small"
kubectl label node k3s-node2 size=small --overwrite
# 2. Deploy a new Pod
kubectl run test-pod -n learning --image=nginx --restart=Never \\\\
--overrides='
{
"spec": {
"affinity": {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "size",
"operator": "NotIn",
"values": ["small"]
}
]
}
]
}
}
}
}
}'
# 3. Check status
kubectl get pods -n learning
# β STATUS = Pending
# 4. Describe to see why
kubectl describe pod test-pod -n learning
# Events: 0/2 nodes are available: 2 node(s) didn't match Pod's node affinity rules.
π Key Insight:
If all nodes are excluded, Pod stays Pending forever β just like a
requiredaffinity with no match.