
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yamlkubectl get deployment metrics-server -n kube-system
kubectl get apiservice v1beta1.metrics.k8s.io -o yaml
kubectl top nodes
kubectl top pods
horizontal-pod-autoscaler.yamlapiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: deployment
name: nginx
minReplicas: 2
maxReplicas: 5
targetCPUUtilizationPercentage: 50 nginx 이라는 Deployment 의 레플리카를 2에서 부터 5까지 증감시킬 수 있는데 증가하는 기준은 cpu 사용률이 50%를 넘었을 때입니다.HPA 리소스 생성
kubectl apply -f horizontal-pod-autoscaler.yaml
Horizontal Pod Autoscaler 리소스에 대한 정보를 조회
kubectl get hpa

CPU 사용량을 증가시킬 수 있도록 애플리케이션에서 wget 이나 요청을 하는 프로그램을 만들기
: traffic_simulator.py
import requests
while True:
response = requests.get("https://backend.yachae1101.com/")
print(response)
targetCPUUtilizationPercentage 값을 2로 낮춰서 확인
cpu 사용률이 2% 가 넘으면 pod를 증가시키도록 설정 수정
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: deployment
name: nginx
minReplicas: 2
maxReplicas: 5
targetCPUUtilizationPercentage: 2
현재 상태 확인
kubectl get hpa
현재 cpu 사용량은 1% 이고, 2% 이상을 사용하게 되면, pod가 증가

현재는 pod가 2개만 존재한다.

위에서 작성한 python 프로그램 실행
python traffic_simulator.py
CPU 사용량이 임계값이 넘어가면 파드의 개수가 증가한다.
kubectl get hpa
파드의 개수가 3개로 늘어났다.

kubectl get pods
