본문 바로가기
K8S/Scheduling

Taints and Tolerations⭐⭐⭐

by HH_g 2024. 1. 18.

  쿠버네티스에서 노드 간의 스케쥴링과 pod의 배치를 제어하는 데에 Taints 와 Tolerations 가 사용됩니다.

이를 통해 특정 노드에서 실행되길 원치 않는 파드를 특정할 수 있습니다.

Taint 는 노드에, Toleration은 파드에 설정됩니다.

 

Taints

  Taint는 노드에 부착되는 레이블과 유사한 메커니즘으로, 다른 점은 반대의 의미를 갖고 있다는 것입니다.

테인트를 설정하면 해당 노드에서 실행되길 원치 않는 pod 를 정의할 수 있습니다.

 

특정 노드에 특별한 요구사항이 있는 경우, 이 노드에 테인트를 추가하여 다른 파드가 그 노드에 스케쥴링 되는 것을 방지합니다.

 

node affinity와 다른 점은, node affinity는 pod가 특정 노드를 선택하게 하는 속성이었다면 taint는 반대로 노드가 파드를 제외하도록 노드에 설정하는 속성입니다.

 

Taint 설정 명령어

kubectl taint nodes <node-name> <key>=<value>:<taint-effect>

kubectl taint nodes node1 app=myapp:NoSchedule # 예시

kubectl taint nodes node1 app=myapp:NoSchedule- # 적용한 테인트 삭제 시 기존 명령어에 - 추가

 

Tolerations

  Toleration은 파드가 특정 노드 스케쥴러에 어떤 조건을 허용하는 것을 지정하는 메커니즘입니다.

파드가 특정 테인트가 있는 노드에 스케쥴되기를 원한다면, 해당 파드에 toleration을 추가하여 특정 테인트를 통과하여 해당 노드에 스케쥴링 될 수 있습니다.

apiVerion:
kind: Pod
metadata:
    name: myapp-pod
spec:
    containers:
        - name: nginx-contianer
            image: nginx
    **tolerations:
        - key: "app"
            operator: "Equal"
            value: "myapp"
            effect: "NoSchedule"**

 

operator : Equal, Exists

  • Equal: key, value, effiect 필드 값이 테인트의 설정값과 모두 같은 지 확인
    • 즉 특정 키가 특정 값과 정확하게 일치하는 노드에서만 파드를 스케쥴링 가능
  • Exists: 앞 세 가지 필드를 선별해서 사용할 때 설정. (operator가 Exists인 경우 value 필드 사용 불가)
    • 특정 키가 설정된 노드에서 스케쥴링 가능

 

kubectl taint nodes node1 key1=value1:NoSchedule

 

예를 들어서 위의 taint를 가진 노드 node1을 가정해봅시다.

tolerations:
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoSchedule"
tolerations:
- key: "key1"
  operator: "Exists"
  effect: "NoSchedule"

위의 두 tolerations 설정 모두 위에서 생성하는 taint와 일치하므로 해당 tolerations를 가지는 파드는 node1에 스케쥴링 될 수 있습니다.

 

예시

 

예를 들어 클러스터에 특수한 노드가 존재하며, 이 노드에는 고성능 워크로드만 스케쥴링하려고 한다고 가정해봅시다.

  1. kubectl taint nodes special-node gpu=NoSchedule
  2. 이 명령은 special-node 노드에 gpu taint를 설정하여 해당 노드에 파드를 스케쥴링하지 않도록 합니다.
  3. 특정 파드를 특수한 노드에 스케쥴링하려면 해당 파드에 toleration을 설정해야 합니다.이제 gpu taint가 설정된 노드에도 이 파드를 스케쥴링할 수 있습니다.
  4. tolerations: - key: "gpu" operator: "Exists" effect: "NoSchedule"

 

 

위의 그림에서 Node 1 은 Taint=blue를 가지므로 blue가 아닌 것은 Node1 에 스케쥴링 할 수 없습니다.

오직 Pod D만 Node 1 에 스케쥴링 가능하지만, 다른 노드에는 taint가 없어서 어디든 가능하므로 D가 노드 1에 반드시 배정된다는 보장은 없습니다.

 

만약 이미 파드들이 배정된 상태에서 새로운 taint가 설정되면 해당 taint에 toleration을 가지고 있지 않은 pod는 kill 됨.

 

마스터 노드의 Taint

 

 

마스터 노드에는 모든 파드가 배정되는 것을 막는 taint가 존재합니다.

 

Pod가 pending인 이유 찾고 수정하기

 

mosquito pod

 

파드의 warning 이벤트 - node has untolerated taint

 

2개의 노드에 대해 각각 node-role.kubernetes.io/control-plane, spray:mortein  테인트를 가지지 않은 파드라서 스케쥴링이 안된다는 warning 메세지를 확인할 수 있습니다.

 

controlplane과 node01의 Taint

  • control plane의 taint  
    • node-role.kubernetes.io/control-plane 키를 가지며 effect로 NoSchedule 설정됨.
    • control plane 역할을 수행하는 노드에 noschedule 효과로 테인트가 설정되어 있기에 이 노드에는 일반적으로 pod를 스케쥴징하지 않습니다.
  • node01 의 taint :
    • spray=mortein 키를 가지며 effect로 NoScheudle이 설정됨.
    • spray=mortein toleration 를 가지지 않은 파드를 스케쥴링하지 않습니다.

 

이런 상황에서 해당 파드를 node01에 배치되게 하기 위해서는 파드가  spray=mortein 에 대한 toleration을 가지게 하거나 노드에 존재하는 테인트를 해지해야합니다.

파드에 toleration 추가하기

Solution 1) Pod bee를 먼저 만들고 edit 명령어로 toleration 추가하기

command 라인에서 toleration을 설정할 수 없음.

 

Solution 2) yaml 파일 만들어서 create 하기

apiVersion: v1
kind: Pod
metadata:
    name: bee
spec:
    container:
        - image: nginx
            name: bee
        tolerations:
            - key: spray
                value: mortein
                effect: Noschedule
                operator: Equal

toleration 필드 작성 시 값에 “” 해도 되고 안 해도 됩니다.

 

노드에 존재하는 taint 삭제하기

solution1) edit 명령어로 taints 필드 내용 삭제

kubectl edit nodes node1

 

solution 2) NoSchedule- 

 

kubectl taint nodes controlplane [node-role.kubernetes.io/control-plane:NoSchedule-](http://node-role.kubernetes.io/control-plane:NoSchedule-)

 

 

 

출처

Certified Kubernetes Adiminstrtor(CKA) with Practice Tests by Mumshad Mannambeth, KodeKloud Training

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

Daemon Sets  (0) 2024.01.19
[Scheduling] Resource Requirements and Limits  (0) 2024.01.19
Node Selectors & Node Affinity⭐⭐⭐  (0) 2024.01.19
[Scheduling] Pod Scheduling Process, nodeName  (0) 2024.01.18
Labels & Selectors  (1) 2024.01.18