How To Create Pod Using Kubernetes Python Client?

Reading Time: 3 minutes

What Is POD ?

Pod is a atomic unit of scheduling in kubernetes. Pods are the smallest deployable units of computing that you can create and manage in kubernetes. Pods are encapsulate the containers within it and it represents your application.

A Pod always runs on a Node. A node is a worker machine in kubernetes and may be either a virtual or physical machine, depending on a cluster.

And also pod is a kubernetes abstraction that represents a group of one or more application containers and some shared resources for those containers.

Installing Python Kubernetes Client :

Before we start creating POD using k8s client. We need to follow some prerequisites like:

$ pip install kubernetes
installation

The kubernetes python client is successfully installed now.

So; now let’s see how we can create a pod via kubernetes client.

Creating POD :

First I will create a directory name POD and inside this I will make a file named pod.py

cd Desktop  
mkdir POD
cd POD 
touch pod.py
creating pod

After that you need to start your minikube

minikube start
start minikube

Now that we have the k8s package installed, we can import it as:

from kubernetes import client, config

Now; My pod.py file contains the following code for creating a Pod using k8s Python Client.

Code for creating POD via kubernetes python client :

from kubernetes import client, config

def create_pod(data , string_data , client_api):
    secret = client.V1Secret(
        api_version="v1",
        kind="Pod",
        metadata=client.V1ObjectMeta(name="my-pod"),
        data=data , 
        string_data=string_data
    )

    api = client_api.create_namespaced_pod(namespace="default", body=secret)
    return api

api_server_endpoint = "YOUR API"
bearer_token = "YOUR BEARER TOKEN"


configuration = client.Configuration()
configuration.host = api_server_endpoint
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + bearer_token}
client.Configuration.set_default(configuration)
client_api = client.CoreV1Api()

cm={
    "name": "mayuri"
}
create_pod({} , cm , client_api)

Now, It’s time to create the pod. So, I will now run the python code.

$ python3 pod.py
running file

Now; if i check my pod is created or not so i will simply run the command :

kubectl get pods

As you can see here my pod named –> my-pod is successfully created.

get pod

So, We are successfully done now. This is how we can play with the K8s python client.

Conclusion :

So; in this blog we have seen how easily we created pod using k8s python client. I hope this blog will help you. Thank you for sticking to the end. If you liked my blog please do share.

Happy Learning..!!!

Reference :

https://kubernetes.io/docs/home/

Written by 

Mayuri Dhote is a Software Consultant at Knoldus Software. She has completed her MCA from VIT University. She has very dedicated towards her work. She has always ready to learn new things. Her practice area is Devops. When not working, you will find her writing poems and poetry.