How to get Nodes using Kubernetes Python Client

close up photo of programming of codes
Reading Time: 3 minutes

Hello Readers! In this blog we will see how we can list the nodes inside any cluster using Kubernetes Python Client. As we all know that generally we use kubectl commands for creating, listing, updating, deleting the kubernetes resources. But In this blog we will see how we can use python for doing these things with nodes. 


Before we move forward with getting nodes in a cluster using K8s python client we have some prerequisites that we need to follow.

Kubernetes library provides us modules such as client and config which we will use here. So, let’s install Kubernetes Python Client:

$ pip install kubernetes

Now, we have the python-kubernetes package-installed.

So, let’s get started!

Getting Nodes inside a cluster:

Step1: Firstly I will make a directory and inside this I will make a file in which I will write kubernetes python client code for listing all the nodes inside any cluster. 

Step2: Move to this file and write here the python code. Now that we have the python-kubernetes package installed, we can import it as:

from kubernetes import client, config

For Nodes, we use the CoreV1Api class from the kubernetes python client module. Authentication to the Kubernetes Python Client in other cluster is done by: 

configuration.api_key = {"authorization": "Bearer" + bearer_token}

We will use the Bearer Token which enables requests to authenticate using an access key. We are going to list all the nodes with their attached labels using this code. Here is the full code which will list the nodes inside a cluster:

from kubernetes import client
from kubernetes.client import ApiClient

def __get_kubernetes_client(bearer_token,api_server_endpoint):
        configuration = client.Configuration() = api_server_endpoint
        configuration.verify_ssl = False
        configuration.api_key = {"authorization": "Bearer " + bearer_token}
        client_api = client.CoreV1Api()
        return client_api
    except Exception as e:
        print("Error getting kubernetes client \n{}".format(e))
        return None

def __format_data_for_nodes(client_output):
        #print("JSON_DATA OF KUBERNETES OBJECT:{}".format(json_data))
        if len(json_data["items"]) != 0:
            for node in json_data["items"]:
                    "node": node["metadata"]["name"],
                    "labels": node["metadata"]["labels"],
        return temp_list

def get_nodes(cluster_details, namespace="default",all_namespaces=False):
        client_api= __get_kubernetes_client(
        node_list = client_api.list_node()
        print (data)

if __name__ == "__main__":



In the file we have a function for getting nodes. Give your cluster details in which you want to list the nodes:


After providing the details, we need to call that function by:


Step3: Now, let’s run our code and see the result. Run the code using following command:

$ python3

The output is here:

kubernetes python client

Here is the list of all the nodes and their labels which are in my GKE cluster. In the output you will see list of nodes inside a cluster with labels in the following format:

[{'node':'<node_name>', 'labels':{'<list_of_labels>'}}]

So, We are successfully done now. This is how we can list nodes inside any cluster using Kubernetes python client.


Thank you for sticking to the end. In this blog we have seen how easily we can list nodes inside any cluster using Kubernetes python client. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.


Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.