This is a simple web server which demos the use of Flask along with a load balancer. The environment is virtually created using Docker.
app runs at
http://localhost/
mongo-express is available at
http://localhost:8081
If you pass user_id
as a query paramter in the GET
request to /
, the server responds with the associated user_key
which it fetches from a mongodb database along with the port number of the service responding, otherwise it simply shows which Flask app is serving your request. The load is balanced within 3 flask servers using nginx.
Author: Shreyas Padhye
Machine should have Docker installed in it.
One-time setup:
- Set mongo db credentials that you would want in two separate environment variables using following commands (where
username
andpassword
are values that you want to set):
a.export mongo_user=username
b.export mongo_pass=password
Note: sample commands specifically for mac & linux systems - Mount containers: Open root project directory
rundocker-compose up --build
- SSH in mongo's docker container: Open a separate terminal in root project directory
rundocker exec -it mongo /bin/bash
- Here, create database using dunp:
In the docker container terminal from step 3,
runmongorestore --db myusers dump/myusers -u username -p password --authenticationDatabase admin
- Run following commands to create role and authentication for
myusers
database:
. mongo -u username -p password --authenticationDatabase admin
. use myusers
. db.createUser({ user: "username", pwd: "password", roles: [{ role: "readWrite", db: "myusers" }] })
Note: use same username-password that you created in ENV variables
- run
exit
to exit docker container shell - Proceed with testing (refer to Test section)
For every time you want to run / restart containers:
- Open terminal in project's root directory and run following commands
docker-compose build
docker-compose up
or alternatively:docker-compose up --build
To stop running services: Press Ctrl + Z
and RUN docker-compose stop
On Browser:
- To test load balancer working, you can keep reloading the home page at http://localhost/ and you'll see the page being served from a different flask server everytime.
- To test
user_key
retrieval, pass in theuser_id
as a query parameter to '/' route eghttp://localhost/?user_id=1
will returnone@5cdb5896d7c8
as the key. The response will also contain the port of the flask server that is responding. - If a
user_id
is passed which does not exist in the database, the server returns an error with 400 status code - If the request does not contain the
user_id
param, it will return port - user_id from 1 to 7 are available in database and more can be added through mongo-express
Additionally, you can use a REST API client like Postman to GET
request http://localhost/
with the user_id
query param
docker-compose ps
mongodump --db myusers -u username -p password --authenticationDatabase admin
- Carefully went over the requirement and understood different aspects of the assignment.
- Created virtual environment for flask app along with its folder structure and created requirements.txt file so that it can be served through uWSGI
- Created wsgi config & basic flask routing at '/' and tested working through
flask run
- Dockerized flask and nginx
- Added code to spin off 3 flask servers via docker containers
- Added load balancer code to share load among the three flask servers
- Added mongo integration and included dump restore functionality in Readme to ease deployment
- Implemented query params handling to query mongodb and fetch appropraite key
- Handle server side exceptions eg when Internal Server Error happens due to authentication failed
- Change Dockerfile code to use ENV variable to set .ini file & implement through single docker file
- Explore if it can be done through single app.ini file
- Can add checks for DDoS and block IPs