My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Kubernetes Web-UI with Python-CGI

Kubernetes Web-UI with Python-CGI

Rachit Sharma's photo
Rachit Sharma
·Jun 28, 2021·

3 min read

What is Kubernetes?

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.

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:

  1. It can launch pods with specific name given by user.

  2. Run deployment using image and name given by user.

  3. Expose services on given user input port number.

  4. Scale the replica according to user need.

  5. Delete complete environment created.

  6. Delete specific resources given by user.

Step1: Checking Requirements

Install kubernetes cluster on your VM and start httpd service in the VM, stop firewall as shown below.

Screenshot 2021-06-28 at 08-44-08 Kubernetes Web-UI with Python-CGI.png

Step2: Create a WebUI

To develop front-end of our web page, we will be using HTML, CSS and Javascript. HTML (the Hypertext Markup Language) and CSS (Cascading Style Sheets) are two of the core technologies for building Web pages. HTML provides the structure of the page, CSS the (visual and aural) layout, for a variety of devices. Along with graphics and scripting, HTML and CSS are the basis of building Web pages and Web Applications. The Web page is as:

Screenshot 2021-06-28 at 08-52-04 Kubernetes Web-UI with Python-CGI.png

Step3: Create Executable Python File

The following code snippet shows the required python code:

#!/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 the file executable by running the following command:

chmod +x <filename.py>

Step4: Test Lets launch a Pod using Web UI

Screenshot 2021-06-28 at 08-56-39 Kubernetes Web-UI with Python-CGI.png

Output:

Screenshot 2021-06-28 at 08-57-51 Kubernetes Web-UI with Python-CGI.png

Launching a Deployment using Web UI-

Screenshot 2021-06-28 at 08-59-00 Kubernetes Web-UI with Python-CGI.png

Output:

Screenshot 2021-06-28 at 08-59-19 Kubernetes Web-UI with Python-CGI.png

Deleting these resource created-

Screenshot 2021-06-28 at 09-00-42 Kubernetes Web-UI with Python-CGI.png

Output-

Screenshot 2021-06-28 at 09-01-55 Kubernetes Web-UI with Python-CGI.png

Hence, all tasks are successful.