Why is my haproxy load balancer not working?
I'm on centos 7.
I've set up 2 servers "nginx-node01" and "nginx-node02". As the name implies these 2 servers are of nginx. They're up and running.
The server's IP address being 192.168.169.133(hostname=nginx-node01) and 192.168.169.135(hostname=nginx-node02) respectively.
Then I've set up haproxy on another server.
It's IP address being 192.168.169.128(hostname=haproxy-centos8). (I'm on centos 7, ignore the hostname centos8)
When I do curl 192.168.169.128 in haproxy server, I get:
curl: (7) Failed connect to 192.168.169.128:80; Connection refused
This is what I've done so far:
- Configure the /etc/hosts files in haproxy server.
vi /etc/hosts and add the following lines there
192.168.169.128 haproxy-centos8
192.168.169.133 nginx1.loadbalancer.me nginx-node01
192.168.169.135 nginx2.loadbalancer.me nginx-node02
Save the file and exit the editor.
Go to hosts file on /etc/hosts on nginx-node01 and paste the following lines:
192.168.169.128 haproxy-centos8
192.168.169.133 nginx-node01
192.168.169.135 nginx-node02
Do the same for nginx-node02
Go to haproxy-centos8 server.
Edit haproxy.cfg file like this:
#---------------------------------------------
# Global settings
#---------------------------------------------
global
log 127.0.0.1 local2 #Log configuration
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy #Haproxy running under user and group "haproxy"
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------
listen haproxy3-monitoring *:8080 #Haproxy Monitoring run on port 8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats #URL for HAProxy monitoring
stats realm Haproxy\ Statistics
stats auth howtoforge:howtoforge #User and Password for login to the monitoring dashboard
stats admin if TRUE
default_backend app-main #This is optionally for monitoring backend
#---------------------------------------------
# FrontEnd Configuration
#---------------------------------------------
frontend main
bind *:80
option http-server-close
option forwardfor
default_backend app-main
#---------------------------------------------
# BackEnd roundrobin as balance algorithm
#---------------------------------------------
backend app-main
balance roundrobin #Balance algorithm
option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost #Check the server application is up and healty - 200 status code
server nginx-node01 192.168.169.133:80 check #Nginx1
server nginx-node02 192.168.169.135:80 check #Nginx2
Then configure rsyslog for HAProxy.
Edit this file
vi /etc/rsyslog.conf
Uncomment this line to enable the UDP connection:
$ModLoad imudp
$UDPServerRun 514
If you want to use a specific IP, you can add a new line like the one below:
$UDPServerAddress 127.0.0.1
Save the file and exit
Then create new haproxy configuration file for rsyslog:
cd /etc/rsyslog.d/
vi haproxy.conf
Paste configuration below:
local2.=info /var/log/haproxy-access.log #For Access Log
local2.notice /var/log/haproxy-info.log #For Service Info - Backend, loadbalancer
Then I did things like restarting rsyslog, starting haproxy, enabling haproxy etc. My nginx was already configured.