Skip to content

Latest commit

 

History

History
125 lines (89 loc) · 5.05 KB

load-balancing-deployment.md

File metadata and controls

125 lines (89 loc) · 5.05 KB

Configure nginx as a load balancer for a Node.js application run with PM2

The goal of this exercice is to use PM2, a Node.js process manager, to deploy 3 instances of a Node.js web application on your server, and to configure Nginx as a load balancer to distribute incoming traffic to these 3 instances.

Goal

The application you must deploy is an IP address locator. Its source code is available on GitHub.

WARNING: the main feature of the application (geolocating IP addresses) may not work due to rate limiting issues. However, it is deployable and still illustrates the load balancing capabilities of nginx.

When a proper load balancing configuration is performed, nginx should direct your request to one of 3 application instances, each with a different background color, every time you refresh the page. (This does not work on the Heroku deployment linked above.)

Load Balancing with nginx and PM2

Setting up a load balancer allows your application to serve more clients. Since there are 3 instances available to respond to requests, it can in theory respond to 3 times as many requests in parallel.

Requirements

You must have Node.js version 10+ installed on your server.

Install the application 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.

Run the application with PM2

PM2 is a process manager dedicated to running Node.js applications.

You will configure PM2 to run 3 instances of the locator application on 3 different ports, then configure systemd to run PM2 automatically on startup.

Install PM2

To install PM2 on your server, execute the following command:

$> sudo npm install -g pm2

Run 3 instances of the application

Read PM2's ecosystem file documentation.

Create a PM2 ecosystem file to run 3 instances of your application on 3 different ports and with 3 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.

  • Hint: the script key in the PM2 ecosystem file defines the file PM2 will execute to launch the application. Look at the start script in the application's package.json file to find the right file to execute.
  • Hint: the watch key 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.

Configure systemd to run 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.

Configure nginx as a load balancer

Create an nginx site configuration file that does the following:

  • Serve the site on the subdomain locator.john-doe.archidep.ch (replacing john-doe with your username).
  • Use the path to the locator application's public directory as the site's root.
  • Configure load balancing to your 3 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.

The end

Make sure the load balancing works (you should see a different color each time you refresh your locator page).