Kubernetes Module In python

Prince Prashant Saini
2 min readApr 12, 2021

First we install kubernetes module in python https://pypi.org/project/kubernetes/

pip install kubernetes

then our req to print all the name space and pods

then our req to prints all the pods and its namespace so we create code for that. so we have import kubernetes module then sub-module client, config
# Configs can be set in Configuration class directly or using helper utility

from kubernetes import client, config


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))

we have loaded the config and
then we call API
we have print one statement
then we call all pods in all namespace then we have run for loop then print all IP , namespaces and name of pods

First check how many pods running in our k8s

kubectl get pods

Then check all the namespaces

kubectl get ns

now check with out python code its working all not

yes its working✌✌😎

--

--