使用 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/24 # 虛擬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/24
}
}
注意:備份服務器 Server B 配置中 state 要改成 BACKUP,同時調低 priority。