Kubernetes Integration with Python-CGI and Running K8s commands through WebGUI in English.
Heya Folks, excited for this one too? Remember what we did in our previous blog? ummm, wasn't it something very similar? YES!, In the previous blog, we used a WebGUI to run the docker commands. In this particular blog, we would be using normal English, to run Kubernetes commands through a WEB GUI.
The current world has been acing at revolutionizing technology. Kubernetes is also one of them. It is an open-source system for automating deployment, scaling, and management of containerized applications. So let’s begin!
Initially, we would need to configure our Red hat Linux. The prerequisite for this is to have Kubernetes and Kubectl configured in our system. To do this, from minikube, take the “admin.conf” file and transfer it to Red hat through any medium. Use the official documentation for installing Kubectl on Linux. Now, once again we require to start our httpd server. Since we already have the httpd module we need to start it again.
systemctl start httpd
Utilizing the same concept, we again have to create an HTML and python CGI file. The HTML file is stored in “/var/www/html/” and cgi code in “/var/www/cgi-bin/”. We have created the files Kubernetes.html and kub.py.
The HTML file shows the output and the python file runs at the backend.
Now we need to be aware of the fact that the user will run the command in normal English. We need to inform the user of the basic criteria to provide particular info for each command.
For example, for the creation of a pod in K8s, we need to ask from the user to use launch keyword, and should mandatorily give the pod and image name. The user can give a command in the manner “ Please launch a pod “HI” and image name centos.A similar approach should be given for all the commands.
Let’s see the code!
Kubernetes.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Kubernetes-webgui</title>
<link rel="stylesheet" href="./stylesheet.css" />
</head>
<script>
function shash() {
var xhr = XMLHttpRequest();
var x = document.getElementById("value").value
xhr.open("GET", "<your_ip>/cgi-bin/kub.py?x="+x, true);
xhr.send();
var final = xhr.responseText;
document.getElementById("value").innerHTML = final;
}
function shash1() {
document.getElementById("value").value = "";
}
</script>
<style>
* {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
section {
text-align: center;
}
button {
width: 8rem;
}
</style>
<body>
<section>
<h1>Your Kubernetes WebGui</h1>
<div>
Give your command here, and get your output, Please write the name of the pod, or the image, and be a little specific about your requirements.
</div>
<div>(ITS SO COOOOOL!)</div>
</section>
<br /><br /><br /><br />
<section>
<input type="text" id="value" />
<br /><br />
<button onclick="shash()">Run Command</button>
<button onclick="shash1()">Reset</button>
</section>
</body>
</html>
kub.py:
#!/usr/bin/python3
print("content-type: text/html")
print()
import cgi
import subprocess
f=cgi.FieldStorage()
cmd=f.getvalue("x")
if "launch" in cmd:
if "pod" in cmd:
x=cmd.split('pod')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf run "+ x1[1] + " --image="+x1[-1])
if "create" in cmd:
if "deployment" in cmd:
x=cmd.split('deployment')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf create deployment "+ x1[1] + " --image="+x1[-1]) if "expose" in cmd:
if "deployment" in cmd:
x=cmd.split('deployment')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf expose deployment "+ x1[1] + " --image="+x1[-1])if "scale" in cmd:
if "deployment" in cmd:
x=cmd.split('deployment')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf scale deployment "+ x1[1] + " --replica="+x1[-1])if "environment" in cmd:
if "delete" in cmd:
x=cmd.split('delete')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf delete svc "+ x1[1])])if "delete" in cmd:
if "pod" in cmd:
x=cmd.split('pod')
x1=x[-1].split(' ')
o=subprocess.getoutput("kubectl --kubeconfig /usr/share/httpd/admin.conf delete pod "+ x1[1])if "kubectl" in cmd:
o=subprocess.getoutput(cmd)
print(o)
The WebGUI that we create should be able to execute commands given to it like can launch pods with specific name given by user or Run deployment using image and name given by user, or Expose services on given user input port number or Scale the replica according to user need or Delete complete environment created or Delete specific resources given by user.
We see with the above code, all the requirements are met. Isn’t it magical that we have created a webGUI through which we can execute normal English commands for K8s! Seeya on the next one.