1.Haproxy下载地址:
http://www.haproxy.org/ 截止目前最新版本为1.5.12
2、准备安装环境:
两个前端服务器安装HA和keepalived:
192.168.10.204 主机名:node4.a.com
192.168.10.206 主机名:node6.a.com
两个后端tomcat服务器:
192.168.10.209 主机名:node9.a.com
192.168.10.210 主机名:node10.a.com
解决依赖关系:
yum install openssl
3、安装haproxyt:
解压安装包:
[root@node6 haproxy]#tar xvf haproxy-1.5.12.tar.gz
进入到解压路径:
[root@node6 haproxy]#cd haproxy-1.5.12
指定编译内核版本和安装路径:
[root@node6 haproxy]#make TARGET=linux26 PREFIX=/opt/haproxy
复制配置文件到安装路径下:
[root@node6 haproxy]#cp examples/haproxy.cfg /opt/haproxy/
4、编辑配置文件:
node6主机:
[root@node6 ~]# vim /opt/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /opt/haproxy
pidfile /opt/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
optionhttp-server-close
option redispatch
retries 3
timeouthttp-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeouthttp-keep-alive 10s
timeout check 10s
maxconn 3000
mode http
option httplog
log global
balance roundrobin
server 209 node9.a.com:8080 cookie node9
server 210 node10.a.com:8080 cookie node10
node4主机:
global
log 127.0.0.1 local2
chroot /opt/haproxy
pidfile /opt/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
optionhttp-server-close
option redispatch
retries 3
timeouthttp-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeouthttp-keep-alive 10s
timeout check 10s
maxconn 3000
mode http
option httplog
log global
balance roundrobin
server 209 node9.a.com:8080 cookie node9
server 210 node10.a.com:8080 cookie node10
启动服务:
[root@node6 haproxy]#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
5.验证是否启动成功:
node6主机:
node4主机:
6、在客户端使用浏览器访问网页是否会被评价调用至两台后端服务器:
首次访问:
刷新网页:
7、将keepalived级别较高的Master服务器的keepalived停止,查看IP是否会自动转移到另一台服务器,并查看网页是否可以继续访问:
关闭Master的keepalived:
在Backup服务器查看IP信息:
在客户端访问网页是否在正常并自动切换:
网页依然可以正常访问,不受Mster服务器Keepalived关闭的影响。
由此可见,已经实现了负载均衡(Haproxy)和高可用(Keepalived)的效果。