[root@nagios ~]# ln -s /usr/src/kernels/2.6.18-308.13.1.el5-x86_64/ /usr/src/linux
10年积累的成都网站设计、成都做网站、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有新余免费网站建设让你可以放心的选择与我们合作。[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规则
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。