用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

相关推荐

  • 软件包管理

    软件包管理 软件运行和编译 ABI:应用程序二进制接口;直接给编译好的二进制使用; windows与linux不兼容, windows: ELF linux: PE 库级别的虚拟化: linux:WINE windows:Cywin API:定义了和库之间的接口;编写应用程序时调用的函数之类的; 编译程序源代码(.c) –> 预编译(.i)…

    Linux干货 2017-04-21
  • FTP部署及简单应用

    文件服务: ftp:应用层,C/S,文件共享;file transfer protocol; nfs,cifs:文件系统接口,网络文件系统;     nfs:network file system     cifs:common …

    Linux干货 2016-10-19
  • 马哥教育Net20第二十二天:在Centos7上实现lmnp

    要求: vhost1: pma.stuX.com, phpMyAdmin, 同时提供https服务; vhost2: wp.stuX.com, wordpress vhost3: dz.stuX.com, Discuz  环境说明: DNS是:192.168.100.7 vhos…

    Linux干货 2016-07-02
  • 源码包安装Apache服务

    1、安装development tools程序包组 安装Apache服务器前需要解决依赖性问题,比如需要安装GCC、GCC++、OPENSSL等等,因为是在虚拟机里做实验,这里就通过直接安装包组的方法解决依赖性问题。 (1)挂载安装光盘,并安装“Development tools”包组 [root@localhost ~]# mount&…

    Linux干货 2016-08-24
  • week4

    一,复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没 有任何访问权限; cp -r /etc/skel/ /home/tuser1 chmod -R go= /home/tuser1/ 二,编辑/etc/group文件,添加组hado…

    Linux干货 2016-11-18
  • linux基础中的基础 —- 用户管理、文本处理、正则表达式等命令的使用(博客第三周作业)

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。     who | cut -d' ' -f1 | uniq 2、取出最后登录到当前系统的用户的相关信息。     who | tail -1 3、取出当前系统上被用户当作其默认shell的最…

    Linux干货 2016-07-22