Kubernetes Web-UI with Python-CGI

Kubernetes Web-UI with Python-CGI

What is Kubernetes?

Kubernetes is a free and open source container orchestration technology for automating application deployment, administration, and scalability. Learn how Kubernetes makes cloud-native development more cost-effective.

Kubernetes, often known as "k8s" or "kube," is a container orchestration platform for scheduling and automating containerized application deployment, administration, and scaling.

Kubernetes was created by Google employees before being released as open source in 2014. It's a descendent of Borg, Google's internal container orchestration engine. The helm in the Kubernetes logo is derived from the Greek word kubernetes, which means helmsman or pilot (link resides outside IBM).

Today, Kubernetes and the container ecosystem as a whole are growing into a general-purpose computing platform and ecosystem that competes — if not outperforms — virtual machines (VMs) as the fundamental building blocks of modern cloud infrastructure and applications. This ecosystem enables businesses to provide a high-productivity Platform-as-a-Service (PaaS) that tackles many infrastructure- and operations-related chores and difficulties associated with cloud-native development, allowing development teams to focus only on code and innovation.

So what we are going to do?

We gonna create a Web-UI that will send request to my Virtual Machine for running commands of kubernetes written in common English as per a Web Menu mentioned in the web page. The Web-UI can perform following task:

  • It can launch pods with specific name given by user.
  • Run deployment using image and name given by user.
  • Expose services on given user input port number.
  • Scale the replica according to user need.
  • Delete complete environment created.
  • Delete specific resources given by user.

Step1: Checking Requirements

You need to install kubernetes cluster on your VM and for the installation purpose one can visit the official site. you need to start httpd service in the VM (Redhat in my case), stop firewall as shown below. Screenshot (322).png

Step2: Create a WebUI

We will be using a HTML + CSS + JavaScript to develop our web page. HTML and CSS will focus more on look and feel of the web page that includes input box to enter commands written in English and button to execute them. And below that there is Web menu containing examples and syntax for various tasks. The Web page is as: Screenshot (323).png

Step3: Create Executable Python File

We will create a python file in cgi-bin folder that will interact with Web service and our Linux to run Kubernetes command. The code for the python file is:

#!/usr/bin/python3
import subprocess
import cgi
print("Access-Control-Allow-Origin:*")
print("content-type: text/html")
print()
s=cgi.FieldStorage().getvalue("command")

if "create" in s and "deployment" in s:
    a=s.split(" ")
    b=a[2]
    c=b[5:len(b)]
    d=a[3]
    e=d[6:len(d)]
    output=subprocess.getoutput("sudo kubectl create deployment "+c+ " --image "+ e)

elif "create" in s and "pod" in s:
    a = s.split(" ")
    b=a[2]
    c=b[5:len(b)]
    d=a[3]
    e=d[6:len(d)]
    output=subprocess.getoutput("sudo kubectl run "+c+ " --image "+ e     )

elif "expose" in s and "deployment" in s: 
    a = s.split(" ")
    b=a[2]
    c=b[5:len(b)]
    d=a[3]
    e=d[5:len(d)]
    output=subprocess.getoutput("sudo kubectl expose deployment "+c+ " --port="+e+ " --type=NodePort")

elif "scale" in s and "deployment" in s: 
    a=s.split(" ")
    b=a[2]
    c=b[5:len(b)]
    d=a[3]
    e=d[8:len(d)]
    output=subprocess.getoutput("sudo kubectl scale deployment " +c+ " --replicas="+e)

elif ("delete" in s) and ("pod" in s or "deployment" in s):
    a=s.split(" ")
    if "pod" in s:
        b=a[2]
        c=b[5:len(b)]
        #output=c
        output=subprocess.getoutput("sudo kubectl delete pod "+c)
    elif "deployment" in s: 
        b=a[2]
        c=b[5:len(b)]
        output=subprocess.getoutput("sudo kubectl delete deployment "+c)

elif "destroy" in s and "all" in s:
    output=subprocess.getoutput("sudo kubectl delete all --all")


else:
    output=subprocess.getoutput("sudo " +s)

print("<br><br>")
print("<pre>")
print(output)
print("</pre>")

Now we will make this file executable by following command:

chmod +x <filename.py>

And we are ready to test.

##Step-4: Test Lets launch a Pod using Web UI-

Screenshot (324).png Output is as:

Screenshot (325).png

Now lets launch a Deployment using Web UI-

Screenshot (326).png

Output is as:

Screenshot (327).png

Now lets Delete these resource created:-

Screenshot (328).png

Output is as:

Screenshot (329).png

So we were successful in integrating python-cgi and kubernetes. One can visit my Github Url to refer to the code.