Within A Pod

If you need to access the Kubernetes API from within a pod running in the cluster, don’t waste your time trying to do so using bearer tokens, etc. The native Kubernetes Python client supports configuring the client using an in-cluster authentication scheme, which assumes the service-account the pod is running as.

To do so, call the config.load_incluster_config() method. Here’s an example of this in use:

from kubernetes import client as k8s_client, config as k8s_config
 
k8s_config.load_incluster_config()
 
core = k8s_client.CoreV1Api()  
pods = core.list_namespaced_pod(namespace="my-namespace")
for item in pods.items:  
    ns = item.metadata.namespace  
    name = item.metadata.name  
    phase = (item.status.phase or "").lower()  
    print(f"{ns}/{name}  [{phase}]")

Source: Kubernetes documentation