Kubernetes Asynchronous Python Client

35.0.1 · active · verified Sun Mar 29

kubernetes-asyncio is an asynchronous (AsyncIO) client library for interacting with the Kubernetes API. It provides non-blocking access to Kubernetes clusters from Python applications, built using the same OpenAPI generator approach as the official Kubernetes Python client, but with full async/await support. The library is actively maintained, with releases frequently aligning with the Kubernetes project's own approximately three-times-a-year release cadence.

Warnings

Install

Imports

Quickstart

This quickstart example demonstrates how to load Kubernetes configuration, create an API client using an async context manager, and then list all pods across all namespaces in your cluster.

import asyncio
from kubernetes_asyncio import client, config
from kubernetes_asyncio.client.api_client import ApiClient

async def main():
    # Configs can be loaded from default locations (e.g., ~/.kube/config)
    # or explicitly from a file, or in-cluster configuration.
    # No argument provided means it will try default locations.
    await config.load_kube_config()

    # Use the context manager to ensure http sessions are closed automatically
    async with ApiClient() as api:
        v1 = client.CoreV1Api(api)
        print("Listing pods with their IPs:")
        ret = await v1.list_pod_for_all_namespaces()
        for i in ret.items:
            print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →