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负载 ...
随机推荐
- myeclipse maven pom.xml 配置错误
http://www.oschina.net/question/2265006_219341#tags_nav maven pom.xml 配置文件错误 腾讯云消息队列CMQ架构解析> ...
- Centos7 搭建 Keepalived+LVS 备注
NAT模型需要RealServer gateway设定为,DR模式需要执行 RealServer.sh.需要先安装network-tools. #!/bin/bash#description : st ...
- SiteFactory简单配制
进入后台管理: 指向节点,有点节点ID,这个节点ID就是siteFactory根据规则能生成的页面,页面更新网址: http://www.elexcon.com/Category_节点ID/Index ...
- float浮动深入理解
[CSS深入理解之float浮动]听课总结 (http://www.imooc.com/learn/121) 1.float的原本作用:为了实现文字环绕 2.float的包裹性和破坏性: 包裹性: ...
- 初学JAVA
通过eclipse开发java程序:1.创建一个java项目 2.创建程序包 3.编写java源程序 4.运行java程序 例如开发"helloworld "java程序: 1.进 ...
- <转>浏览器内核分类
浏览器的种类成千上百,但所基于的内核,却没有几个.目前主流的浏览器内核主要为以下四种: 一.Trident内核,代表产品Internet Explorer说起Trident,很多人都会感到陌生,但提起 ...
- osg,qt编译的一些问题
osg编译例子的时候,打开文件就出问题,可能是一些不兼容的问题 qt编译的是时候要添加qt和vs2010的整合工具,这样才能把 vs2010里面的QTDIR变量和环境变量QTDIR关联起来 同是右击文 ...
- URAL 2080 Wallet 莫队算法
题目链接:Wallet 题意:给出n张卡片,k次使用.要求每次使用的卡片都在最上面.首先希望你合理的安排每张卡片的初始位置,并且输出.然后,问每次使用完卡片之后插入的位置上面有几张卡片,才能使得每次使 ...
- 找做IT的男朋友会不会没有隐私
找做IT的男朋友会不会没有隐私你不觉得自己在网上不断的“秀”啊,“晒”啊的行为才是根本所在吗?你应该怕自己的这种行为才对吧-—————————————————————————————————————— ...
- maven3.04管理jetty9.2.10启动web项目
在pom.xml文件中添加如下: <build> <pluginManagement> <plugins> <plu ...