試用Keepalived來做雙機熱備,服務器信息如下:
服務器 | 操作系統 | IP | 虛擬IP |
Server 1 | Centos | 192.168.18.20 | 192.168.18.22 |
Server 2 | Centos | 192.168.18.21 | 192.168.18.22 |
1. 安裝Keepalived
2臺Server都使用下面的命令安裝Keepalived:
yum install keepalived -y
2. Server1 Keepalived 配置
$ vi /etc/keepalived/keepalived.conf
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.18.22 # 虛擬IP
}
}
3. Server2 Keepalived 配置
$ vi /etc/keepalived/keepalived.conf
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.18.22 # 虛擬IP
}
}
4. 啟動Keepalived
$ service keepalived start
啟動keepalived后,可看到2臺Server都綁定了虛擬IP:
$ ip a
# Server 1:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:24:8c:8c:67:43 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.20/24 brd 192.168.18.255 scope global eth0
inet 192.168.18.22/32 scope global eth0
inet6 fe80::224:8cff:fe8c:6743/64 scope link
valid_lft forever preferred_lft forever
# Server 2:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:23:54:bf:ab:17 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.21/24 brd 192.168.18.255 scope global eth0
inet 192.168.18.22/32 scope global eth0
inet6 fe80::223:54ff:febf:ab17/64 scope link
valid_lft forever preferred_lft forever
5. 測試
瀏覽器訪問http://192.168.18.22,出現 This is Server 1.
將192.168.18.20關閉,再訪問http://192.168.18.22,出現This is Server 2.
微信訂閱號:
源文地址:http://blog.gopersist.com/2014/09/24/keepalived/