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.
pip install pykube-ngVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your project uses Python 3.6 or newer.
Always initialize `api = pykube.HTTPClient(config)` and pass `api` to object methods (e.g., `Pod.objects(api)`).
Implement explicit retry and reconnection logic around watch loops in your application code.
Wrap `update()` calls in a retry loop (e.g., with exponential backoff) to handle conflicts gracefully.
Ensure 'pykube-ng' is correctly installed and that no conflicting 'pykube' package or local 'pykube.py' file exists. Uninstall the old 'pykube' if present (`pip uninstall pykube`) and make sure 'pykube-ng' is installed (`pip install pykube-ng`). Imports should be 'import pykube' or 'from pykube import KubeConfig'.
For local development, verify that '~/.kube/config' exists and is readable by the Python process. For in-cluster usage, use 'pykube.KubeConfig.from_service_account()' for service account token authentication. Alternatively, 'pykube.KubeConfig.from_env()' attempts both in-cluster and local config loading. If using a custom path, specify it directly: 'pykube.KubeConfig.from_file('/path/to/your/kubeconfig')'.Grant the appropriate RBAC permissions to the user or service account. This involves creating or modifying a Kubernetes Role (for namespace-specific permissions) or ClusterRole (for cluster-wide permissions) and binding it to the user/service account via a RoleBinding or ClusterRoleBinding with the required verbs (e.g., 'get', 'list', 'watch') for the resource.
Ensure the library is installed using `pip install pykube-ng`. Then, use the correct import statement: `import pykube` or `from pykube import KubeConfig, HTTPClient`.