question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

read_namespaced_pod_log with follow=True hangs when there is a long gap between logs from pod

See original GitHub issue

What happened (please include outputs or screenshots): If I try to read the logs of a pod with the following:

from kubernetes import config, client
config.load_incluster_config()
k8s = client.CoreV1Api()
namespace = "default"
pod_name="watch-this"
logs = k8s.read_namespaced_pod_log(
    name=pod_name,
    namespace=namespace,
    follow=True,
    _preload_content=False,
)
for line in logs:
  print(line)

and there is a gap (say 5 minutes) between line1 and line2 of the log being written, then the function hangs after the first line

What you expected to happen:

the function should read all the logs as they are written from the watched pod and then exit once finished

How to reproduce it (as minimally and precisely as possible):

Create the following pod to watch:

apiVersion: v1
kind: Pod
metadata:
  name: watch-this
  namespace: default
spec:
  containers:
  - command:
    - python3
    - -u
    - -c
    - |
      from time import sleep
      for i in range(3):
        print(i)
        if i != 2:
          sleep(300)
    image: python:3.7.4-slim-stretch
    imagePullPolicy: IfNotPresent
    name: python
    resources: {}
    stdin: true
    stdinOnce: true
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
  priority: 0
  restartPolicy: Never
  schedulerName: default-scheduler
  serviceAccountName: default
  terminationGracePeriodSeconds: 30

then watch the pod with the following:

kubectl -n default run -i -t test-python-kube-client --image=python:3.7.4-slim-stretch --restart=Never --command -- /bin/sh

pip install kubernetes==11.0.0
python
from kubernetes import config, client
config.load_incluster_config()
k8s = client.CoreV1Api()
namespace = "default"
pod_name="watch-this"
logs = k8s.read_namespaced_pod_log(
    name=pod_name,
    namespace=namespace,
    follow=True,
    _preload_content=False,
)
for line in logs:
  print(line)

the output should get stuck like this: b'0\n'

however if I run the equivalent request using kubectl proxy and the kubernetes API, it works as expected:

kubectl proxy --port=8080 &
curl http://localhost:8080/api/v1/namespaces/default/pods/watch-this/log?follow=true
0
1
2

Anything else we need to know?:

Environment:

  • Kubernetes version (Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:11:31Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"darwin/amd64"} Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.13", GitCommit:"1da71a35d52fa82847fd61c3db20c4f95d283977", GitTreeState:"clean", BuildDate:"2020-07-15T21:59:26Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}):
  • OS ( MacOS 10.14.6):
  • Python version (3.7.4)
  • Python client version (kubernetes==11.0.0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
raphael-boscommented, Sep 20, 2020

I have faced the same problem this last week. After spending a fair amount of time investigating, I came to the conclusion the problem is with the TCP Keep Alive socket option, which urllib3 doesn’t set by default. So, while running a long http request, the server doesn’t get any tcp keep alive probes from the client and decides to close the connection without notifying the client, which then hangs waiting for the complete response from the server. These socket options worked for me (the times chosen are only for demonstration):

socket_options= [
    (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
    (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 5),
    (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30),
    (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
]

Socket options should be set in the urllib3.PoolManager class here: https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L106

Another side effect of running long tcp connections can be seen in #1158 . Basically, connections used by the PoolManager are never closed and therefore are unusable for subsequently requests. This also makes the client hangs if it tries to make another request (read_namespaced_pod, for example) after a long running one. A dirty hack is to call rest_client.pool_manager.clear() after a long running request.

0reactions
k8s-ci-robotcommented, Nov 11, 2022

@Ark-kun: You can’t reopen an issue/PR unless you authored it or you are a collaborator.

In response to this:

/reopen

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue using readNamespacedPodLog - Stack Overflow
There was an issue with one specific pod. Issue has gone after pod restart. For all other pods log sizes are correct.
Read more >
How to check the logs of running and crashed pods in ...
Here I will show you how to check the logs of your Kubernetes pods for both running and crashed pods using the kubectl...
Read more >
Kubernetes Logging: 101 Guide to Logs, Best Practices & More
From various Kubernetes log types to the different methods you can use, learn how to set up your infrastructure for efficient logging and...
Read more >
How to “Live Tail” Kubernetes Logs - Papertrail
Review container and pod activity; Monitor the result of actions, ... are limited in their ability to live tail logs or tail multiple...
Read more >
CoreV1Api.readNamespacedPodLog - Java - Tabnine
Print out the Log for specific Pods * * @param namespace * @param podName * @throws ApiException */ public static void printLog(String namespace, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found