haproxy的安装(主备都一样):
#tar -zxvf haproxy-1.5.14.tar.gz
#cd haproxy-1.5.14
#make TARGET=linux26 PREFIX=/usr/local/haproxy
#makeinstall PREFIX=/usr/local/haproxy
#cd/usr/local/haproxy/
#mkdir conf
#mkdir logs
#vi haproxy.cfg
global
log127.0.0.1 local0
log127.0.0.1 local1 notice
maxconn 4096
chroot/usr/local/haproxy
uid 99
gid 99
daemon
#debug
#quiet
nbproc 1
pidfile/usr/local/haproxy/logs/haproxy.pid
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 65535
optionredispatch
timeout connect5000
timeout client50000
timeout server50000
stats uri /haproxy
stats auth admin:admin
listen *:80
cookie SERVERID rewrite
mode http
balance roundrobin
option httpchkHEAD /index.html HTTP/1.0
server web1 10.2.32.99:80 cookie app1inst1 weight 5check inter 2000 rise 2 fall 5
server web2 10.2.32.110:80 cookie app1inst2 weight 3check inter 2000 rise 2 fall 5
二:先主后从,两台机器上都分别启动:
/etc/init.d/keepalivedstart (如果之前没有启动haproxy,这条命令会自动把haproxy启动)
[root@test100 haproxy]# ps -ef | grephaprox
root 30344 1 0 11:43 ? 00:00:00 /bin/bash/etc/keepalived/check_haproxy.sh
nobody 30349 1 0 11:43 ? 00:00:00/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
[root@test100 haproxy]# ps -ef | grepkeepali
root 30337 1 0 11:43 ? 00:00:00 keepalived -D
root 30339 30337 1 11:43 ? 00:00:00 keepalived -D
root 30340 30337 1 11:43 ? 00:00:00 keepalived -D
root 30372 2876 0 11:43 pts/0 00:00:00 grep keepali
三:测试:
1.再两台HA上分别执行ip ad sh |grep 10.2.32
主:
inet10.2.32.100/24 brd 10.2.32.255 scope global eth0
inet10.2.32.201/32 scope global eth0
备:
inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
2.停掉主上的haproxy,3秒后keepalived会自动将其再次启动
3.停掉主的keepalived,备机马上接管服务
备: ip ad sh |grep 10.2.32
inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
inet10.2.32.201/32 scope global eth0
四:通过浏览器进行测试
两台web的页面会在你刷新时进行切换。此文将nginx安装,配置过程省略了,如果有问题可以查看以下相关文章。
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
CentOS 6.3下配置Nginx加载ngx_pagespeed模块
CentOS 6.4安装配置Nginx+Pcre+php-fpm
Haproxy+Keepalived搭建Weblogic高可用负载均衡集群
CentOS 6.3下Haproxy+Keepalived+Apache配置笔记