Keepalived Hot Standby

Generally speaking, dual-machine hot standby means that both machines are running, but not both machines are providing services at the same time.
When the one that provides the service fails, the other one will automatically take over and provide the service immediately, and the switching time is very short.
Let's use keepalived combined with tomcat to implement a dual-machine hot backup of a web server.
The working principle of keepalived is VRRP (Virtual Router Redundancy Protocol) virtual routing redundancy protocol.
There are two important concepts in VRRP: VRRP routers and virtual routers, master routers and backup routers.
A VRRP router is a router that runs VRRP and is a physical entity. A virtual router is created by the VRRP protocol and is a logical concept. A group of VRRP routers work together to form a virtual router. There is an election mechanism in Vrrp, which is used to elect the route that provides services, that is, the master route, and the others become backup routes. When the master route fails, a master route will be re-elected from the backup route to continue working to ensure uninterrupted service.
Our test environment in this article is as follows:
two physical servers and one virtual server (vip):
master: redhat 2.6.18-53.el5 192.168.8.4
backup: redhat 2.6.18-53.el5 192.168.8.6
vip: 192.168. The network topology diagram of the 8.100
test environment is as follows:



 

Node A 192.168.8.4 (primary node), Node B 192.168.8.6 (standby node), virtual IP (IP 192.168.8.100 for external services)
In this mode, the virtual IP can only belong to a certain node at a certain time, Another node exists as a standby node.
When the primary node is unavailable, the standby node takes over the virtual IP (that is, the virtual IP is drifted to Node B) to provide normal services.
The principle of keepalived can be simply understood as follows:
keepalived is installed on two physical servers and monitors each other whether the other is running normally.
When node A is normal: keepalived on node A will broadcast the following information:
192.168.8.100 The MAC address corresponding to this IP is the MAC address of the network card of node A.
Other computers in the graph such as client and NodeB will update their own ARP table, corresponding to the MAC address of 192.168.8.100 = the MAC address of the network card of node A.
When node A fails, keepalived on node B will detect it and broadcast the following information:
192.168.8.100 The MAC address corresponding to this IP is the MAC address of the network card of node B.
Other computers in the graph, such as the client, will Update your own ARP table, the MAC address corresponding to 192.168.8.100 = the MAC address of the node B network card.

 

1. Install keepalived on the main and standby machines, the
steps are as follows: download keepalived-1.1.15.tar.gz, then decompress and install
#tar zxvf keepalived-1.1.15.tar.gz #cd keepalived-1.1.15
#
./configure
#make
#make install 2, configure the state MASTER in the
keepalived configuration to determine the priority of the node as the primary node. For example, when there are multiple standby nodes, after the primary node fails, the priority value is larger. The master node is configured as follows:


global_defs {  
    router_id NodeA  
}  
vrrp_instance VI_1 {  
    state MASTER #Set as the master server  
    interface eth0 #Monitor network interface  
    virtual_router_id 51 #The master and the backup must be the same  
    priority 100 #(The main and standby machines take different priorities, the main machine value is larger, the backup machine value is smaller, the larger the value, the higher the priority)  
    advert_int 1 #VRRP Multicast broadcast cycle seconds  
    authentication {  
    auth_type PASS #VRRP authentication method, the master and backup must be the same  
    auth_pass 1111 #(password)  
}  
virtual_ipaddress {  
    192.168.8.100/24 ​​#VRRP HA virtual address  
}

 The configuration of the standby node is as follows:

global_defs {  
    router_id NodeB  
}  
vrrp_instance VI_1 {  
    state BACKUP #Set as the main server  
    interface eth0 #Monitor network interface  
    virtual_router_id 51 #The master and the backup must be the same  
    priority 90   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)  
    advert_int 1   #VRRP Multicast广播周期秒数  
    authentication {  
    auth_type PASS  #VRRP认证方式,主备必须一致  
    auth_pass 1111   #(密码)  
}  
virtual_ipaddress {  
    192.168.8.100/24  #VRRP HA虚拟地址  
} 

 3,启动keepalived:

keepalived -D -f /usr/local/etc/keepalived/keepalived.conf
查看log消息:
tail -f /var/log/messages
启动主节点A后的日志为:会广播ARP消息

[root@srv4 ~]# tail -f /var/log/messages  
Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes  
Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]  
Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100  
Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 192.168.8.100 added  
Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.8.100 added  
Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 192.168.8.100 on eth0.  
Sep 20 01:45:36 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100  

 通过ip a 命令可以看到192.168.8.100/24绑定到了eth0上

[root@srv4 bin]# ip a  
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue   
    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 qlen 1000  
    link/ether 00:0c:29:50:2d:9d brd ff:ff:ff:ff:ff:ff  
    inet 192.168.8.4/24 brd 192.168.8.255 scope global eth0  
    inet 192.168.8.100/24 scope global secondary eth0  
    inet6 fe80::20c:29ff:fe50:2d9d/64 scope link   
       valid_lft forever preferred_lft forever  

 

启动备用节点B后的日志为:

Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: Configuration is using : 34262 Bytes  
Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  
Sep 20 01:47:31 hadoopsrv Keepalived: Starting VRRP child process, pid=20567 

 

4,在两台机器上安装tomcat,安装步骤省略
安装完成后在节点A的机器上创建一个html文件内容如下

[html]  view plain  copy
 
  1. this is the test page  
  2. <br>  
  3. from server 192.168.8.4  

通过下面的url验证能够正常访问
http://192.168.8.4:8080/test/test.html

安装完成后在节点B的机器上创建一个html文件内容如下

[html]  view plain  copy
 
  1. this is the test page  
  2. <br>  
  3. from server 192.168.8.6  

通过下面的url验证能够正常访问
http://192.168.8.6:8080/test/test.html
在主节点,节点A正常的时候通过下面的url访问
192.168.8.100:8080/test/test.html
返回的内容应该为主节点上的html

[html]  view plain  copy
 
  1. this is the test page  
  2. <br>  
  3. from server 192.168.8.4  

将节点A的keepalived停止: killall keepalived
通过下面的url访问
192.168.8.100:8080/test/test.html
返回的内容应该为备用节点上的内容

[html]  view plain  copy
 
  1. this is the test page  
  2. <br>  
  3. from server 192.168.8.6  

同时查看节点B的日志:发现节点B转为主节点并且会广播ARP消息

[html]  view plain  copy
 
  1. Sep 20 01:55:44 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
  2. Sep 20 01:55:45 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
  3. Sep 20 01:55:45 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
  4. Sep 20 01:55:45 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100  
  5. Sep 20 01:55:45 hadoopsrv avahi-daemon[3769]: Registering new address record for 192.168.8.100 on eth0.  
  6. Sep 20 01:55:50 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100  


本文的目的主要是演示keepalived实现双机热备的功能和过程。
对于应用本身(tomcat)发生错误,以及"脑裂"等问题是不能解决的。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119574&siteId=291194637