fix(monitoring): give kube-apiserver its own scrape size limit (#4427)

## What this PR does

kube-apiserver's `/metrics` grows with the number of resources and CRDs,
and on a full install it outgrows the 32MB `promscrape.maxScrapeSize`
that monitoring-agents sets globally. vmagent then refuses every scrape
of the API server, `up` drops to 0, and `KubeAPIDown` and `TargetDown`
fire against a healthy control plane while every rule over API server
metrics goes blind.

The apiserver `VMServiceScrape` endpoint now sets its own
`max_scrape_size` (128MB by default, `vmagent.apiserverMaxScrapeSize`).
vmagent uses a per-job value in place of the global flag for that job
only, so the 32MB guard for every other target is unchanged. The value
stays finite on purpose: vmagent reads the whole uncompressed response
into memory before parsing, `streamParse` included, so this limit is
also a memory ceiling.

A new `VMAgentScrapeSizeExceeded` warning fires on
`vm_promscrape_max_scrape_size_exceeded_errors_total`, so an oversized
target is reported as a size problem instead of only as a target that is
down. The counter has no target label, so the description points at the
vmagent targets page to find it.

Upgrade note: the per-job value replaces the global flag in both
directions. An install that raised `promscrape.maxScrapeSize` above
128MB through `vmagent.extraArgs` should raise
`vmagent.apiserverMaxScrapeSize` as well.

Verified on a dev cluster with the same operator (v0.68.4) and vmagent
(v1.136.0): a test vmagent with a 1MB global limit scraped all three API
servers with `max_scrape_size: 64MB` on the endpoint and refused all
three without it. The new counter reached storage with the `namespace`
and `pod` labels the alert uses, and the alert expression returned a
series. The `VMRule` passes the operator's admission webhook, which
rejects a malformed expression. Guest clusters get the same default,
since their HelmRelease overrides other `vmagent` keys only.

Fixes #4343

### Screenshots

Not a UI change.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(monitoring): kube-apiserver is scraped with its own 128MB size limit (`vmagent.apiserverMaxScrapeSize` in the monitoring-agents component) instead of the global 32MB, so a large `/metrics` no longer makes KubeAPIDown fire on a healthy API server. A new VMAgentScrapeSizeExceeded alert reports any target whose response exceeds the scrape size limit.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Kubernetes API server metrics can now use a configurable scrape-size
limit, set to 128 MB by default. Leaving the setting empty uses the
global limit.
* **Bug Fixes**
* Added a warning alert when vmagent reports scrape-size-limit errors.
The alert identifies the affected vmagent pod and service and provides
guidance for finding the target and adjusting its limit.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Backported-from: 08c535c295
This commit is contained in:
Myasnikov Daniil committed 2026-11-26 16:18:52 +00:00
commit 9f8ee9559a
4 files changed
+105

No files matched your search

@@ -0,0 +1,26 @@
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMRule
metadata:
name: alerts-vmagent
spec:
groups:
- name: vmagent
params: {}
rules:
- alert: VMAgentScrapeSizeExceeded
expr: increase(vm_promscrape_max_scrape_size_exceeded_errors_total[10m]) > 0
for: 5m
labels:
severity: warning
exported_instance: '{{ $labels.namespace }}/{{ $labels.pod }}'
service: vmagent
annotations:
summary: vmagent refuses scrape responses larger than its size limit.
description: >-
vmagent {{ $labels.namespace }}/{{ $labels.pod }} in cluster {{ $labels.cluster }} dropped
scrape responses that exceed -promscrape.maxScrapeSize or the scrape's max_scrape_size.
The affected targets report up == 0, so TargetDown or KubeAPIDown may fire for a healthy
target. The counter carries no target label: find the target on the vmagent /targets
page, where the last error names the size limit, then raise max_scrape_size on that
scrape. For kube-apiserver on the management cluster this is the
vmagent.apiserverMaxScrapeSize value of the monitoring-agents component.
@@ -9,6 +9,9 @@ spec:
- bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
port: https
scheme: https
{{- with .Values.vmagent.apiserverMaxScrapeSize }}
max_scrape_size: {{ . | quote }}
{{- end }}
relabelConfigs:
- action: labeldrop
regex: (endpoint|namespace|pod|container)
@@ -0,0 +1,69 @@
suite: kube-apiserver scrape size limit and its alert
# kube-apiserver's /metrics grows with the number of resources and CRDs, and a
# full install pushes it past the global 32MB -promscrape.maxScrapeSize. vmagent
# then refuses every scrape of it, so up == 0 and KubeAPIDown fires on a
# healthy API server. The apiserver endpoint carries its own max_scrape_size,
# which replaces the global flag for that job only (upward too), so the guard
# for every other target stays at 32MB. vmagent buffers the whole uncompressed
# body even with streamParse, so the per-job limit is a memory ceiling and must
# stay finite.
release:
name: monitoring-agents
namespace: cozy-monitoring
tests:
- it: apiserver endpoint gets its own scrape size limit by default
template: templates/apiserver-scrape.yaml
asserts:
- equal:
path: spec.endpoints[0].max_scrape_size
value: 128MB
- it: apiserver scrape size limit follows the value
template: templates/apiserver-scrape.yaml
set:
vmagent.apiserverMaxScrapeSize: 512MB
asserts:
- equal:
path: spec.endpoints[0].max_scrape_size
value: 512MB
- it: a byte count renders as a string, as the CRD field requires
template: templates/apiserver-scrape.yaml
set:
vmagent.apiserverMaxScrapeSize: 134217728
asserts:
- matchRegex:
path: spec.endpoints[0].max_scrape_size
pattern: ^[0-9.e+]+$
- it: an empty value falls back to the global flag
template: templates/apiserver-scrape.yaml
set:
vmagent.apiserverMaxScrapeSize: ""
asserts:
- notExists:
path: spec.endpoints[0].max_scrape_size
- it: the global limit for every other target is not loosened
template: templates/vmagent.yaml
asserts:
- equal:
path: spec.extraArgs["promscrape.maxScrapeSize"]
value: 32MB
- it: a refused oversized scrape raises its own alert
template: templates/alerts.yaml
documentSelector:
path: metadata.name
value: alerts-vmagent
asserts:
- equal:
path: spec.groups[0].rules[0].alert
value: VMAgentScrapeSizeExceeded
- equal:
path: spec.groups[0].rules[0].expr
value: increase(vm_promscrape_max_scrape_size_exceeded_errors_total[10m]) > 0
- equal:
path: spec.groups[0].rules[0].labels.severity
value: warning
@@ -283,6 +283,13 @@ vmagent:
- http://vminsert-shortterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus
- http://vminsert-longterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus
extraArgs: {}
# Replaces the global promscrape.maxScrapeSize (32MB) for the kube-apiserver
# job only. Its /metrics grows with the number of CRDs and outgrows 32MB on a
# full install. vmagent buffers the whole uncompressed response even with
# streamParse, so keep this finite. It replaces the global in both
# directions: if extraArgs raises promscrape.maxScrapeSize above this value,
# raise this one too. An empty value falls back to the global.
apiserverMaxScrapeSize: 128MB
fluent-bit:
rbac: