Lvs:keepalived + LVs +tomcat realize master-slave hot standby + load Balancing

Source: Internet
Author: User
Tags web services file permissions
Preface

Taobao architect Li Zhihui Daniel's book "Large Web Site Technology architecture" and Xu Long's blog---"Large-scale Web site technology practice" Primary article: the use of lvs+keepalived to achieve load balance.

From the reference to this article, this article is intended to record their own learning process, at the same time for everyone to refer to, the wrong place to welcome everyone to pat the bricks, and also hope that we can write the reasons for everyone to understand and harvest.

Related concepts I do not speak here, can be from such as the book He Xurong Brother's blog to understand, then the following directly to start our practice.

Environmental preparedness

This machine + VirtualBox + 4 CentOS virtual machine, the following figure

VirtualBox installation and CentOS installation is no longer demonstrated here, we set up their own; the role of this machine in this experiment is the client, played a role in sending requests, two CentOS do load balancing server (one for the host, one for the standby machine), The other two are real Web servers (with Tomcat installed).

This experiment is based on Dr Load Balancing mode (direct route, Xu Long's blog has explained), set a VIP (Virtual IP) for 192.168 1.200, users need only access to this IP address to get Web services. Among them, load balanced host is 192.168.1.114 (master), Standby machine is 192.168.1.112 (Brucelee). Web Server A is 192.168.1.111 (youzhibing), Web Server B is 192.168.1.115 (YOUZHIBING03), and four machine names are not normal except Master, but do not affect the experiment. All four CentOS firewalls need to be shut down. Using the Xshell link CentOS, the following figure

Configuring two Web servers

As with the local deployment Web project, deploy MyWeb to Tomcat, open Tomcat, host access (VirtualBox install Linux, and build tomcat please click here), as shown below

The two Web servers are configured as LVS Real server, edit the realserver script file, go to the specified folder: cd/etc/init.d/, edit the script file: Vim Realserver, as shown below

/etc/init.d/realserver content is as follows

#vi/usr/local/sbin/realserver.sh #!/bin/bash # description:config realserver lo and apply Noarp #Written By:netseek htt P://www.linuxtone.org sns_vip=192.168.1.200.
       /etc/rc.d/init.d/functions case "in Start" Ifconfig lo:0 $SNS _vip netmask 255.255.255.255 broadcast $SNS _VIP
       /sbin/route add-host $SNS _vip Dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl-p >/dev/null 2>&1 Echo RealServer
Start OK ";; Stop) Ifconfig lo:0 down Route del $SNS _VIP >/dev/null 2>&1 echo "0" >/pro C/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sy S/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/alL/arp_announce echo "Realserver stoped";; *) echo "Usage: $ {Start|stop}" Exit 1 Esac Exit 0

Change the file permissions after you save the script file: chmod 755 realserver, open the Realserver services: Service realserver start;

Note: Configuring Real server is a Web server that needs to be configured and how many sets are configured.

Configuring the primary load Server

1. Installation of keepalived related packages

Yum Install-y keepalived

Under CentOS, the software package can be easily installed through the Yum Install command, but the prerequisite is your virtual confidential network, if there is no networking, first download the package from the network, and then copy or upload to the Linux system, and then install;

2. Edit keepalived.conf configuration file

① Enter keepalived.conf directory: cd/etc/keepalived

② first clear out keepalived original configuration:> keepalived.conf

③ keepalived config file: vi keepalived.conf

The contents are as follows:

Global_defs {notification_email {997914490@qq.com} notification_email_from sns-lvs@gmail.com SM Tp_server 192.168.1.114 smtp_connection_timeout router_id lvs_master # set LVS ID, in a network should be unique} vrrp_inst ance vi_1 {State MASTER # Specifies the role of keepalived, master, backup for standby interface eth0 # Current network connection for VRRP communication Port Card (current CentOS network card) virtual_router_id 66 # Virtual route number, master and subordinate to always priority 100 # priority, the larger the number, the higher the priority to get processing requests ADV
    Ert_int 1 # Check interval, default to 1s (vrrp multicast cycle seconds) authentication {Auth_type Pass Auth_pass 1111} virtual_ipaddress {192.168.1.200 # defines the virtual IP (VIP), can be set more, each line A}} # defines the VIP of the externally provided LVS and port Virtual_server 192            .168.1.200 8080 {delay_loop 6 # Set Health check time, in seconds Lb_algo WRR # set up the algorithm for load dispatch WRR Lb_kind DR # Set the mechanism of the LVS implementation load, there are NAT, TUN, Dr Three mode nat_mask 255.255.255.0 persistence_timeout 0 # All requests in the same IP 0 seconds are sent to the same REA L Server Protocol TCP Real_server 192.168.1.111 8080 {# Specifies real Server1 's IP address weight 3 # Configure node weights, the higher the value the greater the weight Tcp_check { Connect_timeout nb_get_retry 3 delay_before_retry 3}} Real_serve
            R 192.168.1.115 8080 {weight 3 tcp_check {connect_timeout Nb_get_retry 3     Delay_before_retry 3}}}

Configuring from a Load server

Like the primary load server, install keepalived First, then edit keepalived.conf, as follows, slightly different from the primary load server

Global_defs {notification_email {997914490@qq.com} notification_email_from sns-lvs@gmail.com SM Tp_server 192.168.1.112 smtp_connection_timeout router_id lvs_backup # set LVS ID, in a network should be unique} vrrp_inst ance vi_1 {State BACKUP # Specifies the role of keepalived, master, backup for standby interface eth0 # Current network connection for VRRP communication
    Port card (currently for VRRP Communication Network interface card) virtual_router_id 66 # Virtual route number, master and subordinate to consistent priority 99 # priority, the larger the number, the higher the priority to get processing requests 
    Advert_int 1 # Check interval, default to 1s (vrrp multicast cycle seconds) authentication {Auth_type Pass Auth_pass 1111}  virtual_ipaddress {192.168.1.200 # defines a virtual IP (VIP), can be set more than one}} # to define the VIP and port of the external supply of LVS Virtual_server 192.168.1.200 8080 {delay_loop 6 # Set Health check time, in seconds Lb_algo WRR # Set the algorithm for load dispatch to WRR Lb_kind Dr # set LVs to implement the load mechanism, there are NAT, TUN, Dr Three mode nat_mask 255.255.255.0 persistence_timeout 0 # requests in the same IP 0 seconds are sent to the same A real server ProtocoL TCP real_server 192.168.1.111 8080 {# Specifies real Server1 IP address weight 3 # Configure node weights, the larger the value the higher the weight tcp_che CK {connect_timeout nb_get_retry 3 delay_before_retry 3}} real_s
            Erver 192.168.1.115 8080 {weight 3 tcp_check {connect_timeout Nb_get_retry 3     Delay_before_retry 3}}}

Start the KeepAlive service, the master and slave to start.

Service keepalived Start

Effect Show

1. Load Balancing Demo

There is a problem here, found that the same browser for a period of time to refresh, the result is the same, that is, the same IP a certain period of time the request to the same real server processing, using CentOS Browser test is also a period of time the request is the same result, But the host is different, such as host to get the same 192.168.1.115, and CentOS on the 192.168.1.111, can be clearly I configured Persistence_timeout 0, found a lot of information, or failed to solve. Hope to know the friends can not hesitate to enlighten, in the comments area to leave a message, thank you.

A.web server fails

192.168.1.115, shut down 115, youzhibing03 Tomcat service.

Then the host and the virtual machine results on only one, have the following results

B.web Server repair, reboot 115 Tomcat service

The host gets the following results

But the result of 192.168.111 is still on the virtual machine.

2. Master-Slave Hot Standby Demo

A. Turn off the keepalived service for the primary load server

Refresh the page and still get the following results

Then it is also explained from the load server (Brucelee) took over, let's look at the log

found that the load server did take over the tasks of the primary load server

B. When the previous primary load server (master) was repaired, the log files are as follows

After the primary load server recovers, it is back from the load server to the original master-slave state.

Summarize

In general, the final effect is still in line with the title, although there is a bit of doubt in the load balancing, it does not seem to achieve the purpose of load balancing, but it is just as if, as a whole, there is a load balancing effect, such as the host of the result is 115, and the virtual machine is the result of 111;

At present, in the popular LVS solution, the collocation type such as Nginx+tomcat is adopted in the Web server, and static files and dynamic files are processed separately, which is an effective attempt. In the days to come, I will continue to practice and share with you.

Something

This blog was actually written in Saturday, but has been entangled in the load balance that position, the delay has not been published, but also hope for their own and the people responsible for it; but there is good intentions do not necessarily have better results, access to a lot of information, but also made a lot of changes, the effect of the load balance has not changed. I expect some discrepancies, although there are discrepancies, but I think the effect of load balancing is there, but with a single browser refresh and get two effects between the switch some different!

In addition, if you want to practice, I suggest that everyone's hardware is not too low, my i7 processor + 4G memory, running host + 4 virtual machine A bit of cards, we recommend that the memory on the big point, preferably 8G above;

Finally, it is emphasized that practice is the sole criterion for testing truth. Some Xu's impulse is to go to practice quickly.

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.