Skip to content

ruby-rhino/k8s-python-client-101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

k8s-python-client-101

Install: pip3 install kubernetes Import: from kubernetes import client, config Auth: config.load_kube_config() (local) · config.load_incluster_config() (in-pod)


API group reference

Client API group Typical use
CoreV1Api() v1 Pods, Services, Secrets, Nodes
AppsV1Api() apps/v1 Deployments, StatefulSets, DaemonSets
BatchV1Api() batch/v1 Jobs, CronJobs
NetworkingV1Api() networking.k8s.io/v1 Ingress, NetworkPolicy
RbacAuthorizationV1Api() rbac.authorization.k8s.io/v1 Roles, ClusterRoles
AutoscalingV2Api() autoscaling/v2 HPA
StorageV1Api() storage.k8s.io/v1 StorageClass, CSI
PolicyV1Api() policy/v1 PodDisruptionBudget
AdmissionregistrationV1Api() admissionregistration.k8s.io/v1 Webhooks
CoordinationV1Api() coordination.k8s.io/v1 Lease (leader election)

Verb legend

Tag HTTP Description
list GET (collection) List all resources, optionally filtered
read GET (single) Get one resource by name
create POST Create a new resource
patch PATCH Partial update (strategic merge or JSON merge)
replace PUT Full update — you own the entire object
delete DELETE Delete one resource
delete_collection DELETE Bulk delete matching resources

Naming pattern

{verb}_[namespaced_]{resource}[_for_all_namespaces][_status]

Examples:

  • list_namespaced_deployment — deployments in one namespace
  • list_deployment_for_all_namespaces — cluster-wide
  • read_namespaced_deployment_status — only the .status subresource
  • patch_namespaced_deployment — partial update

AppsV1Api() | workloads

Init: apps_v1 = client.AppsV1Api() Resources: Deployment · StatefulSet · DaemonSet · ReplicaSet · ControllerRevision

Deployment

Method Verb
list_deployment_for_all_namespaces() list
list_namespaced_deployment(namespace) list
read_namespaced_deployment(name, namespace) read
read_namespaced_deployment_status(name, namespace) read
create_namespaced_deployment(namespace, body) create
patch_namespaced_deployment(name, namespace, body) patch
replace_namespaced_deployment(name, namespace, body) replace
delete_namespaced_deployment(name, namespace) delete
delete_collection_namespaced_deployment(namespace) delete

StatefulSet

Method Verb
list_stateful_set_for_all_namespaces() list
list_namespaced_stateful_set(namespace) list
read_namespaced_stateful_set(name, namespace) read
create_namespaced_stateful_set(namespace, body) create
patch_namespaced_stateful_set(name, namespace, body) patch
delete_namespaced_stateful_set(name, namespace) delete

DaemonSet

Method Verb
list_daemon_set_for_all_namespaces() list
list_namespaced_daemon_set(namespace) list
read_namespaced_daemon_set(name, namespace) read
create_namespaced_daemon_set(namespace, body) create
patch_namespaced_daemon_set(name, namespace, body) patch
delete_namespaced_daemon_set(name, namespace) delete

ReplicaSet

Method Verb
list_replica_set_for_all_namespaces() list
list_namespaced_replica_set(namespace) list
read_namespaced_replica_set(name, namespace) read
patch_namespaced_replica_set(name, namespace, body) patch
delete_namespaced_replica_set(name, namespace) delete

ControllerRevision

Method Verb
list_namespaced_controller_revision(namespace) list
read_namespaced_controller_revision(name, namespace) read

CoreV1Api() | core primitives

Init: core_v1 = client.CoreV1Api() Resources: Pod · Service · ConfigMap · Secret · Node · Namespace · PersistentVolume · PersistentVolumeClaim · ServiceAccount · Endpoints · Event

Pod

Method Verb
list_pod_for_all_namespaces() list
list_namespaced_pod(namespace) list
read_namespaced_pod(name, namespace) read
read_namespaced_pod_log(name, namespace) read
read_namespaced_pod_status(name, namespace) read
create_namespaced_pod(namespace, body) create
patch_namespaced_pod(name, namespace, body) patch
delete_namespaced_pod(name, namespace) delete
connect_get_namespaced_pod_exec(name, namespace, ...) exec

Service

Method Verb
list_namespaced_service(namespace) list
read_namespaced_service(name, namespace) read
create_namespaced_service(namespace, body) create
patch_namespaced_service(name, namespace, body) patch
delete_namespaced_service(name, namespace) delete

ConfigMap

