首先安装HAProxy
注:负载均衡建议使用多台服务器,达到更高的性能。
Centos使用
yum install haproxy -y
Debian使用
vi /etc/apt/sources.list
添加如下内容
deb http://ftp.us.debian.org/debian/ wheezy-backports main
然后
apt-get update apt-get install haproxy
接下来设置配置文件
vi /etc/haproxy/haproxy.cfg
清空配置文件后,输入如下内容
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /var/haproxy
uid 99
gid 99
daemon
nbproc 1
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch # 自动重定向到健康机器
maxconn 200000
timeout check 2000ms # 检查超时
timeout connect 5000ms # 连接超时
timeout client 50000ms # 客户端连接超时
timeout server 50000ms # 服务端连接超时
#配置客户端
listen webinfo :80
mode http
balance roundrobin
stats uri /ha_status
option httpclose
option forwardfor
server web1 127.0.0.1:810 check weight 1 minconn 1 maxconn 200 check inter 40000 #其中一台web服务器
server web2 127.0.0.1:811 check weight 1 minconn 1 maxconn 200 check inter 40000 #其中一台web服务器
server web4 127.0.0.1:812 check weight 1 minconn 1 maxconn 200 check inter 40000 #其中一台web服务器
#配置控制台
listen stats :6553
mode http
transparent
stats uri / haproxy-stats
stats realm Haproxy \ statistic
stats auth admin:admin