How To Create Ingress Using Kubernetes Python Client?

black laptop computer turned on showing computer codes
Reading Time: 3 minutes

Hello Folks… I hope you all are doing well. In this blog we will see how to create Ingress 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. So stay tuned with me.

What Is Ingress ?

Ingress makes available HTTP and HTTPS routes to cluster services from outside the cluster. Rules set on the Ingress resource govern how traffic is routed.

An Ingress can be set up to load balance traffic, terminate SSL/TLS, provide name-based virtual hosting, and provide Services with externally-reachable URLs.

A load balancer is typically used by an ingress controller to fulfil ingress requests, but it may also set up your edge router or additional frontends to assist in handling traffic.

No unrestricted ports or protocols are exposed by an Ingress. A service of type Service is frequently used to expose services other than HTTP and HTTPS to the internet. NodePort or Service as the type.

Type = LoadBalancer.

Installing Python Kubernetes Client :

Before we start creating Ingress using kubernetes python client. We need to follow some prerequisites like:

$ pip install kubernetes

The kubernetes python client is successfully installed now.

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

Creating Ingress :

First I will create a directory name Ingress_Demo and inside this I will make a file named ingress.py

cd Desktop
mkdir Ingress_Demo
cd Ingress_Demo
touch ingress.py

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

Now that we have the k8s package installed, we can import it as:
from kubernetes import client, config

Code for creating Ingress via python client :

from kubernetes import client, config

def create_ingress(networking_v1_api):
    body = client.V1Ingress(
        api_version="networking.k8s.io/v1",
        kind="Ingress",
        metadata=client.V1ObjectMeta(name="my-ingress", annotations={
            "nginx.ingress.kubernetes.io/rewrite-target": "/"
        }),
        spec=client.V1IngressSpec(
            rules=[client.V1IngressRule(
                host="example.com",
                http=client.V1HTTPIngressRuleValue(
                    paths=[client.V1HTTPIngressPath(
                        path="/",
                        path_type="Exact",
                        backend=client.V1IngressBackend(
                            service=client.V1IngressServiceBackend(
                                port=client.V1ServiceBackendPort(
                                    number=5678,
                                ),
                                name="service-example")
                            )
                    )]
                )
            )
            ]
        )
    )
    networking_v1_api.create_namespaced_ingress(
        namespace="default",
        body=body
    )


def main():
    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()
    networking_v1_api = client.NetworkingV1Api()

    create_ingress(networking_v1_api)


if __name__ == "__main__":
    main()

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

$ python3 ingress.py

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

kubectl get ingress

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

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

Conclusion :

So; in this blog we have seen how easily we created ingress using kubernetes python client. I hope this blog will help you. Thank you for sticking to the end.

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.

Discover more from Knoldus Blogs

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

Continue reading