⭐️ HPA = Horizontal Pod Autoscaler
cpu-percent=50 설정일 때, 평균 CPU 사용량이 50% 넘으면 Pod 를 하나 더 늘려줌| 개념 | 설명 |
|---|---|
| 접속량 증가 | 유저나 외부 시스템이 API 요청을 많이 보내는 상황 |
| 그로 인해 | CPU 사용률, 메모리 사용률, 네트워크 사용량 증가 |
| 결과적으로 | 시스템에 "부하" 가 생기고, 리소스를 더 필요로 함 |
| 그래서 | K8s 가 Pod 를 자동으로 스케일 아웃 시킴(복제) |
✅ K8s 클러스터가 CPU 사용률을 알아야 하기 때문에 metrics-server 설치
minikube addons enable metrics-server
→ 반환
💡 metrics-server is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
▪ Using image registry.k8s.io/metrics-server/metrics-server:v0.7.2
🌟 'metrics-server' 애드온이 활성화되었습니다
✅ 기존 spring-k8s-deployment.yaml 파일에 resources 추가
spec:
containers:
- name: spring-k8s-demo
image: spring-k8s-demo:v2
imagePullPolicy: Never
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m" # 최소 0.1 core 필요
limits:
cpu: "200m" # 최대 0.2 core 까지 사용 허용
✅ 적용하기
kubectl apply -f spring-k8s-deployment.yaml
kubectl autoscale deployment spring-k8s-demo \
--cpu-percent=50 \
--min=1 \
--max=5
✅ 확인
kubectl get hpa
→ 반환
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
spring-k8s-demo Deployment/spring-k8s-demo cpu: 3%/50% 1 5 1 4m9s
✅ 테스트용 Pod 생성
kubectl run stress --rm -i --tty \
--image=busybox \
-- /bin/sh
| 파트 | 설명 |
|---|---|
| kubectl run stress | 이름이 stress 인 Pod 하나 생성 |
| --rm | Pod 종료되면 자동 삭제 (임시 테스트용) |
| -i | 표준 입력을 받을 수 있게 해줌(쉘 사용 가능하게) |
| --tty | 터미널 세션을 만들 수 있게 함 |
| --image=busybox | busybox 이미지를 사용해서 컨테이너 실행 |
| -- /bin/sh | 이 컨테이너에 들어갈 때 쉘을 실행해줘! |
✅ 쉘 안에 들어가게 되면 부하 주기
while true; do wget -q -O- http://spring-k8s-service:8080/hello; done
→ 반환
Hello K8s! 🚀 this is version2!!Hello K8s! 🚀 this is version2!!Hello K8s! 🚀 this is version2!!Hello K8s! 🚀 this is version2!!Hello K8s! 🚀 this is version2!!Hello K8s! 🚀 this is version2!!
.... 무한반복
👉 1초에 수백~수천 번씩 요청을 날림
👉 CPU 점유율이 쭉 올라감
👉 K8s 가 "와, 이거 진짜 바쁘네?" → Pod 늘려줌
✅ 확인
kubectl get hpa
→ 반환
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
spring-k8s-demo Deployment/spring-k8s-demo cpu: 134%/50% 1 5 3 7m51s
시간이 지나 확인해보면..
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
spring-k8s-demo Deployment/spring-k8s-demo cpu: 3%/50% 1 5 1 1h