Git Product home page Git Product logo

nginx-load-balancing-docker's Introduction

Load Balancing with nginx

Load balancing involves effectively distributing incoming network traffic across a group of backend servers. A load balancer is tasked with distributing the load among the multiple backend servers that have been set up.

Here we will trying out nginx as a load balancer with default round robin algorithm.

TASK

  • Run three backend docker container and nginx container
  • Default Load balancing three docker container with Nginx
  • Weighted load balancing
  • Health check (Active health check is for Nginx plus, so using passive)

Structer

This diagram will give an overview about the system architecture

Diagram

Idea

We have created three backend server with FastAPI, the code is very simple. All server has one endpoint and return a json response with server name.

{
  "message": "Hello from server 01"
}

Setup & Run

Please make sure docker and docker compose is installed.

Run this command

docker-compose up --build

Now, goto http://localhost/ or http://localhost:80

Preview,

Response Preview

Nginx Configuration

We have created a nginx.conf file that distribute request to all this three servers.

Nginx config:

upstream api {
    server server-01:8001;
    server server-02:8002;
    server server-03:8003;
}

server {

    listen 80;

    location / {
        proxy_pass http://api;
    }

}

In this case main part focus point is,

upstream api {
    server server-01:8001;
    server server-02:8002;
    server server-03:8003;
}

Here by default its using round robin algorithm to distribute the traffic. This way each request made to http://localhost is served by those three servers in a round-robin fashion.

Weight distribution

upstream api {
    server server-01:8001 weight=5;
    server server-02:8002 weight=3;
    server server-03:8003;
}

By default weight for each server is 1. This involves assigning a weight to each server and then distributing traffic among the servers based on these weights. This ensures that servers with more capacity receive more traffic, that helps to prevent overloading of any one server.

For example, here we set the weight of the 1st server to 5, then out of 10 requests, 5 of them (not sequentially though) are redirected to the 1st server and the rest 5 will go to the remaining servers.

Health Check

upstream api {
    server server-01:8001;
    server server-02:8002 max_fails=5 fail_timeout=30s;
    server server-03:8003;
}

Load balancer are often used to distribute incoming traffic among servers. Health check is important part to ensure load balancer is routing traffic correctly, it will help to identify which server is down or not functioning properly.

Nginx active health checks are available for Nginx plus.

For passive health check, to define a server unavailable we need to define max_fails and failed_timeout

max_fails: sets the number of unsuccessful attempts to communicate server that happen in the durantion set in failed_timeout

failed_timeout: the time during the specified number of unsuccessful attempts to communicate consider as unavailable.

Also we can configure nginx with Least connection , Weighted least connection, IP Hash etc.

To check more configuration,

Resources:

nginx-load-balancing-docker's People

Contributors

frfahim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.