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.
pip install lightkubeVerified import paths — ran on the pinned version, not inferred.
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.
For direct `next()` calls, convert to an iterator: `my_iterator = iter(client.list(...)); next(my_iterator)`.
Review model instantiations for `apiVersion` and `kind` fields. It may be safe to remove explicit settings if using `kubernetes-models>=1.33`.
Consult `lightkube`'s `pyproject.toml` or `setup.cfg` for exact `httpx` version requirements if encountering `httpx`-related errors. Upgrade `lightkube` and ensure `httpx` is within the supported range.
Ensure your Python environment is `3.9` or higher. Check `lightkube`'s `pyproject.toml` for the exact `requires_python` range.
Install `lightkube-models` with a version range matching your Kubernetes API server, e.g., `pip install 'lightkube-models>=1.28,<1.29'` for Kubernetes 1.28.
Ensure a `~/.kube/config` file exists and is correctly configured, or that the application is running inside a Kubernetes cluster with a service account mounted.
Before calling `next()`, explicitly convert the `ListIterable` to an iterator: `my_iterator = iter(client.list(...)); next(my_iterator)`.
Inspect the `e.status` attribute of the caught `lightkube.ApiError` for more details on the Kubernetes API error (e.g., `e.status.message`, `e.status.code`). Check API server logs for the specific issue.
Verify that `lightkube-models` is installed and its version range is compatible with the target Kubernetes cluster API version. For example, for K8s 1.28, you might need `pip install 'lightkube-models>=1.28,<1.29'`.