Scheduling - Node Affinity
선호하는 노드를 설정하는 방법으로, nodeSelector 보다 확장된 Label Selector 기능을 지원한다.
types - requiredDuringSchedulingIgnoredDuringExecution / preferredDuringSchedulingIgnoredDuringExecution
affinity > nodeAffinity > ${type} > nodeSelectorTerms > matchExpression > {key/{values}/operator}
Q) How many labels exists on node01?
$ k describe no node01 | grep -i -A 5 label
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=node01
kubernetes.io/os=linux
Annotations: flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"62:4a:ba:d5:c3:63"}
A) 5
Q) What is the value set to the label key beta.kubernetes.io/arch on node01?
A) amd64
Q) Apply a label color=blue to node node01
$ k label no node01 color=blue
node/node01 labeled
Q) Create a new deployment named blue with the nginx image and 3 replicas.
$ k create deployment blue --image=nginx --replicas=3
deployment.apps/blue created
Q) Which nodes can the pods for the blue deployment be placed on?
$ k describe no controlplane | grep -i taints
Taints: <none>
$ k describe no node01 | grep -i taints
Taints: <none>
A) controlplane and node01
Q) Set Node Affinity to the deployment to place the pods on node01 only.
Name: blue
Replicas: 3
Image: nginx
NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
Key: color
value: blue
$ k edit deployment blue
# spec > template > spec아래 추가
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: color
operator: In
values:
- blue
deployment.apps/blue edited
Q) Which nodes are the pods placed on now?
$ k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
blue-5659879b55-7d9zf 1/1 Running 0 85s 172.17.1.4 node01 <none> <none>
blue-5659879b55-l6mmw 1/1 Running 0 84s 172.17.1.5 node01 <none> <none>
blue-5659879b55-slr74 1/1 Running 0 83s 172.17.1.6 node01 <none> <none>
A) node01
Q) Create a new deployment named red with the nginx image and 2 replicas, and ensure it gets placed on the controlplane node only.
Use the label key - node-role.kubernetes.io/control-plane - which is already set on the controlplane node.
Name: red
Replicas: 2
Image: nginx
NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution=
Key: node-role.kubernetes.io/control-plane
Use the right operator
$ k create deploy red --image=nginx --replicas=2 --dry-run=client -o yaml > reddeploy.yaml
$ vi reddeploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: red
name: red
spec:
replicas: 2
selector:
matchLabels:
app: red
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: red
spec:
containers:
- image: nginx
name: nginx
resources: {}
# affinity 추가
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists
status: {}
$ k apply -f reddeploy.yaml
deployment.apps/red created