web1 IP 192.168.0.47
web2 IP 192.168.0.48
haproxy_master 192.168.0.200
haproxy_backup 192.168.0.199
VIP 192.168.0.155
一.安装keepalived
- #wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
- #tar zxvf keepalived-1.1.15.tar.gz
- #cd keepalived-1.1.15
- #./configure
- #make
- #make install 将keepalived做成启动脚务
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
配置keepalived.conf
#vim /etc/keepalived/keepalived.conf
- print?! Configuration File for keepalived
- global_defs {
- notification_email {
- gaoming@123.com
- }
- notification_email_from gaoming@123.com
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_instance VI_1 {
- state MASTER #备用服务器上改为 BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #备用服务器上改为99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.155/24 dev eth0 scope global
- }
- }
启动keepalived
service keepalived start
启动之后可以用ip add 查看网卡多了一条192.168.0.155的IP
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ff:6c:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.200/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.155/24 scope global secondary eth0
inet6 fe80::20c:29ff:feff:6cc7/64 scope link
valid_lft forever preferred_lft forever
说明keepalived启动成功,配置生效
二.安装haproxy
#wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
#uname -a
Linux ip1 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux
#make TARGET=linux26 prefix=/usr/local/haproxy install
#cd /usr/local/haproxy
配置haproxy
#vim haproxy.conf
- [php] view plaincopyprint?global
- maxconn 5120
- chroot /usr/local/haproxy
- uid 100
- gid 100
- daemon
- quiet
- nbproc 2
- pidfile /usr/local/haproxy/haproxy.pid
- ########################################################
- defaults
- log global
- log 127.0.0.1 local3
- mode http
- option httplog
- option dontlognull
- option redispatch
- retries 3
- maxconn 3000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- stats enable
- stats uri /admin
- stats auth admin:admin
- stats realm Haproxy \ statistic
- ##########################################################
- frontend www.gaoming.com
- bind *:80
- default_backend server_pool
- backend server_pool
- mode http
- balance roundrobin
- option forwardfor
- option httpclose
- option httpchk GET /index.html
- server web1 192.168.0.48:80 check inter 5000 fall 1 rise 2
- server web1 192.168.0.47:80 check inter 5000 fall 1 rise 2
haproxy的配置2台机器相同
启动haproxy
#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
域名解析到VIP上,用户访问VIP会负载到后端2台WEB上
测试高可用,关掉主haproxy,备用的会接管VIP