您的位置:首页 > 其它

keepalived 配置使用

2016-06-27 10:40 351 查看
这里举例一个使用场景 :Keepalived 双机热备
热备又分为两种情况:第一种情况:服务器层的热备(比如服务器宕机、keepalived宕、网络不同,这个主要针对ip及keepalived)第二种情况:应用层(web)的热备(比如web应用端口不同,nginx进程被意外kill,这个主要针对端口)

使用 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 {
notification_email {
user@example.com #设置报警邮件地址,可以设置多个,每行一个。需要开启sendmail服务。
}

notification_email_from mail@example.org #设置邮件的发送地址
smtp_server 192.168.200.1 #设置SMTP Server地址
smtp_connect_timeout 30 #设置SMTP Server的超时时间
router_id LVS_DEVEL #表示运行Keepalived服务器的一个标示。发邮件时显示邮件主题的信息
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51 # #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识,即同一个vrrp_instance下的MASTER和BACKUP必须一致
priority 100         # 优先级 (主服务器较高)
advert_int 1         #组播信息发送间隔,两个节点设置必须一样 (单位秒)

# mcast_src_ip 192.168.65.142 #vrrp实体服务器的IP  发送多播包的地址,如果不设置默认使用绑定网卡的primary ip

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 # 虚拟IP地址,可以多个。
}

vrrp_script xxxx1{ //启动后首先会运行的命令, 如果正常则进入master角色,否则进入fault状态
script ""      //根据返回值检测是否正常
interval 2     //执行检查的时间间隔

}

track_script{ #  track_script指定检查脚本,定期运行它们来改变优先级,并最终引发主备切换
xxxx1
}
notify_master "xxxx.sh" #切换到master角色后执行的脚本
notify_backup "xxxx.sh" #切换到back角色后执行的脚本
notify_fault "xxxx.sh"
}
Server B
$ sudo vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
user@example.com #设置报警邮件地址,可以设置多个,每行一个。需要开启sendmail服务。
}

notification_email_from mail@example.org #设置邮件的发送地址
smtp_server 192.168.200.1 #设置SMTP Server地址
smtp_connect_timeout 30 #设置SMTP Server的超时时间
router_id LVS_DEVEL #表示运行Keepalived服务器的一个标示。发邮件时显示邮件主题的信息
}
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_script xxxx1{ //启动后首先会运行的命令, 如果正常则进入master角色,否则进入fault状态
script ""      //根据返回值检测是否正常
interval 2     //执行检查的时间间隔

}

track_script{ #  track_script指定检查脚本,定期运行它们来改变优先级,并最终引发主备切换
xxxx1
}
notify_master "xxxx.sh" #切换到master角色后执行的脚本
notify_backup "xxxx.sh" #切换到back角色后执行的脚本
notify_fault "xxxx.sh"
}

注意:备份服务器 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

<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

<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

<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