用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

(0)
nenenene
上一篇 2017-10-14 21:44
下一篇 2017-10-15

相关推荐

  • 苦上半年时间

    这是一种享受

    Linux干货 2016-08-08
  • sed简介及常见用法

    sed是一个文本流处理器,配合正则表达式用可以实现很多文本处理操作。 和grep一样,sed是一行一行的处理的。sed处理文本时,首先会将源文件复制一份到内存中,然后将文本一行一行拿到模式空间内进行操作,最后输出到标准输出,即屏幕上。 在模式空间中,每一行都会根据用户给的条件进行匹配,匹配到了进行编辑后输出,没有匹配到,直接输出到标准输出。sed除了模式空间…

    2018-01-04
  • 优云老王(四)干货总结:数据带来的奇妙世界

    作为产品经理的我们,不仅仅需要业务敏感的直觉,还需要数据驱动我们的产品不断创新!关于如何看数据,我列了主要的四个场景。 场景一:看页面点击,了解用户关注什么,卡在哪里 给谁看:产品经理和交互设计师 看什么: 由于我们对Web应用的所有界面,可交互元素都做了埋点,那么我们就可以看用户在页面里都做了哪些点击动作。首先是整个页面的横向比较,每个页面本身的操作数(重…

    2016-09-19
  • 浅谈技术管理(转载,讲的非常不错,技术和产品都值得一看)

      针对这些年旁观和经历过的技术产品场景,做一些个人的总结和判定,尽量不涉及争议性话题,比如对一个互联网公司而言,技术重要还是产品重要之类的,这种话题一扯开,各有道理,谁也别指望说服谁。     此外,加一个前缀,主要针对非技术领导者所面临的技术管理困境,在很多从传统企业转型或个人站转型的互联网企业里,这个问…

    Linux干货 2015-04-04
  • Python

    Python中os和shutil模块
    csv文件和ini文件简介

    Linux干货 2017-10-30
  • 萝卜运维记–第一周

    part1— ①描述计算机的组成及功能 ▲计算机是什么? 计算机(ENIAC):简述—-接收用户的指令,经过中央处理器处理过后,产生对使用者有用的信息。具有存储记忆,处理复杂数据运算的计算机器 Ⅰ 计算机组成及功能 ☆硬件部分: 1.中央处理器CPU(Central Processing Unit)↓↓↓↓:  &n…

    Linux干货 2016-12-06