kr8s
kr8s is a simple, extensible Python client library for Kubernetes, designed to feel familiar for users accustomed to `kubectl`. It provides a high-level API for interacting with Kubernetes clusters, offering both synchronous and asynchronous interfaces. The library abstracts away many low-level API details, focusing on developer experience and ease of use for scripting, automation, and building Kubernetes operators. It is actively maintained with regular updates.
Warnings
- gotcha Unlike some auto-generated Kubernetes client libraries, kr8s is designed with a `kubectl`-like API. It simplifies interactions and makes assumptions about authentication and API versions from your environment/kubeconfig. This is a design choice to reduce boilerplate but may differ from expectations if coming from more verbose, low-level clients.
- gotcha kr8s's object model is an extensible, flexible, minimal set, not a 1:1 reflection of the full Kubernetes OpenAPI schema. While it covers common resources, complex or custom resource definitions might require direct API calls or subclassing `kr8s.objects.APIObject` for full fidelity. For strict typing and schema validation, consider integrating with libraries like `Lightkube`.
- gotcha kr8s provides both synchronous (`import kr8s`) and asynchronous (`import kr8s.asyncio`) APIs. The synchronous API is a wrapper around the asynchronous one. Mixing them incorrectly or using the synchronous API within an existing `asyncio` event loop without proper handling can lead to blocking or unexpected behavior.
- breaking kr8s aims to support actively maintained Kubernetes versions. As Kubernetes versions reach their end-of-life (EOL) from the community and cloud providers, kr8s may drop official support and testing for these versions. This could lead to compatibility issues for users on older, unsupported Kubernetes clusters.
- deprecated In version 0.20.15, a change was implemented to "Use `content` arg instead of `data` when calling api". This implies that using the `data` argument in direct API calls might be deprecated or will be removed in future versions.
Install
-
pip install kr8s
Imports
- kr8s
import kr8s
- Pod
from kr8s.objects import Pod
- asyncio
import kr8s.asyncio
Quickstart
import kr8s
# Connect to the Kubernetes API (uses kubeconfig by default)
# Equivalent to 'kubectl get nodes'
try:
# Synchronous API
nodes = kr8s.get("nodes")
print("Synchronous API - Nodes found:")
for node in nodes:
print(f"- {node.name}")
# Example using a specific object type and creating one
from kr8s.objects import Pod
# Define a simple Pod specification as a Python dictionary
pod_manifest = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "my-test-pod",
"labels": {"app": "my-app"}
},
"spec": {
"containers": [
{"name": "nginx", "image": "nginx:latest"}
]
}
}
print(f"\nAttempting to create pod: {pod_manifest['metadata']['name']}")
test_pod = Pod(pod_manifest)
test_pod.create()
print(f"Pod '{test_pod.name}' created in namespace '{test_pod.namespace}'.")
# Wait for the pod to be ready (optional)
print("Waiting for pod to be ready...")
test_pod.wait(conditions=["Ready"], timeout=60)
print("Pod is ready!")
# Clean up the pod
print(f"\nDeleting pod: {test_pod.name}")
test_pod.delete(cascade=True)
print("Pod deleted.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you have a running Kubernetes cluster and valid kubeconfig configured (e.g., `minikube start`).")