💻
DevOps Cookbook
  • Home
    • Meiko DevOps Cookbook
  • Gitlab
    • Tips
      • 사전 정의된 CI/CD 변수의 기본 세트
    • 딥다이브 Gitlab CI/CD
      • GitLab CI/CD 시작하기
        • .gitlab-ci.yml 파일
        • Runner
        • 파이프라인
        • CI/CD 변수
        • CI/CD 컴포넌트
      • CI/CD YAML 구문 참조
    • CI/CD Notifications
      • CI/CD slack notifiaction 구축작업 결과
  • Kubernetes
    • Persistence Volume
      • play-cluster
    • Ingress
      • Ingress란
      • 수동으로 ingress 배포하기
    • Security
    • HPA
      • hpa troubleshooting history
        • 1. HELM_UPGRADE_VALUES_FILE로 hpa가 추가되지 않는 원인
        • 2. targetCPUUtilizationPercentage 계산은 어떻게 되는가
        • 3. helm values에 cpu resource 단위를 1로 했을 때 실제 파드에 1m( 0.001 ) 코어가 부여된 것
        • 4. pod cpu resources가 할당받은 것을 나타내는 것인 지 현재 사용량을 나타내는 것인지 검증
        • 5. 설정한 hpa를 기반으로 pod auto scaling이 동작하는 지 검증
      • HorizontalPodAutoscaler의 behavior 필드 중 stabilizationWindowSeconds 값이 Kubernetes 코드에서 어떻게 사용되는지 분석
  • prometheus
    • prometheus 리소스 alert 세팅
      • 슬랙 채널 구성
      • alert 환경 구성 가이드
  • Loki
    • loki-grafana alert 세팅
      • 1차 검증 결과
      • Alert 환경 구성 가이드
  • load test
    • nGrinder
      • nGrinder Test Configuration 값들
      • 질문리스트
        • groovy는 JUnit 스타일에 포함되지 않는건가 ? + GTest가 뭔지 좀 헷갈린다
      • Script
        • Groovy Script Structure
          • reference
          • Groovy script deep dive
          • Groovy Script 실행 구조 분석
      • Test
        • [nGrinder] single endpoint load test 하기
          • [nGrinder] single endpoint load test script 기반 cpu 사용량 테스트
          • [nGrinder] single endpoint load test script 기반 pod autoscaling 테스트
        • [nGrinder] multi endpoint load test 하기
          • [nGrinder] multi endpoint load test script에 정의한 test들이 실행 순서를 보장 받는가?
  • AWS
    • aws-cli
      • eks cluster vpc 스펙보기
    • aws-vpc
  • EKS
    • youtube links
    • EKS best practice
    • 질문 정리
  • Istio
    • Istio Basic
      • Istio 컴포넌트 별 역할
      • Kubernetes Ingress와 Istio VirtualService의 관계
    • Gateway
      • Gateway 주요 특징
      • Istio Gateway와 Kubernetes Ingress의 주요 차이점
  • IAC
    • Terraform
      • 테라폼 설치
      • 테라폼 문서
      • 테라폼 개념
        • 언어 구조
        • 사용 순서
        • 상태파일 (tfstate)
        • 변수 정의 방법
      • Terraform - AWS VPC
    • Ansible
      • Ansible 초기 학습 내용
      • Ansible Playbook
      • Ansible Study
        • Inventory
        • Playbook
          • Module
        • Variable
  • etc
    • Toss SLASH24
    • Elastic Load Balancing
    • 낙서장
      • IRSA
    • deep dive
      • Istio 공식문서 번역
        • Overview
          • What is Istio?
          • Why choose Istio?
          • Sidecar or ambient?
        • Concepts
          • Traffic Management
        • Page
      • eks 에서 control plane < - > data plane 통신 원리
Powered by GitBook
On this page
Edit on GitHub
  1. Kubernetes
  2. HPA

HorizontalPodAutoscaler의 behavior 필드 중 stabilizationWindowSeconds 값이 Kubernetes 코드에서 어떻게 사용되는지 분석

