From 45f6aa3d3bf610b8bdffbc9cd996fdfb9b0adfe3 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 2 Dec 2026 15:11:12 -0800 Subject: [PATCH] Add an optional HPA for the query-frontend With the query-scheduler enabled the query-frontend holds no queue and can be scaled horizontally. Add query_frontend.autoscaling, mirroring the querier, and leave replicas off the deployment when it's enabled. Signed-off-by: Srinivas Chippagiri --- CHANGELOG.md | 2 + README.md | 7 ++++ .../query-frontend/query-frontend-dep.yaml | 2 + .../query-frontend/query-frontend-hpa.yaml | 42 +++++++++++++++++++ values.yaml | 14 +++++++ 5 files changed, 67 insertions(+) create mode 100644 templates/query-frontend/query-frontend-hpa.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 66ba48b6..677873da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master / unreleased +* [FEATURE] Add `query_frontend.autoscaling` to create a HorizontalPodAutoscaler for the query-frontend, for setups that use the query-scheduler #706 + ## 3.7.0 / 2026-11-20 * [ENHANCEMENT] Render `podAnnotations` with `tpl`, so the default `prometheus.io/port` annotations follow `config.server.http_listen_port` #410 diff --git a/README.md b/README.md index d685180f..5a395e63 100644 --- a/README.md +++ b/README.md @@ -742,6 +742,13 @@ Kubernetes: `^1.19.0-0` | query_frontend.​affinity.​podAntiAffinity.​preferredDuringSchedulingIgnoredDuringExecution[0].​podAffinityTerm.​topologyKey | string | `"kubernetes.io/hostname"` | | | query_frontend.​affinity.​podAntiAffinity.​preferredDuringSchedulingIgnoredDuringExecution[0].​weight | int | `100` | | | query_frontend.​annotations | object | `{}` | | +| query_frontend.​autoscaling.​behavior | object | `{}` | Ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-configurable-scaling-behavior | +| query_frontend.​autoscaling.​enabled | bool | `false` | Creates a HorizontalPodAutoscaler for the query-frontend pods. Only scale the query-frontend when the query-scheduler is enabled, otherwise the query queue lives in the query-frontend. Ref: https://cortexmetrics.io/docs/operations/scaling-query-frontend/ | +| query_frontend.​autoscaling.​extraMetrics | list | `[]` | Optional custom and external metrics for the query-frontend pods to scale on | +| query_frontend.​autoscaling.​maxReplicas | int | `10` | | +| query_frontend.​autoscaling.​minReplicas | int | `2` | | +| query_frontend.​autoscaling.​targetCPUUtilizationPercentage | int | `80` | | +| query_frontend.​autoscaling.​targetMemoryUtilizationPercentage | int | `0` | | | query_frontend.​containerSecurityContext.​enabled | bool | `true` | | | query_frontend.​containerSecurityContext.​readOnlyRootFilesystem | bool | `true` | | | query_frontend.​enabled | bool | `true` | | diff --git a/templates/query-frontend/query-frontend-dep.yaml b/templates/query-frontend/query-frontend-dep.yaml index 20a8adcc..120314d6 100644 --- a/templates/query-frontend/query-frontend-dep.yaml +++ b/templates/query-frontend/query-frontend-dep.yaml @@ -9,7 +9,9 @@ metadata: annotations: {{- toYaml .Values.query_frontend.annotations | nindent 4 }} spec: + {{- if not .Values.query_frontend.autoscaling.enabled }} replicas: {{ .Values.query_frontend.replicas }} + {{- end }} selector: matchLabels: {{- include "cortex.queryFrontendSelectorLabels" . | nindent 6 }} diff --git a/templates/query-frontend/query-frontend-hpa.yaml b/templates/query-frontend/query-frontend-hpa.yaml new file mode 100644 index 00000000..b9b14559 --- /dev/null +++ b/templates/query-frontend/query-frontend-hpa.yaml @@ -0,0 +1,42 @@ +{{- if and .Values.query_frontend.enabled .Values.query_frontend.autoscaling.enabled -}} +{{- with .Values.query_frontend.autoscaling -}} +apiVersion: {{ include "cortex.hpaVersion" $ }} +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "cortex.queryFrontendFullname" $ }} + namespace: {{ $.Release.Namespace }} + labels: + {{- include "cortex.queryFrontendLabels" $ | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "cortex.queryFrontendFullname" $ }} + minReplicas: {{ .minReplicas }} + maxReplicas: {{ .maxReplicas }} + metrics: + {{- with .targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + target: + type: Utilization + averageUtilization: {{ . }} + {{- end }} + {{- with .targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ . }} + {{- end }} + {{- with .extraMetrics }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .behavior }} + behavior: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} +{{- end }} diff --git a/values.yaml b/values.yaml index f6c4bec5..fadd0981 100644 --- a/values.yaml +++ b/values.yaml @@ -925,6 +925,20 @@ query_frontend: enabled: true replicas: 2 + autoscaling: + # -- Creates a HorizontalPodAutoscaler for the query-frontend pods. Only scale the query-frontend + # when the query-scheduler is enabled, otherwise the query queue lives in the query-frontend. + # Ref: https://cortexmetrics.io/docs/operations/scaling-query-frontend/ + enabled: false + minReplicas: 2 + maxReplicas: 10 + targetCPUUtilizationPercentage: 80 + targetMemoryUtilizationPercentage: 0 # 80 + # -- Ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-configurable-scaling-behavior + behavior: {} + # -- Optional custom and external metrics for the query-frontend pods to scale on + extraMetrics: [] + service: annotations: {} labels: {}