网络环境:
193.168.120.69 负载均衡
193.168.120.91 WEB服务器
193.168.120.60 WEB服务器
安装HAPROXY
yum install gcc -y
下载软件:
tar -zxvf haproxy-1.8.3.tar.gz
cd haproxy-1.8.3
[root@cos64 haproxy-1.8.3]# uname -a
Linux cos64 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
make TARGET=linux2632 PREFIX=/data/haproxy #此处linux2632根据上一步的命令查询得来,否则make报错
make install PREFIX=/data/haproxy
cp /data/haproxy/sbin/haproxy /usr/sbin/
cp ./examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
useradd -r haproxy
修改/etc/rsyslog.conf,加入
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
[root@cos64 haproxy-1.8.3]# tail -f /etc/rsyslog.conf
# A template to for higher precision timestamps + severity logging
$template SpiceTmpl,"%TIMESTAMP%.%TIMESTAMP:::date-subseconds% %syslogtag% %syslogseverity-text%:%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
:programname, startswith, "spice-vdagent" /var/log/spice-vdagent.log;SpiceTmpl
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
添加haproxy配置文件及其对应的目录
mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg
[root@cos64 ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3 info
chroot /data/haproxy
user haproxy
group haproxy
daemon
maxconn 4000
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server centos64 193.168.120.91:80 check inter 2000 rise 3 fall 3 weight 30
server MSLINUX 193.168.120.60:80 check inter 2000 rise 3 fall 3 weight 30
配置好两台WEB服务器
index.html对应haproxy.cfg文件中的option httpchk GET /index.html
[root@MSLINUX ~]# cat /var/www/html/index.html
60index.html
[root@centos64 html]# cat /var/www/html/index.html
91index.html
[root@centos64 html]#
启动负载均衡服务器的对应服务
service rsyslog restart
service haproxy start
测试访问:
刷新
状态:
分别访问两台WEB服务器:
配置完成,比较简单