Introduction

Kubernetes has become the de-facto standard for container orchestration, and Python, with its simplicity and robust library support, stands as a go-to language for many DevOps engineers. Combining the two can lead to a powerful synergy. In this post, we’ll dive into how one Use Python to interact with Kubernetes API, streamlining your workflows and bridging the gap between development and operations.

You can use the following guide to install Kubernetes How to Deploy Kubernetes Using Kubespray

Examples for Python and Kubernetes API

The following are basic functions for python to list Kubernetes objects

List Nodes

A typical deployment of Kubernetes will have 5+ nodes with 3 control plane nodes and at least 2 workers. You can list the nodes with the following function:

def list_nodes(api):
    nodes = api.list_node()
    for node in nodes.items:
        print(f"Node Name: {node.metadata.name}")
        print(f"Status: {node.status.conditions[-1].type}")

List Events

When pods are not working or deployment is not progressing in Kubernetes it’s smart to check the events for additional information. Here’s a function for that:

def list_events(api):
    events = api.list_event_for_all_namespaces()
    for event in events.items:
        print(f"{event.message} - {event.reason} at {event.first_timestamp}")

Check Services

Services gather all the pods under a specific criteria (by label or other filtering) and provide an abstraction for accessing the pods. Usually it’s with a Cluster IP which is reachable for all the pods. Here’s a function for it:

def check_services(api):
    services = api.list_service_for_all_namespaces()
    for service in services.items:
        print(f"Service: {service.metadata.name} in Namespace: {service.metadata.namespace}")
        print(f"Type: {service.spec.type}, Cluster IP: {service.spec.cluster_ip}")

List Namespaces

Namespaces separate projects and resource allocation in Kuberenetes for better utilization, access control and management. Here’s a function to list all the namespaces:

def list_namespaces():
    # List all namespaces
    namespaces = v1.list_namespace()
    for ns in namespaces.items:
        print(f"Namespace: {ns.metadata.name}")

Check Application Logs in a specific Namespace using Kuberenetes API

When one will need to troubleshoot logs in an application, it’s better to check the pod’s STDOUT log. In order to do that with python we can use the read_namespaced_pod_log function. Here’s a more elaborated example:

from kubernetes import client, config
import time

def monitor_logs(namespace):
    # Use this if running inside the cluster
    # config.load_incluster_config()
    
    # Uncomment the line below if running the script from outside the cluster
    config.load_kube_config()
    
    # Create API client
    v1 = client.CoreV1Api()
    
    # List all the pods in the namespace
    pods = v1.list_namespaced_pod(namespace)
    
    # Loop through all pods in the given namespace
    for pod in pods.items:
        print(f"Monitoring logs for pod: {pod.metadata.name}")
        
        # Get pod logs
        pod_logs = v1.read_namespaced_pod_log(name=pod.metadata.name, namespace=namespace, tail_lines=10, follow=True)
        
        # Check for keyword and alert (Example)
        if "Error" in pod_logs:
            print(f"ALERT: 'Error' found in logs of pod {pod.metadata.name}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <namespace>")
        sys.exit(1)
    
    namespace = sys.argv[1]
    monitor_logs(namespace)

To run the script run the following:

$ python log-monitor.py <your-namespace>

Summary

Harnessing How to Use Python to interact with Kubernetes API proves to be a game-changer. It not only simplifies complex tasks but also automates repetitive ones, freeing up valuable time for DevOps engineers. This approach enables engineers to concentrate more on the development side, leading to rapid iterations and a more efficient pipeline. At Octopus Computer Solutions, our expertise in Kubernetes and Python can provide you with tailored solutions to optimize and automate your Kubernetes environment.