This document is a guide to understanding how to deploy a Python Flask application in Kubernetes.
Kubernetes is an orchestration tool that is preferred when one has to manage large and complex clusters. It provides the user with ample flexibility and has an enviable list of stand-out features. Flask, a micro-framework for Python, is robust and light. It is typically used to build websites and apps with relative ease and in a shorter time span.
We will delve into a code pattern to create a Python Flask application that incorporates MongoDB as a microservice in Docker containers. Subsequently, these containers are deployed in Kubernetes.
Given below is the process to be followed in order to deploy a Python Flask application in Kubernetes.
Let us take the example of logging in and tracking the number of days an employee has worked out of office, worked remotely, or has availed of sick leave and/ or vacation days while at work.
Consequently, three pieces of data were included in this application:
- The total number of days that an employee has worked remotely
- Each location where the employee has worked
- The number of days that the employee has worked remotely in each of these locations
In this code pattern, how to create a Work Log web application using Flask, MongoDB, and Kubernetes is explained in depth. The Work Log application is used to keep track of different types of days that are associated with work. The different types of days include:
- Working in the office
- Working remotely
- Vacation days
- Sick days
After completing this pattern, one will understand how to:
- Create a Python Flask application
- Incorporate MongoDB into a Python application
- Deploy and run microservices on Kubernetes
The diagrammatic process flow detailed below explains how one can deploy a Python Flask application in Kubernetes.
Let us get an understanding of the steps involved in the above process:
- A user interacts with the App UI to initially create an account, log in to the account, or reset a password for their account. Once a user is logged in, they can view, add, and edit their work log data.
- The functionality of the App UI that the user interacts with is handled by React. React is where the API calls are initialised.
- The API calls are processed in the Flask API microservice on Kubernetes and are handled accordingly.
- The data is stored, gathered, and/ or modified in MongoDB depending on the API calls.
- The response from the API calls are handled accordingly by the App UI.
Read more about how to get the code here.
The components included in this code creation include:
- IBM Cloud Container Service: IBM Bluemix Container Service manages highly available apps inside Docker containers and Kubernetes clusters on the IBM Cloud.
- Swagger: A framework of API developer tools for the OpenAPI Specification that enables development across the entire API lifecycle.
Uses and applications of a Python Flask application
A Python Flask application is widely deployed owing to its ease of use.
In addition to being able to deploy a Python Flask application in Kubernetes, let us take a look at where else the app can be successfully used. For instance, Flask can be used for making database- driven webpages that require mostly static pages. In the social media setup, it can be used to create a Facebook or Twitter bot, for example. An online social network can also be created using Flask, and so can an e-commerce system.
Basis the above information, it has been established that a Python Flask-based microservice can be run successfully on an orchestration tool such as Kubernetes to create data records maintain the same, as required.