Kubernetes Web-UI with Python-CGI

Sampreethi Bokka
4 min readJun 29, 2021
kubernetes

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.

Server python CGI Code:

#!/usr/bin/python3import cgiimport subprocessprint("content-type: text/html")print("Access-Control-Allow-Origin: *")print()f = cgi.FieldStorage()t = f.getvalue("x")q = f.getvalue("y")val = t.split()if val[0] == "1":     deployment_name = val[2]     image_name = val[1]     cmd = "kubectl create deployment " + (deployment_name) + " --                 image=" + (image_name)     output = subprocess.getoutput( cmd + " --kubeconfig    admin.conf")      print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "2":     pod_name = val[2]     image_name = val[1]     cmd = "kubectl run " + (pod_name) + " --image=" + (image_name)     output = subprocess.getoutput(cmd + " --kubeconfig admin.conf")     print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "3":       pod_name = val[1]       cmd = "kubectl delete pod " + (pod_name)       output = subprocess.getoutput(cmd + " --kubeconfig admin.conf")       print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "4":       deployment_name = val[1]       cmd = "kubectl delete deployment " + (deployment_name)       output = subprocess.getoutput(cmd + " --kubeconfig admin.conf")
print('<h4> <pre> {} </pre> </h4>'.format(output))
elif val[0] == "5": deployment_name = val[1] port_no = val[2] Expose_type = val[3] cmd = "kubectl expose deployment " + (deployment_name) + " --port=" + (port_no) + " --type=" + (Expose_type) output = subprocess.getoutput(cmd + " --kubeconfig admin.conf") print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "6": deployment_name = val[1] replica = val[2] cmd = "kubectl scale deployment " + (deployment_name) + " --replicas=" + (replica) output = subprocess.getoutput(cmd + " --kubeconfig admin.conf") print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "7": cmd = "kubectl get pods --kubeconfig admin.conf" output = subprocess.getoutput(cmd) print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "8": cmd = "kubectl get deployments --kubeconfig admin.conf" output = subprocess.getoutput(cmd) print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "9": cmd = "kubectl get svc --kubeconfig admin.conf" output = subprocess.getoutput(cmd) print('<h4> <pre> {} </pre> </h4>'.format(output))elif val[0] == "11": print("<h4> I Can Help you in Setting-Up Kubernetes Resources from your Instructions. </h4>")elif val[0] == "12": print("<h4 > Heyy..!! I'm Kube Bot </h4>")elif q == "15": cmd = "kubectl delete all --all --kubeconfig admin.conf" output = subprocess.getoutput(cmd) print(output)elif val[0] == "13": print("<h4> 192.168.99.102 </h4>")elif val[0] == "404": print("Please Write Something")

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.

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.

Step3: launch a Deployment using image and name and port number Web UI.

Step4: Scale the replica using Web UI.

Step3: launch pods with specific name.

!!!Thanks for reading this article!!!

--

--

Sampreethi Bokka

Intern at LinuxWorld informatics Pvt Ltd student from vellore institute of technology.