Python: how to use the Kubernetes API

Python: how to use the Kubernetes API

In this article, we will explore how to use Kubernetes APIs in Python to manage cluster resources.

Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications. One of the powerful features of Kubernetes is its Application Programming Interface (API), allowing developers to interact with the cluster and automate many operations. In this article, we will explore how to use Kubernetes APIs in Python to manage cluster resources.


Before getting started, make sure you have a working and properly configured Kubernetes cluster. You will also need a Python 3.x environment with the kubernetes module installed. You can install the module using the following command:

pip install kubernetes

Connect to the Kubernetes Cluster

To interact with Kubernetes APIs in Python, we need to establish a connection with the cluster. We will use the kubernetes.client module for this purpose. Here is an example of how to do it:

from kubernetes import client, config

# Load the cluster configuration

# Create an object of the `ApiClient` class to communicate with the cluster
api_instance = client.CoreV1Api()

Retrieve Information from the Cluster

Once the connection is established, we can use Kubernetes APIs to retrieve information about the cluster's state. For example, to get the list of nodes in the cluster, we can use the following code:

# Retrieve the list of nodes in the cluster
nodes = api_instance.list_node()

print("List of nodes in the cluster:")
for node in nodes.items:
    print(f"Name: {}, IP Address: {node.status.addresses[0].address}")

Create and Manage Resources

We can use Kubernetes APIs to create, update, or delete resources in the cluster. Below is an example of how to create a pod:

from kubernetes.client import V1Container, V1Pod, V1PodSpec

# Define the pod's container
container = V1Container(name="my-container", image="nginx:latest")

# Define the pod's specifications
pod_spec = V1PodSpec(containers=[container])

# Create the Pod object
pod = V1Pod(metadata={"name": "my-pod"}, spec=pod_spec)

# Create the pod in the cluster
api_instance.create_namespaced_pod(namespace="default", body=pod)

Handle Errors and Exceptions

When working with Kubernetes APIs, it's important to properly handle errors. For example, if a resource creation operation fails, an ApiException exception will be raised. It is good practice to include error handling in your code to ensure the program behaves correctly even in unforeseen situations.

from import ApiException

    # Code that might raise an exception
    api_instance.create_namespaced_pod(namespace="default", body=pod)
except ApiException as e:
    print(f"An error occurred: {e}")


In this article, we have explored how to use Kubernetes APIs in Python to interact with a Kubernetes cluster. We covered connecting to the cluster, retrieving information, and managing resources. It's important to note that Kubernetes offers many other APIs and resources that can be used based on the specific needs of the project. For further details, it is recommended to consult the official documentation of Kubernetes and the kubernetes Python module.