Creating DaemonSet Using Kubernetes Python Client

Reading Time: 3 minutes

Hello Readers! In this blog we will see how we can create a daemonset using Kubernetes Python Client. As we all know that generally we use kubectl commands for creating, listing, updating, deleting the kubernetes resources. But In this blog we will see how we can use python for doing these things with resources. 

Prerequisites

Before we move forward with creating daemonset using K8s python client we have some prerequisites that we need to follow.

Kubernetes library provides us modules such as client and config which we will use here. So, let’s install Kubernetes Python Client:

$ pip install kubernetes

Now, we have the python-kubernetes package-installed.

So, let’s get started!

Creating the Daemonset:

I will create a directory and inside this I will make a file named create.py. 

Kubernetes Python Client

My create.py file contains the following code for creating a daemonset using Kubernetes Python Client. Now that we have the python-kubernetes package installed, we can import it as:

from kubernetes import client, config

Inside this code I am creating two functions one is create_daemon_set_object( ) and second is create_daemon_set( ). I am creating this daemonset in my default namespace, if you want you can use another also. 

from kubernetes import client, config

def create_daemon_set_object():
    container = client.V1Container(
        name="ds-redis",
        image="redis",
        image_pull_policy="IfNotPresent",
        ports=[client.V1ContainerPort(container_port=6379)],
    )
    # Template
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "redis"}),
        spec=client.V1PodSpec(containers=[container]))
    # Spec
    spec = client.V1DaemonSetSpec(
        selector=client.V1LabelSelector(
            match_labels={"app": "redis"}
        ),
        template=template)
    # DaemonSet
    daemonset = client.V1DaemonSet(
        api_version="apps/v1",
        kind="DaemonSet",
        metadata=client.V1ObjectMeta(name="daemonset-redis"),
        spec=spec)

    return daemonset

def create_daemon_set(apps_v1_api, daemon_set_object):
    # Create the Daemonset in default namespace
    # You can replace the namespace with you have created
    apps_v1_api.create_namespaced_daemon_set(
        namespace="default", body=daemon_set_object
    )
def main():
    # Loading the local kubeconfig
    config. load_kube_config()
    apps_v1_api = client.AppsV1Api()
    core_v1_api = client.CoreV1Api()
    daemon_set_obj = create_daemon_set_object()

    create_daemon_set(apps_v1_api, daemon_set_obj)

if __name__ == "__main__":
    main()

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

$ python3 create.py

As you can see here my daemonset is successfully created.

Kubernetes Python Client

Updating the Daemonset:

For updating this daemonset I will create a file named update.py. Inside this file I will update the container image here. My file contains the following code for updating the daemonset:

from kubernetes import client, config


def create_daemon_set_object():
    container = client.V1Container(
        name="ds-redis",
        image="redis",
        image_pull_policy="IfNotPresent",
        ports=[client.V1ContainerPort(container_port=6379)],
    )
    # Template
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "redis"}),
        spec=client.V1PodSpec(containers=[container]))
    # Spec
    spec = client.V1DaemonSetSpec(
        selector=client.V1LabelSelector(
            match_labels={"app": "redis"}
        ),
        template=template)
    # DaemonSet
    daemonset = client.V1DaemonSet(
        api_version="apps/v1",
        kind="DaemonSet",
        metadata=client.V1ObjectMeta(name="daemonset-redis"),
        spec=spec)

    return daemonset


def create_daemon_set(apps_v1_api, daemon_set_object):
    # Create the Daemonset in default namespace
    # You can replace the namespace with you have created
    apps_v1_api.create_namespaced_daemon_set(
        namespace="default", body=daemon_set_object
    )


def update_daemon_set(apps_v1_api, daemonset):
    # Update container image
    daemonset.spec.template.spec.containers[0].image = "redis:6.2"
    daemonset_name = daemonset.metadata.name
    # Patch the daemonset
    apps_v1_api.patch_namespaced_daemon_set(
        name=daemonset_name, namespace="default", body=daemonset
    )



def main():
    # Loading the local kubeconfig
    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()
    core_v1_api = client.CoreV1Api()
    daemon_set_obj = create_daemon_set_object()

    update_daemon_set(apps_v1_api, daemon_set_obj)


if __name__ == "__main__":
    main()

Here in this code I have used update_daemon_set( ) function for updating this daemonset. Inside this function I am updating the container image. Now, I will run the code for updation:

$ python3 update.py

As you can see here that my container image got updated:

Kubernetes Python Client

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

Conclusion:

Thank you for sticking to the end. In this blog we have seen how easily we can create and update a daemonset using kubernetes python client. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading