0%

LVS-Keepalived 高可用群集


Keepalived起初是专门针对LVS设计的一款强大的辅助工具, 主要用来提供故障切换(Failover)和健康检查(Health Checking)功能–判断LVS负载调度器、节点服务器的可用性, 及时隔离并替换为新的服务器, 当故障主机恢复后将其重新加入群集.

Keepalived概述及安装

Keepalived的官方网站

Keepalived的热备方式

Keepalived采用VRRP(Virtual Router Redundancy Protocol, 虚拟路由冗余协议)热备份协议, 以软件的方式实现Linux服务器的多机热备功能.

VRRP是针对路由器的一种备份解决方案:由多台路由器组成一个热备组, 通过共用的虚拟IP地址对外提供服务;
每个热备组内同一时刻只有一台主路由器提供服务, 其他路由器处于冗余状态, 若当前在线的路由器失效, 则其他路由器会自动接替(优先级决定接替顺序)虚拟IP地址, 以继续提供服务.

热备组内的每台路由器都可能成为主路由器, 虚拟路由器的IP地址(VIP)可以在热备组内的路由器之间进行转移, 所以也称为漂移IP地址, 使用Keepalived时, 漂移地址的实现不需要手动建立虚接口配置文件, 而是由Keepalived根据配置文件自动管理.

Keepalived的安装与服务控制

安装支持软件

1
2
3
yum -y install gcc
yum -y install kernel-devel openssl-devel popt-devel
yum -y install ipvsadm

安装Keepalived

1
2
3
4
tar zxf keepalived-2.0.16.tar.gz
cd keepalived-2.0.16
./configure --prefix=/
make && make install

使用Keepalived服务

1
2
chkconfig -add keepalived
chkconfig keepalived on

使用Keepalived实现双机热备

主服务器的配置

Keepalived服务的配置目录位于/etc/keepalived, 其中keepalived.conf是主配置文件.
另外包括一个子目录samples/, 提供了许多配置样例作为参考.

在Keepalived的配置文件中:

global_defs {...}指定全局参数
router_id本路由器(服务器)的名称
vrrp_instance 实例名称 {...}指定VRRP热备参数, 注释文字以"!"符号开头.
state热备状态
interface承载VIP地址的物理接口
virtual_router_id虚拟路由器的ID号, 每个热备组保持一致.
priority优先级, 数值越大优先级越高.
advert_int通告间隔秒数(心跳频率)
authentication {...}认证信息, 每个热备组保持一致.
auth_type认证类型
auth_pass密码字串
virtual_ipaddress {...}指定漂移地址(VIP), 可以有多个.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
global_defs {
router_id HA_TEST_R1
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.24
}
}

systemctl start keepalived
ip addr show dev ens33
2: ens33:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:3a:8a:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.10/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.24/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d294:3d50:4d71:df7f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

备用服务器的配置

在同一个Keepalived热备组内, 所有服务器的Keepalived配置文件基本相同, 包括路由器名称、虚拟路由器的ID号、认证信息、漂移地址、心跳频率等.

不同之处主要在于路由器名称、热备状态、优先级.
路由器名称建议为每个参与热备的服务器指定不同的名称
热备状态主服务器将状态设为MASTER, 备用服务器将状态设为BACKUP.
优先级数值越大则取得VIP控制权的优先级越高, 主服务器优先级应设为最高, 其他备用服务器的优先级可依次递减, 但不要相同, 以免在争夺VIP控制权时发生冲突.
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
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
global_defs {
router_id HA_TEST_R2
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.24
}
}

systemctl start keepalived
ip addr show dev ens33
systemctl stop firewalld

测试双机热备功能

Keepalived的日志消息保存在/var/log/messages文件中, 在测试主、备故障自动切换功能时, 可以跟踪此日志文件来观察执备状态的变化.

连通性测试

在客户机中执行”ping -t 漂移IP地址”, 能够正常、持续ping通
①禁用主服务器的ens33网卡, 发现ping测试只中断了1或2个包即恢复正常, 说明已有其他服务器接替VIP地址, 并及时响应客户机请求.