Method Verb
list_namespaced_config_map(namespace) list
read_namespaced_config_map(name, namespace) read
create_namespaced_config_map(namespace, body) create
patch_namespaced_config_map(name, namespace, body) patch
replace_namespaced_config_map(name, namespace, body) replace
delete_namespaced_config_map(name, namespace) delete

Secret

Method Verb
list_namespaced_secret(namespace) list
read_namespaced_secret(name, namespace) read
create_namespaced_secret(namespace, body) create
patch_namespaced_secret(name, namespace, body) patch
delete_namespaced_secret(name, namespace) delete

Node

Method Verb
list_node() list
read_node(name) read
read_node_status(name) read
patch_node(name, body) patch

Namespace

Method Verb
list_namespace() list
read_namespace(name) read
create_namespace(body) create
delete_namespace(name) delete

PersistentVolume

Method Verb
list_persistent_volume() list
read_persistent_volume(name) read
create_persistent_volume(body) create
delete_persistent_volume(name) delete

PersistentVolumeClaim

Method Verb
list_namespaced_persistent_volume_claim(namespace) list
read_namespaced_persistent_volume_claim(name, namespace) read
create_namespaced_persistent_volume_claim(namespace, body) create
delete_namespaced_persistent_volume_claim(name, namespace) delete

ServiceAccount

Method Verb
list_namespaced_service_account(namespace) list
read_namespaced_service_account(name, namespace) read
create_namespaced_service_account(namespace, body) create
delete_namespaced_service_account(name, namespace) delete

Event

Method Verb
list_namespaced_event(namespace) list
read_namespaced_event(name, namespace) read

BatchV1Api() | jobs

Init: batch_v1 = client.BatchV1Api() Resources: Job · CronJob

Job

Method Verb
list_job_for_all_namespaces() list
list_namespaced_job(namespace) list
read_namespaced_job(name, namespace) read
read_namespaced_job_status(name, namespace) read
create_namespaced_job(namespace, body) create
patch_namespaced_job(name, namespace, body) patch
delete_namespaced_job(name, namespace) delete
delete_collection_namespaced_job(namespace) delete

CronJob

Method Verb
list_cron_job_for_all_namespaces() list
list_namespaced_cron_job(namespace) list
read_namespaced_cron_job(name, namespace) read
create_namespaced_cron_job(namespace, body) create
patch_namespaced_cron_job(name, namespace, body) patch
delete_namespaced_cron_job(name, namespace) delete

NetworkingV1Api() | networking

Init: net_v1 = client.NetworkingV1Api() Resources: Ingress · NetworkPolicy · IngressClass

Ingress

Method Verb
list_ingress_for_all_namespaces() list
list_namespaced_ingress(namespace) list
read_namespaced_ingress(name, namespace) read
create_namespaced_ingress(namespace, body) create
patch_namespaced_ingress(name, namespace, body) patch
delete_namespaced_ingress(name, namespace) delete

NetworkPolicy

Method Verb
list_namespaced_network_policy(namespace) list
read_namespaced_network_policy(name, namespace) read
create_namespaced_network_policy(namespace, body) create
patch_namespaced_network_policy(name, namespace, body) patch
delete_namespaced_network_policy(name, namespace) delete

IngressClass

Method Verb
list_ingress_class() list
read_ingress_class(name) read
create_ingress_class(body) create
delete_ingress_class(name) delete

RbacAuthorizationV1Api() | RBAC

Init: rbac_v1 = client.RbacAuthorizationV1Api() Resources: Role · RoleBinding · ClusterRole · ClusterRoleBinding

Role

Method Verb
list_namespaced_role(namespace) list
read_namespaced_role(name, namespace) read
create_namespaced_role(namespace, body) create
patch_namespaced_role(name, namespace, body) patch
delete_namespaced_role(name, namespace) delete

RoleBinding

Method Verb
list_namespaced_role_binding(namespace) list
read_namespaced_role_binding(name, namespace) read
create_namespaced_role_binding(namespace, body) create
patch_namespaced_role_binding(name, namespace, body) patch
delete_namespaced_role_binding(name, namespace) delete

ClusterRole

Method Verb
list_cluster_role() list
read_cluster_role(name) read
create_cluster_role(body) create
patch_cluster_role(name, body) patch
delete_cluster_role(name) delete

ClusterRoleBinding

Method Verb
list_cluster_role_binding() list
read_cluster_role_binding(name) read
create_cluster_role_binding(body) create
patch_cluster_role_binding(name, body) patch
delete_cluster_role_binding(name) delete

AutoscalingV2Api() | autoscaling

