122.225.32.134:
# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { lr@isadba.com } notification_email_from lvs@isadba.com 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 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 122.225.32.142 } } virtual_server 122.225.32.142 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 //此值为0,主要是为了方便测试,每次刷新页面结果会不一样 protocol TCP real_server 122.225.32.136 80{ weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 122.225.32.137 80{ weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }122.225.32.135:
和122.225.32.134相比,值需要删除state MASTER 和 修改priority的值小于master的。
real server:
在所有real server上添加下面脚本,名执行# cat /sbin/lvs_real
#!/bin/bash #description:start realserver vip=122.225.32.157 source /etc/rc.d/init.d/functions case $1 in start) echo "Start Realserver" /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up 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 ;; stop) echo "Stop Realserver" /sbin/ifconfig lo:0 down 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 ;; *) echo "Usage: $0 (start | stop)" exit 1 esac执行脚本以后,使用ip add命令查看lo接口是否添加了虚拟ip地址。
当然,real server上面需要安装http服务,并启动,在index.html里面添加自己的IP地址,方便客户端访问时候辨别访问的是哪个主机。
3、配置完成以后,开始测试:
在122.225.32.134和122.225.32.135上使用ip add检查,虚拟IP应该配置在134上面。
测试1{主要测试lvs}:访问虚拟IP是否可以访问真实服务器,是否轮询real server
在master上使用 ipvsadm命令查看lvs状态。
# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 122.225.32.142:http rr -> 122.225.32.137:http Route 1 0 0 -> 122.225.32.136:http Route 1 0 0