Trying to use the python kubernetes API to stream the output of kubernetes pod logs. (eventual goal is to stream the logs out via websocket)
Based off this PR that was merged into python kubernetes module, i thought watch would work with read_namespaced_pod_log?
v1 = client.CoreV1Api()
w = watch.Watch()
for e in w.stream(v1.read_namespaced_pod_log, name=pod, namespace=namespace, follow=True, tail_lines=1, limit_bytes=560, _preload_content=False):
print(e)
But i get the error below, am i missing something that needs to be passed to watch? or read_namespaced_pod_log?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/kubernetes/watch/watch.py", line 132, in stream
resp = func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 18538, in read_namespaced_pod_log
(data) = self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs)
File "/usr/local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 18576, in read_namespaced_pod_log_with_http_info
" to method read_namespaced_pod_log" % key
TypeError: Got an unexpected keyword argument 'watch' to method read_namespaced_pod_log
To get Kubectl pod logs, you can access them by adding the -p flag. Kubectl will then get all of the logs stored for the pod. This includes lines that were emitted by containers that were terminated.
You can view these logs in a continuous stream in your browser using the Papertrail Event Viewer, as well as the Papertrail CLI client or Papertrail HTTP API. Papertrail shows all logs by default, but you can limit these to a specific pod, node, or deployment using a flexible search syntax.
If you want to show logs of all pods, you can use -l and specify a lable, but at the same time -f won't be used. "but this will choose one pod of the deployment, not all pods" -> true and I've spent a lot of time debugging my app before realizing that not all logs were being displayed.
As mentioned in other answers, the best way is to have your logs centralized via logging agents or directly pushing these logs into an external service. Alternatively and given the logging architecture in Kubernetes, you might be able to fetch the logs directly from the log-rotate files in the node hosting the pods.
You should just do:
v1 = client.CoreV1Api()
w = Watch()
for e in w.stream(v1.read_namespaced_pod_log, name=pod, namespace=namespace):
print(e)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With