您的位置:首页 > 运维架构 > 网站架构

轻量级keepalive实现高可用和热备系列一之WEB服务的简单高可用

2017-11-14 10:02 507 查看
一、keepalive软件概述:

那么什么是keepalived呢?keepalived见名知意,那就是保持存活,那么在网络术语中就是保持在线,这就是所谓的高可用或热备,用来防止单点故障的发生。那说到keepalived时不得不说的一个协议就是VRRP协议,可以说这个协议就是keepalived实现的基础,那么首先我们来看看VRRP协议;

vrrp协议概述:

随着Internet的不断发展,人们对于网路可靠性的要求越来越高,特别是针对于依赖于网络办公的终端用户,能够实时与网路其他部分保持联系是非常重要的。由于公网IP资源的稀少,我们不得不依靠其他的网路技术来实现上网功能;下面这幅图就是一个典型的网路拓扑:



主机A、B、C通过网关联系互联网,以实现上网通信的目的,但是此时我们不得不考虑一个问题,那就是当我们只拥有一个网关设备时,这个网关就是在整个的网络中的一个关键点;我们不得不去考虑当网关宕机的时候该如何去处理;此时另外一种技术应运而生:那就是VRRP。

VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)将可用承担网关功能的一组路由器加入到备份组中形成有一台虚拟路由器,由VRRP的选举机制决定哪台路由器承担转发任务,局域网内的主机只需要将虚拟路由器配置为网关即可实现正常的上网功能,并且在物理网关中的某台或某几台设备宕机之后也不影响客户端的使用。拓扑如下:


我们使用多台的路由设备,将其组成一台虚拟路由器,此时客户端只需要知道虚拟路由的地址配置网关即可,不仅实现了网络的高可用,同时也减少了网络管理的繁琐工作;

VRRP备份组简介:

  VRRP将局域网内的一组路由器划分在一起,称为一个备份组。备份组由一个master路由器和多个backup路由器组成。Master由选举产生,优先级高的成为master,优先级的取值范围在0到255之间,可配置的范围在1到254之间。

  在同一个物理设备上可以配置多个组,组与组之间依靠组ID进行区分;同组之间的backup路由器有两种工作模式:

  非抢占方式:依靠各路由器的优先级进行自主选择,选择完成后如非路由故障,否则即使其他路由器的优先级被提升也不会成为master。

  抢占方式:备份路由器如果工作在抢占方式下,那么一旦路由的优先级发生变动,那么master角色就会根据优先级重新选举产生;

  那么VRRP是如何知道优先级变化呢?

  VRRP自身会维护两种定时器:VRRP通告报文间隔时间定时器和VRRP抢占延时时间定时器;VRRP会在固定的时间内向外发送VRRP通告报文,当backup路由器在固定时间内没有收到报文,并且间隔三次时间之后依然没有收到报文,那么backup路由器就会认定自己是master路由器,并向外通告进行重新选举;

  由于报文需要在网络中进行流转发送,VRRP提供了三种认证方式:

  无认证:不进行任何的VRRP报文的合法性认证,不提供安全性保障;

  简单字符认证:发送VRRP报文的路由器将认证字填入到报文中,而收到报文的路由器会将报文中的认证字和本地配置的认证字进行对比,从而实现认证安全保障;www.it165.net

  MD5认证:发送报文的路由器利用认证字和MD5算法对VRRP报文进行加密,加密后的报文保存在Authentication Header中,收到报文的路由器利用认证字解密报文,检查该报文的合法性。

基于这些技术,keepalive实现了高可用。

三、keepalive安装配置

1、安装准备:

keepalived、lm_sensors、httpd软件包

两台主机:

node1:172.16.12.8

node2:172.16.12.9

虚拟IP:172.16.12.254

2、配置:

master配置:

view
sourceprint?

01.
#
cd /etc/keepalived/

02.
#
vim keepalived.conf

03.
!
Configuration File
for
keepalived

04.
global_defs
{

05.
notification_email
{

06.
wangej@126.com

07.
}

08.
notification_email_from
kanotify@magedu.com

09.
smtp_connect_timeout
3

10.
smtp_server
127.0.0.1

11.
router_id
LVS_DEVEL

12.
}

13.
vrrp_script
chk_httpd {

14.
script
"killall
-0 httpd"

15.
interval
2

16.
#
check every 2 seconds

17.
weight
-2

18.
#
if failed, decrease 2 of the priority

19.
fall
2

20.
#
require 2 failures for failures

21.
rise
1

22.
#
require 1 sucesses for ok

23.
}

24.
vrrp_script
chk_schedown {

25.
script
"[[
-f /etc/keepalived/down ]] && exit 1 || exit 0"

26.
interval
2

27.
weight
-2

28.
}

29.
vrrp_instance
VI_1 {

30.
interface
eth0

31.
#
interface for inside_network, bound by vrrp

32.
state
MASTER

33.
#
Initial state, MASTER|BACKUP

34.
#
As soon as the other machine(s) come up,

35.
#
an election will be held and the machine

36.
#
with the highest "priority" will become MASTER.

37.
#
So the entry here doesn't matter a whole lot.

38.
priority
101

39.
#
for electing MASTER, highest priority wins.

40.
#
to be MASTER, make 50 more than other machines.

41.
virtual_router_id
51

42.
#
arbitary unique number 0..255

43.
#
used to differentiate multiple instances of vrrpd

44.
#
running on the same NIC (and hence same socket).

45.
garp_master_delay
1

46.
authentication
{

47.
auth_type
PASS

48.
auth_pass
pass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class=
"keylink"
>word</a>

49.
}

50.
track_interface
{

51.
eth0

52.
}

53.
#
optional, monitor these as well.

54.
#
go to FAULT state if any of these go down.

55.
virtual_ipaddress
{

56.
172.16.12.254/16
dev eth0 label eth0:0

57.
}

58.
track_script
{

59.
chk_httpd

60.
chk_schedown

61.
}

62.
notify_master
"/etc/keepalived/notify.sh
master"

63.
notify_backup
"/etc/keepalived/notify.sh
backup"

64.
notify_fault
"/etc/keepalived/notify.sh
fault"

65.
}


