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

cd Desktop
mkdir Ingress_Demo
cd Ingress_Demo

Now; My 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(
        metadata=client.V1ObjectMeta(name="my-ingress", annotations={
            "": "/"

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


if __name__ == "__main__":

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

$ python3

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 :

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.