运行环境:
Keepalived
master:192.168.99.130
backup:192.168.99.131
vip:192.168.99.200
Apache
192.168.99.130:8080
192.168.99.131:8080
一、Keepalived安装与配置
创建软链接
ln -s /usr/src/kernels/2.6.18-308.13.1.el5-i686 /usr/src/linux
如果没有kernel内核开发包
yum -y install kernel-devel
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
./configure --prefix=/data/apps/keepalived1.2.2
make && make install
编辑Keepalived配置文件
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf
//========================MASTER=======================
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 nopreempt priority 100 advert_int 1 virtual_ipaddress { 192.168.99.200 } }
//========================BACKUP=======================
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 nopreempt priority 50 advert_int 1 virtual_ipaddress { 192.168.99.200 } }
加入系统服务
cp /data/apps/keepalived1.2.2/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /data/apps/keepalived1.2.2/etc/sysconfig/keepalived /etc/sysconfig/
cp /data/apps/keepalived1.2.2/sbin/keepalived /usr/sbin/
chkconfig --add keepalived
chkconfig keepalived on
service keepalived start
此时在MASTER上执行ip a可以看到
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:27:c6:4d brd ff:ff:ff:ff:ff:ff inet 192.168.99.130/24 brd 192.168.99.255 scope global eth0 inet 192.168.99.200/32 scope global eth0 inet6 fe80::20c:29ff:fe27:c64d/64 scope link valid_lft forever preferred_lft forever 3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0
附:如果通过killall停止keepalived的话,可能会出现vip依然绑定的情况,删除vip的方法:
ip addr del 192.168.99.200/24 dev eth0
二、Haproxy的安装与配置
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.13.tar.gz
tar -zxvf haproxy-1.4.13.tar.gz
cd haproxy-1.4.13
make TARGET=linux26 PREFIX=/data/apps/haproxy
make install PREFIX=/data/apps/haproxy
编辑Haproxy配置文件
vim /data/apps/haproxy/haproxy.conf
global maxconn 5120 chroot /data/apps/haproxy uid 502 gid 501 daemon quiet nbproc 2 defaults log global mode http option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen appli1 0.0.0.0:80 mode http option httplog option dontlognull log 127.0.0.1 local3 cookie SERVERID rewrite balance roundrobin option httpchk stats uri /stats stats auth admin:admin server app1_3 192.168.99.130:8080 cookie app1inst3 check inter 2000 rise 2 fall 5 server app1_4 192.168.99.131:8080 cookie app1inst4 check inter 2000 rise 2 fall 5
启动Haproxy
/data/apps/haproxy/sbin/haproxy -f /data/apps/haproxy/haproxy.conf
主从配置相同