Step 1: Install HAProxy
- Install HAProxy by running the following command:
sudo apt install haproxy -y
- Run the haproxy service and add it to startup:
sudo systemctl enable --now haproxy
Step 2: Configure HAProxy
- Move the default configuration file:
sudo mv /etc/haproxy/haproxy.cfg{,.original}
- Create a new configuration file and open it for editing by running the following command:
sudo nano /etc/haproxy/haproxy.cfg
Example configuration for balancing the postgresql, rabbitmq, and web services:
global
maxconn 100000
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
mode tcp
log global
retries 2
timeout queue 5s
timeout connect 5s
timeout client 60m
timeout server 60m
timeout check 15s
listen stats
mode http
bind haproxy-server.your_domain:7000
stats enable
stats uri /
### Web ELMA365 ###
listen elma365_web
bind haproxy-server.your_domain:80
mode http
balance leastconn
no option http-use-htx
option forwardfor
option httpclose
option httpchk HEAD /
server elma365-1 elma365-1.your_domain:80 check
server elma365-2 elma365-2.your_domain:80 check
server elma365-3 elma365-3.your_domain:80 check
### Web ELMA365 ###
### PostgresQL ###
listen postgres_master
bind haproxy-server.your_domain:5000
option tcplog
option httpchk OPTIONS /master
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
server postgres-server-1 postgres-server-1.your_domain:5432 check port 8008
server postgres-server-2 postgres-server-2.your_domain:5432 check port 8008
server postgres-server-3 postgres-server-3.your_domain:5432 check port 8008
listen postgres_replicas
bind haproxy-server.your_domain:5001
option tcplog
option httpchk OPTIONS /replica
balance roundrobin
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
server postgres-server-1 postgres-server-1.your_domain:5432 check port 8008
server postgres-server-2 postgres-server-2.your_domain:5432 check port 8008
server postgres-server-3 postgres-server-3.your_domain:5432 check port 8008
listen postgres_replicas_sync
bind haproxy-server.your_domain:5002
option tcplog
option httpchk OPTIONS /sync
balance roundrobin
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
server postgres-server-1 postgres-server-1.your_domain:5432 check port 8008
server postgres-server-2 postgres-server-2.your_domain:5432 check port 8008
server postgres-server-3 postgres-server-3.your_domain:5432 check port 8008
listen postgres_replicas_async
bind haproxy-server.your_domain:5003
option tcplog
option httpchk OPTIONS /async
balance roundrobin
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
server postgres-server-1 postgres-server-1.your_domain:5432 check port 8008
server postgres-server-2 postgres-server-2.your_domain:5432 check port 8008
server postgres-server-3 postgres-server-3.your_domain:5432 check port 8008
### PostgresQL ###
### RabbitMQ ###
listen rabbitmq
bind haproxy-server.your_domain:5672
mode tcp
balance roundrobin
server rabbitmq-server1 rabbitmq-server1.your_domain:5672 check inter 2s rise 2 fall 3
server rabbitmq-server2 rabbitmq-server2.your_domain:5672 check inter 2s rise 2 fall 3
server rabbitmq-server3 rabbitmq-server3.your_domain:5672 check inter 2s rise 2 fall 3
listen rabbitmq_management
bind haproxy-server.your_domain:15672
balance source
server rabbitmq-server1 rabbitmq-server1.your_domain:15672 check check inter 2s
server rabbitmq-server2 rabbitmq-server2.your_domain:15672 check check inter 2s
server rabbitmq-server3 rabbitmq-server3.your_domain:15672 check check inter 2s
### RabbitMQ ###
|
- Restart haproxy:
sudo systemctl restart haproxy
Found a typo? Highlight the text, press ctrl + enter and notify us