In this learning activity, we will implement the MEAN stack using a multi-instance architecture. We will then scale the architecture, first by separating the app and database tiers, and then by scaling the app tier using a load balancer.
The goal of this learning activity is to gain experience with:
* Creating a Lightsail instance complete with a pre-installed stack
* Using a launch script to perform advanced configuration
* Interacting with an on-instance MongoDB
* Connecting to an instance to configure a MEAN-based application
* Application testing and verification
* Using multi-instance applications
* Using snapshots to clone instances
* Using load balancers to enable mass scaling
#### Helpful Links:
https://pm2.io/doc/en/runtime/overview/
https://github.com/linuxacademy/aws-lightsail-deep-dive/blob/master/Scenario3/learning_activity_2/frontendlaunch.sh
https://github.com/linuxacademy/aws-lightsail-deep-dive/blob/master/Scenario3/learning_activity_2/mongolaunch.sh
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Deploy an instance-based MongoDB.
- Deploy an instance with 2 GB of memory using the Ubuntu OS Only blueprint.
- Name the instance
mongo
. - Use the following launch script to configure the database: https://github.com/linuxacademy/aws-lightsail-deep-dive/blob/master/Scenario3/learning_activity_2/mongolaunch.sh
- When the instance is built, log in using SSH and check that the database is running.
mongo --host $(hostname -i) show dbs
- Note the private IP of the instance; we will need it later.
Note:
ipconfig -a
can show the private IP of the Mongo instance. The public IP is shown for the instances on the box that represents each instance.- Deploy the application front end on an instance.
- Deploy a new instance using the 512 MB ram size.
- Name the instance
node-fe-1
. - Use the Node.js blueprint (we don’t need an on-instance database, and the app will install everything it needs).
- Use the following launch script: https://github.com/linuxacademy/aws-lightsail-deep-dive/blob/master/Scenario3/learning_activity_2/frontendlaunch.sh
- When the instance finishes building, we need to point it at the database server. Log in to the
node-fe-1
instance using SSH and run:IP=<MONGODB-PRIVATE-IP> cd ~/todo sudo sh -c "cat > ./.env" << EOF PORT=80 DB_URL=mongodb://$IP:27017/ EOF
- Next, we need to make the application auto-start and do a final configuration.
sudo pm2 startup ubuntu sudo pm2 start /home/bitnami/todo-mean/bin/www sudo pm2 save
- Then we need to show the logs to ensure everything is working correctly.
sudo pm2 logs www
- Once the logs are streaming, navigate to
http://<frontendinstanceip>
to test the application.
- Clone and scale the front end.
Snapshot the Working Front-End Instance
- Click the context (
...
) menu for thenode-fe-1
instance. - Choose Manage, then Snapshots.
- Create a snapshot and wait for it to complete.
Create Additional Front-End Instances
- Locate the snapshot. Click the context (
...
) menu and select Create New Instance. - Name the instance
node-fe-2
. - Repeat this process to create a new front-end instance called
node-fe-3
. - Navigate to each new instance’s public IP and test that the application works.
- Create a task on one instance, and make sure it’s visible on all instances.
- Click the context (
- Distribute traffic across the front end with a load balancer.
- Create a load balancer and name it
todo-lb
.- Once available, attach all front-end instances to the load balancer (
node-fe-1
,node-fe-2
, andnode-fe-3
). - Ensure that health checks are enabled.
- Make sure each instance passes health checks.
- When all instances are healthy, locate the load balancer DNS name and open it in a browser.
- If no task exists, create one.
- Refresh the page and ensure the task still exists. Note the hostname changes at the bottom showing which front-end instance is being used.
- Once available, attach all front-end instances to the load balancer (
- Create a load balancer and name it