[root@nagios ~]# ln -s /usr/src/kernels/2.6.18-308.13.1.el5-x86_64/ /usr/src/linux
[root@nagios ~]# yum install ipvsadm
[root@nagios ~]# tar -zxvf keepalived-1.2.7.tar.gz -C /usr/local
[root@nagios ~]# cd keepalived-1.2.7/
[root@nagios ~]# ./configure
[root@nagios ~]# make && make install
[root@nagios ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@nagios ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@nagios ~]# mkdir /etc/keepalived
[root@nagios ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@nagios ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@nagios ~]# service keepalived start
[root@nagios ~]# vim /etc/keepalived/keepalived.conf
修改配置文件内容如下
! Configuration File for keepalived
global_defs {
notification_email {
yhl5555@126.com
}
notification_email_from yhl5555@126.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.160
}
}
virtual_server fwmark 1 {
delay_loop 6
lb_algo wrr
lb_kind DR
# persistence_timeout 60
protocol TCP
real_server 172.16.1.171 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server fwmark 2 {
delay_loop 6
lb_algo wrr
lb_kind DR
# persistence_timeout 60
protocol TCP
real_server 172.16.1.172 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
在lvs调度器上面操作
注意:mangle表的匹配顺序是从下到上
[root@nagios ~]# iptables -t mangle -I PREROUTING -s 172.16.0.68 -p tcp -d 172.16.1.160 --dport 80 -j MARK --set-mark 1
[root@nagios ~]# iptables -t mangle -I PREROUTING -s 172.16.1.173 -p tcp -d 172.16.1.160 --dport 80 -j MARK --set-mark 2
分别在两台web服务器上执行realserver.sh绑定虚拟ip,脚本内容如下
#!/bin/bash
SNS_VIP=172.16.1.160
. /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
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
测试效果
1、当来源ip为172.16.0.68的主机访问目标地址172.16.1.160的80端口时,转向了keepalived定义的fwmark 1规则
2、当来源ip为172.16.1.173的主机访问目标地址172.16.1.160的80端口时,转向了keepalived定义的fwmark 1规则
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。