kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.56k stars 3.24k forks source link

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

Closed 0xdholleran closed 1 day ago

0xdholleran commented 3 years ago

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:

roycaihw commented 3 years ago

/assign cc @palnabarun

palnabarun commented 3 years ago

/assign

roycaihw commented 3 years ago

You can use the watch util to stream logs. For example: https://github.com/kubernetes-client/python-base/blob/fd322f70aa6c33782ab466afd4eaae271a27cbcb/watch/watch_test.py#L83-L85

0xdholleran commented 3 years ago

hi @roycaihw, that doesn't work either. If I try to stream the logs of the pod defined above with:

from kubernetes import config, client, watch
config.load_incluster_config()
k8s = client.CoreV1Api()
namespace = "default"
pod_name="watch-this"
w = watch.Watch()
for e in w.stream(k8s.read_namespaced_pod_log, name=pod_name, namespace=namespace):
    print(e)

This doesn't work either. It also hangs after the first line

raphael-bos commented 3 years ago

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.

fejta-bot commented 3 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot commented 3 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle rotten

fejta-bot commented 3 years ago

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community. /close

k8s-ci-robot commented 3 years ago

@fejta-bot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/python/issues/1234#issuecomment-780730298): >Rotten issues close after 30d of inactivity. >Reopen the issue with `/reopen`. >Mark the issue as fresh with `/remove-lifecycle rotten`. > >Send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
mrpowerus commented 3 years ago

/reopen

k8s-ci-robot commented 3 years ago

@mrpowerus: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to [this](https://github.com/kubernetes-client/python/issues/1234#issuecomment-820664667): >/reopen Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
Ark-kun commented 1 year ago

/reopen

k8s-ci-robot commented 1 year ago

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

In response to [this](https://github.com/kubernetes-client/python/issues/1234#issuecomment-1311469563): >/reopen Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
whybeyoung commented 1 month ago

/reopen

k8s-ci-robot commented 1 month ago

@whybeyoung: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to [this](https://github.com/kubernetes-client/python/issues/1234#issuecomment-2141134179): >/reopen Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
k8s-triage-robot commented 1 day ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-ci-robot commented 1 day ago

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to [this](https://github.com/kubernetes-client/python/issues/1234#issuecomment-2205154316): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue with `/reopen` >- Mark this issue as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close not-planned > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.