brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jul 19. 2021

쿠버1탄-30. 쿠버네티스 - 오토스케일링

다음은 쿠버네티스 스터디 자료를 참고해  정리한 내용입니다.


<1> 오토 스케일링 3가지

<2> 부하 테스트 환경 만들기

<3> 부하 테스트

<4> 다음 과정



<1> 오토 스케일링 3가지


1

HPA - 서버수를 늘리는 것 , Scale-Out

(Horizontal Pod Autoscaling) 


2

VPA - 서버 사용을 높이는것, 스케일 업. Scale-UP

(Vertical Pod Autoscaling)


3

CA는 Node 수를 늘리는것

Cluster Autocaler

CA 는 클라우드 사업자가 제공해야 한다.

node 수 자체를 늘려준다.



4

HPA  (pod  늘리기)

metrics-server 가 node에 있는  Kubelet 을 통해 pod와 node 정보를 수집한다.

리소스 API를 생성해 정보를 전달한다.

외부에서  kubectl top 으로 Resource API를 호출해 정보 확인이 가능하다.

HPA에 전달해 , ReplicaSet 에 이야기해서 늘린다.


그림 소개 : 출처 - 김태민님 기술 블로그




<2>  부하 테스트 환경 만들기


실습 자료

https://kubernetes.io/ko/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/


CPU 부하를 줘서  늘려보자~


1

모니터링

watch -d 'kubectl get ns,no,po,svc,deploy,rs,ing,ep -o wide'

or

watch -d 'kubectl top pod --use-protocol-buffers=true; echo; kubectl get deploy,pods,svc,ep -o wide'



2

cat << EOF > php-apache.yaml 

apiVersion: apps/v1

kind: Deployment

metadata:

  name: php-apache

spec:

  selector:

    matchLabels:

      run: php-apache

  replicas: 1

  template:

    metadata:

      labels:

        run: php-apache

    spec:

      containers:

      - name: php-apache

        image: k8s.gcr.io/hpa-example

        ports:

        - containerPort: 80

        resources:

          limits:

            cpu: 500m

          requests:

            cpu: 200m

---

apiVersion: v1

kind: Service

metadata:

  name: php-apache

  labels:

    run: php-apache

spec:

  ports:

  - port: 80

  selector:

    run: php-apache

EOF



파일 다운 받으려면

https://raw.githubusercontent.com/kubernetes/website/main/content/ko/examples/application/php-apache.yaml



3

kubectl apply -f php-apache.yaml 


4

 index.html에 접속 하면 CPU 과부하 연산 수행한다.

부하가 발생 된다.


5

kubectl get svc

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE

kubernetes   ClusterIP   100.64.0.1       <none>        443/TCP   21h

php-apache   ClusterIP   100.69.138.201   <none>        80/TCP    67s



6

현재 pod 1개

kubectl get pod -o wide

NAME      READY   STATUS    RESTARTS  AGE  IP   NODE      NOMINATED NODE   READINESS GATES

php-apache-d4cf67d68-rh42c   1/1     Running   0          89s   100.112.62.138   ip-172-20-39-212.ap-southeast-1.compute.internal   <none>           <none>



7

Pod의 shell 실행해보자, 모니터링 하자

kubectl exec -it php-apache-d4cf67d68-rh42c -- bash


서버에 로그온한다.

root@php-apache-d4cf67d68-rh42c:/var/www/html# top


top - 09:50:38 up 21:23,  0 users,  load average: 0.11, 0.14, 0.15

Tasks:   8 total,   1 running,   7 sleeping,   0 stopped,   0 zombie

%Cpu(s):  1.6 us,  2.3 sy,  0.0 ni, 94.9 id,  0.0 wa,  0.0 hi,  0.0 si,  1.2 st

KiB Mem:   3969424 total,  3841344 used,   128080 free,    85392 buffers

KiB Swap:        0 total,        0 used,        0 free.  3021380 cached Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND

      1 root      20   0  166260  19256  13984 S   0.0  0.5   0:00.10 apache2

      8 www-data  20   0  166284   7132   1848 S   0.0  0.2   0:00.00 apache2

      9 www-data  20   0  166284   7132   1848 S   0.0  0.2   0:00.00 apache2

     10 www-data  20   0  166284   7132   1848 S   0.0  0.2   0:00.00 apache2


root@php-apache-d4cf67d68-rh42c:/var/www/html# exit



8

