Skip to content

Practice Exercise 1: High availability

Objectives

Run two flask apps to understand the concept of high availability.

Scenario

You have two web instances web1 and web2 that are being load balanced, however you want to identify which host you are being redirected to by the load balancer.

  • <name>-a1t-inf-ds-web1.acad.opswerks.net
  • <name>-a1t-inf-ds-web2.acad.opswerks.net

Tasks

  • Connect to the web2 instance
  • Create a directory for your app and navigate into it:

    mkdir my_flask_app
    cd my_flask_app
    

  • Create a virtual environment

    python3 -m venv flask_env
    

  • Activate the virtual environment

    source flask_env/bin/activate
    

  • Install Flask in the virtual environment:

    pip install flask
    

  • Create a Python file for your Flask appĀ app.py ensure you can differentiate it from your previous flask app like below:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return 'Hello, and welcome to my second Flask app!'
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=80)
    

  • Start the flask app:

    sudo flask_env/bin/python3 app.py
    

  • Ensure that the flask app from Module 3 - Activity 6 is also running on web1 and that the nginx service is not

  • Visit the link <YOUR_NAME>-a1t-inf-ds-lb.acad.opswerks.net
  • Stop the flask app on either of the web instances.
    You should see the changes reflect when you revisit the lb url as the site would still be up.
    You will notice which web instance you are redirected to based on the text displayed.

Conclusion

As you continue to explore distributed systems, you should grasp the significance of load balancing in optimizing resource utilization, ensuring high availability, and enhancing user experience. This hands-on activity provides a visual and practical demonstration of load balancing, making it easier to appreciate its role in maintaining the robustness and efficiency of distributed applications.