Integration of Kubernetes with Python — Flask

Ajmal Muhammed
2 min readJun 30, 2021

In this article, we are going to integrate multiple technologies like Python-CGI, HTML, CSS, and JavaScript. The user is provided a webpage from which he/she could execute Kubernetes commands without focusing on how to set up the environment for the same.

Task Description 📄
👉 It can launch pods with the specific names given by users.
👉 Run deployment using the image and name given by the user.
👉 Expose services on given user input port number.
👉 Scale the replica according to user needs.
👉 Delete complete environment created.
👉 Delete specific resources given by the user.
👉 Extra features related to k8s ( Optional)
Note — There should be a web UI-based menu display so that users can get to know what your web app can do.

📌 This app will help the user to run all the Kubernetes commands:

In this tutorial, we use the python flask framework to integrate Kubernetes. Also, using an AWS ec2 instance to implement this integration.

So, make sure you have created an AWS ec2 instance and set up the Kubernetes cluster in it in order to keep up this tutorial.

The Flask framework is used to connect python with web applications easily. It helps us to connect with the remote system/server running in the aws. So, we can easily access and connect with the remote server.

Let start discussing how we going to implement this.

First, we need to create a basic HTML file containing an input tag and button. It is your choice to style the HTML layout using CSS

The second step is to write the javascript code, it is always recommended to write in a separate file. Always remember to add script files in HTML code.

Here, we using Ajax (Asynchronous Javascript XML) to run the command without reloading the page. Also, it helps us to run multiple functions/events at a time without waiting for the first function to complete.

In the third step, we need to connect our webpage with a remote server running in AWS in order to run the Kubernetes commands remotely.

Sample code of this python flask setup is given below:

import re
import boto3
import botocore
import paramiko
from flask import Flask, request, render_template
from flask_cors import CORS , cross_origin
app = Flask('Docker')
cors = CORS(app, resources={r"/*": {"origins": "*"}})
key = paramiko.RSAKey.from_private_key_file('/root/RHEL8.pem')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname="ec2-13-233-229-206.ap-south-1.compute.amazonaws.com", username="ec2-user", pkey=key)

@app.route('/pod', methods=['GET', 'POST'])
@cross_origin()
def run():
get = request.args.get("x")
stdin, stdout, stderr = client.exec_command(f"sudo {get}")#f'sudo kubectl create deployment {d_name} --image={img_name} --replicas={int(rep)}')
return(stdout.read())
app.run(host='localhost',port=3487,debug=True)

The feature included in the webpage as follows-
👉 It can launch pods with the specific names given by users.
👉 Run deployment using the image and name given by the user.
👉 Expose services on given user input port number.
👉 Scale the replica according to user needs.
👉 Delete complete environment created.
👉 Delete specific resources given by the user.

For more help visit the GitHub link attached here: https://github.com/ajmalmohammedn/summer_program_2021/tree/main/task%209

Thank You!

--

--