②重新启用主服务器的ens33网卡, 发现ping测试再次中断1或2个包即恢复正常, 说明主服务器已恢复正常,并夺回VIP地址的控制权.

1
2
3
4
ping -t 漂移IP地址
ifconfig ens33 down
ifconfig ens33 up
ip addr show dev ens33

查看日志记录

在执行主、备故障切换的过程中, 分别观察各自的/var/log/messages日志文件, 可以看到MASTER、SLAVE状态的迁移记录.
①主服务器中, 状态先变为失效、放弃控制权, 恢复后重新变为MASTER.

②备用服务器中, 状态先切换为MASTER, 待主服务器恢复后再交回控制权.

获取控制权
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: (VI_1) Receive advertisement timeout
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: (VI_1) Entering MASTER STATE
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: (VI_1) setting VIPs.
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: Sending gratuitous ARP on ens33 for 192.168.100.24
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.100.24
Aug 12 19:22:54 localhost Keepalived_vrrp[29229]: Sending gratuitous ARP on ens33 for 192.168.100.24

Aug 12 19:22:54 localhost avahi-daemon[723]: Registering new address record for 192.168.100.24 on ens33.IPv4.
Aug 12 19:22:59 localhost Keepalived_vrrp[29229]: Sending gratuitous ARP on ens33 for 192.168.100.24
Aug 12 19:22:59 localhost Keepalived_vrrp[29229]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.100.24
Aug 12 19:22:59 localhost Keepalived_vrrp[29229]: Sending gratuitous ARP on ens33 for 192.168.100.24
交回控制权
Aug 12 19:21:40 localhost Keepalived_vrrp[29229]: (VI_1) Master received advert from 192.168.100.10 with higher priority 100, ours 99
Aug 12 19:21:40 localhost Keepalived_vrrp[29229]: (VI_1) Entering BACKUP STATE
Aug 12 19:21:40 localhost Keepalived_vrrp[29229]: (VI_1) removing VIPs.
Aug 12 19:21:40 localhost avahi-daemon[723]: Withdrawing address record for 192.168.100.24 on ens33.
1
less /var/log/messages

LVS-DR+Keepalived高可用群集

准备搭建环境:
服务主机内网(仅主机模式)外网(NAT模式)网关
LVSens33:192.168.100.252/24
ens33:0:192.168.100.253/24
ens33:(VM8)192.168.100.1
LVS2ens33:192.168.100.254/24
ens33:0:192.168.100.253/24
ens33:(VM8)192.168.100.1
RSens33:192.168.10.10/24ens37:192.168.100.10/24ens33:(VM1)192.168.10.1
RS2ens33:192.168.10.11/24ens37:192.168.100.11/24ens33:(VM1)192.168.10.1
NFSens33:192.168.10.20/24ens33:(VM1)192.168.10.1

配置主调度器

漂移地址使用LVS群集的VIP地址.

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
vim /etc/keepalived/keepalived.conf
#全局配置、热备配置
global_defs {
router_id LVS_HA_R1
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.253
}
}

#Web服务器池配置
virtual_server 192.168.100.253 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 30
protocol TCP

real_server 192.168.100.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}

real_server 192.168.100.40 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
systemctl start 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
vim /etc/keepalived/keepalived.conf
#全局配置、热备配置
global_defs {
router_id LVS_HA_R2
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.253
}
}

#Web服务器池配置
virtual_server 192.168.100.253 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 30
protocol TCP

real_server 192.168.100.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}

real_server 192.168.100.40 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
systemctl start keepalived

NFS和节点服务器配置

后续配置 -> LVS负载均衡群集

测试高可用群集

客户端进行访问

1
2
3
4
curl http://漂移IP地址:80
ifconfig ens33 down
ifconfig ens33 up
ip addr show dev ens33
-------------------本文结束 感谢阅读-------------------