PaaS/Kubernetes

(Kubernetes)NodeSelector And NodeAffinity

armyost 2021. 3. 21. 20:30
728x90

NodeSelector

nodeSelector 는 가장 간단하고 권장되는 노드 선택 제약 조건의 형태이다. nodeSelector 는 PodSpec의 필드이다. 이는 키-값 쌍의 매핑으로 지정한다. 파드가 노드에서 동작할 수 있으려면, 노드는 키-값의 쌍으로 표시되는 레이블을 각자 가지고 있어야 한다(이는 추가 레이블을 가지고 있을 수 있다). 일반적으로 하나의 키-값 쌍이 사용된다.

Node affinity
Node affinity는 Pod가 특정 node로 배포되도록 하는 기능이다. 예전에 label에서 설명했던 node selector 도 node의 label과 pod의 selector label이 매칭되는 node에만 배포하도록 하기 때문에, 사실상 Node affinity와 같은 기능을 한다

Node affinity는 Hard affinity와 Soft affinity가 있다. Node affinity는 Pod가 조건이 딱 맞는 node 에만 배포되도록 하는 기능이고, Soft affinity는 Pod가 조건에 맞는 node에 되도록(반드시가 아니라)이면 배포되도록 하는 기능이다. 앞에서 언급한 node selector는 Hard affinity에 해당한다.

아래는 Pod 설정 YAML에서 node affinity를 적용한 예제이다.

requiredDuringSchedulingIgnoredDuringExecution는 Hard affinity 정의 이다. nodeSelectorTerms 부분에, matchExpressions을 사용하여, label set-based selector 문법을 이용하면 된다.

위의 예제는 node에 label key “kubernetes.io/e2e-az-name” 의 값이 eze-az1 이나 eze-az2 인 node를 선택하도록 하는 설정이다.

Node affinity는 여러 affinity를 동시에 적용할 수 있는데, 위의 예제에서는 두 개의 Affinity를 정의하였다. 두번째 Affinity는 Soft affinity로, preferredDuringSchedulingIgnoredDuringExecution: 으로 정의한다. Soft affinity는 앞서 언급한것과 같이 조건에 맞는 node로 되도록이면 배포될 수 있도록 그 node로 배포 선호도를 주는 기능이다. 이때 weight 필드를 이용해서 선호도를 조정할 수 있는데, weight은 1~100이고, node 의 soft affinity의 weight 값들을 합쳐서 그 값이 높은 node를 우선으로 고려하도록 우선 순위를 주는데 사용할 수 있다.

-DuringScheduling : When created at first time.
-DuringExcution : The POD has been running and a change is made in the environment.

-Required : Mandatory(엄격함)
-Preferred : If it is not match, go any available nodes(유연함)

 

아래는 YAML에 적용될 Affinity의 예제이다.


// color 라벨의 값이 blue인 경우
affinity:
 nodeAffinity:
  requiredDuringSchedulingIgnoredDuringException:
   nodeSelectorTerms:
    -matchExpressions:
      -key:color
        operator:In
        value:blue


// 특정 key가 라벨로 있는 경우
affinity:
 nodeAffinity:
  requiredDuringSchedulingIgnoredDuringException:
   nodeSelectorTerms:
    -matchExpressions:
      -key: node-role.kubernetes.io/master
        operator: Exists

 

 

 

Taint And Affinity

taint와 node Affinity를 모두 사용할 케이스는 다음과 같다.

앞의 3노드는 정해진 POD가 배치되길 원하고 뒤의 2노드는 others가 위치하길 원하는 상태에서 다음과 같이 blue, green, red taint와 blue, green, red Affinity를 모두 사용해야 각자 적절한 위치로 배치된다.

 

'PaaS > Kubernetes' 카테고리의 다른 글

(Kubernetes) DeamonSet And StaticPod  (0) 2021.03.22
(Kubernetes) Resource Limits 와 Edit POD  (0) 2021.03.21
(Kubernetes) kubectl 커맨드 모음 -2  (0) 2021.03.17
(Kubernetes) Taints and Toleration  (0) 2021.03.17
(Kubernetes) Scheduler  (0) 2021.03.16