print logs from Kubernetes api print logs from Kubernetes api kubernetes kubernetes

print logs from Kubernetes api


What i was missing is stream=True in request.get parameter which allows to iterate through the response content so my code looks like this:

import requestsclass Logs():    def __init__(self, url='https://192.168.0.1:6443/api/v1/',                 cert='./client.crt',                 key='./client.key'):        self.url = url        self.cert = cert        self.key = key        requests.packages.urllib3.disable_warnings()    def get_pod_logs(self, namespace, pod_name):        params = dict(            follow="true",            tailLines="100"        )        r = requests.get(self.url + "namespaces/" + namespace + "/pods/" + pod_name + "/log", params=params,                         cert=(self.cert, self.key), verify=False, stream=True)        for chunk in r.iter_content(chunk_size=256):            if chunk:                print(chunk)logs = Logs()logs.get_pod_logs(namespace="my-ns",pod_name="my-pod")


You're almost there. I think what you want is the following:

kubectl logs -f <your-pod-name> --tail=100

You can get pod names with the following command:

kubectl get pods

Hope that helps