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负载 ...
随机推荐
- 使用CSS3 BACKFACE-VISIBILITY属性制作翻转动画效果
摘要: 通过backface-visibility:hidden;属性,我们可以使一个元素在翻转之后消失,这是可以使用另一个元素放在它的背面,从而制作出一种元素翻转之后出现另一个元素的效果. ... ...
- Django1.9开发博客(11)- 富文本与代码高亮
TinyMCE是一个轻量级的基于浏览器的所见即所得编辑器,支持目前流行的各种浏览器,由JavaScript写成. 功能配置灵活简单(两行代码就可以将编辑器嵌入网页中),支持AJAX.另一特点是加载速度 ...
- cocos2d-x lua绑定解析
花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...
- NodeJS学习笔记 - Express4.x路由操作
一.为Express添加about路由 1.新建js文件,about.js 2.打开about.js,并输入以下代码: var express=require('express'); var rout ...
- 【汇编】mov [1000],bx:immediate operand not allowed
],bx;在目的操作数中编译器会忽略[],所以[1000]就成了立即数,出现“immediate operand not allowed”改: ;mov bx,[1000];而在源操作数中,就不会出现 ...
- js对汉字首字母排序
var city = ["北京","天津","上海","重庆","杭州"];city.sort(fu ...
- PDF 补丁丁 0.4.2.950 测试版发布:按文件夹合并生成单独的PDF文件
新的测试版实现了将文件夹的内容合并为单独的PDF文件的功能.以下图为例讲解操作步骤. 点击工具栏的“合并文件”按钮,打开合并功能. 设M盘下有“test”和“test2”文件夹,里面包含了需要合并的文 ...
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- tslib1.4与Qt4.8.6的交叉编译与移植
最近开始正式接触QT开发,网上看了些移植教程都写的有点乱,博客园的emouse思·睿有一篇写的很好的文章,下面是文章的链接 http://www.cnblogs.com/emouse/archive/ ...
- linux getch()实现
#include <termio.h> int getch(void){ struct termios tm, tm_old; int fd = 0, ch; ...