Load balancer haproxy

 

  • Cài đặt

[root@loadbalancer ~]dnf update -y

[root@loadbalancer ~]dnf install -y haproxy

[root@loadbalancer ~]haproxy -v

  • Cấu hình:

Mở file:

[root@loadbalancer ~]nano /etc/haproxy/haproxy.cfg

#################################################

global 

    maxconn 50000

    log /dev/log local0 

    log /dev/log local1 notice

    user haproxy

    group haproxy 

    daemon 

defaults

    log     global

    mode    http

    option  httplog

    option  dontlognull

    timeout connect 5s

    timeout client  50s

    timeout server  50s

    timeout http-request 5s

    timeout http-keep-alive 20s

###############

# HTTP (80)

###############

frontend http-in

    # listen 80 port

    bind *:80

    # set default backend

    default_backend    backend_servers

    # send X-Forwarded-For header

    option             forwardfor

# define backend

backend backend_servers

    mode http

    # balance with roundrobin

    balance            roundrobin

    # define backend servers

    server             Web1 10.128.59.x:80 check

    server             Web2 10.128.59.x:80 check

#Tùy chọn để giám sát backend

listen stats

  bind 0.0.0.0:9000

  mode http

  stats enable

  stats uri /stats

  stats refresh 5s

  stats auth admin:pass_cua_ban

  • Kiểm tra cấu hình

[root@loadbalancer ~]haproxy -c -f /etc/haproxy/haproxy.cfg

Nếu thấy:

Configuration file is valid

→ Ok

  • Khởi động và enable HAProxy

[root@loadbalancer ~]systemctl start haproxy

[root@loadbalancer ~]systemctl enable haproxy

[root@loadbalancer ~]systemctl status haproxy

  • Mở port firewall:

<port protocol="tcp" port="80"/>

<port protocol="tcp" port="443"/>

<port protocol="tcp" port="9000"/>

Truy cập kiểm tra:  http://IP:9000/stats