Skip to main content
Build status Code Coverage Documentation PyPI version Python versions Apache License CalVer

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 kelproject.com or follow us on Twitter @projectkel.

Features

  • HTTP interface using requests using kubeconfig for authentication

  • Python native querying of Kubernetes API objects

Installation

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
>>> [d.name for d in Deployment.objects(api)]

Usage

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")
pod.obj["spec"]["containers"][0]["image"]

Selector query:

pods = pykube.Pod.objects(api).filter(
    namespace="gondor-system",
    selector={"gondor.io/name__in": {"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={"metadata.name": "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())
api.version

Requirements

  • 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

License

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

Contributing

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.

Metadata

Release files for pykube-ng 23.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pykube-ng 23.6.0
File Size Uploaded
pykube-ng-23.6.0.tar.gz 26.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pykube-ng 23.6.0
File Interpreter ABI Platform
pykube_ng-23.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.2 kB

Release files / pykube-ng-23.6.0.tar.gz

Download URL pykube-ng-23.6.0.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
46de8e17ed87c1a1014667d60e7d94a1f3fa2b8037b41e67d32c28b5869af35d
BLAKE2b-256 checksum
How to use checksums
10eec78cd9b1a574b63ce365b6a6f066082dbbc8aee93aacc83d7bc32aed7919
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-73-generic

Release files / pykube_ng-23.6.0-py3-none-any.whl

Download URL pykube_ng-23.6.0-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63f20f634bfcd83966edec32f892286f75dffb817a2c097434ecc039e558ec8f
BLAKE2b-256 checksum
How to use checksums
5bb94292259e80f93163d9ba0c69ccb3d44cf240acbdcb18adbc81a8f8d2e3d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-73-generic
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page