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:
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.
Step1: Checking Requirements
Install kubernetes cluster on your VM and start httpd service in the VM, stop firewall as shown below.
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:
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
Output:
Launching a Deployment using Web UI-
Output:
Deleting these resource created-
Output-
Hence, all tasks are successful.