Lightweight Kubernetes Client

0.19.1 · active · verified Thu Apr 16

Lightkube is a modern, lightweight Python client library for Kubernetes, providing a simple, type-hinted interface for interacting with the Kubernetes API. It supports both synchronous and asynchronous operations, facilitates loading resources from YAML, and automatically handles pagination. The current version is 0.19.1 and the library maintains an active release cadence with frequent updates to support new Kubernetes versions and add features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an `AsyncClient` and list all pods across all namespaces using asynchronous iteration. Lightkube automatically handles Kubernetes authentication via `kubeconfig` or in-cluster service accounts.

import asyncio
from lightkube import AsyncClient, ALL_NS
from lightkube.resources.core_v1 import Pod

async def list_all_pods():
    client = AsyncClient() # Automatically loads kubeconfig or in-cluster config
    try:
        print("Listing all pods in all namespaces:")
        async for pod in client.list(Pod, namespace=ALL_NS):
            print(f"  Pod: {pod.metadata.name}, Namespace: {pod.metadata.namespace}, Status: {pod.status.phase}")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(list_all_pods())

view raw JSON →