Microservices are an architectural and organizational approach to software development where software is composed of small independent services that communicate over well-defined APIs. Microservices architectures make applications easier to scale and faster to develop, enabling innovation and accelerating time-to-market for new features. Docker and Kubernetes are almost synonymous to 'microservices' as they help package and manage the different components of a project/ application, thereby easing up the implementation of a microservices architecture.
In this project, you will work with Docker and Kubernetes to make an easily deployable and portable blogging web-app using Flask and MongoDB.
The microservices architecture will deploy a Kubernetes cluster with a mongodb server pod fronted with a web admin interface and a pod to run the flask app.
.
|-- README.md
|-- app
| |-- app.py
| |-- requirements.txt
| `-- templates
| |-- base.html
| |-- create-post.html
| |-- edit-post.html
| `-- home.html
|-- configmap.yaml
|-- deployments.yaml
|-- flask-app-image.dockerfile
|-- secret.yaml
`-- services.yaml
The app
directory contains all the code pertaining to the flask app. You are only required to configure the mongo connection string variables as specified in app.py
.
The flask-app-image.dockerfile
should specify the insructions to assemble the docker image for the flask app.
The .yaml
files in the root directory are to specify the kubernetes manifests that will bring up your microservices deployment of the problem statement.
-
MongoDB Server
- Use the
mongo
image publicly available on DockerHub. Read through the configuration and other details of the image here. Note down the necessary environment variables to be configured. - Create the
Deployment
for the mongodb server underdeployments.yaml
. Remember to configure the ports and setup the environment variables correctly. - Environment variables such as username, password, etc. are sensitive information and are defined as a
Secret
. Define asecret.yaml
file to hold the sensitive information required by the mongodb server. You may create a secret using a configuration file and use the secret in your deployment as an environment variable. Read more. - Create a
Service
for the mongodb server underservices.yaml
.
- Use the
-
Mongo-Express Web Service
- Use the
mongo-express
image. Note down the necessary environment variables like before from here. - Define a
configMap
to store the mongodb server url. As above, use the configmap to configure the container with environment variables. Read more. - Create a
Deployment
for the mongo-express service underdeployments.yaml
and configure the necessary ports and environment variables (drawn from the secret and configmap). - Also define a
service
for the pod underservices.yaml
.
- Use the
-
Flask WebApp
- Use the image created from the
flask-app-image.dockerfile
. - Create a
Deployment
for the flask app underdeployments.yaml
. - Also define a
Service
for the pod inservices.yaml
.
- Use the image created from the
Bring up all the microservices. Once all the microservices are up and running,
- Inside the flask-app pod, write and run a python script to insert records into the mongodb database. Insert into: database = 'blog' and collection = 'posts'.
- Run
app.py
inside the pod. Visithttp://localhost:<port>/
to view the Blog App. The Home Page should display the records inserted into the database in the previous step.
- You can view the database frontend exposed by mongo-express. To do so, on your browser, navigate to
EXTERNAL_IP:port
exposed by the mongo-express service. Here is the sample output:
You can also play around with the Mongo CLI. Refer Mongo Shell Guide
Login to the mongo shell and -
- Create a Database
- Insert a Collection
- Fill it with records
Feel free to play around for brownie points!