轉載:http://www.rainsts.net/article.asp?id=1023
Keepalived 雙機熱備[ 2010-06-30 20:37:34 | 作者:
yuhen ] 字型大小: 大 |
中 | 小 使用
Keepalived 做雙機熱備非常簡單,經常和
LVS 搭配來實現高可用Server Load Balancer方案。
1. Master / Slave
首先準備兩台測試伺服器和一個虛擬IP。
Server A: 192.168.1.10 (主伺服器)Server B: 192.168.1.20Virtual IP: 192.168.1.100
測試服務: 在兩台伺服器上分別安裝 Nginx,並修改預設的 index.html 檔案,顯示當前伺服器 IP 以便識別。
1. 在兩台伺服器上分別安裝 keepalived。
$ sudo apt-get install keepalived
2. 添加設定檔。
Server A
$ sudo vim /etc/keepalived/keepalived.confglobal_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.confglobal_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 a1: 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 forever2: 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 a1: 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 forever2: 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 a1: 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 forever2: 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.100Server 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 a1: 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 forever2: 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 a1: 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 forever2: 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 a1: 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 forever2: 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 使用)。
更詳細的資訊請參考官方網站