본문 바로가기
INFRA/DevOps

[k8s] Kubernetes AutoScaling

by BTC_비웃는사나이 2023. 5. 2.

Kubernetes AutoScaling

1. Cluster level scalability

  • 기존의 worker node의 resource가 부족할 경우 worker node를 자동으로 확장
  • GCP, AWS 및 Azure와 같은 cloud infrastructure를 통해서 사용
  • OpenStack의 Auto Scaling Kubernetes cluster
  • Cluster Autoscaler (CA)
    • Pod가 node 리소스를 할당 받지 못해 pending 될 때 worker node를 확장
    • Node pool의 min/max를 기준으로 그 범위 내로 node 확장
    • 할당된 node가 장시간 충분히 활용되지 못하면 node를 해제
    • 10초마다 불필요한 node 확인, 10분간 적은 리소스 유지하면 scale down

<이미지 출처 : https://www.youtube.com/watch?v=-9SCEP4bEdk&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=9>

 

2. Pods layer autoscale

  • Horizontal Pod Autoscaler(HPA)
    • Metrics-Server
      • Pod와 node들의 CPU/Memory 사용량을 주기적으로 모니터링하고 metrics 정보를 수집하여 API에 제공
      • kubectl top command 지원
      • Horizontal pod autoscaler를 통해 원하는 replicas 수 결정 : 원하는 replicas 수 = ceil[현재 replicas 수 * (현재 metrics 값 / 원하는 metrics 값)]
    • Pod의 replicas 수를 관리
      • 구동 중인 Pod의 CPU/Memory 사용률을 기반으로 Pod를 Scale-out
      • 확장/축소할 최소/최대 Pods 수량은 Pods의 Deployment에 의해 제어
    • HPA 동작 조건
      • HPA는 기본 30초 간격으로 Pod 리소스 사용량을 check HPA에 설정한 임계 값을 초과할 경우 pod를 확장
      • Scale-out 이후 3분 대기, Scale-in 이후 5분 대기
    • https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ 참고

<이미지 출처 : https://www.youtube.com/watch?v=-9SCEP4bEdk&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=9>

  • Vertical Pods Autoscaler(VPA)
    • VerticalPodAutoscaler라는 사용자 정의 리소스 정의
    • Pod의 리소스를 관리
      • Pod에 대한 CPU/Memory 리소스를 추천
      • Pod에 대한 CPU/Memory 리소스를 자동으로 조정
    • 동작방식
      • Metrics를 10초 간격으로 지속적으로 확인
      • 할당된 CPU/Memory의 임계치를 넘으면 pod template를 변경하여 pod의 리소스 할당 값을 변경한 후 pod를 다시 시작
    • VerticalPodAutoscaler라는 사용자 정의 리소스로 구성
     

<이미지 출처 : https://www.youtube.com/watch?v=-9SCEP4bEdk&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=9>

 

HPA Autoscaling 운영

Metrics-server 설치

kubectl apply -f <https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml>

 

Metrics-server 설정

  • args에 추가
kubectl edit deployments.apps --namespace kube-system metrics-server
...
      - args:
        - --kubelet-insecure-tls
...

 

Metrics-server 설치 확인

kubectl get deployments.apps --all-namespaces
NAMESPACE     NAME             READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   coredns          2/2     2            2           5d
kube-system   metrics-server   1/1     1            1           23s
kubectl top nodes
NAME                 CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
master.example.com   127m         6%     1339Mi          34%
node1.example.com    22m          1%     667Mi           17%
node2.example.com    29m          1%     656Mi           17%
node3.example.com    33m          1%     805Mi           21%
kubectl top pods --all-namespaces
NAMESPACE     NAME                                         CPU(cores)   MEMORY(bytes)
kube-system   coredns-6d4b75cb6d-gd65q                     2m           21Mi
kube-system   coredns-6d4b75cb6d-pr92v                     2m           20Mi
kube-system   etcd-master.example.com                      16m          82Mi
kube-system   kube-apiserver-master.example.com            43m          329Mi
kube-system   kube-controller-manager-master.example.com   17m          51Mi
kube-system   kube-proxy-2j6k5                             2m           25Mi
kube-system   kube-proxy-4pqvq                             1m           24Mi
kube-system   kube-proxy-s52r7                             1m           23Mi
kube-system   kube-proxy-vss8c                             1m           25Mi
kube-system   kube-scheduler-master.example.com            4m           22Mi
kube-system   metrics-server-79b54f797c-xbjfv              4m           14Mi
kube-system   weave-net-57k7x                              2m           100Mi
kube-system   weave-net-8v24p                              2m           98Mi
kube-system   weave-net-8vdvf                              3m           98Mi
kube-system   weave-net-jbttf                              2m           101Mi

 