Previous5. 설정한 hpa를 기반으로 pod auto scaling이 동작하는 지 검증Nextprometheus 리소스 alert 세팅

Last updated 1 year ago

코드 분석

horizontal.go 파일에는 HPA 스케일링 로직을 담당하는 핵심 기능들이 포함되어 있습니다. 특히 stabilizationWindowSeconds 값이 스케일 다운 안정화 기간을 어떻게 제어하는지 확인.

  1. 파일 구조 및 핵심 함수:

    • 주요 기능이 구현된 함수:

      • computeDesiredReplicas

      • stabilizeRecommendation

    • behavior 필드에 따른 스케일 업/다운 로직은 computeDesiredReplicas 함수에서 처리

  2. 스케일 다운 로직 분석:

    • computeDesiredReplicas 함수 내에서 스케일 다운 로직이 어떻게 작동하는지 확인하기 위해 stabilizationWindowSeconds 값이 사용되는 부분을 조사.

    • 스케일 다운 로직은 stabilizeRecommendation 함수에서 stabilizationWindowSeconds 값을 활용하여 안정화 기간을 설정.

코드 분석 보고서

1. 분석 보고서: stabilizationWindowSeconds 값 사용 방식

stabilizationWindowSeconds 값 사용 코드

  • horizontal.go 파일 내 computeDesiredReplicas 함수 및 stabilizeRecommendation 함수에서 사용.

  • computeDesiredReplicas 함수:

    • horizontal.go의 약 800번째 줄에 정의되어 있다.

    • stabilizeRecommendation 함수를 호출하여 안정화 기간에 따른 파드 수를 조정.

kubernetes/pkg/controller/podautoscaler/horizontal.go의 주요 코드 발췌:

func computeDesiredReplicas(
    currentReplicas int32,
    scaleUpBehavior *autoscalingv2.HPAScalingRules,
    scaleDownBehavior *autoscalingv2.HPAScalingRules,
    ...
) (int32, string, error) {
    ...

    // Stabilize the recommendation if needed
    if stabilizationWindowSeconds := behavior.ScaleDown.StabilizationWindowSeconds; stabilizationWindowSeconds != nil {
        // stabilizeRecommendation 함수 호출
        proposedReplicas, timestamp = stabilizeRecommendation(
            *stabilizationWindowSeconds, // stabilizationWindowSeconds 값 전달
            timestamp,
            recommendations,
            proposedReplicas,
        )
    }

    ...
}

stabilizeRecommendation 함수 구현 코드:

  • stabilizeRecommendation 함수는 stabilizationWindowSeconds 값에 따라 스케일 다운 안정화 기간을 제어.

func stabilizeRecommendation(
    stabilizationWindowSeconds int32,
    timestamp time.Time,
    recommendations []timestampedRecommendation,
    proposedReplicas int32,
) (int32, time.Time) {
    stableTimestamp := timestamp
    stableReplicas := proposedReplicas

    for _, rec := range recommendations {
        // stabilizationWindowSeconds 값을 기준으로 안정화 기간을 비교
        if rec.timestamp.Add(time.Duration(stabilizationWindowSeconds) * time.Second).After(timestamp) {
            if rec.replicas < stableReplicas {
                stableReplicas = rec.replicas
                stableTimestamp = rec.timestamp
            }
        }
    }

    return stableReplicas, stableTimestamp
}

요약:

  • stabilizationWindowSeconds 값은 computeDesiredReplicas 함수에서 stabilizeRecommendation 함수로 전달.

  • stabilizeRecommendation 함수는 안정화 기간 내에 결정된 파드 수를 유지하기 위해 이전의 스케일 다운 이벤트 이후 안정화 기간을 계산.

예시 매니페스트: stabilizationWindowSeconds 값을 활용한 HorizontalPodAutoscaler 매니페스트

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300  # 안정화 기간 300초
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max
kubernetes/pkg/controller/podautoscaler/horizontal.go at master · kubernetes/kubernetesGitHub
Logo