keepalived—>vrrp(虚拟路由冗余协议)—>实现路由器的高可用(一个master,多个backup vip)
1、概念
keepalived是什么
keepalived是集群管理中保证集群高可用的一个服务软件(防止单点故障)
它能够保证当个别节点宕机时,整个网络可以不间断的运行,所以,keepalived一方面也可以实现系统网络服务的高可用功能
2、keepalived工作原理
是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Prorocol,即,虚拟路由冗余协议
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了
3、Keepalived高可用故障切换转移原理
Keepalived高可用服务对之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的。
在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活看,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
那么,什么是VRRP呢?
VRRP ,全 称 Virtual Router Redundancy Protocol ,中文名为虚拟路由冗余协议 ,VRRP的出现就是为了解决静态踣甶的单点故障问题,VRRP是通过一种竞选机制来将路由的任务交给某台VRRP路由器的
实验
server1和server4,server4做主用,server1做备用
server2和server3是后端服务器,配置http服务
server1–server4的ip分别为
172.25.23.1
172.25.23.2
172.25.23.3
172.25.23.4
server4(主)
1)编译
tar zxf keepalived-2.0.6.tar.gz
vim /etc/yum.repos.d/rhel-source.repo
yum install openssl-devel -y
yum instal libnl libnl-devel -y
yum install libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm ##此包需要官网下载
yum install gcc -y
cd keepalived-2.0.6
./configure --with-init=SYSV --prefix=/usr/local/keepalived ##编译源码,--prefix是安装路径
make && make install
2)作链接,做连接是为了和环境一致
cd /usr/local/
scp -r keepalived/ root@172.25.23.1:/usr/local/ ##将server4中的文件发送到server1中
du -sh keepalived/ ##查看keepalived/大小,此步实验不用做
chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived ##给权限
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
yum install ipvsadm -y
3)修改配置文件
vim /etc/keepalived/keepalived.conf
3 global_defs {
4 notification_email {
5 root@localhost ##宕机给谁发邮件
6 }
7 notification_email_from keepalived@localhost ##keepalived发邮件
8 smtp_server 127.0.0.1 ##发送邮件到本机
9 smtp_connect_timeout 30 ##缓冲30s
10 router_id LVS_DEVEL
11 vrrp_skip_check_adv_addr
12 #vrrp_strict
13 vrrp_garp_interval 0
14 vrrp_gna_interval 0
15 }
16
17 vrrp_instance VI_1 {
18 state MASTER ##状态是MASTER
19 interface eth0 ##接口
20 virtual_router_id 74 ##虚拟路由节点地址
21 priority 100 ##权重是100
22 advert_int 1
23 authentication {
24 auth_type PASS
25 auth_pass 1111
26 }
27 virtual_ipaddress {
28 172.25.23.100
29 }
30 }
31
32 virtual_server 172.25.23.100 80 {
33 delay_loop 3 ##循环检测3次后才会报错
34 lb_algo rr
35 lb_kind DR ##DR模式
36 #persistence_timeout 50 ##注释掉,此句是保持连接,不注释就会影响轮询效果
37 protocol TCP
38
39 real_server 172.25.23.2 80 {
40 TCP_CHECK
41 weight 1
42 connect_timeout 3
43 retry 3
44 delay_before_retry 3
45 }
46 }
47
48
49 real_server 172.25.23.3 80 {
50 TCP_CHECK
51 weight 1
52 connect_timeout 3
53 retry 3
54 delay_before_retry 3
55 }
56 }
57
58 }
4) 安装邮件服务
yum install mailx -y ##安装邮件软件
scp /etc/keepalived/keepalived.conf root@172.25.23.1:/etc/keepalived/ ##把server4中的配置文件发送给server1
/etc/init.d/keepalived start
tail -f /var/log/messages ##查看日至,看启动是否正常
ipvsadm -l
server1(备)
(配置与server4同,配置文件修改成备用,权重修改到小于主用)
chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
/etc/init.d/ldirectord stop
chkconfig ldirectord off
ipvsadm -l
ipvsadm -C
ipvsadm -l
ip addr show
ip addr del 172.25.23.100/24 dev eth0
ip addr show
ipvsadm -l
vim /etc/keepalived/keepalived.conf
18 state BACKUP ##装态,备用
19 interface eth0
20 virtual_router_id 74
21 priority 50 ##权重值
22 advert_int 1
yum install mailx -y
/etc/init.d/keepalived start
tail -f /var/log/messages ##查看日至,看启动是否正常
测试
关掉server2的http服务
在真机多次访问
server4 mail查看邮件
主机下线,备机服务,主机上线,切换回主机