backup配置:

view
sourceprint?

01.
#
cd /etc/keepalived/

02.
#
vim keepalived.conf

03.
!
Configuration File
for
keepalived

04.
global_defs
{

05.
notification_email
{

06.
wangej@126.com

07.
}

08.
notification_email_from
kanotify@magedu.com

09.
smtp_connect_timeout
3

10.
smtp_server
127.0.0.1

11.
router_id
LVS_DEVEL

12.
}

13.
vrrp_script
chk_httpd {

14.
script
"killall
-0 httpd"

15.
interval
2

16.
#
check every 2 seconds

17.
weight
-2

18.
#
if failed, decrease 2 of the priority

19.
fall
2

20.
#
require 2 failures for failures

21.
rise
1

22.
#
require 1 sucesses for ok

23.
}

24.
vrrp_script
chk_schedown {

25.
script
"[[
-f /etc/keepalived/down ]] && exit 1 || exit 0"

26.
interval
2

27.
weight
-2

28.
}

29.
vrrp_instance
VI_1 {

30.
interface
eth0

31.
#
interface for inside_network, bound by vrrp

32.
state
BACKUP

33.
#
Initial state, MASTER|BACKUP

34.
#
As soon as the other machine(s) come up,

35.
#
an election will be held and the machine

36.
#
with the highest "priority" will become MASTER.

37.
#
So the entry here doesn't matter a whole lot.

38.
priority
100

39.
#
for electing MASTER, highest priority wins.

40.
#
to be MASTER, make 50 more than other machines.

41.
virtual_router_id
51

42.
#
arbitary unique number 0..255

43.
#
used to differentiate multiple instances of vrrpd

44.
#
running on the same NIC (and hence same socket).

45.
garp_master_delay
1

46.
authentication
{

47.
auth_type
PASS

48.
auth_pass
pass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class=
"keylink"
>word</a>

49.
}

50.
track_interface
{

51.
eth0

52.
}

53.
#
optional, monitor these as well.

54.
#
go to FAULT state if any of these go down.

55.
virtual_ipaddress
{

56.
172.16.12.254/16
dev eth0 label eth0:0

57.
}

58.
track_script
{

59.
chk_httpd

60.
chk_schedown

61.
}

62.
notify_master
"/etc/keepalived/notify.sh
master"

63.
notify_backup
"/etc/keepalived/notify.sh
backup"

64.
notify_fault
"/etc/keepalived/notify.sh
fault"

65.
}


内容简述:

view
sourceprint?

01.
vrrp_instance
VI_1 {                                
#定义虚拟路由

02.
state
MASTER                                    
#定义初始状态下的主从路由

03.
interface
eth0                                  
#定义连接端口

04.
virtual_router_id
51                            
#定义虚拟路由的ID

05.
priority
100                                    
#定义优先级

06.
advert_int
1
#定义通告间隔时间

07.
authentication
{

08.
auth_type
PASS                              
#定义认证类型

09.
auth_pass
1111
#定义认证密码

10.
}

11.
virtual_ipaddress
{                             
#定义虚拟地址

12.
192.168.200.16

13.
}

14.
}


四、keepalive功能验证

1、安装httpd

view
sourceprint?

1.
#
yum -y install httpd

2.
#
echo '<h1>node2.wangej.com</h1>' > /var/www/html/index.html

3.
#
cat /var/www/html/index.html

4.
#
service httpd start






2、启动keepalive服务

在两台机器上启动keepalive服务

view
sourceprint?

1.
#
service keepalived start




关闭主节点,重新测试页面:



至此,一个简易的为WEB服务提供高可用服务的集群就搭建完成了;

PS:关闭主节点是在脚本中完成的,脚本内容如下:

view
sourceprint?

01.
#!/bin/bash

02.
#
Author: MageEdu <linuxedu@foxmail.com>

03.
#
description: An example of notify script

04.
#

05.
ifalias=${2:-eth0:0}

06.
interface=$(
echo
$ifalias
|
awk
-F:
'{print $1}')

07.
vip=$(ip
addr show $interface |
grep
$ifalias
|
awk
'{print
$2}')

08.
#contact='linuxedu@foxmail.com'

09.
contact='root@localhost'

10.
workspace=$(
dirname
$0)

11.
notify()
{

12.
subject=
"$ip
change to $1"

13.
body=
"$ip
change to $1 $(date '+%F %H:%M:%S')"

14.
echo
$body
| mail -s
"$1
transition"
$contact

15.
}

16.
case
"$1"
in

17.
master)

18.
notify
master

19.
exit
0

20.
;;

21.
backup)

22.
notify
backup

23.
/etc/rc.d/init.d/httpd
restart

24.
exit
0

25.
;;

26.
fault)

27.
notify
fault

28.
exit
0

29.
;;

30.
*)

31.
echo
'Usage:
$(basename $0) {master|backup|fault}'

32.
exit
1

33.
;;

34.
esac


在/etc/keepalived目录中创建一个down文件即可实现关闭主节点的目的。

我们最后再看一下master和backup的IP地址:



转载:http://www.it165.net/admin/html/201305/1216.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  负载均衡