Configuring HAProxy and Keepalived - HTTP
Install HAProxy and Keepalived on both ubuntu nodes.
|
apt-get install haproxy
apt-get install keepalived
|
Load balancing in HAProxy also requires the ability to bind to an IP
address that are nonlocal, meaning that it is not assigned to a device
on the local system. Below configuration is added so that
floating/shared IP can be assigned to one of the load balancers. Below
line get it done.
|
net.ipv4.ip_nonlocal_bind=1
|
Now you know what’s happening with above lines let’s edit the
sysctl.conf.
Add the below lines.
|
net.ipv4.ip_nonlocal_bind=1
|
To enable the changes made in
sysctl.conf you will need to run the command.
root@S-09:~# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
vi /etc/defaults/haproxy
CONFIG="/etc/haproxy/haproxy.cfg" ENABLED=1
|
Now let’s create
keepalived.conf file on each instances. All the below Keepalived configurations are explained in the
User guide provided by Keepalived and refer
this guide as well.
|
vim /etc/keepalived/keepalived.conf
|
Add the below configuration on the master node
global_defs {
# Keepalived process identifier
lvs_id haproxy_DH
}
# Script used to check if HAProxy is running
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}
# Virtual interface
# The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
# The virtual ip address shared between the two loadbalancers
virtual_ipaddress {
192.168.6.164
}
track_script {
check_haproxy
}
}
|
Add the below configuration on the slave node.
global_defs {
# Keepalived process identifier
lvs_id haproxy_DH_passive
}
# Script used to check if HAProxy is running
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}
# Virtual interface
# The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
state SLAVE
interface eth0
virtual_router_id 51
priority 100
# The virtual ip address shared between the two loadbalancers
virtual_ipaddress {
192.168.6.164
}
track_script {
check_haproxy
}
}
|
-
Restart Keepalived.
Now let’s configure HAProxy on both instances. You will have do the below steps on master node as well as slave node.
set the property
ENABLED to
1.
|
vim /etc/haproxy/haproxy.cfg
|
Note that below configurations are as per my requirement.
- global
- log /dev/log local0
- log /dev/log local1 notice
- chroot /var/lib/haproxy
- stats socket /run/haproxy/admin.sock mode 660 level admin
- stats timeout 30s
- user haproxy
- group haproxy
- daemon
- # Default SSL material locations
- ca-base /etc/ssl/certs
- crt-base /etc/ssl/private
- # Default ciphers to use on SSL-enabled listening sockets.
- # For more information, see ciphers(1SSL).
- ssl-default-bind-ciphers kEECDH+aRSA+AES:kRSA+AES:+AES256:RC4-SHA:!kEDH:!LOW:!EXP:!MD5:!aNULL:!eNULL
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- timeout connect 5000
- timeout client 50000
- timeout server 50000
- errorfile 400 /etc/haproxy/errors/400.http
- errorfile 403 /etc/haproxy/errors/403.http
- errorfile 408 /etc/haproxy/errors/408.http
- errorfile 500 /etc/haproxy/errors/500.http
- errorfile 502 /etc/haproxy/errors/502.http
- errorfile 503 /etc/haproxy/errors/503.http
- errorfile 504 /etc/haproxy/errors/504.http
- frontend haproxy_in
- bind *:80
- # mode http
- default_backend haproxy_out_http
- backend haproxy_out_http
- mode http
- balance roundrobin
- #option forwardfor
- #http-request set-header X-Forwarded-Port %[dst_port]
- #http-request add-header X-Forwarded-Proto https if { ssl_fc }
- #option httpchk HEAD / HTTP/1.1\r\nHost:localhost
- #cookie SERVERID insert indirect nocache
- server web01 212.77.98.9:80 check #cookie s1
- server web02 213.180.141.140:80 check #cookie s1
- listen stats *:8989
- stats enable
- stats uri /
- stats hide-version
- stats auth admin:admin
-
- # jeśli potrzebujesz proxy dla smtp
listen smtp :25
mode tcp
option tcplog
balance roundrobin
server smtp 192.168.0.1:25 check
server smtp1 192.168.0.2:25 check
# jeśli potrzebujesz połączenie do RDP
listen rdp :3389
mode tcp
option tcplog
balance rdp-cookie
server sx01 10.1.1.11:3389 check
server sx02 10.1.1.12:3389 check
|
If above configurations are not clear please refer
this guide from HAProxy.
Testing
Let’s check whether our configuration has been done correctly. Try executing
ip addr on master node. As you can see on the master node
192.168.6.164 IP is assigned.
1
2
3
4
5
6
7
8
9
10
11
12
|
root@S-08:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bb:05:06 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.169/28 brd 192.168.6.175 scope global eth0
inet 192.168.6.164/32 scope global eth0
inet6 fe80::250:56ff:febb:506/64 scope link
valid_lft forever preferred_lft forever
|
Try executing
ip addr on slave node.
|
root@S-09:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bb:05:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.170/28 brd 192.168.6.175 scope global eth0
inet6 fe80::250:56ff:febb:508/64 scope link
valid_lft forever preferred_lft forever
|
Let stop the HAProxy on the master node. Now you can see that IP address
192.168.6.164 will be assigned to the salve node. If you start HAproxy instance on the master node IP address
192.168.6.164 will be assigned back to master node. See the O/P below.
|
root@S-08:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bb:05:06 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.169/28 brd 192.168.6.175 scope global eth0
inet6 fe80::250:56ff:febb:506/64 scope link
valid_lft forever preferred_lft forever
|
1
2
3
4
5
6
7
8
9
10
11
12
|
root@S-09:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bb:05:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.170/28 brd 192.168.6.175 scope global eth0
inet 192.168.6.164/32 scope global eth0
inet6 fe80::250:56ff:febb:508/64 scope link
valid_lft forever preferred_lft forever
|
Try http://192.168.6.169:8989/stats or http://192.168.6.170:8989/stats URL to login into statistics report for HAProxy.
Statistics Report for HAProxy
If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).