This problem statement has two objectives:
- Get familiar with reading error stack trace and debugging code.
- Convert a monolith architecture based docker-compose application into a microservices based architecture.
- docker and docker-compose. Follow the guides based on your operating system.
- Internet. Pull docker image
python:3.8-alpine
beforehand to avoid connectivity issues.
├── README.md
├── docs
│ └── <documentation related images/files>
├── microservices
│ ├── Docker-compose.yaml
│ ├── landing
│ │ ├── app
│ │ │ ├── app.py
│ │ │ ├── requirements.txt
│ │ │ └── templates
│ │ │ └── index.html
│ │ └── Dockerfile
│ │
# under the microservices directory
# NOTE: For any code changes to be reflected, the build command must be rerun, and then up
docker-compose build
# run without the -d flag incase you want to observe the logs
docker-compose up -d
docker-compose down
NOTE: It is possible your first build will not be successful and that's alright. Read the stack trace and debug the errors. The resources and links provided within the manual are sufficient to successfully complete the project.
-
- The following modules are required by the
landing-service
: flask
flask-restful
requests
- Dockerfiles help building images using a given set of instructions. You can learn about the basics of Dockerfiles here. Use this document to understand the meaning of certain keywords used by Dockerfiles.
- For this project, you need to write a Dockerfile for a python application. I have provided sample Dockerfiles here and here.
- Specifications for the
Dockerfile
:- Use the image
python:3.8-alpine
- Copy the contents of
./app/requirements.txt
to/app/requirements.txt
- Set the working directory to
/app
- Run the following command:
apk add --update \ && pip install --upgrade pip \ && pip install -r requirements.txt \ && rm -rf /var/cache/apk/*
- Copy the contents of the
./app
to/app
folder - Run the command
python app.py
- Use the image
- The following modules are required by the
-
- By default, Python considers all input as string. Due to this, the arithmetic operations do not seem to be working as intended.
- Fix the type of the two variable values received from
index.html
- This bug is observed only after fixing the previous bug.
- By default, no values are provided by
index.html
, causing the application to throw exceptions and crash when localhost:5050 is loaded initially. - Avoid the crash by handling the exceptions raised in the landing-service app.
-
- The four arithmetic functions currently reside under landing-service. However, if landing-service were to become unavailable for whatever reason, the four functions would be unavailable as well.
- Create separate flask applications, Addition, Subtraction, Multiplication and Division. Each application will consist of a class which inherits the
Resource
class offlask_restful
module. - Define a GET method within the class with necessary parameters. An example of this class with the method defined can be found here and here
- Use the
add_resource
function to add the class as a resource and define the API endpoint. Make sure to also mention the type of the parameters in the endpoint. Example:api.add_resource(<class-name>, '/<int:argument0>/<int:argument1>')
- Update the
Docker-compose.yaml
to recognize the newly added flask applications as separate services. TheDocker-compose.yaml
lets you define the port number and network alias that will be used bylanding-service
to communicate within the entire architecture - Test the new microservices-based application.
- Proceed to add three more services that perform a certain function. You will also have to make changes to the frontend defined in
index.html
to make these functions available. Mentioned below are possible functions you could implement but feel free to add your own. gcd
: Takes two numbers as arguments and returns their Greatest Common Divisorlcm
: Takes two numbers as arguments and returns their Least Common Multiplemodulus
: Returns the remainder of two numbers after division. Referenceexponent
: Returns the result of the arithmetic operation abgreater_than
: ReturnTrue
if the first value is greater than the second else ```False``less_than
: returnTrue
if the first value is less than the second elseFalse
equal
: returnTrue
if the first value is equal to the second elseFalse
The diagram only shows the services already defined within the microservice architecture for visualization purposes. You still need to add services of your own.
- Directory structure of additional arithmetic microservices you will be adding:
├── <name of the service>
│ ├── Dockerfile # same as landing/Dockerfile
│ ├── app
│ │ ├── app.py # TODO: by yourself
│ │ └── requirements.txt # same as landing/app/requirements.txt
│ │