liveBook · Manning

Load balancing

As explained in Road to Kubernetes

Load balancing is the distribution of incoming traffic across one or more servers capable of handling it.

Load balancing distributes incoming traffic across multiple servers or application instances that can handle it. NGINX acts as a reverse proxy: it receives a request, selects a configured destination, and forwards the request there. This can scale containerized or non-containerized applications without immediately requiring a larger orchestration system. section 3.6 section 7.5

NGINX can rotate requests among destinations using several load-balancing algorithms:

  • Round Robin sends requests one after another and is the default. section 7.4.1
  • Weighted Round Robin prefers some destinations over others. section 7.4.1
  • Least Connections selects the least busy destination. section 7.4.1
  • IP Hash selects destinations according to the requesting IP address. section 7.4.1

Balance several application instances

Define an NGINX upstream named myapp with destinations such as localhost:8080, localhost:8081, and servers on other IP addresses. Proxy requests for / to http://myapp. In Docker Compose, run several copies of the same application under service names such as bruce, tony, and thor, then configure the upstream with bruce:8080, tony:8080, and thor:8080. Publish NGINX on port 8000 while the application services remain behind it. section 7.4.1

Listing 7.16 NGINX load balancing basics
upstream myapp {
    server localhost:8080;
    server localhost:8081;
    server my-ip-address:80;
    server my-other-ip-address:80;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://myapp;
    }

    location /api {
        proxy_pass http://localhost:3000;
    }
}

Shows an NGINX upstream containing several local and remote destinations, with a separate route for /api. section 7.4.1

How Road to Kubernetes covers it

How deep it goes
Defines reverse-proxy forwarding, demonstrates upstream configuration, and names algorithms while deferring deeper algorithm configuration.
Examples
Uses NGINX upstream files, repeated Compose services, localhost ports, remote IP addresses, and Python and Node.js applications.
What it assumes
Assumes basic application deployment and introduces NGINX forwarding, upstream destinations, and Compose service-name addressing.
What you can do after
Apply it via NGINX and Docker Compose to distribute requests across repeated application services.
How it connects
Connects reverse proxies, NGINX, Docker Compose, containers, service discovery, and Docker Swarm.

Also explained in

Compare how 13 books explain load balancing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.