Horizontal Pod Autoscaling
Argo Rollouts는 표준 Kubernetes Horizontal Pod Autoscaler(HPA)를 사용한 오토스케일링을 지원해요. 점진적 롤아웃 중 애플리케이션 부하에 따라 파드 수를 관리해요.
출처: 문서
본문
Argo Rollouts는 표준 Kubernetes Horizontal Pod Autoscaler(HPA)를 사용한 오토스케일링을 지원해요. 점진적 롤아웃 중 애플리케이션 부하에 따라 파드 수를 관리해요.
Argo Rollouts의 HPA 지원
Argo Rollouts는 autoscaling/v2beta 이상과 안정적인 autoscaling/v2 API(Kubernetes 1.23+에서 사용 가능)를 통해 Horizontal Pod Autoscaler(HPA)와 함께 동작해요. 이 API들은 HPA가 Argo Rollout 객체를 포함한 커스텀 리소스를 대상으로 삼고 스케일링할 수 있는 기능을 제공해요.
HPA가 Argo Rollouts와 함께 동작하는 방식
v0.3.0부터 Argo Rollouts는 표준 Kubernetes Deployment와 같은 방식으로 /scale 하위 리소스를 노출해요. 이로써 Horizontal Pod Autoscaler(HPA)가 Rollout 리소스를 찾을 수 있어요. HPA는 /scale 하위 리소스에 접근해 Rollout의 status.replicas 필드에서 현재 replica 수를 가져와요.
HPA가 모니터링하는 메트릭(예: CPU, 메모리, 커스텀 메트릭)을 기반으로, HPA는 스케일링이 필요한지 결정해요. 스케일링이 필요하면 HPA는 동일한 /scale 하위 리소스를 통해 Rollout의 spec.replicas 필드에 새 desired replica 수를 써요.
HPA는 replica 수나 ReplicaSet을 직접 관리하지 않아요. 대신 Rollout 리소스의 spec.replicas 필드를 수정해서 desired replica 수를 설정해요. 그러면 Argo Rollouts 컨트롤러가 spec.replicas 필드의 변경을 감지하고, HPA가 업데이트한 replica 수를 보면 ReplicaSet에 파드를 만들거나 삭제하라고 지시해요.
요약하면, HPA는 총 원하는 파드 수를 결정해서 스케일링 결정을 하고, Argo Rollouts는 배포 변경을 수행하며 구성된 롤아웃 전략과 속성에 따라 파드를 할당해요.
예시 구성 (Example Configuration):
다음 YAML은 Rollout, 두 서비스(blue/green 또는 canary용), 오토스케일링용 HPA를 포함한 기본 구성을 제공해요. 이후 섹션에서 설명한 것처럼 다양한 배포 전략과 시나리오에 맞게 이 구성을 조정하세요.
1. Rollout
핵심 Argo Rollout 객체는 배포 전략(예: blue/green 또는 canary)을 정의하고 Kubernetes Deployment를 대체해요. HPA는 이 객체를 대상으로 원하는 총 replica 수를 관리해요.
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: rollout-hpa-example
spec:
replicas: 3 # HPA will scale this between min/max replicas
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: demo
image: https://github.com/argoproj/rollouts-demo
ports:
- containerPort: 8080
strategy:
blueGreen | canary: # Replace with either blueGreen or canary
autoPromotionEnabled: false
2. Service
두 개의 Kubernetes Service가 필요해요. 하나는 실시간 트래픽용 stable 서비스, 다른 하나는 전체 승격 전에 새 버전을 테스트할 preview 서비스예요.
apiVersion: v1
kind: Service
metadata:
name: argo-rollouts-stable-service
spec:
ports:
- port: 80
targetPort: 8080
selector:
app: demo
---
apiVersion: v1
kind: Service
metadata:
name: argo-rollouts-preview-service
spec:
ports:
- port: 80
targetPort: 8080
selector:
app: demo
3. HorizontalPodAutoscaler
HPA는 스케일링을 위해 Rollout을 대상으로 삼고, 파드의 평균 메트릭을 모니터링하며 총 desired 파드 수를 조정해요.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: demo-hpa
labels:
app: demo
spec: # The min and max number of pods the HPA can scale
minReplicas: 1
maxReplicas: 10
scaleTargetRef: # The HPA targets the Rollout object for scaling.
apiVersion: argoproj.io/v1alpha1
kind: Rollout
name: rollout-hpa-example
metrics: # Defines the scaling trigger
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 16Mi
Blue/Green 배포와 HPA
Blue/Green 배포 동안 Rollout 컨트롤러는 두 ReplicaSet을 관리해요. 현재 버전용 active(Blue)와 새 버전용 preview(Green)가 있어요. HPA가 배포를 확장/축소하면 Rollout의 spec.replicas 필드에 총 desired replica 수를 업데이트해요. Rollout 컨트롤러는 이 변경을 감지하고 active와 preview ReplicaSet 양쪽에 적용해요. 결과적으로 blue와 green 버전 모두 동시에 스케일되며 동일한 파드 수를 유지해요.
전체 승격 후 preview ReplicaSet이 새 active ReplicaSet이 되고, 이전 active ReplicaSet은 0으로 스케일 돼요. 그 시점부터 Rollout은 단일 active ReplicaSet만 관리하고, 이후의 HPA 스케일링 조정은 이 active ReplicaSet에만 적용돼요.
Blue/Green 배포를 구현하려면 기본 Rollout 리소스에 다음 strategy 구성을 추가하세요.
strategy:
blueGreen:
previewService: argo-rollouts-preview-service
activeService: argo-rollouts-stable-service
autoPromotionEnabled: false
중요: Blue와 Green 환경을 동시에 실행하면 리소스 사용량과 비용이 두 배가 돼요. 오토스케일링은 그 위에 추가 비용이 발생할 수 있어요. 긴 테스트 기간 동안 preview 비용을 최소화하려면 다음 섹션에서 설명하는 previewReplicaCount 속성을 사용하세요.
previewReplicaCount를 사용한 Blue/Green
Blue/Green 전략에서 previewReplicaCount 필드를 사용하면, Rollout 컨트롤러는 HPA가 지시한 대로 stable ReplicaSet 수를 변경하면서, preview ReplicaSet은 Rollout 매니페스트의 previewReplicaCount 필드에 지정된 파드 수로 고정해요.
롤아웃이 완전히 승격된 후 preview 버전이 새 stable ReplicaSet이 돼요. 그 시점에 HPA는 Rollout을 관리해 stable 버전의 파드 수를 확장하거나 축소할 수 있어요.
strategy:
blueGreen:
previewService: argo-rollouts-preview-service
activeService: argo-rollouts-stable-service
previewReplicaCount: 1 # Pins the number of pods in the preview
autoPromotionEnabled: false
previewReplicaCount는 HPA가 preview ReplicaSet을 스케일링하지 못하게 하여 테스트 중에 고정시켜요. 오직 stable ReplicaSet만 HPA 스케일링 결정에 응답해요. 구성에서 이 previewReplicaCount를 생략하면 HPA가 stable과 preview ReplicaSet을 동시에 스케일할 수 있고, 이는 추가 리소스 사용량과 비용을 발생시켜요.
경고: previewReplicaCount는 현재 부하나 HPA 스케일링 결정과 무관하게 항상 우선시되므로 주의해서 사용하세요.
Canary 배포와 HPA
Blue/Green 배포와 달리 Canary 배포의 오토스케일링은 더 복잡해요. 트래픽이 canary와 stable 두 활성 버전으로 분할되기 때문이에요.
트래픽 매니저 없는 Canary (setWeight만 사용)
트래픽 매니저가 없으면 Kubernetes Service는 모든 사용 가능한 파드에 트래픽을 균등하게 분배하고, Rollout 컨트롤러는 파드 수를 사용해 preview(canary)와 stable 버전 사이에 트래픽을 분할해요. 결과적으로 20%의 canary 파드가 대략 20%의 트래픽을 받아요.
애플리케이션 부하가 계속 변함에 따라 HPA는 총 파드 수를 확장하거나 축소해요. 승격(promotion) 동안 setWeight가 증가하면(예: 20% -> 50%), Rollout 컨트롤러는 새 비율에 맞추기 위해 stable 파드를 줄이고 canary 파드를 늘려요.
HPA는 라벨 셀렉터(예: app: demo)로 파드 집합을 모니터링해 어떤 파드를 모니터링할지 식별해요. stable과 canary ReplicaSet의 파드가 이 라벨을 공유하므로, HPA는 그들을 단일 그룹으로 보고 모두 합친 파드에 걸쳐 평균 메트릭 값을 계산해요.
HPA는 이 공식을 사용해 desired replica 수를 계산해요.
desired_replicas = ⌈ current_replicas x ( current_metric_value / desired_metric_value) ⌉
위 공식은 올림(ceiling) 함수를 보여주며, 어떤 분수든 항상 다음 정수로 올림해요.
그런 다음 Rollout 컨트롤러는 Rollout 전략에 정의된 setWeight에 따라 총 desired replica를 어떻게 분배할지 결정해요. 이 공식으로 canary와 stable 파드 수를 계산해요.
canary_replicas = ⌈ HPA_total_desired_replicas x (setWeight / 100)⌉
stable_replicas = HPA_total_desired_replicas - canary_replicas
setWeight: 20 예시
HPA가 현재 부하를 처리하려면 10개 파드가 필요하다고 결정하면, Rollout 컨트롤러는 다음으로 설정해요.
Canary Pods: ceil(10 * 0.2) = 2 pods
Stable Pods: 10 - 2 = 8 pods
트래픽이 갑자기 급증해서 HPA가 20개 파드로 스케일하면, Rollout 컨트롤러는 즉시 다음으로 조정해요.
Canary Pods: ceil( 20 * 0.2) = 4 pods
Stable Pods: 20 - 4 = 16 pods
setWeight 속성은 Rollout 매니페스트의 strategy.canary.steps 필드 안에 정의돼요.
strategy:
canary:
canaryService: canary-service
stableService: stable-service
steps:
- setWeight: 20
- pause: {}
- setWeight: 50
- pause: {}
- setWeight: 100
- pause: {}
참고: 롤아웃이 중단되고 롤백되면 stable 파드를 수동으로 다시 스케일 업해야 해요.
트래픽 매니저를 사용한 Canary
트래픽 매니저(예: Traefik, Istio, Ingress 등)를 사용하면, 트래픽 분할 책임이 Argo Rollouts에서 트래픽 매니저로 옮겨져요. Rollouts 컨트롤러는 파드 수를 조정하는 대신 각 setWeight 스텝에서 트래픽 매니저의 구성을 업데이트해, 새 canary 버전으로 보낼 트래픽 비율을 지정해요.
각 setWeight 스텝에서 컨트롤러는 트래픽 매니저의 커스텀 구성 리소스(예: Traefik용 TraefikService 또는 Istio용 VirtualService)를 편집해 weight 필드를 업데이트해요. 트래픽 매니저는 이 구성 변경을 감지하고 즉시 지정된 비율의 트래픽을 canary 서비스로 보내기 시작하며, 나머지 트래픽은 stable 서비스로 계속 흘러가요.
예를 들어 setWeight: 20은 각 버전에 몇 개의 파드가 실행 중인지와 무관하게 트래픽의 20%를 canary로, 80%를 stable 버전으로 보내요.
기본 동작(트래픽 매니저 없음)과의 핵심 차이는, Argo Rollouts가 파드 수를 20/80 비율로 조정해 20% 트래픽 전환을 달성한다는 점이에요. 트래픽 매니저를 사용하면 트래픽이 파드 수와 무관하게 분할돼요. 트래픽 분배와 파드 수를 모두 관리하려면 다음 섹션을 보세요.
경고: 스케일링 격리 부족
stable과 canary 파드 모두에 단일 HPA를 사용하면 스케일링이 stable과 canary 버전 사이에 분리되지 않는다는 단점이 있어요. canary에 성능 문제(예: 메모리 누수나 CPU 급증)가 있으면, HPA는 모든 파드에 걸쳐 높은 평균 메트릭을 보고 전체 애플리케이션을 스케일 업해요. 즉, canary의 문제로 인해 stable 버전도 스케일 업된다는 뜻이에요. 따라서 원활한 canary 릴리스를 위해 메모리 누수와 성능 문제가 없는 애플리케이션을 개발하는 것이 중요해요.
예시 구성 (Example Configuration):
이 예시에서는 Traefik을 트래픽 매니저로 사용해요. 설정에는 서비스를 노출하는 Traefik용 IngressRoute, canary와 stable 사이의 가중치 로드 밸런싱을 처리하는 TraefikService, 그리고 Traefik의 trafficRouting을 사용하도록 구성된 Rollout이 필요해요.
HPA는 이전 예시들처럼 여전히 Rollouts 리소스를 대상으로 해요.
1. IngressRoute
IngressRoute 리소스는 Traefik을 통해 애플리케이션을 외부 세계에 노출하고, 들어오는 트래픽을 TraefikService로 전달해요.
# ingress-route.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: demo-ingress
namespace: default
spec:
entryPoints:
- web
routes:
- match: PathPrefix(`/`)
kind: Rule
services:
- name: traefik-service # Points to the TraefikService
namespace: default
kind: TraefikService
2. TraefikService
커스텀 리소스 TraefikService는 Traefik이 두 Kubernetes Service(예: stable과 canary) 사이에 트래픽을 어떻게 분배할지 정의해요.
# traefik-service.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
name: traefik-service
spec:
weighted:
services:
- name: rollout-canary-stable
port: 80
- name: rollout-canary-preview
port: 80
3. Traefik trafficRouting이 있는 Rollout
Rollout 매니페스트에 trafficRouting 속성을 포함하세요. trafficRouting.traefik.name은 Rollout 전략으로 트래픽을 라우팅하기 위해 TraefikService 이름과 일치해야 해요.
strategy:
canary:
canaryService: canary-service
stableService: stable-service
trafficRouting: # Add trafficRouting in the Rollout manifest
traefik:
weightedTraefikServiceName: traefik-service # This name MUST match
steps:
- setWeight: 20
- pause: {}
트래픽 매니저로 Canary 분리하기 (setCanaryScale)
setCanaryScale 필드는 각 롤아웃 스텝에서 canary ReplicaSet을 고정된 파드 수에 고정해서 canary 스케일링을 HPA에서 분리해요. HPA는 계속 총 replica 수를 관리하고, Rollout 컨트롤러는 canary 파드 수가 고정되도록 보장해요. 그래서 HPA 스케일링은 stable 파드에만 적용돼요. stable 파드 수는 다음과 같이 계산돼요.
stable_replicas = HPA_total_desired_replicas - pinned_canary_replicas
Rollout 컨트롤러는 메트릭을 기반으로 stable ReplicaSet이 HPA와 함께 동적으로 스케일되도록 보장하는 반면, canary ReplicaSet은 고정된 파드 수로 실행되고 setWeight에 따라 트래픽을 받아요. 이는 테스트 중 잘못된 canary 동작으로 인해 canary가 과도한 리소스를 소비하거나 stable 환경의 불필요한 스케일링을 유발하는 것을 막아줘요.
이 전략은 트래픽 가중치와 파드 수를 분리해요.
setWeight: 20과 setCanaryScale.replicas: 1을 사용하면 트래픽 매니저는 고정된 단일 canary 파드에 트래픽의 20%를 보내고, 나머지 80%는 stable 서비스(오토스케일된 모든 stable 파드가 받침)로 가요.
setWeight: 80과 setCanaryScale.replicas: 1을 사용하면 트래픽 매니저는 stable 파드가 몇 개 실행 중인지와 무관하게 총 트래픽의 80%를 그 단일 고정 canary 파드로 보내요.
이를 통해 트래픽을 제어하면서 canary 리소스 사용량을 최소로 유지할 수 있어요.
strategy:
canary:
canaryService: canary-service
stableService: stable-service
trafficRouting:
traefik:
weightedTraefikServiceName: traefik-service
steps:
- setWeight: 20
- setCanaryScale: replicas: 1 # one canary pod receives 20% traffic
- pause: {}
- setWeight: 50
- setCanaryScale: replicas: 3 # three canary pods receive 50% traffic
- pause: {}
- setWeight: 90
- setCanaryScale: replicas: 5 # five canary pods receive 90% traffic
- pause: {}
- setWeight: 100
- setCanaryScale: replicas: 8 # eight canary pods receive 100% traffic
- pause: {}
모범 사례 (Best Practices)
-
올바른 전략을 선택하세요: 단순한 배포에는 표준 Blue/Green을, 비용 최적화에는
previewReplicaCount를 추가하고, 최대한의 제어와 격리를 원한다면setCanaryScale을 사용한 canary를 고려하세요. -
배포 중 두 버전 모두 모니터링하세요: 모니터링이 stable과 canary/preview 버전을 모두 포함하도록 해 성능 이상을 조기에 감지하세요.
-
적절한 HPA 임계값을 설정하세요: HPA의 min/max replica와 대상 메트릭을 애플리케이션의 특정 성능 특성에 맞게 구성하세요.
-
롤백 시나리오를 테스트하세요: 일부 canary 시나리오에서는 수동 스케일 백이 필요해요.
-
적절한 리소스 요청과 한도를 구현하세요: 파드에 적절한 리소스 request와 limit을 설정하면 HPA가 정확한 스케일링 결정을 내리는 데 도움이 돼요.
-
파드 비율에만 의존하기보다 통제된 배포를 위해 트래픽 매니저를 사용하세요.