Sub topic 11
Scaling on custom, external and event-driven metrics
This chapter assumes you can create a Horizontal Pod Autoscaler (HPA) with CPU utilization and read its status with kubectl describe hpa. It also assumes you know what Metrics Server provides. CPU and memory are poor signals for some workloads. A queue consumer can be idle on CPU while thousands of messages wait. A web service can be slow because of request volume, not CPU load.
The three metric types beyond resources
The autoscaling/v2 API accepts more than Resource metrics. Each type is served by a different Kubernetes API.
| Type | Describes | API served from | Typical example |
|---|---|---|---|
| `Pods` | A metric measured per Pod | `custom.metrics.k8s.io` | Requests per second per Pod |
| `Object` | A metric of one other object | `custom.metrics.k8s.io` | Requests per second on an Ingress |
| `External` | A metric from outside the cluster | `external.metrics.k8s.io` | Queue length in a cloud service |
Metrics Server only implements the resource metrics API. The custom and external APIs need an adapter that you install yourself. Common choices are Prometheus Adapter, which exposes Prometheus queries, and KEDA, which is covered below. If no adapter is registered, the HPA reports the metric as unavailable.
A Pods metric
This HPA keeps the average request rate at 100 requests per second per Pod. It assumes an adapter exposes a metric named http_requests_per_second.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: "100"The formula is the same as for CPU. With 4 Pods averaging 150 requests per second, the HPA computes ceil[4 * (150 / 100)] = 6. Pods metrics support AverageValue targets only. Percent utilization has no meaning here, because there is no request value to compare against.
An External metric
An external metric is not tied to any Kubernetes object. For a queue consumer, you can target the backlog per replica.
metrics:
- type: External
external:
metric:
name: queue_messages_ready
selector:
matchLabels:
queue: orders
target:
type: AverageValue
averageValue: "30"With AverageValue, the HPA divides the total metric by the replica count. A backlog of 600 messages and a target of 30 gives 20 replicas, limited by maxReplicas. With a Value target, the HPA compares the raw total to the target instead.
Event-driven scaling with KEDA
KEDA (Kubernetes Event-driven Autoscaling) is a separate project, installed in the cluster. It provides scalers for many sources, such as Kafka, RabbitMQ, SQS and cron schedules. You describe the source in a ScaledObject. KEDA then creates and manages an HPA for you and acts as the external metrics adapter.
KEDA can also scale a workload to zero replicas and back up when events arrive. A plain HPA cannot do this by default. It needs the HPAScaleToZero feature gate and an Object or External metric, and the feature is not enabled in most clusters.
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: orders-worker
spec:
scaleTargetRef:
name: orders-worker
minReplicaCount: 0
maxReplicaCount: 20
triggers:
- type: rabbitmq
metadata:
queueName: orders
mode: QueueLength
value: "30"
hostFromEnv: RABBITMQ_URLCommon errors
- Metric shows
<unknown>. The adapter is not installed, the metric name is wrong, or the label selector matches nothing. Checkkubectl get apiservicesforv1beta1.custom.metrics.k8s.ioorv1beta1.external.metrics.k8s.io, and make sure it reportsAvailable. - Two autoscalers on one Deployment. Do not create your own HPA for a workload that a
ScaledObjectalready manages. The two will fight over the replica count. - Several metrics in one HPA. The HPA computes a replica count for each metric and uses the highest. This is safe, but one noisy metric can keep the workload large.
- Metrics that lag. A queue length that updates every minute makes scaling slow. Check the adapter's polling interval.
Exercise
An HPA uses an External metric with an AverageValue target of 25 for a queue. The queue holds 400 messages, and maxReplicas is 12. How many replicas does the HPA choose, and why?
Show answer
Dividing 400 by 25 gives 16 replicas, but maxReplicas caps the result at 12. The HPA sets the Deployment to 12 replicas, and the backlog per replica stays above the target until the queue drains.