Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

전공공부

[K8S - CKA] Node Selectors & Node Affinity 본문

Study/K8S

[K8S - CKA] Node Selectors & Node Affinity

monitor 2023. 10. 3. 14:01

1. Node Selectors


정의 : 노드 붙을 곳을 정해주는 역할을 수행하는 것

 

pod-definition.yaml

apiVersion:
kind: Pod
metadata:
 name: myapp-pod
spec:
 containers:
 - name: data-processor
   image: data-processor
 nodeSelector:
   size: Large

 

 

 

kubectl create -f pod-definition.yaml

1-1 Label Node


위와 같이 size 가 key 이고 Large가 value인 Label을 포함하는 노드가 존재해야 그 노드에 붙을 수 있게 된다. 따라서, 아래와 같은 명령어를 이용해서 붙여보자

kubectl label nodes <node-name> <label-key>=<label=value>

위 셋팅 이후에 pod-definition.yaml 파일을 적용하면 size: Large 노드에 붙게 된다.

<pod-definition.yaml 파일로 만들어진 pod가 위치를 찾아가는 모습>

그러나, 노드 셀렉트는 한계가 존재한다. 파드를 Small이 아닌 모든 노드에 붙인다던가 하는 경우는 어떻게 해야 할까?

이러한 경우들을 위해서 Node Affinity를 사용한다.

2. Node Affinity


정의 : 특정 노드에 pod를 붙일 수 있게 하는 역할을 함. (nodeSelector 보다 좀 더 복잡하다)

아래 예제는 위에서 설명한 node Selector의 역할과 같다.

pod-definition.yml

apiVersion: 
kind: Pod
metadata:
 name: myapp-pod
spec:
 containers:
 - name: data-processor
   image: data-processor
 affinity:
  nodeAffinity:
   requiredDuringSchedulingIgnoredDuringExecution: # no description
    nodeSelectorTerms:
    - matchExpressions:
      - key: size 
        operator: In 
        values:
        - Large

예제로 보다 싶이 node Affinity로 설정 시 훨씬 depth가 늘어나고 복잡하게 된다.

 

그러나, 보다 정확하고 내가 원하는 작업을 수행하기가 쉽다는 장점이 있다.

 

operator 값 In : 해당 값을 가진 모든 노드에 대해서 붙일 수 있게 끔 합니다. (즉, - Small을 value 밑에 붙이면 size: Small 노드에도 붙을 수 있습니다.)

 

operator 값 NotIn : 해당 값을 제외한 모든 노드에 대해서 붙을 수 있게 끔 합니다. (만일 value가 medium 이라면 medium 노드 빼고는 다 붙을 수 있습니다.)

 

operator 값 Exists : value 값은 존재하지 않고 key : 값만 일치하면 해당 key 값이 붙은 노드로 pod가 붙습니다.

 

만일 node의 label key 값인 size를 삭제 해버리면 어떻게 될까요? 이 때 노드에 붙어 있던 pod들은 모두 없어질까요?

2-1 Node Affinity Types


  • requiredDuringSchedulingIgnoredDuringExecution : 규칙이 만족되지 않으면 스케줄러가 파드를 스케줄링 할 수 없다. 하지만, 이미 Pod가 설정되었던 노드에 붙어 있지만 Node Label 설정이 바뀌었을 경우에는 Label 설정 무시하고 계속 노드에 붙어 있는다.
  • preferredDuringSchedulingIgnoredDuringExecution : 스케줄러는 조건을 만족하는 노드를 찾으려고 노력한다. 해당 되는 노드가 없더라도, 스케줄러는 여전히 파드를 스케줄링한다. 하지만, 이미 Pod가 설정되었던 노드에 붙어 있지만 Node Label 설정이 바뀌었을 경우에는 Label 설정 무시하고 계속 노드에 붙어 있는다.
  DuringScheduling DuringExecution
Type 1 Required Ignored
Type 2 Preferred Ignored
Type 3 Required Required

 

  • requiredDuringSchedulingIgnoredDuringExecution : 규칙이 만족되지 않으면 스케줄러가 파드를 스케줄링 할 수 없다. 그리고, 이미 Pod가 설정되었던 노드에 붙어 있지만 Node Label 설정이 바뀌었을 경우 Pod를 Evicted 처리하고 퇴출한다.

'Study > K8S' 카테고리의 다른 글

[K8S - CKA] Static Pods  (1) 2023.10.16
[K8S - CKA] Multiple Schedulers  (0) 2023.10.15
[K8S - CKA] Scheduling & Taints and Tolerant  (0) 2023.09.10
[K8S - CKA] Services  (1) 2023.08.26
[K8S - CKA] Kubectl Apply Command  (0) 2023.08.26