Monday, February 1, 2021

Load Balancing with NGINX

 Load Balancing with NGINX

Server information
IP        Host
192.168.56.5    master.expanor.local - load Balancer
192.168.56.6    worker1.expanor.local
192.168.56.7    worker2.expanor.local


Vagrant - go to vagrantup.com
- Easy to create and destroy virtual machines
- Supports VirtualBox by default
- Can be configured to support
  - VMWare
  - Docker
  - AWS
  - and much more ...


We will have
clients -------> Load Balancer(1) ----------> Web servers (2)

clients connecting to load balancer and traffic is distributed to the web servers (worker1/2)
if one of the webserver is not available, traffic will be pass to another web server.

NGINX load balancing options
* Round-Robin -> load is distributed in round robin fashaion. First request go to first server and second goes to second one and so on. This is a default behavior.
  - Default

* Least-connected -> traffic goes to the server with least number of connection. use least_conn on config file.
  - least_conn;

* ip-hash - Uses hash function to which server to select based on the client IP address. Use for sticky sessions mean that certain user session (the traffic) is always routed to the same server. You use ip_has on config for this feature.

  - Use for sticky sessions
  - ip_hash;

Lets install web server such as apache, nginx

# yum install epel-release -y
# yum install nginx -y

# netstat -tulnp | grep 80

# uname -n | tee  

# mkdir /var/www/expanor.local/public_html
# vi index.html
<html">
  <head>
     <title>Welcome to the club !!!</title>
  </head>
  <body>
    <h1>Cool! Welcome to my home page!!</h1>
  </body>
</html>


# chown -R nginx: /var/www/expanor.local/public_html


Create config file
# vi /etc/nginx/conf.d/default.conf - default one...  if you want to change, go to root and change to
root     /mydefault_DocRoot

or
# vi /etc/nginx/conf.d/expanor.local.conf

server {
    listen 80;
    listen [::]:80;

    root /var/www/expanor.local/public_html;

    index index.html;

    server_name expanor.local www.example.local;

    access_log /var/log/nginx/expanor.local.access.log;
    error_log /var/log/nginx/expanor.local.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}


test
# nginx -t

Start web server
# systemctl start nginx

# curl localhost
You will see the content of default page.


Now, repeat same steps to your worker node2 as well.

- install nginx
- create default page


Now, go back to master which is going to be load balancer.
Install nginx here as well
# yum install -y nginx

edit configuration file
# vi /etc/nginx/conf.d/default.conf

go all the way down and add the following information

upstream web backend {
    # ip_hash;    If you want sticky feature.
    192.168.56.6
    192.168.56.7
}

server {
    listen 80;

# pass all the traffic on the loadbalancer.
    location / {
        proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
        proxy_pass http://web_backend;
    }
}



# systemctl restart nginx

Now, test the connection
$ curl localhost

keep doing it,  you will see the change.




This guide is prepared from the training on udemy
linux traninig academy


No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...