Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: kubelet will not output logs after log file is rotated #115702

Merged
merged 1 commit into from
Oct 14, 2023

Conversation

xyz-li
Copy link
Contributor

@xyz-li xyz-li commented Feb 11, 2023

What type of PR is this?

/kind bug

What this PR does / why we need it:

kubectl logs POD_NAME -f won't output logs after kubelet rotate log file of the container .

Which issue(s) this PR fixes:

Fixes #115701

Special notes for your reviewer:

Comment from the method Wacher.Add

// # Watching directories
//
// All files in a directory are monitored, including new files that are created
// after the watcher is started. Subdirectories are not watched (i.e. it's
// non-recursive).
//
// # Watching files
//
// Watching individual files (rather than directories) is generally not
// recommended as many tools update files atomically. Instead of "just" writing
// to the file a temporary file will be written to first, and if successful the
// temporary file is moved to to destination removing the original, or some
// variant thereof. The watcher on the original file is now lost, as it no
// longer exists.
//
// Instead, watch the parent directory and use Event.Name to filter out files
// you're not interested in. There is an example of this in [cmd/fsnotify/file.go].

Does this PR introduce a user-facing change?

Fixed the bug that kubelet could't output logs after log file rotated when kubectl logs POD_NAME -f is running.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 11, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @xyz-li. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label Feb 11, 2023
@k8s-ci-robot k8s-ci-robot added area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 11, 2023
@bart0sh bart0sh added this to Triage in SIG Node PR Triage Feb 11, 2023
@xyz-li xyz-li changed the title FIX: kubelet will not output logs after log file is rotated Fix: kubelet will not output logs after log file is rotated Feb 12, 2023
Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a unit test to TestReadLogs to cover the failure scenario?

pkg/kubelet/kuberuntime/logs/logs.go Outdated Show resolved Hide resolved
@saschagrunert
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 13, 2023
@xyz-li
Copy link
Contributor Author

xyz-li commented Feb 14, 2023

Can we add a unit test to TestReadLogs to cover the failure scenario?

Working on it.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 15, 2023
@xyz-li xyz-li requested review from saschagrunert and removed request for derekwaynecarr and SergeyKanzhelev February 15, 2023 17:46
@xyz-li
Copy link
Contributor Author

xyz-li commented Jul 17, 2023

/assign @sjenning

@tuibeovince
Copy link

I was able to confirm that this patch/fix allows continuous output of kubectl logs -f even after log rotation. I would love to have this patch merged soon.

@tuibeovince
Copy link

I also did a confirmation of the performance of this fix by recursively executing the kubectl logs command over 100 times. A stuck log during rotation didn't occur even once using this fix.

I used the reproduction method in #115701

  • As supplimentary info:

As of the the release of v1.28, executing kubectl logs POD_NAME -f numerous times may also trigger a "failed to watch file" error.

Specifically speaking, I executed the logging command for the pod 100 times. It occurs very very rarely, about once in one hundred executions. This PR also addresses and fixes this error's occurence as far as my reproduction methodology is concerned.

Stdout:

...
print log to stand out 1064678
print log to stand out 1064679
print log to stand out 1064680
failed to watch file "/var/log/pods/default_testlogrotator_41df37c5-02e3-4687-a237-329bec3b2d25/testlogrotator/0.log": no such file or directory

@dims
Copy link
Member

dims commented Oct 14, 2023

this has waited long enough. let's see if something shakes up in the CI

/approve
/lgtm

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, xyz-li

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:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 14, 2023
@k8s-ci-robot k8s-ci-robot merged commit 4911aad into kubernetes:master Oct 14, 2023
12 checks passed
SIG Node PR Triage automation moved this from Needs Approver to Done Oct 14, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.29 milestone Oct 14, 2023
@tuibeovince
Copy link

this has waited long enough. let's see if something shakes up in the CI

/approve /lgtm

Thank you very much for this approval. I believe this log rotation problem has been an issue for other people for a while now.

@zwpaper
Copy link
Member

zwpaper commented Oct 16, 2023

as this fixes a bug many people had met in production environments, will this be cherry-picked to the previous releases?

rfratto added a commit to rfratto/agent that referenced this pull request Oct 26, 2023
Versions of Kubernetes that do not contain kubernetes/kubernetes#115702
will fail to detect rolled log files, causing the API to stop sending
logs to the agent for processing.

To work around this, this commit intorduces a rolling average calculator
to determine the average delta between log entries per target. If 3x the
normal delta time has elapsed since the last entry, the tailer is
restarted.

False positives here are acceptable, but false negatives mean that log
lines may not appear for an extended period of time until the rolling
detection succeeds.

Closes grafana#5040
rfratto added a commit to rfratto/agent that referenced this pull request Oct 26, 2023
Versions of Kubernetes that do not contain kubernetes/kubernetes#115702
will fail to detect rolled log files, causing the API to stop sending
logs to the agent for processing.

To work around this, this commit intorduces a rolling average calculator
to determine the average delta between log entries per target. If 3x the
normal delta time has elapsed since the last entry, the tailer is
restarted.

False positives here are acceptable, but false negatives mean that log
lines may not appear for an extended period of time until the rolling
detection succeeds.

Closes grafana#5040

Co-authored-by: Edward Welch <[email protected]>
rfratto added a commit to rfratto/agent that referenced this pull request Oct 26, 2023
Versions of Kubernetes that do not contain kubernetes/kubernetes#115702
will fail to detect rolled log files, causing the API to stop sending
logs to the agent for processing.

To work around this, this commit intorduces a rolling average calculator
to determine the average delta between log entries per target. If 3x the
normal delta time has elapsed since the last entry, the tailer is
restarted.

False positives here are acceptable, but false negatives mean that log
lines may not appear for an extended period of time until the rolling
detection succeeds.

Closes grafana#5040

Co-authored-by: Edward Welch <[email protected]>
rfratto added a commit to grafana/agent that referenced this pull request Oct 30, 2023
* component/prometheus: fix panic in interceptor when child isn't set

This commit fixes a panic in prometheus.Interceptor where an interceptor
which doesn't forward samples to another appendable panics when
appending data.

Co-authored-by: Edward Welch <[email protected]>

* loki.source.kubernetes: improve detection of rolled log files

Versions of Kubernetes that do not contain kubernetes/kubernetes#115702
will fail to detect rolled log files, causing the API to stop sending
logs to the agent for processing.

To work around this, this commit intorduces a rolling average calculator
to determine the average delta between log entries per target. If 3x the
normal delta time has elapsed since the last entry, the tailer is
restarted.

False positives here are acceptable, but false negatives mean that log
lines may not appear for an extended period of time until the rolling
detection succeeds.

Closes #5040

Co-authored-by: Edward Welch <[email protected]>

* loki.source.kubernetes: support clustering

Add support for loki.source.kubernetes to distribute targets using
clustering.

Closes #4502

Co-authored-by: Edward Welch <[email protected]>

* loki.source.podlogs: support clustering

Add support for loki.source.podlogs to distribute targets using
clustering.

* service/cluster: add common block for clustering arguments

* remove irrelevant TODO comment

#5623 (comment)

---------

Co-authored-by: Edward Welch <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubelet cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/node Categorizes an issue or PR as relevant to SIG Node. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

kubectl logs will not output logs after the log file is rotated.