Problem Statement

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.

Pre-Requisites/ Pre-Installation:

  1. Docker (Windows | Ubuntu | MacOS)
  2. Kubernetes (Windows | Ubuntu | MacOS)

File Structure

.
|-- 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.

Task Breakdown/ Deliverables:

  1. MongoDB Server

    1. 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.
    2. Create the Deployment for the mongodb server under deployments.yaml. Remember to configure the ports and setup the environment variables correctly.
    3. Environment variables such as username, password, etc. are sensitive information and are defined as a Secret. Define a secret.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.
    4. Create a Service for the mongodb server under services.yaml.
  2. Mongo-Express Web Service

    1. Use the mongo-express image. Note down the necessary environment variables like before from here.
    2. Define a configMap to store the mongodb server url. As above, use the configmap to configure the container with environment variables. Read more.
    3. Create a Deployment for the mongo-express service under deployments.yaml and configure the necessary ports and environment variables (drawn from the secret and configmap).
    4. Also define a service for the pod under services.yaml.
  3. Flask WebApp

    1. Use the image created from the flask-app-image.dockerfile.
    2. Create a Deployment for the flask app under deployments.yaml.
    3. Also define a Service for the pod in services.yaml.

Bringing it all together

Bring up all the microservices. Once all the microservices are up and running,

  1. 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'.
  2. Run app.py inside the pod. Visit http://localhost:<port>/ to view the Blog App. The Home Page should display the records inserted into the database in the previous step.

  1. You can view the database frontend exposed by mongo-express. To do so, on your browser, navigate toEXTERNAL_IP:port exposed by the mongo-express service. Here is the sample output:

  1. docker-compose build --no-cache
  2. minikube image load flask-app
  3. kubectl apply -f secrets.yaml kubectl apply -f configmap.yaml kubectl apply -f services.yaml kubectl apply -f deployment.yaml
  4. kubectl get pods kubectl get svc kubectl get deployments
  5. minikube ip
  6. Open the application on firefox. URL: http://<minikube_ip>:/

MongoDB CLI

You can also play around with the Mongo CLI. Refer Mongo Shell Guide

Login to the mongo shell and -

  1. Create a Database
  2. Insert a Collection
  3. Fill it with records

Feel free to play around for brownie points!

All Done! :)