Pykube-ng

23.6.0 · active · verified Tue Apr 14

Pykube-ng is a lightweight Python 3.6+ client library for Kubernetes, providing a Python-native way to interact with the Kubernetes API. It's a community-maintained fork of the unmaintained `kelproject/pykube` library, focusing on simplicity and direct object interaction. The current stable version is 23.6.0, with releases occurring periodically to maintain compatibility and add features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `pykube-ng` client using environment or local kubeconfig, and then list all Kubernetes pods and retrieve a specific deployment.

import pykube

# KubeConfig.from_env() attempts to load config from in-cluster service account or ~/.kube/config
config = pykube.KubeConfig.from_env()
api = pykube.HTTPClient(config)

# List all pods in all namespaces
pods = pykube.Pod.objects(api, namespace=pykube.all)

print(f"Found {len(list(pods))} pods across all namespaces:")
for pod in pods:
    print(f"  {pod.namespace}/{pod.name} (Status: {pod.obj['status']['phase']})")

# Get a specific deployment (replace 'my-deployment' and 'default' with actual values)
try:
    deployment = pykube.Deployment.objects(api).get(name="my-deployment", namespace="default")
    print(f"\nFound deployment: {deployment.name} in namespace {deployment.namespace}")
except pykube.exceptions.ObjectDoesNotExist:
    print("\nDeployment 'my-deployment' not found in 'default' namespace.")

view raw JSON →