sungsu9022 / study-kubernetes-in-action

study-kubernetes
12 stars 8 forks source link

15. 파드와 클러스터 노드의 오토스케일링 #15

Open sungsu9022 opened 4 years ago

sungsu9022 commented 4 years ago

15. 파드와 클러스터 노드의 오토스케일링

15.1 수평적 파드 오토스케일링

15.1.1 오토스케일링 프로세스 이해

1) 파드 메트릭 얻기

메트릭 얻는 부분에 대한 추가내용

2) 필요한 파드수 계산

스크린샷 2020-09-16 오후 11 34 36

3) 스케일링된 리소스의 레플리카 수 갱신

스크린샷 2020-09-16 오후 11 38 08

전체 오토스케일링 과정 이해

스크린샷 2020-09-16 오후 11 39 03

15.1.2 CPU 사용률 기반 스케일링

CPU사용률을 판단하는 기준

CPU 사용량을 기반으로 HorizontalPodAutoscaler 생성

오토스케일링을 위한 deployment 준비
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubia
spec:
  replicas: 3
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
      - image: luksa/kubia:v1
        name: nodejs
        resources:
          requests:
            cpu: 100m
  selector:
    matchLabels:
      app: kubia
minikube metrics-server 설정
minikube addons list

minikube addons enable metrics-server
HorizontalPodAutoscaler 생성
# HorizontalPodAutoscaler 생성
kubectl autoscale deployment kubia --cpu-percent=30 --min=1 --max=5

# HorizontalPodAutoscaler 정보 조회
kubectl get hpa.v2beta1.autoscaling kubia -o yaml
스크린샷 2020-09-16 오후 11 59 03

최초 오토 리스케일 이벤트 보기

# 오토스케일러 조회(레플리카수가 1인것을 확인할 수 있다.)
kubectl get hpa

# deployment 조회
kubectl get deploy

# pod 조회
kubectl get pod
스크린샷 2020-09-17 오전 12 51 36
# hpa 상태 조회
kubectl describe hpa kubia
스크린샷 2020-09-17 오전 12 52 20

스케일 아웃 일으키기

#  service 생성
kubectl expose deployment kubia --port=80 --target-port=8080

# deploy watch
kubectl get deploy --watch

# hpa watch
kubectl get hpa --watch

# 부하를 줄수있는 일회성 파드 생성
kubectl run -it --rm --restart=Never loadgenerator --image=busybox -- sh -c "while true; do wget -O - -q http://kubia.default; done"

오토스케일러가 디플로이먼트를 스케일 아웃하는지 확인

watch 내역
스크린샷 2020-09-17 오전 1 13 40
hpa event 확인
스크린샷 2020-09-17 오전 1 12 23

최대 스케일링 비율 이해

기존 HPA 오브젝트에서 목표 메트릭 값 변경

15.1.3 메모리 소비량에 기반을 둔 스케일링

쿠버네티스 1.8 메모리 기반 오토스케일링

공식 문서
블로그(hpa를 사용하지 않고 직접 스크립트를 만들어 처리하는 방법)
google cloud 가이드
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 100Mi
  # Uncomment these lines if you create the custom packets_per_second metric and
  # configure your app to export the metric.
  # - type: Pods
  #   pods:
  #     metric:
  #       name: packets_per_second
  #     target:
  #       type: AverageValue
  #       averageValue: 100
autoscaler github

15.1.4 기타 그리고 사용자 정의 메트릭 기반 스케일링

메트릭 유형으로 들어갈 수 있는 항목

리소스 : 리소스 메트릭 기반(CPU 등)
스크린샷 2020-09-17 오전 1 45 40
파드
스크린샷 2020-09-17 오전 1 46 45
오브젝트
스크린샷 2020-09-17 오전 1 47 07

15.1.5 오토스케일링에 적합한 메트릭 결정

15.1.6 레플리카를 0으로 감소

15.2 수직적 파드 오토스케일링

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: my-app-vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind:       Deployment
    name:       my-app
  updatePolicy:
    updateMode: "Auto"

15.2.1 리소스 요청 자동 설정

이 내용은 공식문서나 레퍼런스가 없음(?)

15.2.2 파드가 실행되는 동안 리소스 요청 수정

15.3 수평적 클러스터 노드 확장

15.3.1 클러스터 오토스케일러

클라우드 인프라스트럭처에 추가 노드 요청

스크린샷 2020-09-17 오전 2 15 13

노드 종료

수동으로 노드 금지(cordoning), 배출(draining) 하기

15.3.3 클러스터 스케일 다운 동안에 서비스 중단 제한

# PodDisruptionBudget 생성
kubectl create pdb kubia-pdb --selector=app=kubia --min-available=3
스크린샷 2020-09-17 오전 2 25 23

Reference