kubernetes / kube-state-metrics

Add-on agent to generate and expose cluster-level metrics.
https://kubernetes.io/docs/concepts/cluster-administration/kube-state-metrics/
Apache License 2.0
5.2k stars 1.92k forks source link

fix(server): Add read and write timeouts #2412

Closed Pokom closed 1 week ago

Pokom commented 3 weeks ago

What this PR does / why we need it:

There are a few documented scenarios where kube-state-metrics will lock up(#995, #1028). I believe a much simpler solution to ensure kube-state-metrics doesn't lock up and require a restart to server /metrics requests is to add default read and write timeouts and to allow them to be configurable. At Grafana, we've experienced a few scenarios where kube-state-metrics running in larger clusters falls behind and starts getting scraped multiple times. When this occurs, kube-state-metrics becomes completely unresponsive and requires a reboot. This is somewhat easily reproduceable(I'll provide a script in an issue) and causes other critical workloads(KEDA, VPA) to fail in weird ways.

Adds two flags:

Updates the metrics http server to set the ReadTimeout and WriteTimeout to the configured values.

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

Does not change.

Fixes #2413

linux-foundation-easycla[bot] commented 3 weeks ago

CLA Signed

The committers listed above are authorized under a signed CLA.

k8s-ci-robot commented 3 weeks ago

Welcome @Pokom!

It looks like this is your first PR to kubernetes/kube-state-metrics πŸŽ‰. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-state-metrics has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. :smiley:

mrueg commented 3 weeks ago

Should we set timeouts for the telemetryserver as well? https://github.com/kubernetes/kube-state-metrics/pull/2412/files#diff-dc8fe36bd1edf1b4cd03bacbd94b02cd4226717fe7e1a6474c534f5b5db30227R315

How did you chose the duration for each timeout setting? Should we provide any additional suggestions?

Pokom commented 3 weeks ago

Should we set timeouts for the telemetryserver as well? https://github.com/kubernetes/kube-state-metrics/pull/2412/files#diff-dc8fe36bd1edf1b4cd03bacbd94b02cd4226717fe7e1a6474c534f5b5db30227R315

I'm sure it would be a good idea to set timeouts for the telemetry server as well, but I don't have enough experience with that type of scraping to provide meaningful defaults πŸ˜… I can follow up with an issue to set those in a future PR, how does that sound?

How did you chose the duration for each timeout setting? Should we provide any additional suggestions?

These were picked somewhat arbitrarily, but the intent is to align them some sane scrape times. When running in our dev cluster with dozens+ nodes, I have both aligned with our scrape interval and timeout(15s). As far as guidance, I think the minimum value should be the scrape timeout. The safest option for the defaults would be following the default scrape_interval from Prometheus which is 60s.

mrueg commented 3 weeks ago

/ok-to-test

What happens if a request takes longer than a timeout? Does the server send any log output? Is it visible on the telemetry server metrics?

mrueg commented 3 weeks ago

Should we set timeouts for the telemetryserver as well? https://github.com/kubernetes/kube-state-metrics/pull/2412/files#diff-dc8fe36bd1edf1b4cd03bacbd94b02cd4226717fe7e1a6474c534f5b5db30227R315

I'm sure it would be a good idea to set timeouts for the telemetry server as well, but I don't have enough experience with that type of scraping to provide meaningful defaults πŸ˜… I can follow up with an issue to set those in a future PR, how does that sound?

That works for me as well. Just want to make sure that we don't have --server- prefixed arguments and it's not clear for which server they are.

How did you chose the duration for each timeout setting? Should we provide any additional suggestions?

These were picked somewhat arbitrarily, but the intent is to align them some sane scrape times. When running in our dev cluster with dozens+ nodes, I have both aligned with our scrape interval and timeout(15s). As far as guidance, I think the minimum value should be the scrape timeout. The safest option for the defaults would be following the default scrape_interval from Prometheus which is 60s.

That makes sense to me, thanks for updating!

Pokom commented 3 weeks ago

What happens if a request takes longer than a timeout? Does the server send any log output? Is it visible on the telemetry server metrics?

I'm not sure if anything is logged on kube-state-metrics side, that's a good question. On the scraper side, it entirely depends on how they chose to handle a cancelled context. Ideally you would see something similar to:

2024/06/07 14:15:34 error: Get "http://localhost:8080/metrics": context deadline exceeded
mrueg commented 2 weeks ago

/hold

for others to review.

Changes

/lgtm

dgrisonnet commented 2 weeks ago

/assign @dgrisonnet @richabanker /triage accepted

richabanker commented 1 week ago

/lgtm

k8s-ci-robot commented 1 week ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrueg, Pokom, richabanker

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes/kube-state-metrics/blob/main/OWNERS)~~ [mrueg] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
mrueg commented 1 week ago

/hold cancel