Pod가 생성된  node에서 모니터링

htop





<3>  부하 테스트


1

kubectl get svc

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE     SELECTOR

service/kubernetes   ClusterIP   100.64.0.1      <none>        443/TCP   17m     <none>

service/php-apache   ClusterIP   100.71.163.94   <none>        80/TCP    9m18s   run=php-apache


2

SVCIP=<SVC IP>

SVCIP=100.71.163.94

curl $SVCIP;echo

OK!


3

1초에 한번 수행

while true;do curl -s $SVCIP; sleep 1; done


0.1초에 한번 수행

while true;do curl -s $SVCIP; sleep 0.1; done

while true;do curl -s $SVCIP; sleep 0.01; done



4

cat << EOF >  php-apache.yaml 

apiVersion: autoscaling/v1

kind: HorizontalPodAutoscaler

metadata:

  name: php-apache

spec:

  scaleTargetRef:

    apiVersion: apps/v1

    kind: Deployment

    name: php-apache

  minReplicas: 1

  maxReplicas: 10

  targetCPUUtilizationPercentage: 50

EOF


리플리카스가 최소 1개, 최대 10개까지

total이 cpu가 50% 넘어가면 늘려줌



 kubectl apply -f  php-apache.yaml



6

모니터링

watch -d 'kubectl get hpa,deploy,pods,svc,ep -o wide'

while true; do kubectl top pod --use-protocol-buffers=true; sleep 1; done



7

파드가 생성된 노드들에서 모니터링 실행

htop


8

HPA 생성

kubectl apply -f php-apache.yaml

horizontalpodautoscaler.autoscaling/php-apache created


or

kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/ko/examples/application/hpa/php-apache.yaml


9

모니터링 - 이 명령어는 모니터링이고 과부하 발생 아닙니다! 

while true; do kubectl get hpa; sleep 1; done


NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE

php-apache   Deployment/php-apache   0%/50%    1         10        1          52s

NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE

php-apache   Deployment/php-apache   0%/50%    1         10        1          53s

NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE

php-apache   Deployment/php-apache   0%/50%    1         10        1          54s



10

확인

kubectl describe hpa php-apache


Name:                                                  php-apache

Namespace:                                             default

Labels:                                                <none>

Annotations:                                           <none>

CreationTimestamp:                                     Mon, 19 Jul 2021 10:02:12 +0000

Reference:                                             Deployment/php-apache

Metrics:                                               ( current / target )

  resource cpu on pods  (as a percentage of request):  0% (1m) / 50%

Min replicas:                                          1

Max replicas:                                          10

Deployment pods:                                       1 current / 1 desired

Conditions:

  Type            Status  Reason               Message

  ----            ------  ------               -------

  AbleToScale     True    ScaleDownStabilized  recent recommendations were higher than current one, applying the highest recent recommendation

  ScalingActive   True    ValidMetricFound     the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)

  ScalingLimited  False   DesiredWithinRange   the desired count is within the acceptable range

Events:           <none>




11

과부하 발생 테스트


kubectl get svc

ubuntu@ip-172-20-40-2:~$ kubectl get svc

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE

kubernetes   ClusterIP   100.64.0.1       <none>        443/TCP   21h

php-apache   ClusterIP   100.69.138.201   <none>        80/TCP    18m



SVCIP=<SVC IP>

SVCIP=100.69.138.201

curl $SVCIP;echo


while true;do curl -s $SVCIP; sleep 1; done

while true;do curl -s $SVCIP; sleep 0.1; done

while true;do curl -s $SVCIP; sleep 0.01; done


12

파드 갯수 증가 확인 

1개 -> 2개 -> 5개

watch -d 'kubectl get hpa,deploy,pods,svc,ep -o wide'


kubectl get pod                                                                                                      

NAME                         READY   STATUS    RESTARTS   AGE

php-apache-d4cf67d68-28svt   1/1     Running   0          80s

php-apache-d4cf67d68-jcvlk   1/1     Running   0          16m

php-apache-d4cf67d68-lxxl2   1/1     Running   0          95s

php-apache-d4cf67d68-tv4f2   1/1     Running   0          95s

php-apache-d4cf67d68-vthrd   1/1     Running   0          110s







    




<4> 다음 과정


https://brunch.co.kr/@topasvga/1744


감사합니다.


매거진의 이전글 쿠버1탄-29.쿠버네티스-ResourceQuota(선택
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari