用keepalived高可用LVS-dr模型

环境四个虚拟机

Real server:node1:172.16.100.6         CeotOS 6

Real server:node3:172.16.100.69         CeotOS 7

Director1:172.16.100.67                        CeotOS 7

Director1:172.16.100.68                       CeotOS 7

Node1

# rpm –q httpd

# echo “<h1>RS1 CentOS 6</h1>” > /var/www/html/index.html

Node3

# rpm –q httpd

# echo “<h1>RS1 CentOS 7</h1>” > /var/www/html/index.html

在浏览器中测试一下

D1

# yum install ipvsadm –y

# ip addr add 172.16.100.88/32 dev ens33      配置vip

# ip addr list

用别的主机ping 172.16.100.88

Node1

# vim set.sh

#!/bin/bash

case $1 in

start)

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

echo 2 > /proc/sys/net/ipv4/conf/all/arp_ignore

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

;;

stop)

echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore

echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore

echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore

echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore

;;

esac

# bash set.sh

# cat /proc/sys/net/ipv4/conf/all/arp_ignore

1

# ls

# scp set.sh 172.16.100.69:/root/

Node 3

# bash set.sh

# cat /proc/sys/net/ipv4/conf/all/arp_ignore

1

Node1和Node3

# ifconfig lo:0 172.16.100.88 netmask 255.255.255.255 broadcast 172.16.100.88 up

# route add –host 172.16.100.88 dev lo:0

D1

# ipvsadm –A –t 172.16.100.88 –s rr

# ipvaadm –a –t 172.16.100.88 –r 172.16.100.6 –g –w 1

# ipvaadm –a –t 172.16.100.88 –r 172.16.100.69 –g –w 2

# ipvsadm –L –n

用别的客户端 curl http://172.16.100.88

# ipvsadm –L –n

# ipvsadm –C 清空规则

# ip addr del 172.16.100.88/32 dev ens33

# ip addr list

准备第2个director(172.16.100.68)

# yum install ipvsadm –y

# ip addr add 172.16.100.88/32 dev ens33

# ipvsadm –A –t 172.16.100.88:80 –s wrr

# ipvaadm –a –t 172.16.100.88:80 –r 172.16.100.6 –g –w 1

# ipvaadm –a –t 172.16.100.88:80 –r 172.16.100.69 –g –w 2

# ipvsadm –L –n

用别的客户端 curl http://172.16.100.88

# ipvsadm –C 清空规则

# ip addr del 172.16.100.88/32 dev ens33

# ip addr list

让两个director拥有sorry server (D1和D2)

#yum install httpd –y

#echo “<h1>sorry, under maintenance(68)</h1>” > /var/www/html/index.html

#systemctl start httpd.server

# yum install keepalived -y

# cd /etc/keepalived

# cp keepalived.conf {,.bakeup}

# vim keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

root@localhost

}

notification_email_from keepalive@localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

vrrp_mcast_group4 224.0.0.18

}

 

            vrrp_script chk_mt {

script “[[ -f /etc/keepalived/down ]] && exit 1 || exit 0”

interval 1

weight -2

}

 

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.100.88/16 dev ens33 label ens33:1

}

track_script {

chk_mt

}

notify_master “/etc/keepalived/notify.sh master”

notify_backup “/etc/keepalived/notify.sh backup”

notify_fault “/etc/keepalived/notify.sh fault”

}

virtual_server 172.16.100.88 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.0.0

protocol TCP

real_server 172.16.100.6 80 {

weight 1

HTTP_GET {

url {

path /

status_code 200

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 172.16.100.69 80 {

weight 2

HTTP_GET {

url {

path /

status_code 200

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

# scp keepalived.conf 172.16.100.68:/etc/keepalived/

# vim notify

#!/bin/bash

#

 

vip=172.16.100.88

contact=’root@localhost’ 联系人

 

notify() {

mailsubject=”`hostname` to be $1: $vip floating”    邮件主题

mailbody=”`date ‘+%F %H:%M:%S’`: vrrp transition, `hostname` changed to be $1″    邮件主体部分

echo $mailbody | mail -s “$mailsubject” $contact

}

 

case “$1” in

master)

notify master

exit 0

;;

backup)

notify backup

exit 0

;;

fault)

notify fault

exit 0

;;

*)

echo ‘Usage: `basename $0` {master|backup|fault}’

exit 1

;;

esac

# chmod +x notify.sh

# scp notify.sh 172.16.100.68:/etc/keepalived/

# systemctl start keepalived.service

D2

# vim keepalived.conf

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 51

priority 99

advert_int 1

vrrp_mcast_group4 224.0.0.18

authentication {

auth_type PASS

auth_pass 1111

}

}

# systemctl start keepalived.service

D1

# tcpdump –i ens33 –nn host 172.16.100.67

# ip addr list

# ipvsadm –L –n

# mail

用浏览器访问172.16.100.88

D1和D2

# vim keepalived.conf(定义sorry server)

virtual_server 172.16.100.88 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.0.0

protocol TCP

sorry_server 127.0.0.1 80

让D1挂了,看D2能否运行

D1 # touch down

# ip addr list

D2 # ip addr list

D1

# systemctl statu keepalived.service   日志功能

原创文章,作者:nene,如若转载,请注明出处:http://www.178linux.com/88149