一、Keepalived简介
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。
二、安装配置
1.拓扑图
说明:1.两个代理服务器通过VIP向外提供数据
2.两个代理服务器都可以代理后端的服务器
3.为测试方便,后端服务器至提供静态页面
2.ip规划
功用 | ip地址 | 安装软件 |
VIP | 192.168.1.99 | |
反向代理1 | 192.168.1.201 | keepalived、haproxy |
反向代理2 | 192.168.1.204 | keepalived、haproxy |
web服务器1 | 192.168.1.202 | httpd |
web服务器2 | 192.168.1.203 | httpd |
3.安装配置haproxy
关于haproxy的详细配置,请参照http://wangfeng7399.blog.51cto.com/3518031/1405758
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
[root@node1 ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration
for
a possible web application. See the
# full configuration options online.
#
# http:
//haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up
in
/
var
/log/haproxy.log you will
# need to:
#
#
1
) configure syslog to accept network log events. This
is
done
# by adding the
'-r'
option to the SYSLOGD_OPTIONS
in
# /etc/sysconfig/syslog
#
#
2
) configure local2 events to go to the /
var
/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /
var
/log/haproxy.log
#
log
127.0
.
0.1
local2
chroot /
var
/lib/haproxy
pidfile /
var
/run/haproxy.pid
maxconn
4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /
var
/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the
'listen'
and
'backend'
sections will
#
use
if
not designated
in
their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except
127.0
.
0.0
/
8
option redispatch
retries
3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn
3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:
80
default_backend
static
#---------------------------------------------------------------------
#
static
backend
for
serving up images, stylesheets and such
#---------------------------------------------------------------------
backend
static
balance roundrobin
server node2
192.168
.
1.202
:
80
check maxconn
2000
server node3
192.168
.
1.203
:
80
check maxconn
2000
#---------------------------------------------------------------------
# round robin balancing between the
var
ious backends
#---------------------------------------------------------------------
|
说明:两个节点的HAproxy的配置文件应该保持一样
4.测试haproxy的配置
5.安装配置keepalived
①、安装keepalived
keepalived的安装可以通过yum源来安装,也可以通过编译源码来安装,本处通过yum源赖安装
②、配置keepalived主节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
[root@node1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
//全局参数
notification_email {
//邮件
sysadmin@firewall.loc
//收件人地址
}
notification_email_from Alexandre.Cassen@firewall.loc
//发件人地址
smtp_server
127.0
.
0.1
//邮件服务器的地址
smtp_connect_timeout
30
//间隔时间
router_id LVS_DEVEL
//邮件服务器的组的id
}
vrrp_script chk_haproxy {
script
"killall -0 haproxy"
//检查haproxy是否在线
interval
1
//检查间隔时间
weight -
5
//如果检查失败,则权重-5
}
vrrp_instance VI_1 {
//定义第一个集群
state MASTER
//初始状态为主节点,从节点应该为BACKUP
interface
eth0
//配置ip的端口
virtual_router_id
51
//本组集群的id号,主从节点必须一样
priority
100
//主节点的优先级,备用节点的优先级必须低于主节点
advert_int
1
//心跳检查间隔时间
authentication {
auth_type PASS
//通信为明文密码通信
auth_pass
1111
//通信的密码,主从节点必须一样
}
virtual_ipaddress {
192.168
.
1.99
//定义一个VIP
}
track_script {
//调用上面的命令
chk_haproxy
}
}
|
③、配置keepalived从节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
[root@node4 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server
127.0
.
0.1
smtp_connect_timeout
30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script
"killall -0 haproxy"
interval
1
weight -
5
}
vrrp_instance VI_1 {
state BACKUP
interface
eth0
virtual_router_id
51
priority
99
advert_int
1
authentication {
auth_type PASS
auth_pass
1111
}
virtual_ipaddress {
192.168
.
1.99
}
track_script {
chk_haproxy
}
}
|
6.测试
1
2
3
4
5
6
|
2
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu
1500
qdisc pfifo_fast state UP qlen
1000
link/ether
00
:0c:
29
:
56
:
78
:cd brd ff:ff:ff:ff:ff:ff
inet
192.168
.
1.204
/
24
brd
192.168
.
1.255
scope global eth0
inet
192.168
.
1.99
/
32
scope global eth0
inet6 fe80::20c:29ff:fe56:78cd/
64
scope link tentative dadfailed
valid_lft forever preferred_lft forever
|
可以看到ip地址已经配置上去了
我们可以看到网页访问正常
下面,我们模拟服务器损坏,将node1上的haproxy关掉,看看ip地址是否会转移到node4上
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@node4 ~]# ip add
1
: lo: <LOOPBACK,UP,LOWER_UP> mtu
16436
qdisc noqueue state UNKNOWN
link/loopback
00
:
00
:
00
:
00
:
00
:
00
brd
00
:
00
:
00
:
00
:
00
:
00
inet
127.0
.
0.1
/
8
scope host lo
inet6 ::
1
/
128
scope host
valid_lft forever preferred_lft forever
2
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu
1500
qdisc pfifo_fast state UP qlen
1000
link/ether
00
:0c:
29
:
56
:
78
:cd brd ff:ff:ff:ff:ff:ff
inet
192.168
.
1.204
/
24
brd
192.168
.
1.255
scope global eth0
inet
192.168
.
1.99
/
32
scope global eth0
inet6 fe80::20c:29ff:fe56:78cd/
64
scope link tentative dadfailed
valid_lft forever preferred_lft forever
|
OK!!可以看到我们的页面访问正常,这就可以实现当前段的一个反向代理服务器宕机或者后端的一个web服务宕机,服务都可以正常对外提供
7.扩展
我们还可以自定义通知机制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#!/bin/bash
# Author: wangfeng7399<wangfeng17399@
163
.com>
# description: An example of notify script
#
vip=
192.168
.
1.99
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
|
在keepalived的配置文件中通过notify来调用,如下所示
1
2
3
|
notify_master
"/etc/keepalived/notify.sh master"
notify_backup
"/etc/keepalived/notify.sh backup"
notify_fault
"/etc/keepalived/notify.sh fault"
|
大功告成,由于本人水平有限,可能有逻辑上的错误,请各位大神匹配指正
本文转自wangfeng7399 51CTO博客,原文链接:http://blog.51cto.com/wangfeng7399/1405785,如需转载请自行联系原作者