[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规则
网页标题:lvs+frmark
标题来源:http://lswzjz.com/article/jhsdig.html