我对这种负载平衡场景还不熟悉,我的任务是弄清楚如何使这种负载平衡正常工作。
我的环境:
Centos 6.4 64 Bit
Webserver: Lighttpd
All running in ESXI
virtual IP: 192.168.1.6
LB1: 192.168.1.4
LB2: 192.168.1.5
Webserver 1: 192.168.1.12
Webserver 2: 192.168.1.13
Gateway: 192.168.1.1
尝试在生产之前使用 HAproxy 和 keepalived 在实验室中运行测试。以下是我在 keepalived 设置中的内容:
! keepalived 的配置文件
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.1.4
smtp_connect_timeout 30
router_id 192.168.1.1
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1 # check every second
weight 2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101 #priority 101 for master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.6
}
track_script {
chk_haproxy
}
}
以下是我对 HAproxy 的设置
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user 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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:80
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend view
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
mode tcp
balance roundrobin
server server1 192.168.1.12:80 check inter 2000 rise 2 fall 5
server server2 192.168.1.13:80 check inter 2000 rise 2 fall 5
当我启动 HAproxy 时,出现了这个错误,我不太清楚从哪里开始修复它。也许有人做过很多次这样的事,可以帮我解释一下?
503 Service Unavailable No server is available to handle this request.
但是手动连接到 webserver1 和 webserver2 就可以了。
我想要的只是对位于 HAproxy 后面的 Web 服务器进行简单的负载平衡。任何建议或意见都非常感谢。请帮忙?非常感谢。
答案1
我从未使用过 HAproxy,但快速搜索后我认为您需要default_backend app
立即添加以下内容frontend main *:80
。我在该配置中没有看到将后端和前端连接在一起的地方。
答案2
问题出在您的 HAProxy 配置中。当我从您的配置中删除所有注释时,我将得到以下信息:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
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
frontend main *:80
backend app
mode tcp
balance roundrobin
server server1 192.168.1.12:80 check inter 2000 rise 2 fall 5
server server2 192.168.1.13:80 check inter 2000 rise 2 fall 5
现在您可以清楚地看到根本没有前端配置。请求通过 HAProxy 到达,frontend main
但 HAProxy 不知道哪些服务器可靠地处理它,因此将返回 503。
您必须使用default_backend
acl 将后端链接到前端。
您也应该使用统计数据,不仅要使用套接字,还要使用受保护的 Web 界面。我可以向您显示有关 haproxy 后面的集群的信息,哪些服务器处于离线状态,哪些服务器存在问题,以及有关响应时间的信息等等。对于调试非常有用。