[CKA독학]kubernetes 스케줄링 정리(taint&tolerations/NodeSelector/NodeAffinity)
IT/CKA

[CKA독학]kubernetes 스케줄링 정리(taint&tolerations/NodeSelector/NodeAffinity)

반응형

Intro

  • kubernetes는 Woker Node위에 Pod들이 배포될때 다양한 스케줄링 기법을 제공합니다.
  • 이번편은 스케줄링 기법들에 대해서 설명드리고자 합니다.
  • 해당 블로그는 쿠버네티스 중급자이상 또는 CKA준비자 들에게 적합합니다.

Topic

  • kubernetes의 스케줄링 기법 3가지 정리
    • taint&tolerations
    • NodeSelector
    • NodeAffinity

taint&tolerations 개념

  • taint가 설정된 Node는 동일한 값의 toleration이 적용된 pod가 배포되도록허용함
  • taint는 Node에 key=value:effect 형태로 할당하고, toleration은 pod에 할당 함
  • taint effect종류: NoSchedule, PreferNoSchedule, NoExecute
    • NoSchedule : 일치하는 Toleration이 없으면 파드를 노드에 스케줄할 수 없음
    • PreferNoSchedule : 일치하는 Toleration이 없으면 파드를 노드에 스케줄할 수 없도록 시도는 하지만, 필수는 아님
    • NoExecute : 해당 이펙트가 있는 Taint가 노드에 추가되면, Taint를 허용하지 않는 파드는 즉시 축출되고 일치하지 않는 Toleration의 Pod는 스케줄링 하지 않음
  • tain&toleration이 적용되어 있어도 특정 조건에 의해 Pod는 다른 Node에 배포가 가능하다는 것이 특징

taint적용된 yml 예시

apiVersion: v1
kind: Node
metadata:
   name: node01
spec:
# -----------------------------------
  - effect: NoSchedule
    key: color
    value: red
# -----------------------------------

tolerations가 적용된 pod.yml 예시

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - image: nginx
    name: test
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
# -----------------------------------
  tolerations:
  - effect: NoSchedule
    key: spray
    operator: Equal
    value: mortein
# -----------------------------------

taint&tolerations관련 명령어 정리

##node에 taint 적용
kubectl taint node <node_name> <key>=<value>:<effect>
kubectl taint node node01 color=red:NoSchedule

##label이 동일한 여러개 Node에 한번에 적용
kubectl taint node -l <key>=<value> <key>=<value>:<effect>
kubectl taint node -l label=dev color=red:NoSchedule

## pod의 tolerations 필드 확인 
kubectl explain pod --recursive | less
kubectl explain pod --recursive | grep -A5 tolerations

## taint 적용 확인
kubectl describe node node01 | grep -i taint

# node에 taint 제거 (describe로 taint확인하여 마지막에 "-"를 넣어 taint 제거)
kubectl taint nodes master <taint_info>-

NodeSelecotr 개념

  • Node에 Label을 설정하고, 해당 lable에 맞추어 pod를 Node에 배포가 가능함
  • pod의 nodeSelector필드에서 key:value 형태로 설정

NodeSelector가 적용된 yaml예시

apiVersion: v1
kind: pod 

metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
  - name: nginx-container
    image: nginx
#----------------------
  nodeSelector:
    size: Large
# -----------------------

NodeSelector관련 명령어

## node에 label추가 
kubectl label nodes <node_name> <label_key>=<label_value>
kubectl label nodes node01 size=Large

NodeAffinity 개념

  • Node에 Label을 설정하고, 해당 lable에 맞추어 pod를 Node에 배포
  • NodeSelctor 와 동일한 개념이지만 NodeAffinity 타입별로 보다 디테일한 설정이가능함

NodeAffinity 타입

  • Available 타입:
  1. requireDuringSchedulingIgnoreDuringExecution : 파드가 노드에 스케줄되도록 반드시 규칙을 만족해야 하는 것
  2. preferredDuringSchedulingIgnoreDuringExecution : 후자는 스케줄러가 시도하려고는 하지만, 보증하지 않는 선호(preferences) 를 지정

참고 : IgnoreDuringExecution : 노드의 레이블이 런타임 중에 변경되어 파드의 어피니티 규칙이 더 이상 충족되지 않아도 파드는 그 노드에서 계속 동작한다는 의미

  • planned 타입:
  1. requireDuringSchedulingRequiredDuringExecution : 파드가 노드에 스케줄되도록 반드시 규칙을 만족해야하며, 런타임 중에 변경되어 파드의 어피니티 규칙이 더이 충족되지 않으면 파드는 그 노드에서 계속 동작하지 않음

NodeAffinity 적용된 yaml예시

apiVersion: v1
kind: pod 
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
  - name: nginx-container
    image: nginx
#### -----------------------
  requireDuringSchedulingIgnoreDuringExecution:
    nodeSelectorTerms:
    - matchExpressions:
      - key: size
        operator: In #NotIn #Exists 이건 해당 노드에만 만들어지게 하는것
        values:
        - Large #Small
#### -----------------------

NodeAffinity관련 명령어

### explain을 통해 필드를 확인하여 선언형으로 설정해야함
kubectl explain deployment --recursive | grep -A22 affinity
반응형