Kubernetes GUI using Python CGI

Kubernetes GUI using Python CGI

Kubernetes, or K8s, is a container orchestration system. In other words, when you use Kubernetes, a container based application can be deployed, scaled, and managed automatically.

The objective of Kubernetes is to abstract away the complexity of managing a fleet of containers that represent packaged applications and include everything needed to run wherever they’re provisioned. By interacting with the Kubernetes REST API, you can describe the desired state of your application, and Kubernetes does whatever is necessary to make the infrastructure conform. It deploys groups of containers, replicates them, redeploys if some of them fail, and so on.

Today we are going to build kubernetes GUI through which we can run K8s commands on a local machine while writing them 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.

Creating 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.

dHA5jGj4K.jfif

Creating Python CGI File:

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

#!/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>")

To make this python file executable by following command:

chmod +x <filename.py>

Let's test our GUI:

launching a Pod:

jDOwLySyA.jfif

Output:

PiTOSG752.jfif

launching a Deployment:

34TsKzOcs.jfif

Output:

CrQgJQVCmW.jfif

Delete all resources we have created:

AQZOCs1UW.jfif

Output:

BUD5s62Y6.jfif

Here we have successfully created Kubernetes GUI using Python CGI.

Thank You.