# HPA 구성하기

## 1. 메트릭 서버 배포&#x20;

Metrics Server는 쿠버네티스에 내장된 autoscaling을 위한 확장 가능하고 효율적인 컨테이너 리소스 메트릭 소스입니다.

이러한 메트릭은 deployments의 확장 동작을 유도 합니다.

[Kubernetes Metrics Server](https://github.com/kubernetes-sigs/metrics-server)를 사용하여 Metrics Server를 배포 합니다.

```
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.1/components.yaml
```

Metrics Server `APIService`의 상태를 확인합니다. (몇 분 정도 소요될 수 있습니다).

```
kubectl get apiservice v1beta1.metrics.k8s.io -o json | jq '.status'
```

![](https://1998608250-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MhByD5wv8eUbA5ZbUY5%2F-MhDpvziSh5AC4pf4HQm%2F-MhDqs2D-6T0yg7o9uAn%2Fimage.png?alt=media\&token=b56350f1-c9e8-4bc7-8a6f-03fce3500a6f)

이제 배포된 애플리케이션을 확장할 준비가 되었습니다.

## 2. HPA로 애플리케이션 확장하기

예제 애플리케이션을 배포하고 TCP 포트 80으로 서비스를 노출 합니다. 이 애플리케이션은 php-apache 이미지를 기반으로 하는 커스텀 빌드 이미지 입니다. index.php 페이지는 계산을 수행하여 CPU 부하를 생성 합니다. 자세한 정보는 [여기](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#run-expose-php-apache-server)에서 확인할 수 있습니다.

```
kubectl create deployment php-apache --image=us.gcr.io/k8s-artifacts-prod/hpa-example
kubectl set resources deploy php-apache --requests=cpu=200m
kubectl expose deploy php-apache --port 80

kubectl get pod -l app=php-apache
```

HPA 리소스를 만듭니다.이 HPA는 컨테이너 리소스로 할당된 CPU가 50%를 초과할 때 확장됩니다.

```
kubectl autoscale deployment php-apache `#The target average CPU utilization` \
    --cpu-percent=50 \
    --min=1 `#The lower limit for the number of pods that can be set by the autoscaler` \
    --max=10 `#The upper limit for the number of pods that can be set by the autoscaler`
```

kubectl을 사용하여 HPA를 봅니다. 1-2분 동안 `<unknown>/50%`가 표시되고 `0%/50%`가 표시 될 것입니다.

```
kubectl get hpa
```

부하를 생성하여 Pod가 확정 되도록 합니다.

Cloud9 환경에서 **새 터미널을 열고** 다음 명령을 실행하여 새 컨테이너의 셸로 이동합니다.

```
kubectl --generator=run-pod/v1 run -i --tty load-generator --image=busybox /bin/sh
```

while 루프를 실행하여 http\:///php-apache 를 계속 가져 옵니다.

```
while true; do wget -q -O - http://php-apache; done
```

이전 탭에서 다음 명령을 사용하여 HPA를 확인합니다.

```
kubectl get hpa -w
```

HPA는 CPU 평균이 목표(50%)보다 낮아 질 때까지 설정된 최대 값(10)까지 파드를 확장하는 것을 볼 수 있습니다.

![](https://1998608250-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MhByD5wv8eUbA5ZbUY5%2F-MhDpvziSh5AC4pf4HQm%2F-MhDtB5snJCw1HCHYISL%2Fimage.png?alt=media\&token=e56bb4a6-c399-4489-bfb3-acb14bfc4d0b)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wjlee81.gitbook.io/amazon-eks/untitled-8/hpa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
