Nginx Load Balancer

In the following example I will have a load balancing setup with 3 kubernetes nodes which has Traefik running on port 80 and port 443

Install

1
2
apt update
apt install -y nginx

Configure

Update the nginx configuration file

/etc/nginx/nginx.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
  worker_connections 768;
}

http {
  upstream node_servers {
    server 10.10.1.10;
    server 10.10.1.11;
    server 10.10.1.12;
  }

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

    server_name _;

    proxy_http_version 1.1;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded_Host $host;
    proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    location / {
      proxy_pass http://node_servers;
    }
  }
}

include /etc/nginx/passthrough.conf;

/etc/nginx/passthrough.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
stream {
  upstream https_servers {
    server 10.10.1.10:443 max_fails=3 fail_timeout=30s;
    server 10.10.1.11:443 max_fails=3 fail_timeout=30s;
    server 10.10.1.12:443 max_fails=3 fail_timeout=30s;
  }

  server {
    listen 443;

    proxy_pass https_servers;
    proxy_next_upstream on;
    ssl_preread on;
  }
}
All rights reserved