用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

相关推荐

  • haproxy 动静分离负载均衡、​stats页面实现​。

    实验环境:一台主机提供haproxy、nfs、mariadb,后端2台apache部署wordpress。 实验目的:haproxy使得动静分离、以及开启stats页面。 haproxy简单介绍 负载均衡的解决方案,支持4、7层,特点是单进程模型(可配置为多进程模型)单进程能支持非常大的并发链接数量(相比较其他软件)。 到今天,马哥课程中的负载均衡方案(lv…

    Linux干货 2017-02-18
  • 系统启动流程

    CentOS 5和6的启动流程服务管理Grub管理自制Linux启动排错编译安装内核 系统启动流程:  POST –> 读取BootSequence (BIOS),决定引导次序 –>读取引导设备的Bootloader(MBR grubstage1–>stage1.5/boot…

    Linux干货 2016-09-13
  • 使用ext_skel和phpize构建php5扩展

    首先声明:我们要构建的是扩展或者模块名为hello_module.该模块提供一个方法:hello_word. 1、php环境的搭建 我们一般使用源码包编译安装,而不是binary包安装。因为使用PHP的二进制分发包安装有些冒险,这些版本倾向于忽略./configure的两个重要选项,它们在开发过程中很便利: 第一个–enable-debug。这个…

    Linux干货 2015-05-28
  • Linux磁盘和文件系统管理进阶(swap、磁盘配额、RAID、LVM、btrfs)

    概述:     上篇已经介绍了一些磁盘和文件系统管理的基础概念,对磁盘的硬件构造,文件系统的基础概念部分有了一个大致的了解。本篇就分享一些关于磁盘和文件系统管理的一些高级应用,具体包含:     1、swap交换分区的管理     2、挂载…

    Linux干货 2016-08-30
  • 深入了解磁盘结构

    一、     linux中常见的磁盘:   无论什么操作系统,归根还是要落实与磁盘上的,对于磁盘的管理也是linux管理必备的一项技能。在linux中“一切皆文件”的思想贯彻整个linux的学习中,包括像是磁盘等的硬件也是在linux的/dev/目录下类似于文件形式的存放,常见的SATA口USB优盘或是SCS…

    Linux干货 2016-08-29
  • Linux实验-搭建路由环境

    Linux实验-搭建路由环境 背景: 在学习网络基础过程中,为了加强理解路由功能,准备用Linux模拟一个路由环境。同时也试试Linux的路由转发功能。 实验简介: 准备5个虚拟机,其中三个做路由,两个做客户机,相连的设备之间在同一个网段,三个路由设备在中间,两个客户机在两侧,最后实现两个虚拟机能相互通信。 实验规划: 这里配的IP都是随意配的,保证相连设备…

    2017-08-19