CPU 기반의 HPA 운영

  • apache-php deployment 실행
vi deploy_web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: smlinux/hpa-example
        name: web
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: svc-web
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: web
kubectl apply -f deploy_web.yaml
kubectl get all
NAME                            READY   STATUS              RESTARTS   AGE
pod/deploy-web-c5cff68f-d2mtv   0/1     ContainerCreating   0          3s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   5d1h
service/svc-web      ClusterIP   10.100.162.151   <none>        80/TCP    3s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deploy-web   0/1     1            0           3s

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/deploy-web-c5cff68f   1         1         0       3s

 

Horizontal Pod Autoscaler 생성

vi hpa_web.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-web
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deploy-web
  targetCPUUtilizationPercentage: 50
kubectl apply -f hpa_web.yaml
kubectl get hpa
NAME      REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
hpe-web   Deployment/deploy-web   <unknown>/50%   1         10        1          30s

 

부하 Test - Scale out 확인

  • Cluster IP 확인
kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   5d17h
svc-web      ClusterIP   10.106.142.164   <none>        80/TCP    24m
  • 부하 test 진행
while true
do
curl 10.106.142.164
done
  • Scale-out 확인
Every 2.0s: kubectl get hpa -o wide                           master.example.com: Wed Jun 22 10:35:36 2022

NAME      REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
hpa-web   Deployment/deploy-web   132%/50%   1         10        8          8m25s
Every 2.0s: kubectl get pods -o wide                                          master.example.com: Wed Jun 22 10:35:47 2022

NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS
GATES
deploy-web-c5cff68f-2trqs   1/1     Running   0          36s   10.44.0.3   node2.example.com   <none>           <none>
deploy-web-c5cff68f-4w2cs   1/1     Running   0          51s   10.47.0.2   node3.example.com   <none>           <none>
deploy-web-c5cff68f-chbjq   1/1     Running   0          36s   10.36.0.3   node1.example.com   <none>           <none>
deploy-web-c5cff68f-fvvcs   1/1     Running   0          25m   10.44.0.1   node2.example.com   <none>           <none>
deploy-web-c5cff68f-g56r9   1/1     Running   0          51s   10.44.0.2   node2.example.com   <none>           <none>
deploy-web-c5cff68f-qs8qf   1/1     Running   0          51s   10.36.0.1   node1.example.com   <none>           <none>
deploy-web-c5cff68f-rlpc4   1/1     Running   0          36s   10.36.0.2   node1.example.com   <none>           <none>
deploy-web-c5cff68f-sh8cd   1/1     Running   0          21s   10.47.0.4   node3.example.com   <none>           <none>
deploy-web-c5cff68f-xfbqx   1/1     Running   0          36s   10.47.0.3   node3.example.com   <none>           <none>
  • Scale-in 확인
Every 2.0s: kubectl get hpa -o wide                           master.example.com: Wed Jun 22 10:36:15 2022

NAME      REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-web   Deployment/deploy-web   18%/50%   1         10        9          9m4
Every 2.0s: kubectl get pods -o wide                                          master.example.com: Wed Jun 22 10:41:02 2022

NAME                        READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINES
S GATES
deploy-web-c5cff68f-fvvcs   1/1     Running   0          30m     10.44.0.1   node2.example.com   <none>           <none>
deploy-web-c5cff68f-rlpc4   1/1     Running   0          5m51s   10.36.0.2   node1.example.com   <none>           <none>
deploy-web-c5cff68f-sh8cd   1/1     Running   0          5m36s   10.47.0.4   node3.example.com   <none>           <none>
deploy-web-c5cff68f-xfbqx   1/1     Running   0          5m51s   10.47.0.3   node3.example.com   <none>           <none>

 

참고 :

https://www.youtube.com/watch?v=-9SCEP4bEdk&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=9

https://www.youtube.com/watch?v=gaYBOH-obJA&list=PLApuRlvrZKohLYdvfX-UEFYTE7kfnnY36&index=10

'INFRA > DevOps' 카테고리의 다른 글

[K8S] Node Schedule  (0) 2023.07.06
[K8S] role & rolebinding  (0) 2023.06.18
[K8s]DNS  (0) 2023.04.17
[k8s] kube-proxy  (0) 2023.04.10
[K8s] Network  (0) 2023.04.10

댓글