Using keepalived to realize dual machine hot standby

Source: Internet
Author: User
Tags reflector

generally speaking, dual-machine hot standby refers to two machines are running, but not both machines are at the same time to provide services.
when one of the service providers fails, the other one will automatically take over and provide the service immediately, and the switch time is very short.
The following is a keepalived combined with Tomcat to implement a Web server's dual-machine hot standby.
the keepalived works by VRRP (virtual Router redundancy Protocol) VPN Routing Redundancy protocol.
There are two important sets of concepts in VRRP: VRRP routers and virtual routers, master routers, and backup routers.
A VRRP router is a router that runs VRRP, a physical entity, and a virtual router is a logical concept created by the VRRP protocol. A group of VRRP routers work together to form a virtual router. There is an electoral mechanism in the VRRP that selects the routing that provides the service, the master route, and the other is the backup route. When the master route fails, a master route is re-elected in the backup route to continue working to guarantee uninterrupted service.
The test environment we have 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.8.100
the network topology diagram for the test environment is as follows:

Node A 192.168.8.4 (master node), Node B 192.168.8.6 (alternate node), virtual IP (IP 192.168.8.100 for external service)
in this mode, the virtual IP can only belong to one node at a time, and the other node exists as an alternate node.
when the primary node is unavailable, the standby node takes over the virtual IP (that is, the virtual IP drift to Node B) and provides normal service.
the principle of keepalived can be so simple to understand:
The keepalived is installed on two physical servers and monitors each other for normal operation.
when Node A is normal: keepalived on Node A will broadcast the following information:
192.168.8.100 the MAC address of this IP corresponds to the MAC address of the Node a network card
other computers in the diagram, such as clients and NodeB, will update their ARP tables, corresponding to the MAC address of the 192.168.8.100 MAC address = Node A NIC.
when Node A fails, the keepalived on Node B is detected and the following information is broadcast:
192.168.8.100 This IP corresponds to the MAC address of the Node B network card
the other computers in the diagram, such as the client, update their ARP table, corresponding to the MAC address of the 192.168.8.100 = Node B network card.

1, install the keepalived on the main standby machine,
the steps are as follows: Download keepalived-1.1.15.tar.gz, then unzip the installation
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure
#make
#make Install
2, Configuration keepalived
The state Master in the configuration determines the node as the primary node
priority is prioritized, such as when there are multiple backup nodes, the priority value of the primary node after the failure of the takeover.
The master node is configured as follows:

global_defs {router_id nodea}vrrp_instance vi_1 {State MASTER #设置为主服务器 interface eth0 #监测网络接口 Virtual_ro UTER_ID #主, prepare must be the same as priority 100 # (Master, standby machine take different priorities, host value is large, backup machine value is small, the higher the value of the higher priority) Advert_int 1 #VRRP multicast broadcast cycle seconds aut hentication {auth_type PASS #VRRP认证方式, master must be consistent auth_pass 1111 # (password)}virtual_ipaddress {192.168.8.100/24 #VRRP ha virtual address}

The alternate node is configured as follows:

global_defs {    router_id  nodeb}vrrp_instance vi_1 {    state backup    # Set master server     interface eth0   #监测网络接口     virtual_router_id  51   #主, preparation must be the same priority 90   # (master, standby machine take different priority, host value is large, the backup machine value is small, the higher the value of the higher priority)      advert_int 1    #VRRP  multicast broadcast cycle seconds      authentication {auth_type pass   #VRRP认证方式, master and standby must be consistent auth_pass 1111   # (password) }virtual_ipaddress {192.168.8.100/24   #VRRP  ha virtual Address} 

3, starting keepalived:
keepalived-d-f/usr/local/etc/keepalived/keepalived.conf
View the log message:
tail-f/var/log/messages
After starting master Node A, the log is: The ARP message is broadcast

[[Email protected] ~]# tail -f /var/log/messagessep 20 01:45:29 srv4  Keepalived_vrrp: Configuration is using : 34546 BytesSep 20  01:45:29 srv4 keepalived_vrrp: vrrp sockpool: [ifindex (2),  proto (),  FD ( 8,9)]sep 20 01:45:30 srv4 keepalived_vrrp: vrrp_instance (VI_1)  Transition  To master statesep 20 01:45:31 srv4 keepalived_vrrp: vrrp_instance (VI_1)  entering master statesep 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.100sep 20 01:45:31 srv4 keepalived_vrrp: netlink reflector  Reports ip 192.168.8.100 addedsEp 20 01:45:31 srv4 keepalived_healthcheckers: netlink reflector reports  IP 192.168.8.100 addedSep 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

with the IP a command, you can see that 192.168.8.100/24 is bound to the eth0

[[email protected] bin]# ip a1: 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 forever2: 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   &nbsP;    valid_lft forever preferred_lft forever 

The log after starting the alternate Node B is:

Sep 01:47:31 hadoopsrv keepalived_vrrp:configuration is using:34262 bytessep 01:47:31 hadoopsrv KEEPALIVED_VRRP: Vrrp_instance (vi_1) Entering BACKUP statesep 01:47:31 hadoopsrv keepalived_vrrp:vrrp sockpool: [IfIndex (2), Proto (112 ), FD (7,8)]sep 01:47:31 hadoopsrv keepalived:starting VRRP child process, pid=20567

4. Install Tomcat on both machines, omit the installation steps
After the installation is complete, create an HTML file on Node A's machine as follows

This is the test Page<br>from server 192.168.8.4

The following URL verifies that the normal access
Http://192.168.8.4:8080/test/test.html

After the installation is complete, create an HTML file on Node B's machine as follows

This is the test Page<br>from server 192.168.8.6


http://192.168.8.6:8080/test/test.html

192.168.8.100:8080/test/test.html
The returned content should be HTML

on the master node

This is the test Page<br>from server 192.168.8.4

Stop the keepalived of Node A: Killall keepalived
Accessed through the following URL
192.168.8.100:8080/test/test.html
The content returned should be the content on the standby node

This is the test Page<br>from server 192.168.8.6

View the log of Node B at the same time: Discovery Node B goes to the primary node and broadcasts the ARP message

sep 20 01:55:44 hadoopsrv  Keepalived_vrrp: vrrp_instance (vi_1)  Transition to MASTER STATESep 20  01:55:45 hadoopsrv keepalived_vrrp: vrrp_instance (vi_1)  entering master statesep  20 01:55:45 hadoopsrv keepalived_vrrp: vrrp_instance (VI_1)  setting  Protocol vips.sep 20 01:55:45 hadoopsrv keepalived_vrrp: vrrp_instance (VI_1)  sending gratuitous arps on eth0 for 192.168.8.100sep 20 01:55:45  hadoopsrv avahi-daemon[3769]: Registering new address record for  192.168.8.100 on eth0. Sep 20 01:55:50 hadoopsrv keepalived_vrrp: vrrp_instance (VI_1)  Sending  gratuitous arps on eth0 for 192.168.8.100 

The purpose of this paper is to demonstrate the function and process of keepalived to realize dual-machine hot standby.
Problems with the application itself (tomcat) and "brain fissures" are not resolved.

Using keepalived to realize dual machine hot standby

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.