Skip to main content

A Kubernetes API library

Project description

Test Codecov pre-commit.ci status Read the Docs EffVer Versioning PyPI Python Version Support Kubernetes Version Support PyPI - Wheel PyPI - License

A simple, extensible Python client library for Kubernetes that feels familiar for folks who already know how to use kubectl.

Highlights

  • API inspired by kubectl for a shallow learning curve.
  • Sensible defaults to reduce boiler plate.
  • No swagger generated code, human readable code only.
  • Has both a standard and an async API that can be used with asyncio and trio.
  • Client caching to reduce passing API objects around.
  • Batteries included by providing useful utilities and methods inspired by kubectl.

Quickstart

Installation

$ pip install kr8s

Examples

[!TIP] See the Examples Documentation for a full set of examples including asyncio examples.

List Nodes

Print out all of the node names in the cluster.

import kr8s

for node in kr8s.get("nodes"):
    print(node.name)

Create a Pod

Create a new Pod.

from kr8s.objects import Pod

pod = Pod({
        "apiVersion": "v1",
        "kind": "Pod",
        "metadata": {
            "name": "my-pod",
        },
        "spec": {
            "containers": [{"name": "pause", "image": "gcr.io/google_containers/pause",}]
        },
    })

pod.create()

Scale a Deployment

Scale the Deployment metrics-server in the Namespace kube-system to 1 replica.

from kr8s.objects import Deployment

deploy = Deployment.get("metrics-server", namespace="kube-system")
deploy.scale(1)

List Pods by label selector

Get all Pods from all Namespaces matching a label selector.

import kr8s

selector = {'component': 'kube-scheduler'}

for pod in kr8s.get("pods", namespace=kr8s.ALL, label_selector=selector):
    print(pod.namespace, pod.name)

Add a label to a Pod

Add the label foo with the value bar to an existing Pod.

from kr8s.objects import Pod

pod = Pod("kube-apiserver", namespace="kube-system")
pod.label({"foo": "bar"})

Generate a Pod

Generate a simple Pod with a couple of keyword arguments.

from kr8s.objects import Pod

pod = Pod.gen(name="example-1", image="nginx:latest")
pod.create()

Cordon a Node

Cordon a Node to mark it as unschedulable.

from kr8s.objects import Node

node = Node("k8s-node-1")

node.cordon()

Pod Exec

Exec a command in a Pod.

from kr8s.objects import Pod

pod = Pod.get("my-pod")

command = pod.exec(["uptime"])
print(command.stdout.decode())
# 13:49:05 up 23:03,  0 users,  load average: 0.66, 0.87, 0.85

Port forward a Pod

Open a port forward to a Pod as a background task/thread.

from kr8s.objects import Pod

pod = Pod.get("my-pod")
pf = pod.portforward(remote_port=1234, local_port=5678)

# Starts the port forward in a background thread
pf.start()

# Your other code goes here

# Optionally stop the port forward thread (it will exit with Python anyway)
pf.stop()

[!TIP] See the Examples Documentation for a full set of examples including asyncio examples.

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

kr8s-0.18.0.tar.gz (2.9 MB view details)

Uploaded Source

Built Distribution

kr8s-0.18.0-py3-none-any.whl (71.2 kB view details)

Uploaded Python 3

File details

Details for the file kr8s-0.18.0.tar.gz.

File metadata

  • Download URL: kr8s-0.18.0.tar.gz
  • Upload date:
  • Size: 2.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for kr8s-0.18.0.tar.gz
Algorithm Hash digest
SHA256 00a1517bfe3b05d9c245885f08961d17e2fbd816c2e31f5ff910d5f3788393c0
MD5 e6ce8acdcbebd3c52efba39bf99f59d6
BLAKE2b-256 42049f3270f1ce981370b97cd29db187d5d7b5881dd1f4b650458ac2fe581945

See more details on using hashes here.

Provenance

The following attestation bundles were made for kr8s-0.18.0.tar.gz:

Publisher: GitHub
  • Repository: kr8s-org/kr8s
  • Workflow: release.yaml
Attestations:
  • Statement type: https://in-toto.io/Statement/v1
    • Predicate type: https://docs.pypi.org/attestations/publish/v1
    • Subject name: kr8s-0.18.0.tar.gz
    • Subject digest: 00a1517bfe3b05d9c245885f08961d17e2fbd816c2e31f5ff910d5f3788393c0
    • Transparency log index: 146237608
    • Transparency log integration time:

File details

Details for the file kr8s-0.18.0-py3-none-any.whl.

File metadata

  • Download URL: kr8s-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for kr8s-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2edb2496fed4e4501b4e8de66067302ddd5be7e576c6f62aec7289839148e002
MD5 91c78b6737374d46e9072bb06672ff30
BLAKE2b-256 ba07211062c36f9e7c2d2df8fd72c34a6c779b6d2acc1bc41e2db423decfe104

See more details on using hashes here.

Provenance

The following attestation bundles were made for kr8s-0.18.0-py3-none-any.whl:

Publisher: GitHub
  • Repository: kr8s-org/kr8s
  • Workflow: release.yaml
Attestations:
  • Statement type: https://in-toto.io/Statement/v1
    • Predicate type: https://docs.pypi.org/attestations/publish/v1
    • Subject name: kr8s-0.18.0-py3-none-any.whl
    • Subject digest: 2edb2496fed4e4501b4e8de66067302ddd5be7e576c6f62aec7289839148e002
    • Transparency log index: 146237610
    • Transparency log integration time:

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