Keepalived 双机热备
使用 Keepalived 做双机热备非常简单,经常和 LVS 搭配来实现高可用负载平衡方案。
1. Master / Slave
首先准备两台测试服务器和一个虚拟IP。
Server A: 192.168.1.10 (主服务器)
Server B: 192.168.1.20
Virtual IP: 192.168.1.100
测试服务: 在两台服务器上分别安装 Nginx,并修改默认的 index.html 文件,显示当前服务器 IP 以便识别。
1. 在两台服务器上分别安装 keepalived。
$ sudo apt-get install keepalived
2. 添加配置文件。
Server A
$ sudo vim /etc/keepalived/keepalived.conf global_defs {
router_id LVS_DEVEL
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51 # 保持主从服务器一致
priority 100 # 优先级 (主服务器较高)
advert_int 1 # 心跳广播间隔(秒) authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.1.100 # 虚拟IP地址,可以多个。
}
}
Server B
$ sudo vim /etc/keepalived/keepalived.conf global_defs {
router_id LVS_DEVEL
} 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.100
}
}
注意:备份服务器 Server B 配置中 state 要改成 BACKUP,同时调低 priority。
3. 启动两台服务器上的 keepalived 服务。
$ sudo service keepalived start
重启后可以使用 "ip a" 查看虚拟 IP 信息。
Server A
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.100/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link
valid_lft forever preferred_lft forever
Server B
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe01:d816/64 scope link
valid_lft forever preferred_lft forever
4. 在第三台机器上进行访问测试。
$ curl http://192.168.1.10 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.10</h1></center>
</body>
</html> $ curl http://192.168.1.20 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.20</h1></center>
</body>
</html> $ curl http://192.168.1.100 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.10</h1></center>
</body>
</html>
我们关掉主服务器 192.168.1.10,再访问 http://192.168.1.100 就会自动切换成备份服务器 (Server B: 192.168.1.20)。
$ curl http://192.168.1.100 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.20</h1></center>
</body>
</html>
同时 Server B 绑定了虚拟 IP。
Server B
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.100/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe01:d816/64 scope link
valid_lft forever preferred_lft forever
重新打开主服务器(Server A: 192.168.1.10),访问恢复。
2. Master / Master
Master / Slave 方案中备份服务器(Server B)平时就是个摆设,有点浪费。我们完全可以用来跑其他服务,让两台主机形成相互热备。
Server A: 192.168.1.10, Virtual IP: 192.168.1.100
Server B: 192.168.1.20, Virtual IP: 192.168.1.200
修改配置文件。
Server A
global_defs {
router_id LVS_DEVEL
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1 authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.1.100
}
} vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
advert_int 1 authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.1.200
}
}
Server B:
global_defs {
router_id LVS_DEVEL
} 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.100
}
} vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1 authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.1.200
}
}
其实很简单,我们增加了一个新的配置 VI_2 (注意 virtual_router_id 不同)。不过这回用 Server B 做主服务器,如此 Server A、Server B 各自拥有主虚拟IP,同时备份对方的虚拟 IP。重启两台服务器的 keepalived 服务后,查看虚拟 IP 绑定信息。
Server A
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.100/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link
valid_lft forever preferred_lft forever
Server B
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:01:d8:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.200/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe01:d816/64 scope link
valid_lft forever preferred_lft forever
正常情况下,会使用各自的主服务器。
$ curl http://192.168.1.100 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.10</h1></center>
</body>
</html> $ curl http://192.168.1.200 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.20</h1></center>
</body>
</html>
一旦任何一台服务器当机,另一台就会自动接管。我们停掉 192.168.1.20,看看访问 http://192.168.1.200 是不是切换到 192.168.1.10 上。
$ curl http://192.168.1.200 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx! 192.168.1.10</h1></center>
</body>
</html>
同时 Server A 绑定虚拟 IP 192.168.1.200。
$ ip a 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 UNKNOWN qlen 1000
link/ether 00:0c:29:4c:e7:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.100/24 scope global secondary eth0
inet 192.168.1.200/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe4c:e7e7/64 scope link
valid_lft forever preferred_lft forever
Server B 重启后,一切恢复正常。
这个方案可以是不同的服务,或者是同一服务的访问分流(配合 DNS 使用)。
更详细的信息请参考官方网站。
Keepalived 双机热备的更多相关文章
- Keepalived双机热备
一,Keepalived双机热备的应用场景 1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等 ...
- Nginx+keepalived双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- keepalived双机热备nginx
nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽 ...
- Nginx+keepalived 双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- 【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!
写在前面 最近出版了<海量数据处理与大数据技术实战>,详情可以关注 冰河技术 微信公众号,查看<我的<海量数据处理与大数据技术实战>出版啦!>一文. 也有不少小伙伴 ...
- nginx + keepalived 双机热备
序 双机热备是指两台机器都在运行,但并非两台机器同时在提供服务. 当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,且切换的时间非常短. keepalived的工作原理是VRRP—— ...
- keepalived双机热备实现故障时发送邮件通知
目前项目已经采用nginx来实现负载均衡,但是nginx调度器只有一台,上次还意外的down机一次,导致整个服务应用全部瘫痪,这次准备再加一个调度器来实现站点高可用性,也就是常说的双机热备了. mas ...
- keepalived双机热备,安装部署文档
keepalived双击热备,安装部署文档: 下载目录:/apps/keepalived-1.2.7.tar.gz 1:---> yum install -y make wget 2:---&g ...
- Nginx+keepalived双机热备(主从模式)
负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行.关于负载均衡介绍,可以参考:linux负载 ...
随机推荐
- Linux 笔记总览
LInux 性能分析 Linux IO实时监控命令详解
- 【ros】rplidar Hector Slam
想用rplidar跑一下hector slam,在网上发现了几个教程写的都不错,但是亲测发现都有点不足,综合了一下,进行补充. 1. 安装ros 和 创建工作空间 http://blog.csdn.n ...
- Android SharePreference 在主进程和次进程间共享数据不同步出错
SharedPreference作为android五大存储(网络,数据库,文件,SharedPreference,contentProvider)之中最方便使用的一个,从类名上来看就不是一个存储大 ...
- 1 Spring MVC 原理
1. 注解式 Spring MVC 响应流程: 重要的接口和类的简单说明: DispatcherServlet:前端控制器,用于接收请求. HandlerMapping接口:用于处理请求的映射. D ...
- Physx入门
[疑问] 1.Physx中的场景有大小的概念么?如果有大小,那么场景中的刚体超出场景边界之后, 如何定义之后的行为. 2.如何给一个刚体增加动量?目前的接口只看到设置速度或者增加作用力.
- JsTree
一.JStree的简单介绍 1.关于jstree jsTree 使用了 jQuery 和 Sarissa,是一个是免费的但是设置灵活的,基于 JavaScript 跨浏览器支持的网页树形部件. jsT ...
- 用c语言编写二分查找法
二分法的适用范围为有序数列,这方面很有局限性. #include<stdio.h> //二分查找法 void binary_search(int a[],int start,int mid ...
- 51nod 1158 全是1的最大子矩阵
题目链接:51nod 1158 全是1的最大子矩阵 题目分类是单调栈,我这里直接用与解最大子矩阵类似的办法水过了... #include<cstdio> #include<cstri ...
- B:冷血格斗场
总时间限制: 1000ms 内存限制: 65536kB描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场.格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一 ...
- Windows Store App 过渡动画
Windows Store App 过渡动画 在开发Windows应用商店应用程序时,如果希望界面元素进入或者离开屏幕时显得自然和流畅,可以为其添加过渡动画.过渡动画能够及时地提示用户屏幕所发 ...