Code Monkey home page Code Monkey logo

pykube's Introduction

pykube

image

image

image

image

image

Python client library for Kubernetes

image

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 [email protected].

pykube's People

Contributors

brosner avatar pcm32 avatar vgarcia-te avatar jayme-github avatar paultiplady avatar davidsiefert avatar ashcrow avatar frewsxcv avatar dave-powell-kensho avatar iliakur avatar adambom avatar sublimino avatar cdrage avatar paralin avatar rajivm avatar reflection avatar shashank-te avatar nebirhos avatar amnk avatar bretthoerner avatar poweld avatar jheiss avatar jonhosmer avatar kbrownlees avatar spladug avatar wbuchwalter avatar yuvipanda avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.