Fork me on GitHub

Configuring Simple Virtual IP Address Failover Using Keepalived

本文主要介绍了利用Keepalived实现简单的VIP自动切换。

Configuring Simple Virtual IP Address Failover Using Keepalived

A typical Keepalived high-availability configuration consists of one master server and one or more backup servers. One or more virtual IP addresses, defined as VRRP instances, are assigned to the master server’s network interfaces so that it can service network clients. The backup servers listen for multicast VRRP advertisement packets that the master server transmits at regular intervals. The default advertisement interval is one second. If the backup nodes fail to receive three consecutive VRRP advertisements, the backup server with the highest assigned priority takes over as the master server and assigns the virtual IP addresses to its own network interfaces. If several backup servers have the same priority, the backup server with the highest IP address value becomes the master server.

The following example uses Keepalived to implement a simple failover configuration on two servers. One server acts as the master, the other acts as a backup, and the master server has a higher priority than the backup server.

the virtual IP address 10.0.0.100 is initially assigned to the master server (10.0.0.71). When the master server fails, the backup server (10.0.0.72) becomes the new master server and is assigned the virtual IP address 10.0.0.100.

配置文件

/etc/keepalived/keepalived.conf on the master server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
global_defs {
notification_email {
root@mydomain.com
}
notification_email_from svr1@mydomain.com
smtp_server localhost
smtp_connect_timeout 30
}
vrrp_instance VRRP1 {
state MASTER
# Specify the network interface to which the virtual address is assigned
interface enp0s8
# The virtual router ID must be unique to each VRRP instance that you define
virtual_router_id 41
# Set the value of priority higher on the master server than on a backup server
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1066
}
virtual_ipaddress {
10.0.0.100/24
}
}

/etc/keepalived/keepalived.conf

The configuration of the backup server is the same except for the values of notification_email_from, state, priority, and possibly interface if the system hardware configuration is different:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
global_defs {
notification_email {
root@mydomain.com
}
notification_email_from svr2@mydomain.com
smtp_server localhost
smtp_connect_timeout 30
}
vrrp_instance VRRP1 {
state BACKUP
# Specify the network interface to which the virtual address is assigned
interface enp0s8
virtual_router_id 41
# Set the value of priority lower on the backup server than on the master server
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1066
}
virtual_ipaddress {
10.0.0.100/24
}
}

查看IP 绑定

1
2
ip addr show
ip addr list

另外切换时 /var/log/messages 记录了主从切换的信息

参考

(Configuring Simple Virtual IP Address Failover Using Keepalived](https://docs.oracle.com/cd/E52668_01/E54669/html/section_uxg_lzh_nr.html)

好记性不如烂笔头,生命不息,学习不止!

分享