Init: hpa_v2 = client.AutoscalingV2Api() Resources: HorizontalPodAutoscaler

Method Verb
list_horizontal_pod_autoscaler_for_all_namespaces() list
list_namespaced_horizontal_pod_autoscaler(namespace) list
read_namespaced_horizontal_pod_autoscaler(name, namespace) read
read_namespaced_horizontal_pod_autoscaler_status(name, namespace) read
create_namespaced_horizontal_pod_autoscaler(namespace, body) create
patch_namespaced_horizontal_pod_autoscaler(name, namespace, body) patch
replace_namespaced_horizontal_pod_autoscaler(name, namespace, body) replace
delete_namespaced_horizontal_pod_autoscaler(name, namespace) delete

StorageV1Api() | storage

Init: storage_v1 = client.StorageV1Api() Resources: StorageClass · VolumeAttachment · CSIDriver · CSINode

StorageClass

Method Verb
list_storage_class() list
read_storage_class(name) read
create_storage_class(body) create
patch_storage_class(name, body) patch
delete_storage_class(name) delete

VolumeAttachment

Method Verb
list_volume_attachment() list
read_volume_attachment(name) read
create_volume_attachment(body) create
delete_volume_attachment(name) delete

CSIDriver / CSINode

Method Verb
list_csi_driver() list
read_csi_driver(name) read
create_csi_driver(body) create
delete_csi_driver(name) delete
list_csi_node() list
read_csi_node(name) read

PolicyV1Api() | policy

Init: policy_v1 = client.PolicyV1Api() Resources: PodDisruptionBudget

Method Verb
list_namespaced_pod_disruption_budget(namespace) list
read_namespaced_pod_disruption_budget(name, namespace) read
read_namespaced_pod_disruption_budget_status(name, namespace) read
create_namespaced_pod_disruption_budget(namespace, body) create
patch_namespaced_pod_disruption_budget(name, namespace, body) patch
delete_namespaced_pod_disruption_budget(name, namespace) delete
delete_collection_namespaced_pod_disruption_budget(namespace) delete

AdmissionregistrationV1Api() | webhooks

Init: admiss_v1 = client.AdmissionregistrationV1Api() Resources: MutatingWebhookConfiguration · ValidatingWebhookConfiguration

MutatingWebhookConfiguration

Method Verb
list_mutating_webhook_configuration() list
read_mutating_webhook_configuration(name) read
create_mutating_webhook_configuration(body) create
patch_mutating_webhook_configuration(name, body) patch
delete_mutating_webhook_configuration(name) delete

ValidatingWebhookConfiguration

Method Verb
list_validating_webhook_configuration() list
read_validating_webhook_configuration(name) read
create_validating_webhook_configuration(body) create
patch_validating_webhook_configuration(name, body) patch
delete_validating_webhook_configuration(name) delete

CoordinationV1Api() | leader election

Init: coord_v1 = client.CoordinationV1Api() Resources: Lease

Method Verb
list_namespaced_lease(namespace) list
read_namespaced_lease(name, namespace) read
create_namespaced_lease(namespace, body) create
patch_namespaced_lease(name, namespace, body) patch
replace_namespaced_lease(name, namespace, body) replace
delete_namespaced_lease(name, namespace) delete
delete_collection_namespaced_lease(namespace) delete

Common kwargs

These keyword arguments work across most list/read/patch/delete methods:

Kwarg Type Description
namespace str Target namespace
label_selector str e.g. "app=my-app,env=prod"
field_selector str e.g. "status.phase=Running"
watch bool Stream events (use with watch.Watch())
timeout_seconds int Request timeout
pretty str "true" for human-readable output
dry_run str "All" to simulate without applying

Quick Examples

from kubernetes import client, config

config.load_kube_config()          # uses ~/.kube/config
# config.load_kube_config(context="my-cluster")  # specific context

apps_v1 = client.AppsV1Api()
core_v1 = client.CoreV1Api()

# list all deployments cluster wide
deploys = apps_v1.list_deployment_for_all_namespaces(watch=False).items

# list pods with label selector
pods = core_v1.list_namespaced_pod(
    namespace="default",
    label_selector="app=my-app"
).items

# read pod logs
log = core_v1.read_namespaced_pod_log(
    name="my-pod-abc123", namespace="default",
    container="app", tail_lines=100
)

# watch events
from kubernetes import watch
w = watch.Watch()
for event in w.stream(core_v1.list_namespaced_pod, namespace="default", timeout_seconds=60):
    print(event["type"], event["object"].metadata.name)

About

Kubernetes Python Client API Cheatsheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors