Kubernetes에서 특정 라벨을 가진 노드에 균등 분산하는 방법

Kubernetes 환경에서 특정 라벨을 가진 노드에 **Pod을 균등하게 분산(scheduling)**하는 방법을 정리합니다.
이 글에서는 ext=test1 라벨을 가진 노드들에만 배포하면서, 특정 노드에 몰리지 않도록 균등 분산하는 방법을 다룹니다.


1️⃣ 문제 정의

  • 현재 3개의 노드가 있으며, ext=test1을 가진 두 개의 노드에만 배포하고 싶음.
  • ext=test2을 가진 노드에는 배포되지 않도록 설정해야 함.
  • ext=test1 노드끼리 균등하게 분산되도록 설정해야 함.

📌 현재 노드 정보

노드 이름ext 라벨 값topology.kubernetes.io/zone
ip-10-118-154-15test1ap-northeast-2c
ip-10-118-153-114test1ap-northeast-2a
ip-10-118-153-171test2ap-northeast-2a

✅ Pod을 ext=test1을 가진 노드(ip-10-118-154-15, ip-10-118-153-114 )에만 배포하고, 균등하게 분산해야 함.
✅ ext=test2 노드(ip-10-118-153-171 )는 제외해야 함.


2️⃣ 해결 방법: nodeAffinity + topologySpreadConstraints 적용

1. nodeAffinity로 특정 노드만 선택

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: ext
operator: In
values:
- test1 # ext=test1이 있는 노드만 선택

✅ Pod이 ext=test1을 가진 노드에서만 스케줄링됨
✅ ext=test2을 가진 노드는 선택되지 않음


2. topologySpreadConstraints로 균등 분산

topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname # 개별 노드별 균등 분산
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
ext: test1 # ext=test1이 있는 노드만 대상

✅ ext=test1을 가진 노드들끼리 균등하게 분산됨
✅ 특정 노드에 Pod이 몰리는 현상 방지
✅ maxSkew: 1 설정으로 각 노드 간 Pod 개수 차이가 1을 넘지 않도록 제한


3️⃣ 최종 YAML 코드 예제

위 설정을 조합하여 최종적으로 적용할 수 있는 Pod 스케줄링 설정 예제입니다.

apiVersion: apps/v1
kind: Deployment
metadata:
name: batch-job
spec:
replicas: 4
selector:
matchLabels:
app: batch-job
template:
metadata:
labels:
app: batch-job
ext: test1 # topologySpreadConstraints의 대상이 되도록 설정
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: ext
operator: In
values:
- test1 # ext=test1을 가진 노드만 선택
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname # 개별 노드별 균등 분산
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
ext: test1 # ext=test1이 있는 노드만 대상
containers:
- name: batch-job
image: my-batch-job:latest
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"

4️⃣ 설정 적용 결과

이제 배포하면, ext=test1이 있는 두 개의 노드에 균등하게 분산됩니다.

✅ 적용 후 예상 배포 결과

노드 이름Pod 개수
ip-10-118-154-15 (ext=test1)2
ip-10-118-153-114 (ext=test1)2
ip-10-118-153-171 (ext=test2)❌ 0

🎯 목표 달성

✔ ext=test1을 가진 노드에만 배포됨
✔ 두 개의 노드에 균등하게 분산
✔ ext=test2 노드는 제외됨


5️⃣ 정리

설정역할
nodeAffinityext=test1 노드만 선택
topologySpreadConstraints선택된 노드들 간 균등 분산 (kubernetes.io/hostname 기준)
maxSkew: 1각 노드 간 Pod 개수 차이를 1 이하로 제한
labelSelectorext=test1 라벨을 가진 Pod만 대상으로 설정

이제 Kubernetes에서 특정 라벨을 가진 노드들끼리 균등하게 분산하는 방법을 완벽하게 설정할 수 있습니다! 🚀

관련 글

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다