Load balancing with NGINX
You can use Nomad's template stanza to configure NGINX so that it can dynamically update its load balancer configuration to scale along with your services.
The main use case for NGINX in this scenario is to distribute incoming HTTP(S) and TCP requests from the Internet to front-end services that can handle these requests. This tutorial shows you one such example using a demo web application.
Reference material
Prerequisites
To perform the tasks described in this tutorial, you need to have a Nomad environment with Consul installed. You can use this Terraform environment to provision a sandbox environment. This tutorial uses a cluster with one server node and three client nodes.
Note
This tutorial is for demo purposes and only uses a single server node. Please consult the reference architecture for production configuration.
Create and run a demo web app job
Create a job for a demo web application and name the file webapp.nomad.hcl
:
This job specification creates three instances of the demo web application for you to target in your NGINX configuration.
Now, deploy the demo web application.
Create and run an NGINX job
Create a job for NGINX and name it nginx.nomad.hcl
. This NGINX instance
balances requests across the deployed instances of the web application.
This configuration uses Nomad's template to populate the load balancer configuration for NGINX. It uses Consul Template. You can use Consul Template's documentation to learn more about the syntax needed to interact with Consul. In this case, the template queries Consul for the address and port of services named
demo-webapp
, which are created in the demo web application's job specification.This job specification uses a static port of
8080
for the load balancer. This allows you to querynginx.service.consul:8080
from anywhere inside your cluster to reach the web application.Please note that although the job contains an inline template, you could alternatively use the template stanza in conjunction with the artifact stanza to download an input template from a remote source such as an S3 bucket.
Now, run the NGINX job.
Verify load balancer configuration
Consul Template supports blocking queries. This means your NGINX deployment (which is using the template stanza) is notified immediately when a change in the health of one of the service endpoints occurs and re-render a new load balancer configuration file that only includes healthy service instances.
You can use the nomad alloc fs
command on your NGINX allocation to
read the rendered load balancer configuration file.
First, obtain the allocation ID of your NGINX deployment (output below is abbreviated). Keep in mind, allocation IDs are environment specific, so yours is expected to be different:
Next, use the alloc fs
command to read the load balancer configuration:
At this point, you can change the count of your demo-webapp
job and repeat the
previous command to verify the load balancer configuration is dynamically
changing.
Make a request to the load balancer
If you query the NGINX load balancer, you should be able to see a response similar to the one shown below (this command should be run from a node inside your cluster):
Note that your request has been forwarded to one of the several deployed instances of the demo web application (which is spread across 3 Nomad clients). The output shows the IP address of the host it is deployed on. If you repeat your requests, the IP address changes based on which backend web server instance received the request.
Note
If you would like to access NGINX from outside your cluster, you
can set up a load balancer in your environment that maps to an active port
8080
on your clients (or whichever port you have configured for NGINX to
listen on). You can then send your requests directly to your external load
balancer.