Microk8s + Flask [part 1]
Dockerize a Flask application
Over the past couple of weeks, I had been struggling with exposing a simple, custom Flask application in Microk8s. After managing to do so, I have decided to share my story with you, hoping it will be useful for similar purposes.
Overview
In this story (part 1), I will basically cover two tasks:
- I will show you the application I have developed. It is by far one of the simplest I have ever built though 😛.
- Following, I will create a docker image for the app, using a dockerfile.
In the part 2, we will be covering how this app can be exposed as a Service in Microk8s.
The application
So, let’s see the application itself, which is basically a Flask app that only renders an HTML template and uses some CSS.
App Structure
Assuming the application is inside a directory called “my-app”, there is:
The app.py file
from flask import Flask, render_template
app = Flask(__name__)@app.route(‘/’)
def index():
return render_template(“index.html”)if __name__ == ‘__main__’:
app.run(debug = True, host=’0.0.0.0')
A /templates directory including the index.html file which will be rendered.
<!DOCTYPE html>
<html lang=”en”><head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<link rel=”stylesheet” href=”{{ url_for(‘static’, filename=’css/styles.css’) }}”>
<title>my-app</title>
</head><body>
<div class=”container”>
<h1>My App</h1>
<h3>This app will be a Kubernetes deployment and …</h3>
<h3>… then it will be exposed as a service</h3>
</div>
</body></html>
The /static/css directory with a styles.css file inside.
html{
height: 100%;
}body{
height: 100%;
margin: 0;
padding: 0;
background-color: #483d8d;
display: flex;
align-items: center; /* center of page */
justify-content: center; /* center of page */
}.container{
background-color: #efcfcb;
border-radius: 10px;
padding: 2rem;
box-sizing: border-box;
}.container h1{
color: #172727;
font-family: 'Abril Fatface', sans-serif;
}.container h3{
font-family: 'DM Sans', sans-serif;
color: #2f4f4f;
}
Dockerizing the Application
Dockerfile
The first part of the process was creating a dockerfile, with the following lines, each of which is explained with comments right above it:
# with this line we import python language.
FROM python:2.7# copy the requirements.txt file in the image
COPY ./requirements.txt /app/requirements.txt# the working directory will be the /app directory which can be
# found inside the python image
WORKDIR /app# let's install the requirements (only Flask is needed)
RUN pip install -r requirements.txt# we can now add our directory to the working directory
COPY ./my-app/ /app# let's run the application
CMD [ "python","app.py" ]
Requirements.txt file
The requirements file contains only a single line. Well, actually only a single word:
Flask
Let’s create the image
First, we must put the dockerfile and the requirements file where the directory “my-app” is.
We can now create the image with the following line of code:
docker build -t flask-docker-image .
After running the command, we can check for the newly created docker image. We can run:
docker images
Then, we should see the following outcome:
And now, we are absolutely ready to go to the next level!
To find out how we can actually deploy the application in Microk8s, take a look at the second part of this story.