The goal of this exercise is to use PM2, a Node.js process manager, to deploy three instances of a Node.js web application on your server, and to configure nginx as a load balancer to distribute incoming traffic to these three instances.
- Goal
- Requirements
- ❗ Install the application on your server
- ❗ Run the application with PM2
- ❗ Configure nginx as a load balancer
- ❗ Make sure it works
The application you must deploy is an IP address locator. Its source code is available on GitHub.
When a proper load balancing configuration is performed, nginx should direct your request to one of three application instances, each with a different background color, every time you refresh the page. (This does not work on the Heroku deployment linked above.)
Setting up a load balancer allows your application to serve more clients. Since there are three instances available to respond to requests, it can in theory respond to three times as many requests in parallel.
You must have Node.js version 10+ installed on your server.
Follow the instructions in the GitHub repository's README to install the application on your server:
- Clone the repository.
- Install its dependencies.
PM2 is a process manager dedicated to running Node.js applications.
You will configure PM2 to run three instances of the locator application on three different ports, then configure systemd to run PM2 automatically on startup.
To install PM2 on your server, execute the following command:
$> sudo npm install -g pm2Read PM2's ecosystem file documentation.
Create a PM2 ecosystem file to run three instances of your application on three different ports and with three different background colors. You can do this by setting the correct environment variables for each instance. Read the locator's configuration documentation to find the correct variables to set.
- 💎 The
scriptkey in the PM2 ecosystem file defines the file PM2 will execute to launch the application. Look at thestartscript in the application'spackage.jsonfile to find the right file to execute.- 💎 The
watchkey should be false or be removed. Watching is for development, when you want the application to automatically reload its code if it changes. You do not want this for a production deployment, as it might take a few seconds to restart, during which clients will get errors.
Once your ecosystem file is ready, run it with PM2.
PM2 can automatically create a systemd service for you. Follow PM2's startup hook documentation.
You will need to install the hook, then save your process list.
Create an nginx site configuration file that does the following:
- Serve the site on the subdomain
locator.jde.archidep.ch(replacingjdewith your username). - Use the path to the locator application's
publicdirectory as the site's root. - Configure load balancing to your three instances. You will find an example of a load balancing nginx configuration in the reverse proxying slides.
Once you have your configuration in place and enabled, tell nginx to reload its configuration.
Make sure the load balancing works (you should see a different color each time you refresh your locator page).
