Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get log and describe of pods in kubernetes by python client

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?

like image 830
check nova Avatar asked May 14 '19 06:05

check nova


People also ask

How do you check logs of pods in Kubernetes?

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.

How do I get a log file from Kubernetes pod?

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>/ .

How do I check logs for pods?

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.

How to retrieve application logs from a pod in Kubernetes?

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 in Kubernetes?

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.

Is it safe to leave a Kubernetes pod pending?

(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.

What is kubectl logs command in Linux?

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:


Video Answer


4 Answers

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.

like image 80
Prafull Ladha Avatar answered Nov 11 '22 16:11

Prafull Ladha


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".

like image 36
Alex Kaszynski Avatar answered Nov 11 '22 16:11

Alex Kaszynski


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

like image 22
coolinuxoid Avatar answered Nov 11 '22 15:11

coolinuxoid


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>

like image 38
Veerendra Avatar answered Nov 11 '22 17:11

Veerendra