I want to get log and describe of my pod in kubernetes by python client. In kubernetes cluster we can use
kubectl logs <NAME_OF_POD>
kubectl describe pods <NAME_OF_pod>
But I want these command in python client of kubernetes.what should I do?
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.
Run kubectl get pod <pod_name> -n <namespace> -o jsonpath='{. spec. nodeName}' to get the node this Pod is running on. ssh into the node and you'll find the logs for the Pod at /var/log/pods/<namespace>_<pod_name>_<pod_id>/<container_name>/ .
Checking the logs of a running pod All that you need to do to do that is to run the following command: kubectl logs nginx-7d8b49557c-c2lx9.
Application logs can be retrieved from a Pod in Kubernetes using kubectl command. In this note i will show how to get logs from a running Pod (including all replicas) and a previously crashed Pod. I will also show how to tail and follow logs from a Pod using kubectl command.
What is Kubectl Describe Pod? Kubectl Describe Pod is a command that describes any resource in Kubernetes. It is used to show data on a single or even a collection of resources. This command combines a number of API calls to create a thorough description of a resource or set of resources.
(Alternatively, you could leave one Pod pending, which is safe.) The kubectl describe pods command gives you complete information about each of the Kubernetes infrastructure pods. Run the command kubectl describe pod if you want to see the output from a specific pod.
The kubectl logs command lets you inspect the logs produced by a named Pod: The Pod’s existing logs will be emitted to your terminal. When the Pod’s formed from more than one container, you must also specify the name of the contaienr you want to inspect:
You can read the logs of a pod using the following code:
from kubernetes.client.rest import ApiException
from kubernetes import client, config
config.load_kube_config()
pod_name = "counter"
try:
api_instance = client.CoreV1Api()
api_response = api_instance.read_namespaced_pod_log(name=pod_name, namespace='default')
print(api_response)
except ApiException as e:
print('Found exception in reading the logs')
The above code works perfectly fine for getting the logs of pod.
To get the output of kubectl describe pod
, all the information provided is in read_namespaced_pod
function. It has all the information you require, and you can use that information in whatever way you require. You can edit the above code and use read_namespaced_pod
in place of read_namespaced_pod_log
to get the info.
None of these answers return the events for a namespaced pod, which is given by default when running kubectl describe
. To get the namespaced events for a given pod, run:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
out = kube.core_api.list_namespaced_event(namespace, field_selector=f'involvedObject.name={pod_name}')
where namespace
is the namespace of interest and pod_name
is your pod of interest.
I needed this when spawning a pod and giving the user a reasonable status report for the current condition of the pod, as well as debugging the state of the pod if it fails to progress beyond "Pending".
As Kubernetes is using REST API, which gives you every possibility that you can call logs and description of objects via python.
Firstly you need to find out your authorization mechanism. You can find it via according your cluster.
kubectl config view --raw
Then you can create api call with this auth method. In below example, I used basic auth for getting pod logs for example.
import json
import requests
from requests.auth import HTTPBasicAuth
user='admin'
password='password'
url='https://cluster-api-url/api/v1/namespaces/default/pods/nginx-ingress-controller-7bbcbdcf7f-dgr57/log'
requests.packages.urllib3.disable_warnings()
resp = requests.get(url, auth=HTTPBasicAuth(user, password), verify=False, json=False)
print(resp.text)
To get the url easily, type command with "--v=8" argument. For example to get url for describe the pod
kubectl describe pod nginx-ingress-controller-7bbcbdcf7f-dgr57 --v=8
and check above part of your real output
I0514 12:31:42.376972 216066 round_trippers.go:383] GET https://cluster-api-url/api/v1/namespaces/default/events?fieldSelector=involvedObject.namespace%3Ddefault%2CinvolvedObject.uid%3D1ad92455-7589-11e9-8dc1-02a3436401b6%2CinvolvedObject.name%3Dnginx-ingress-controller-7bbcbdcf7f-dgr57
I0514 12:31:42.377026 216066 round_trippers.go:390] Request Headers:
I0514 12:31:42.377057 216066 round_trippers.go:393] Accept: application/json, */*
I0514 12:31:42.377074 216066 round_trippers.go:393] Authorization: Basic YWRtaW46elRoYUJoZDBUYm1FbGpzbjRtYXZ2N1hqRWlvRkJlQmo=
I0514 12:31:42.377090 216066 round_trippers.go:393] User-Agent: kubectl/v1.12.0 (linux/amd64) kubernetes/0ed3388
Copy URL from GET https://<URL>
part and change with url
in your python script, then you go.
Hope it helps
You need explore https://github.com/kubernetes-client/python Official K8s Python Client.
But I couldn't find any specific docs to your requirement. I think, below link is the start point,
https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
Try to do dir
on the object and see available methods. For example below code from README
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Do dir(v1)
or dir(ret)
and see methods/vars, etc.. Or may list*
methods gives you details that you see in kubectl describe pod <name>
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