用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

相关推荐

  • 上古神器vim

    概述:     在之前的篇章中,我们介绍了一些文本的处理工具,如grep、sed、cut、sort等,但是那些工具的侧重点都在于有了现成的文本之后,对现有的文本进行一定条件的加工处理后,满足我们进一步对数据的要求。但是目前为止,还没有接触一款专业用来编辑文本的工具,本篇就来介绍一下Linux系统上自带的专业用来文本编辑…

    Linux干货 2016-08-10
  • Linux运维学习历程-第三天-初识Linux

    初识Linux 本章内容    初安装Linus的网络配置    防火墙的关闭    用户    终端    shell    命令纪要 安装linux之后,linux默认网卡开机不是自动激活,并且防火墙开启的,这对初期我们学习linux会…

    Linux干货 2016-08-03
  • 无网不利

                    网络是主机之间交互的必备条件。联网就是将主机互联来形成网络,使得网络中的主机得以交换信息。       &n…

    2017-08-19
  • keepalived+varnish+haproxy+LNAMP

      nfs: 192.168.1.40 rp1: 192.168.1.41 rp2: 192.168.1.42 mariadb: 192.168.1.43 web1: 192.168.1.110 web2: 192.168.1.111 app1: 192.168.1.112 app2: 192.168.1.113 实验效果:用keepalived高…

    2018-02-18
  • week7

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; fdisk /dev/sdb  按提示创建出来10G分区 查看1:…

    Linux干货 2016-12-08
  • iptables实战笔记一

    iptables实战 1.开启防火墙 systemctl start firewalld 2.清空所有的默认规则,我们自己定义自己的规则 iptables -F 查看此时的iptables iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination Chain FOR…

    Linux干货 2017-06-13