一、环境
5台虚拟机,分别是:
1台测试机(192.168.3.111);
2台DirectorServer(192.168.3.221、192.168.3.222);
2台RealServer(192.168.3.231、192.168.3.232);
注:VIP本实验设置为:192.168.3.220
二、安装配置RealServer
yum -y install httpd
chkconfig httpd on
echo ‘R1/R2‘ /var/www/html/index.html #两台的配置都一样,这步为了区别访问的是哪台的数据,所以分为R1与R2,生产机上一般是统一访问后端的数据层的数据;
service httpd start
#!/bin/bash SNS_VIP=192.168.3.220 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP > dev/null 2>&1 echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
复制以上内容丢到/etc/init.d/ 目录下,命名自定义,我是命名为:realserver.sh
chmod u+x /etc/init.d/realserver.sh
ln -s /etc/init.d/realserver.sh /etc/rc.d/rc3.d/S99realserver.sh #设置开机自启动
三、安装配置DirectorServer
yum -y install gcc kernel-devel openssl-devel lrzsz vim ipvsadm
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar xzvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/
make
make install
ln -s /usr/local/sbin/keepalived /sbin/
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
编辑keepalived.conf,内容大致如下:
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.3.220 } } virtual_server 192.168.3.220 80 { delay_loop 6 lb_algo wlc lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.3.231 80 { weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.3.232 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
注:第二台DS服务器(备)需要更改以上两处的内容,一是把MASTER更改为BACKUP,二是把优先级由100改为80即可;
四、校验及测试
4.1、ipvsadm -L -n #正常显示VIP及RIP等内容;
4.2、ip addr list #主服务器正常显示VIP,备服务器没有VIP;
4.3、把主服务器关机,看备服务器会不会把服务接管过来;