Kubernetes/KoudKloud - CKAD with Tests

Deployment에 Node Affinity 설정

비번변경 2021. 11. 30. 18:44

Node Affinity란 노드의 레이블을 기반으로 포드/Deployment를 스케줄 할 수 있는 노드를 제한할 수 있는 방법이다. 

이 글에서는 Node에 Label을 설정하고, Deployment에 Node Affinity를 설정하는 방법을 정리한다.

 

Node의 Label 확인

kubectl describe nodes <NAME>

# 예시
kubectl describe nodes node01

kubectl describe nodes &lt;NAME&gt;

 

Node에 Label 설정

kubectl label nodes <NAME> KEY=VALUE

# 예시
kubectl label nodes node01 color=blue

 

Deployment에 Node Affinity 설정 추가

Node affinity 설정은 yaml 파일에 정의한다. 이미 존재하는 Deployment를 수정하는 경우에는 edit 명령을 사용할 수 있다.

포드 템플릿에 해당하는 부분(spec > templete > spec)을 찾아 affinity 필드를 추가한다.

kubectl edit deployments.apps blue
kubectl create deplyments.apps red --image=nginx --replicas 2 --dry-run=client -o yaml > red.yaml

 

  • Node Label 중 Key에 해당하는 값이 같은 경우
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: blue
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: blue
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: color
                operator: In
                values:
                - blue

 

  • Node에 Label의 Key가 존재하는 경우
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:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-role.kubernetes.io/master
                operator: Exists
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

 

 


참고 문서

https://kubernetes.io/ko/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/

 

노드 어피니티를 사용해 노드에 파드 할당

이 문서는 쿠버네티스 클러스터의 특정 노드에 노드 어피니티를 사용해 쿠버네티스 파드를 할당하는 방법을 설명한다. 시작하기 전에 쿠버네티스 클러스터가 필요하고, kubectl 커맨드-라인 툴이

kubernetes.io

https://kubernetes.io/ko/docs/concepts/scheduling-eviction/assign-pod-node/

 

노드에 파드 할당하기

특정한 노드(들) 집합에서만 동작하도록 파드를 제한할 수 있다. 이를 수행하는 방법에는 여러 가지가 있으며 권장되는 접근 방식은 모두 레이블 셀렉터를 사용하여 선택을 용이하게 한다. 보통

kubernetes.io

 

728x90