Some of these operations include;
create_xxxx
: create a resource object. Ex create_namespaced_pod
and create_namespaced_deployment
, for creation of pods and deployments respectively. This performs operations similar to kubectl create
.read_xxxx
: read the specified resource object. Ex read_namespaced_pod
and read_namespaced_deployment
, to read pods and deployments respectively. This performs operations similar to kubectl describe
.list_xxxx
: retrieve all resource objects of a specific type. Ex list_namespaced_pod
and list_namespaced_deployment
, to list pods and deployments respectively. This performs operations similar to kubectl get
.patch_xxxx
: apply a change to a specific field. Ex patch_namespaced_pod
and patch_namespaced_deployment
, to update pods and deployments respectively. This performs operations similar to kubectl patch
, kubectl label
, kubectl annotate
etc.replace_xxxx
: replacing a resource object will update the resource by replacing the existing spec with the provided one. Ex replace_namespaced_pod
and replace_namespaced_deployment
, to update pods and deployments respectively, by creating new replacements of the entire object. This performs operations similar to kubectl rolling-update
, kubectl apply
and kubectl replace
.delete_xxxx
: delete a resource. This performs operations similar to kubectl delete
.For Futher information see the Documentation for API Endpoints section in https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/README.md
In [ ]:
from kubernetes import client, config
In [ ]:
config.load_kube_config()
In [ ]:
api_instance = client.ExtensionsV1beta1Api()
dep = client.ExtensionsV1beta1Deployment()
spec = client.ExtensionsV1beta1DeploymentSpec()
In [ ]:
name = "my-busybox"
dep.metadata = client.V1ObjectMeta(name=name)
spec.template = client.V1PodTemplateSpec()
spec.template.metadata = client.V1ObjectMeta(name="busybox")
spec.template.metadata.labels = {"app":"busybox"}
spec.template.spec = client.V1PodSpec()
dep.spec = spec
container = client.V1Container()
container.image = "busybox:1.26.1"
container.args = ["sleep", "3600"]
container.name = name
spec.template.spec.containers = [container]
In [ ]:
api_instance.create_namespaced_deployment(namespace="default",body=dep)
In [ ]:
deps = api_instance.list_namespaced_deployment(namespace="default")
for item in deps.items:
print("%s %s" % (item.metadata.namespace, item.metadata.name))
In [ ]:
api_instance.read_namespaced_deployment(namespace="default",name=name)
In [ ]:
dep.metadata.labels = {"key": "value"}
api_instance.patch_namespaced_deployment(name=name, namespace="default", body=dep)
In [ ]:
dep.spec.template.spec.containers[0].image = "busybox:1.26.2"
api_instance.replace_namespaced_deployment(name=name, namespace="default", body=dep)
In [ ]:
api_instance.delete_namespaced_deployment(name=name, namespace="default", body=client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5))
In [ ]: