In [ ]:
from kubernetes import client, config
If you are using a proxy, you can use the client Configuration to setup the host that the client should use. Otherwise read the kubeconfig file.
In [ ]:
config.load_incluster_config()
Pods are a stable resource in the V1 API group. Instantiate a client for that API group endpoint.
In [ ]:
v1=client.CoreV1Api()
In [ ]:
pod=client.V1Pod()
spec=client.V1PodSpec()
pod.metadata=client.V1ObjectMeta(name="busybox")
In this example, we only start one container in the Pod. The container is an instnace of the V1Container class.
In [ ]:
container=client.V1Container()
container.image="busybox"
container.args=["sleep", "3600"]
container.name="busybox"
The specification of the Pod is made of a single container in its list.
In [ ]:
spec.containers = [container]
pod.spec = spec
Get existing list of Pods, before the creation of the new Pod.
In [ ]:
ret = v1.list_namespaced_pod(namespace="default")
for i in ret.items:
print("%s %s %s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
You are now ready to create the Pod.
In [ ]:
v1.create_namespaced_pod(namespace="default",body=pod)
Get list of Pods, after the creation of the new Pod. Note the newly created pod with name "busybox"
In [ ]:
ret = v1.list_namespaced_pod(namespace="default")
for i in ret.items:
print("%s %s %s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
In [ ]:
v1.delete_namespaced_pod(name="busybox", namespace="default", body=client.V1DeleteOptions())
In [ ]: