为什么我的 haproxy 负载均衡器不工作?
我在 centos 7 上。
我设置了 2 个服务器“nginx-node01”和“nginx-node02”。顾名思义,这两个服务器是 nginx 的。它们已启动并正在运行。
服务器的IP地址分别为192.168.169.133(hostname=nginx-node01)和192.168.169.135(hostname=nginx-node02)。
然后我在另一台服务器上设置了 haproxy。它的 IP 地址是 192.168.169.128(主机名=haproxy-centos8)。(我在 centos 7 上,忽略主机名 centos8)
当我在 haproxy 服务器中执行 curl 192.168.169.128 时,我得到:
curl: (7) Failed connect to 192.168.169.128:80; Connection refused
这是我目前所做的:
- 配置haproxy服务器中的/etc/hosts文件。
vi /etc/hosts 并在其中添加以下几行
192.168.169.128 haproxy-centos8
192.168.169.133 nginx1.loadbalancer.me nginx-node01
192.168.169.135 nginx2.loadbalancer.me nginx-node02
保存文件并退出编辑器。
转到 nginx-node01 上的 /etc/hosts 上的 hosts 文件并粘贴以下行:
192.168.169.128 haproxy-centos8 192.168.169.133 nginx-node01 192.168.169.135 nginx-node02
对 nginx-node02 执行相同操作
进入 haproxy-centos8 服务器。编辑 haproxy.cfg 文件如下:
#--------------------------------------------------------------------- # 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
然后为 HAProxy 配置 rsyslog。
编辑此文件
vi /etc/rsyslog.conf
取消注释此行以启用 UDP 连接:
$ModLoad imudp
$UDPServerRun 514
如果您想使用特定的 IP,您可以添加如下所示的新行:
$UDPServerAddress 127.0.0.1
保存文件并退出
然后为 rsyslog 创建新的 haproxy 配置文件:
cd /etc/rsyslog.d/ vi haproxy.conf
粘贴以下配置:
local2.=info /var/log/haproxy-access.log #For Access Log
local2.notice /var/log/haproxy-info.log #For Service Info - Backend, loadbalancer
然后我做了一些事情,比如重新启动 rsyslog、启动 haproxy、启用 haproxy 等。我的 nginx 已经配置好了。