Kubernetes WebApplication

Krithika Sharma
3 min readAug 15, 2021

In this, we will be integrating k8s with CGI-bin and run the k8s commands through a web application. For this the user doesn’t need to know the commands of k8s, the user only needs to specify his/her requirement in normal English sentence and the things will be done. So, let’s get started…

We need to install k8s and use kubectl as a client. We then configure admin.conf with the server as IP of minikube.

Now instead of running the commands directly from here, we will be creating a web app using HTML, CSS, and js. At the backend, we will be using python cgi-bin which will give the interface so that we can execute our cmds and show the output back in the web app.

Here is the python cgi-bin backend file:

#!/usr/bin/python3 
import cgi
import subprocess
print("content-type: text/html")
print()
field = cgi.FieldStorage()
cmd = field.getvalue("x")
print(subprocess.getoutput("sudo kubectl " + cmd + " --kubeconfig admin.conf"))

Now we will be using HTML to give input box to the user where he/she can write their requirement and a button. Upon clicking this button it will call a function. This function will perform the things based on the requirement. This code will be placed in javascript file as follows:

function code(k) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://192.168.99.108/cgi-bin/k8s.py?x=" + k, true);
xhr.send();

xhr.onload = function out() {
var output = xhr.responseText;
document.getElementById("d1").innerHTML = output;
};
}

function f1() {
var i = document.getElementById("in1").value;

if (i.includes("show") && i.includes("pods")) {
var comb = " get pods ";
code(comb);
} else if (i.includes("create") && i.includes("deployment")) {
var podname = prompt("Enter deployment Name");
var imagename = prompt("Enter Image Name");
var comb = " create deployment " + podname + " " + " --image=" + imagename;
code(comb);
} else if (i.includes("create") && i.includes("pod")) {
var podname = prompt("Enter Pod Name");
var imagename = prompt("Enter Image Name");
var comb = " run " + podname + " " + " --image=" + imagename;
code(comb);
} else if (i.includes("show") && i.includes("pods")) {
var comb = " get pods ";
code(comb);
} else if (i.includes("delete") && i.includes("pods") || i.includes("pod")) {
var podname = prompt("Enter Pod Name");
var comb = " delete pod " + podname;
code(comb);

} else if (i.includes("delete") && i.includes("deployment")) {
var podname = prompt("Enter deployment Name");
var comb = " delete deployment " + podname;
code(comb);
} else if (i.includes("scale") && i.includes("deployment")) {
var dname = prompt("Enter Deployment Name", "existing pod name");
var r_no = prompt("Enter Required Replicas", "ex- 1-9");
var comb = " scale deployment " + dname + " " + " --replicas=" + r_no;
code(comb);
} else if (i.includes("expose") && i.includes("deployment")) {
var dname = prompt("Enter Deployment Name", "existing deploy name");
var p_no = prompt("Enter Port No", "ex- 1-9");
var comb =
" expose deployment " +
dname +
" " +
" --port=" +
p_no +
" --type=NodePort";
code(comb);
} else if (i.includes("clear") || i.includes("delete") && i.includes("environment") || i.includes("env")) {
var comb = " delete --all all";
code(comb);
} else if (i.includes("show") && i.includes("exposed") && i.includes("service")) {
var comb = " get service";
code(comb);
} else {
alert("Please Enter valid command");
}
}

We then give our requirements in web portal and our cmds will get proceed further and will give the respective outputs back in the portal.

Thank you! keep learning! keep growing! keep sharing!

Krithika Sharma
If you enjoyed this give it a clap, follow me on Medium for more
Let’s connect on LinkedIn

--

--