Lightweight Python 3+ client library for Kubernetes (pykube-ng)
Go to file
Henning Jacobs c1ac2c9758 v23.6.0 2023-06-16 19:58:47 +02:00
docs -> 2020-10-11 15:15:02 +02:00
pykube v23.6.0 2023-06-16 19:58:47 +02:00
tests fix: resolve KeyError on watch failures 2022-09-16 09:48:36 -04:00
.flake8 Switch to poetry & black (#51) 2019-12-23 19:27:36 +01:00
.gitignore update dependencies 2022-09-18 15:43:23 +02:00
.pre-commit-config.yaml update dependencies 2023-06-16 19:58:12 +02:00
.tools-version #4 ADD certificate authority env variable to override CA used to validate http calls 2021-02-09 15:00:52 +00:00
.travis.yml Switch to poetry & black (#51) 2019-12-23 19:27:36 +01:00 changelog for 0.17 and 0.18 2019-03-03 18:32:39 +01:00
LICENSE initial port of code 2015-07-26 15:02:38 -06:00
Makefile update dependencies 2023-04-07 15:22:53 +02:00
README.rst -> 2020-10-11 15:15:02 +02:00
poetry.lock update dependencies 2023-06-16 19:58:12 +02:00
pyproject.toml v23.6.0 2023-06-16 19:58:47 +02:00



Build status

Code Coverage


PyPI version

Python versions

Apache License


Pykube (pykube-ng) is a lightweight Python 3.6+ client library for Kubernetes.

This is a fork of kelproject/pykube which is no longer maintained (archived). Here the original text of the pykube README:

Kel is an open source Platform as a Service (PaaS) from Eldarion, Inc. that makes it easy to manage web application deployment and hosting through the entire lifecycle from development through testing to production. It adds components and tools on top of Kubernetes that help developers manage their application infrastructure. Kel builds on Eldarion's 7+ years experience running one of the leading Python and Django PaaSes. For more information about Kel, see or follow us on Twitter @projectkel.


  • HTTP interface using requests using kubeconfig for authentication
  • Python native querying of Kubernetes API objects


To install pykube, use pip:

pip install pykube-ng

Interactive Console

The pykube library module can be run as an interactive console locally for quick exploration. It will automatically load ~/.kube/config to provide the api object, and it loads pykube classes (Deployment, Pod, ..) into local context:

python3 -m pykube
>>> [ for d in Deployment.objects(api)]


Query for all ready pods in a custom namespace:

import operator
import pykube

api = pykube.HTTPClient(pykube.KubeConfig.from_file())
pods = pykube.Pod.objects(api).filter(namespace="gondor-system")
ready_pods = filter(operator.attrgetter("ready"), pods)

Access any attribute of the Kubernetes object:

pod = pykube.Pod.objects(api).filter(namespace="gondor-system").get(name="my-pod")

Selector query:

pods = pykube.Pod.objects(api).filter(
    selector={"": {"api-web", "api-worker"}},
pending_pods = pykube.objects.Pod.objects(api).filter(
    field_selector={"status.phase": "Pending"}

Watch query:

watch = pykube.Job.objects(api, namespace="gondor-system")
watch = watch.filter(field_selector={"": "my-job"}).watch()

# watch is a generator:
for watch_event in watch:
    print(watch_event.type) # 'ADDED', 'DELETED', 'MODIFIED'
    print(watch_event.object) # pykube.Job object

Create a Deployment:

obj = {
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "name": "my-deploy",
        "namespace": "gondor-system"
    "spec": {
        "replicas": 3,
        "selector": {
            "matchLabels": {
                "app": "nginx"
        "template": {
            "metadata": {
                "labels": {
                    "app": "nginx"
            "spec": {
                "containers": [
                        "name": "nginx",
                        "image": "nginx",
                        "ports": [
                            {"containerPort": 80}
pykube.Deployment(api, obj).create()

Delete a Deployment:

obj = {
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "name": "my-deploy",
        "namespace": "gondor-system"
pykube.Deployment(api, obj).delete()

Check server version:

api = pykube.HTTPClient(pykube.KubeConfig.from_file())


  • Python 3.6+
  • requests (included in install_requires)
  • PyYAML (included in install_requires)

Local Development

You can run pykube against your current kubeconfig context, e.g. local Minikube:

poetry install
poetry run python3
>>> import pykube
>>> config = pykube.KubeConfig.from_file()
>>> api = pykube.HTTPClient(config)
>>> list(pykube.Deployment.objects(api))

To run PEP8 (flake8) checks and unit tests including coverage report:

make test


The code in this project is licensed under the Apache License, version 2.0 (included in this repository under LICENSE).


Easiest way to contribute is to provide feedback! We would love to hear what you like and what you think is missing. Create an issue or ping try_except_ on Twitter.

PRs are welcome. Please also have a look at issues labeled with "help wanted".

Code of Conduct

In order to foster a kind, inclusive, and harassment-free community, this project follows the Contributor Covenant Code of Conduct.