Managing kubernetes objects using common resource operations with the python client

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

Load config from default location.


In [ ]:
config.load_kube_config()

Create API endpoint instance as well as API resource instances (body and specification).


In [ ]:
api_instance = client.ExtensionsV1beta1Api()
dep = client.ExtensionsV1beta1Deployment()
spec = client.ExtensionsV1beta1DeploymentSpec()

Fill required object fields (apiVersion, kind, metadata and spec).


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]

Create Deployment using create_xxxx command for Deployments.


In [ ]:
api_instance.create_namespaced_deployment(namespace="default",body=dep)

Use list_xxxx command for Deployment, to list Deployments.


In [ ]:
deps = api_instance.list_namespaced_deployment(namespace="default")
for item in deps.items:
    print("%s  %s" % (item.metadata.namespace, item.metadata.name))

Use read_xxxx command for Deployment, to display the detailed state of the created Deployment resource.


In [ ]:
api_instance.read_namespaced_deployment(namespace="default",name=name)

Use patch_xxxx command for Deployment, to make specific update to the Deployment.


In [ ]:
dep.metadata.labels = {"key": "value"}
api_instance.patch_namespaced_deployment(name=name, namespace="default", body=dep)

Use replace_xxxx command for Deployment, to update Deployment with a completely new version of the object.


In [ ]:
dep.spec.template.spec.containers[0].image = "busybox:1.26.2"
api_instance.replace_namespaced_deployment(name=name, namespace="default", body=dep)

Use delete_xxxx command for Deployment, to delete created Deployment.


In [ ]:
api_instance.delete_namespaced_deployment(name=name, namespace="default", body=client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5))

In [ ]: