Programming

쿠버네티스(Kubernetes) 환경에서 스프링부트 리소스 최적화

ipxy 2025. 4. 23. 13:44
728x90

 

✅ 리소스 최적화 목표

  • OOM 방지과도한 리소스 낭비 방지
  • HPA(Horizontal Pod Autoscaler) 대응 가능
  • GC 튜닝과 컨테이너 메모리 사용률의 균형 유지

1. Spring Boot 애플리케이션 유형 분류

 

유형  리소스  사용 특성
REST API 서버 낮은 메모리, 중간 CPU
웹소켓 / 실시간 처리 CPU 민감, GC 튜닝 필요
배치 작업 메모리 집중, 짧은 수명
메시지 소비자 평균 CPU, 가변 메모리

2. 쿠버네티스 리소스 설정 예시

resources:
  requests:
    memory: "512Mi"
    cpu: "250m"
  limits:
    memory: "1024Mi"
    cpu: "500m"

💡 팁

  • requests는 서비스 안정성을 보장
  • limits는 최대치 제한 및 OOM Kill 회피

requests = limits로 설정하면 QoS가 Guaranteed가 되어 우선순위가 높아짐 (노드 자원이 빠듯한 경우 유리)


3. JVM 튜닝 옵션 (필수!)

JAVA_TOOL_OPTIONS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:+UseG1GC"

📌 설명

 

옵션  설명
UseContainerSupport 컨테이너 자원 인식 (JDK 8u191+)
MaxRAMPercentage 전체 메모리의 몇 %를 힙으로 쓸지 지정
UseG1GC 낮은 지연 시간용 GC 방식 (Spring Boot에 적합)

4. Pod 수준 설정: Liveness & Readiness Probe

livenessProbe:
  httpGet:
    path: /actuator/health/liveness
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: /actuator/health/readiness
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5

spring-boot-starter-actuator에 포함된 헬스체크 API를 활용


5. HPA (Horizontal Pod Autoscaler)

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 60

CPU 사용률 기준으로 자동 확장, 필요 시 custom metrics도 활용 가능


6. 리소스 최적화 체크리스트

 

항목  체크
JVM이 container 메모리 인식하도록 설정됨?
memory limit 대비 JVM 힙은 70~75%인가?
GC는 G1GC 또는 ZGC 등 적절히 설정되었는가?
/actuator/metrics 로 메모리/GC 모니터링 가능?
requests 값이 너무 높거나 낮지 않은가?

7. 권장 리소스 테이블

유형  requests.cpu requests.memory limits.cpu limits.memory
간단한 API 250m 512Mi 500m 1024Mi
무거운 API 500m 1024Mi 1 2048Mi
배치 250m 1024Mi 1 2048Mi
Kafka Consumer 300m 768Mi 1 1536Mi

 

728x90