본문 바로가기
MSA/Part4. Ch.6 Kubernetes 안정성 강화 방법

03. [실습] HPA 및 오토스케일링 적용

by Engineer-Lee 2023. 2. 5.
반응형

HPA를 적용할 파드를 생성해기 위해 파일을 작성했다.

test-deploy.yaml 파일 내용은 아래와 같다.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deploy
spec:
  selector:
    matchLabels:
      run: test-deploy
  replicas: 1
  template:
    metadata:
      labels:
        run: test-deploy
    spec:
      containers:
      - name: test-deploy
        image: k8s.gcr.io/hpa-example
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: test-deploy
  labels:
    run: test-deploy
spec:
  ports:
  - port: 80
  selector:
    run: test-deploy

replicas가 1로 하나의 test-deploy pod가 생성된다.

클러스터 IP를 가지고 80번 컨테이너 포트로 다른 파드들과 통신한다.

 

kubectl apply -f test-deploy.yaml로 파드를 배포한다.

 

kubectl autoscale deployment test-deploy --cpu-percent=50 --min=1 --max=10로 HPA를 적용시킨다.

 

HPA는 kubectl get hpa로 확인할 수 있다. 더 자세히 확인하려면 뒤에 -o yaml 옵션을 붙이자

 

최소 파드의 개수를 1개로 설정했기에 현재 파드 개수가 1개인걸 볼 수 있다.

여기에 트래픽 부하를 걸면 파드 개수가 늘어날 것이다.

 

kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://test-deploy; done" 명령어로 부하를 건다.

 

그럼 파드가 계속 늘어나는 걸 볼 수 있다. 이는 max를 10으로 설정했기에 최대 10개의 파드까지 늘어난다.

더 자세히 hpa 상태를 보고싶다면 kubectl describe hpa test-deploy를 통해 볼 수 있다.

 

scale in 이 되는데 까지는 약 5분 정도의 시간이 걸린다.

 

 

반응형