Horizontal Pod Autoscaler는 CPU 사용량의 파드 개수를 자동으로 스케일한다. Horizontal Pod Autoscaler는 크기를 조정할 수 없는 오브젝트에는 적용되지 않는다. (DaemontSet)에는 적용되지 않는다.
Horizontal Pod Autoscaler는 쿠버네티스 API 리소스 및 컨트롤러로 구현된다. 리소스는 컨트롤러의 동작을 결정한다. 컨트롤러는 평균 CPU 사용률, 평균 메모리 사용률 또는 다른 커스텀 메트릭과 같은 관찰 대상 메트릭이 사용자가 지정한 목표값과 일치하도록 조정한다.
hpa.yaml 파일
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa
spec:
maxReplicas: 100
minReplicas: 1 #default 값 = 1
scaleTargetRef: # 스케일링 적용 대상
apiVersion: apps/v1
kind: ReplicaSet
name: rs1
targetCPUUtilizationPercentage: 30 # hpa 사용을 위해 낮은 수치로 지정
# 테스트할 rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs1
spec:
replicas: 3
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
resources: ## 리소스가 없으면 hpa 사용 불가능!!!!!
limits:
cpu: 100m
$ kubectl create -f hpa.yaml -f rs.yaml
$ kubectl get hpa,rs
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa ReplicaSet/rs1 <unknown>/30% 1 100 3 62s
NAME DESIRED CURRENT READY AGE
replicaset.apps/rs1 3 3 3 54s
TARGETS 부분 unknown이 사용 수치로 변할 때 까지 기다려야한다. rs.yaml 파일에서 resources 부분이 없다면 unknown으로 지속된다.
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa ReplicaSet/rs1 0%/30% 1 100 3 3m36s
NAME DESIRED CURRENT READY AGE
replicaset.apps/rs1 1 1 1 3m28s
# 사용량이 낮아서 1로 낮아짐
이렇게 바뀌면 hpa 기능을 사용할 수 있다.
파드 하나를 스트레스를 줘서 scale up을 시도
$ kubectl exec rs1-rdnq5 -- sha1sum /dev/zero
파드 1개에 부하를 줘서 사용량을 100%로 만드니 목표인 30%를 맞추기 위해서 파드 3개를 추가한 상황이다.
사용량을 25%로 맞춘 모습
이런식으로 AWS의 Autoscaling처럼 똑같은 기능을 구현한다는 것을 알 수 있다.