Python client library for Kubernetes
Project description
IMPORTANT: This is a fork of kelproject/pykube which is no longer maintained (archived)!
Python client library for Kubernetes
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-ng
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 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("~/.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 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:
$ pipenv install --dev
$ pipenv run python3
>>> import pykube
>>> config = pykube.KubeConfig.from_file('~/.kube/config')
>>> 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, the Kel Project follows the Contributor Covenant Code of Conduct.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pykube_ng-0.17a2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 582ef9d95c1061e2eac6a0ddad0526cfe560d24ea8d6568237f8ee8015d32e9c |
|
MD5 | f07e3d30daa19a6c76af6dcf928345e1 |
|
BLAKE2b-256 | 2d5ab4e5ffdb90e5e84bc3d347ddc51c297f07127b62042d824ee48672020664 |