Skip to main content

Python client library for Kubernetes

Project description

http://slack.kelproject.com/badge.svg https://img.shields.io/travis/kelproject/pykube.svg https://img.shields.io/pypi/dm/pykube.svg https://img.shields.io/pypi/v/pykube.svg https://img.shields.io/badge/license-apache-blue.svg

Python client library for Kubernetes

https://storage.googleapis.com/kel-assets/kel_full-02_200.jpg

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, follow us on Twitter @projectkel, and join our Slack team.

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

Usage

Query for all ready pods in a custom namespace:

import operator
import pykube

api = pykube.HTTPClient(pykube.KubeConfig.from_file("/Users/<username>/.kube/config"))
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 ReplicationController:

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

Delete a ReplicationController:

obj = {
    "apiVersion": "v1",
    "kind": "ReplicationController",
    "metadata": {
        "name": "my-rc",
        "namespace": "gondor-system"
    }
}
pykube.ReplicationController(api, obj).delete()

Check server version:

api = pykube.HTTPClient(pykube.KubeConfig.from_file("/Users/<username>/.kube/config"))
api.version

HTTPie

pykube can be used together with HTTPie for Kubernetes command line querying goodness. For example:

pip install httpie
http pykube://minikube/api/v1/services

The above example will construct an HTTP request to the cluster behind the minikube context and show you the response containing all services.

Requirements

  • Python 2.7 or 3.3+

  • requests (included in install_requires)

  • PyYAML (included in install_requires)

License

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

Contributing

By making a contribution to this project, you are agreeing to the Developer Certificate of Origin v1.1 (also included in this repository under DCO.txt).

Code of Conduct

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

Commercial Support

Commercial support for Kel is available through Eldarion, please contact info@eldarion.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykube-0.16a1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

pykube-0.16a1-py2.py3-none-any.whl (26.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pykube-0.16a1.tar.gz.

File metadata

  • Download URL: pykube-0.16a1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pykube-0.16a1.tar.gz
Algorithm Hash digest
SHA256 f18903e4d4f195fbb01f344765b0db9654ae418a3c9b4aa109fd89e7a40f60b3
MD5 a383825d2c54f382b5a505c04d7f2454
BLAKE2b-256 9b7c7a599f3fddeb322620d4fad63eb85549210ecac3462054f4801b982f947a

See more details on using hashes here.

File details

Details for the file pykube-0.16a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pykube-0.16a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 47bff36fe6c84ddf85801815917747d90047fa2ff034482a75322fe19d285587
MD5 0a22ba89f3d84995b4bb5d1af0b7722e
BLAKE2b-256 892e78e251b4c1675548fc9d6b96f626aaec9a9a8ffdab5efd7a5bce1ca3cd38

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page