Python client library for interacting with Kubernetes clusters through the Kubernetes API
—
Management of fundamental Kubernetes resources including pods, services, namespaces, config maps, secrets, and persistent volumes. These resources form the basic building blocks for all Kubernetes applications and provide essential cluster functionality.
Create, read, update, and delete pods - the smallest deployable units in Kubernetes that contain one or more containers.
class CoreV1Api:
def create_namespaced_pod(
self,
namespace: str,
body: V1Pod,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Pod:
"""Create a pod in specified namespace."""
def list_namespaced_pod(
self,
namespace: str,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1PodList:
"""List pods in specified namespace."""
def read_namespaced_pod(
self,
name: str,
namespace: str,
pretty: str = None
) -> V1Pod:
"""Read specified pod."""
def patch_namespaced_pod(
self,
name: str,
namespace: str,
body: object,
dry_run: str = None,
field_manager: str = None,
force: bool = None,
pretty: str = None
) -> V1Pod:
"""Patch specified pod."""
def delete_namespaced_pod(
self,
name: str,
namespace: str,
body: V1DeleteOptions = None,
dry_run: str = None,
grace_period_seconds: int = None,
orphan_dependents: bool = None,
propagation_policy: str = None,
pretty: str = None
) -> V1Status:
"""Delete specified pod."""
def create_namespaced_pod_eviction(
self,
name: str,
namespace: str,
body: V1Eviction,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Eviction:
"""Evict a pod using the eviction API."""
def create_namespaced_pod_binding(
self,
name: str,
namespace: str,
body: V1Binding,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Binding:
"""Bind a pod to a node."""
def connect_get_namespaced_pod_proxy(
self,
name: str,
namespace: str,
path: str = None,
**kwargs
) -> str:
"""Connect to proxy of specified pod."""Manage services for exposing applications running on pods to network traffic.
class CoreV1Api:
def create_namespaced_service(
self,
namespace: str,
body: V1Service,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Service:
"""Create a service in specified namespace."""
def list_namespaced_service(
self,
namespace: str,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1ServiceList:
"""List services in specified namespace."""
def read_namespaced_service(
self,
name: str,
namespace: str,
pretty: str = None
) -> V1Service:
"""Read specified service."""Create and manage namespaces for organizing and isolating cluster resources.
class CoreV1Api:
def create_namespace(
self,
body: V1Namespace,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Namespace:
"""Create a namespace."""
def list_namespace(
self,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1NamespaceList:
"""List namespaces."""
def delete_namespace(
self,
name: str,
body: V1DeleteOptions = None,
dry_run: str = None,
grace_period_seconds: int = None,
orphan_dependents: bool = None,
propagation_policy: str = None,
pretty: str = None
) -> V1Status:
"""Delete specified namespace."""Manage configuration data stored as key-value pairs that can be consumed by pods.
class CoreV1Api:
def create_namespaced_config_map(
self,
namespace: str,
body: V1ConfigMap,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1ConfigMap:
"""Create a config map in specified namespace."""
def list_namespaced_config_map(
self,
namespace: str,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1ConfigMapList:
"""List config maps in specified namespace."""Manage sensitive data like passwords, tokens, and keys stored securely in the cluster.
class CoreV1Api:
def create_namespaced_secret(
self,
namespace: str,
body: V1Secret,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1Secret:
"""Create a secret in specified namespace."""
def list_namespaced_secret(
self,
namespace: str,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1SecretList:
"""List secrets in specified namespace."""Manage storage resources including persistent volumes and persistent volume claims.
class CoreV1Api:
def create_persistent_volume(
self,
body: V1PersistentVolume,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1PersistentVolume:
"""Create a persistent volume."""
def create_namespaced_persistent_volume_claim(
self,
namespace: str,
body: V1PersistentVolumeClaim,
dry_run: str = None,
field_manager: str = None,
pretty: str = None
) -> V1PersistentVolumeClaim:
"""Create a persistent volume claim in specified namespace."""
def list_persistent_volume(
self,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1PersistentVolumeList:
"""List persistent volumes."""Read and monitor cluster nodes and their status.
class CoreV1Api:
def list_node(
self,
pretty: str = None,
allow_watch_bookmarks: bool = None,
continue_: str = None,
field_selector: str = None,
label_selector: str = None,
limit: int = None,
resource_version: str = None,
timeout_seconds: int = None,
watch: bool = None
) -> V1NodeList:
"""List cluster nodes."""
def read_node(
self,
name: str,
pretty: str = None
) -> V1Node:
"""Read specified node."""class V1Pod:
api_version: str # "v1"
kind: str # "Pod"
metadata: V1ObjectMeta
spec: V1PodSpec
status: V1PodStatusclass V1Service:
api_version: str # "v1"
kind: str # "Service"
metadata: V1ObjectMeta
spec: V1ServiceSpec
status: V1ServiceStatusclass V1Namespace:
api_version: str # "v1"
kind: str # "Namespace"
metadata: V1ObjectMeta
spec: V1NamespaceSpec
status: V1NamespaceStatusclass V1ConfigMap:
api_version: str # "v1"
kind: str # "ConfigMap"
metadata: V1ObjectMeta
binary_data: dict # Binary data
data: dict # String data
immutable: boolclass V1Secret:
api_version: str # "v1"
kind: str # "Secret"
metadata: V1ObjectMeta
data: dict # Base64 encoded data
string_data: dict # Plain text data
type: str # Secret type (Opaque, kubernetes.io/service-account-token, etc.)
immutable: boolclass V1PersistentVolume:
api_version: str # "v1"
kind: str # "PersistentVolume"
metadata: V1ObjectMeta
spec: V1PersistentVolumeSpec
status: V1PersistentVolumeStatusclass V1Eviction:
api_version: str # "policy/v1"
kind: str # "Eviction"
metadata: V1ObjectMeta
delete_options: V1DeleteOptions
### V1Binding
```python { .api }
class V1Binding:
api_version: str # "v1"
kind: str # "Binding"
metadata: V1ObjectMeta
target: V1ObjectReference
### V1DeleteOptions
```python { .api }
class V1DeleteOptions:
api_version: str
kind: str
dry_run: list[str]
grace_period_seconds: int
orphan_dependents: bool
preconditions: V1Preconditions
propagation_policy: strclass V1PersistentVolumeClaim:
api_version: str # "v1"
kind: str # "PersistentVolumeClaim"
metadata: V1ObjectMeta
spec: V1PersistentVolumeClaimSpec
status: V1PersistentVolumeClaimStatusfrom kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
pod_manifest = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "nginx-pod",
"labels": {"app": "nginx"}
},
"spec": {
"containers": [{
"name": "nginx",
"image": "nginx:1.20",
"ports": [{"containerPort": 80}],
"env": [{"name": "ENV_VAR", "value": "test"}]
}]
}
}
# Create the pod
pod = v1.create_namespaced_pod(namespace="default", body=pod_manifest)
print(f"Pod created: {pod.metadata.name}")from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": "nginx-service"},
"spec": {
"selector": {"app": "nginx"},
"ports": [{
"port": 80,
"targetPort": 80,
"protocol": "TCP"
}],
"type": "ClusterIP"
}
}
# Create service
service = v1.create_namespaced_service(namespace="default", body=service_manifest)
print(f"Service created: {service.metadata.name}")
# List services
services = v1.list_namespaced_service(namespace="default")
for svc in services.items:
print(f"Service: {svc.metadata.name}, ClusterIP: {svc.spec.cluster_ip}")from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
configmap_manifest = {
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {"name": "app-config"},
"data": {
"database_url": "postgresql://localhost:5432/mydb",
"debug": "true",
"max_connections": "100"
}
}
# Create ConfigMap
cm = v1.create_namespaced_config_map(namespace="default", body=configmap_manifest)
print(f"ConfigMap created: {cm.metadata.name}")Install with Tessl CLI
npx tessl i tessl/pypi-kubernetes