Kubernetes Python has the official kubernetes client library (the typed wrapper around the K8s API), the kubectl CLI (the command-line tool), the kopf framework for writing operators (the Pythonic framework for custom controllers), and the pyhelm library for templating Helm charts in Python. The right answer is the official client for talking to the K8s API, Kopf for writing operators, Helm for templating manifests. The mistake every team makes: the team uses kubectl from a Python script via subprocess.run, the team should use the official client — typed, async-capable, and tested.
Table of contents
- The client library — the typed wrapper around the K8s API
- The resources — Pod, Deployment, Service, ConfigMap
- The custom resources — CRDs and the dynamic client
- The Kopf framework — the Pythonic operator framework
- The kubectl from Python — the wrong answer
- The pyhelm library — the Helm wrapper
- The in-cluster config — the right answer for a pod
- The watch API — the right answer for real-time events
- How this fits the rest of the stack
- FAQ
The client library — the typed wrapper around the K8s API
The official kubernetes client library is a Python wrapper around the K8s API. The library is auto-generated from the OpenAPI spec, the library has typed classes for every resource, the library supports sync and async. The right answer is the official client for any Python code that talks to the K8s API.
from kubernetes import client, config
config.load_kube_config() # or load_incluster_config() inside a pod
v1 = client.CoreV1Api()
pods = v1.list_namespaced_pod(namespace='default')
for pod in pods.items:
print(pod.metadata.name)
The resources — Pod, Deployment, Service, ConfigMap
The K8s resources are typed classes: client.V1Pod, client.V1Deployment, client.V1Service, client.V1ConfigMap. The right answer is to use the typed classes for any code that creates or updates resources, the wrong answer is to build a dict and pass it to the API — the team loses the type safety, the team’s editor does not catch the field-name typo.
The custom resources — CRDs and the dynamic client
Custom Resources (CRDs) are user-defined resources. The right answer to talk to a CRD is the kubernetes.dynamic client (from kubernetes import dynamic). The right answer for a custom operator is the Kopf framework, the right answer for a one-off script is the dynamic client.
The Kopf framework — the Pythonic operator framework
Kopf is a framework for writing Kubernetes operators in Python. The team’s operator is a function that is called when a custom resource changes, the function creates or updates K8s resources, the framework handles the reconciliation loop. The right answer is Kopf for a team that needs to write an operator in Python, the wrong answer is to write the operator in Go and re-implement the reconciliation loop in Python.
The kubectl from Python — the wrong answer
The wrong answer is to use kubectl from Python via subprocess.run. The team parses the YAML output, the team deals with the subprocess errors, the team loses the typed API. The right answer is the official client, the right answer for a one-off script is kubectl from the shell.
The pyhelm library — the Helm wrapper
The pyhelm library is a Python wrapper around Helm. The right answer is pyhelm for a Python tool that needs to render a Helm chart (a deploy tool, a CI script). The wrong answer is to call helm from Python via subprocess.run — the team parses the YAML output, the team loses the typed API.
The in-cluster config — the right answer for a pod
The right answer for a Python script running inside a pod is config.load_incluster_config(). The function reads the service account token and the CA cert from the pod’s filesystem, the client authenticates with the API server. The wrong answer is to use load_kube_config() inside a pod — the function reads the developer’s kubeconfig, which is not in the pod.
The right answer for a Python script running outside a pod (a CI job, a developer’s machine) is config.load_kube_config(). The function reads the kubeconfig from ~/.kube/config (or the path in KUBECONFIG).
The watch API — the right answer for real-time events
The watch API streams events from the K8s API. The right answer is the watch parameter on every list call (v1.list_namespaced_pod(namespace='default', watch=True)). The right answer is the watch API for an operator that needs to react to changes, the right answer for a one-off script is the list call without the watch.
How this fits the rest of the stack
The infrastructure question is a small piece of a larger pattern: the team’s runtime, storage, database, secret store, logs, and deployment platform are all parts of the same platform. The right answer is to model the full stack before the project ships, not after. The RunxBuild hosting calculator is the right place to do that exercise — pick the runtime, the memory tier, the storage, the secret store, and the egress, and the calculator shows what the deploy actually costs at the team’s actual usage.
Useful related references:
FAQ
What is the Kubernetes Python client?
The official kubernetes library — a typed Python wrapper around the K8s API, auto-generated from the OpenAPI spec.
How do I list pods in Python?
Use the CoreV1Api: v1 = client.CoreV1Api(); pods = v1.list_namespaced_pod(namespace='default'). The right answer is the official client, not kubectl from a subprocess.
How do I run a Python script inside a Kubernetes pod?
Build a Docker image with Python, push to a registry, deploy as a Deployment. The right answer is to use config.load_incluster_config() for the K8s client.
How do I write a Kubernetes operator in Python?
Use the Kopf framework. The right answer is Kopf for a team that needs to write an operator in Python.
What is the difference between CoreV1Api and AppsV1Api?
CoreV1Api is for the core resources (Pod, Service, ConfigMap). AppsV1Api is for the apps resources (Deployment, StatefulSet, DaemonSet). The right answer is to use the right API for the resource.
How do I use a custom resource in Python?
Use the dynamic client: from kubernetes import dynamic. The right answer is the dynamic client for a CRD, the typed client for a built-in resource.
What is pyhelm?
A Python wrapper around Helm. The right answer is pyhelm for a Python tool that needs to render a Helm chart.
Should I use kubectl from a Python script?
No — the right answer is the official client. kubectl from a subprocess is the wrong answer for a Python tool, the right answer for a shell script.