A simple, extensible Python client library for Kubernetes that feels familiar for folks who already know how to use kubectl.
Highlights
- API inspired by
kubectlfor a shallow learning curve. - Sensible defaults to reduce boiler plate.
- No swagger generated code, human readable code only.
- Has both a standard and an async API that can be used with
asyncioandtrio. - Client caching to reduce passing API objects around.
- Batteries included by providing useful utilities and methods inspired by
kubectl.
Quickstart
Installation
$ pip install kr8s
Examples
List Nodes
Print out all of the node names in the cluster.
import kr8s
for node in kr8s.get("nodes"):
print(node.name)
Create a Pod
Create a new Pod.
from kr8s.objects import Pod
pod = Pod({
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "my-pod",
},
"spec": {
"containers": [{"name": "pause", "image": "gcr.io/google_containers/pause",}]
},
})
pod.create()
Scale a Deployment
Scale the Deployment metrics-server in the Namespace kube-system to 1 replica.
from kr8s.objects import Deployment
deploy = Deployment.get("metrics-server", namespace="kube-system")
deploy.scale(1)
List Pods by label selector
Get all Pods from all Namespaces matching a label selector.
import kr8s
selector = {'component': 'kube-scheduler'}
for pod in kr8s.get("pods", namespace=kr8s.ALL, label_selector=selector):
print(pod.namespace, pod.name)
Add a label to a Pod
Add the label foo with the value bar to an existing Pod.
from kr8s.objects import Pod
pod = Pod("kube-apiserver", namespace="kube-system")
pod.label({"foo": "bar"})
Generate a Pod
Generate a simple Pod with a couple of keyword arguments.
from kr8s.objects import Pod
pod = Pod.gen(name="example-1", image="nginx:latest")
pod.create()
Cordon a Node
Cordon a Node to mark it as unschedulable.
from kr8s.objects import Node
node = Node("k8s-node-1")
node.cordon()
Pod Exec
Exec a command in a Pod.
from kr8s.objects import Pod
pod = Pod.get("my-pod")
command = pod.exec(["uptime"])
print(command.stdout.decode())
# 13:49:05 up 23:03, 0 users, load average: 0.66, 0.87, 0.85
Port forward a Pod
Open a port forward to a Pod as a background task/thread.
from kr8s.objects import Pod
pod = Pod.get("my-pod")
pf = pod.portforward(remote_port=1234, local_port=5678)
# Starts the port forward in a background thread
pf.start()
# Your other code goes here
# Optionally stop the port forward thread (it will exit with Python anyway)
pf.stop()
Metadata
Release files for kr8s 0.20.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kr8s-0.20.15.tar.gz | 2.8 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kr8s-0.20.15-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 2.9 MB
Release files / kr8s-0.20.15.tar.gz
| Download URL | kr8s-0.20.15.tar.gz |
|---|---|
| Size | 2.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
484d5206bbebd48f8cc00dd3ed749d4419294d2f08cd50c6232114cf62ed6d3a
|
|
BLAKE2b-256 checksum How to use checksums |
2055ca9ff53d97413a0dca36c30da80750415350876aa23ea8a762a0b06c6d1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 16, 2026.
Transparency logRelease files / kr8s-0.20.15-py3-none-any.whl
| Download URL | kr8s-0.20.15-py3-none-any.whl |
|---|---|
| Size | 87.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eacc6eb72b6354d932a1e22c0cb698c88adbd815c36164c24c2232a69dfabe40
|
|
BLAKE2b-256 checksum How to use checksums |
59084247abe88116c1e59dd9b62992d8a69b7178f4046c85cca03eda7e3d87e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 16, 2026.
Transparency log