Load balancing for MySQL with lvs+keepalived

Source: Internet
Author: User
Tags server array file permissions iptables

1 Initial knowledge Lvs:linux Virtual Server 1.1 LVs is what

LVS is the short name of Linux virtual server, which is a free software project initiated by Dr. Zhangwensong, and its official site is www.linuxvirtualserver.org. Now LVS is already part of the Linux standard kernel, Prior to the Linux2.4 kernel, the LVS had to be recompiled to support the LVS function module, but since the Linux2.4 kernel, the various functions of LVS have been built-in, without any patching of the kernel, and the functions provided by LVS can be used directly.

What is the role of 1.2 LVs?

LVS is primarily used for server cluster load Balancing . It works on the network layer and can achieve high performance, highly available server clustering technology. It's cheap and combines many low-performance servers together to form a super server. It is easy to use, easy to configure, and has a variety of load balancing methods. It is stable and reliable, and does not affect the overall effect even if one of the servers in the cluster is not working properly. In addition, scalability is also very good.

LVS since 1998, has developed into a more mature technology project now. LVS technology can be used to achieve highly scalable, highly available network services, such as WWW services, cache services, DNS services, FTP services, mail services, video/audio-on-demand services, and so on, there are many more famous sites and organizations are using LVS set up the cluster system, For example: The Linux portal (www.linux.com), real Company (www.real.com), the world's largest open source website (sourceforge.net), which provides audio and video services to RealPlayer.

Architecture of 2.3 LVs

The server cluster system with LVS is composed of three parts:

(1) The most front-end load balancer layer, expressed with load balancer;

(2) Intermediate server cluster layer, expressed in server array;

(3) at the bottom of the data sharing storage layer, expressed with shared storage;

In the view of the user, all internal applications are transparent and users are simply using a virtual server to provide high-performance services.

2.4 LVs Load Balancing mechanism

(1) LVS is a four-layer load balancer, that is, based on the fourth layer of the OSI model-the Transport layer, the transport layer has our familiar TCP/UDP,LVS support tcp/udp load balancing. Since LVS is a four-layer load balancer, it is very efficient compared to other high-level load balancing solutions, such as DNS domain name rotation resolution, application layer load scheduling, and client scheduling.

(2) LVs forwarding mainly by modifying the IP address (NAT mode, divided into the source address modification Snat and the target address modified Dnat), modify the target Mac(DR Mode) to achieve.

①nat mode: Network address translation

NAT (Network address translation) is a technique for external network and Intranet addresses mapping . In NAT mode, the traffic of the network datagram must be processed by LVS. LVS is required as a gateway to the RS (real server). When the package arrives at the LVS, the LVS does the destination address translation (DNAT) and changes the target IP to the IP of Rs. RS receives the packet as if it were sent directly to it by the client. When the RS is processed, the source IP is the RS IP and the destination IP is the IP of the client when the response is returned. At this time, the RS packet through the Gateway (LVS) relay, LVS will do the source address translation (SNAT), the source address of the package to the VIP, so that the package to the client appears as if the LVS directly returned to it. The client cannot perceive the presence of the back-end Rs.

②DR Mode: Direct routing

In Dr Mode, the LVS and RS clusters are required to bind to the same VIP (RS is implemented by binding the VIP to loopback), but the difference with NAT is that the request is accepted by LVS and is returned directly to the user by a real service server (Realserver, RS). return without the LVS. In detail, when a request comes over, LVS only need to change the network frame MAC address to a certain Rs Mac, the package will be forwarded to the corresponding RS processing, note at this time the source IP and destination IP has not changed, LVS just do a bit counter switch. When RS receives the package that the LVS forwards, the link layer discovers that the MAC is its own, to the above network layer, discovers the IP is also own, so this package is legally accepted, RS does not perceive the existence of LVS in front. When RS returns to the response, it is returned directly to the source IP (that is, the user's IP) and is no longer passed LVS.

(3)DR load Balancing mode data distribution process does not modify the IP address, only modify the MAC address, because the actual processing request of the real physical IP address and data request destination IP address consistent, so do not need to through the Load Balancer Server for address translation, The response packet can be returned directly to the user's browser to avoid the Load Balancer server network card bandwidth becoming a bottleneck. Therefore, Dr Mode has better performance and is also the most widely used load balancing method in large-scale websites.

Third, build the actual combat: lvs+keepalived realize MySQL Load Balancer 3.1 experimental structure overview

(1) A server cluster of four Linux (CentOS 6.4) systems based on VMware Workstation, of which two load balancer servers (one for host and another for standby) The other two are real Web servers that provide HTTP services externally, using only the default CentOS HTTP service, and no other Tomcat-like, Jexus services are installed.

(2) This experiment is based on Dr Load Balancing model

Set a VIP (Virtual IP) to 192.168.5.200, the user only need to access this IP address to obtain Web services.

Load Balancer Host for 192.168.5.120

Standby machine for 192.168.5.121

MySQL Server A is 192.168.5.122

MySQL Server B is 192.168.5.123

3.2 Basic Preparatory work

The following work is for all servers, that is, to be configured on four servers:

(1) Network configuration

Refer to the CentOS network configuration to configure 4 virtual machines as static IP

(2) Set host name

① Modify the host name in the current session, execute the command hostname xxxx (here xxxx is the name you want to change)

② modify the hostname in the configuration file, execute command vi/etc/sysconfig/network (√ This step is generally required to permanently change the hostname)

Verification: Restart System reboot

(3) Binding of IP address to host name

Execute command vi/etc/hosts, add a line of content, as follows (from the node below with your own main, the experiment built two slave nodes):

192.168.5.120 Lvs-master

192.168.5.121 Lvs-slave

# Here are the two real-world server nodes for this experiment

192.168.5.122 LVS-MYSQL1

192.168.5.123 LVS-MYSQL2

Exit after saving

Verify:ping lvs-master

(4) shutting down the firewall

① Execute shutdown Firewall command:service iptables Stop

Validation:service iptables stauts

② Execute shutdown Firewall autorun command:chkconfig iptables off

Verification:Chkconfig--list | grep iptables

3.3 Configuring two MySQL servers

The following operations need to be done in two of the roles for the database server and do not need to be operated on the Load Balancer server:

(1) Installing and running MySQL

Refer to an article I wrote earlier: MySQL knowledge summary (i) Installation and configuration (Linux CentOS)

Create a database Bruce

Create the table test and insert the data:

Create TableTest (IDint, namevarchar(Ten));--Insert in aInsert  intoTest VALUES (1,'A');--Insert in BInsert  intoTest VALUES (2,'B');

(2) Editing the Realserver script file

① Enter the specified folder: cd/etc/init.d/

② creating a script file: VI realserver

sns_vip=192.168.5.200/etc/rc.d/init.d/functions Case"$"instart) ifconfig lo:0 $SNS _vip netmask 255.255.255.255Broadcast $SNS _VIP/sbin/route add-host $SNS _vip Dev lo:0Echo"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/NULL2>&1Echo"Realserver Start OK"       ;; Stop) ifconfig lo:0Down Route del $SNS _VIP>/dev/NULL2>&1Echo"0" >/proc/sys/net/ipv4/conf/lo/Arp_ignore Echo"0" >/proc/sys/net/ipv4/conf/lo/arp_announce Echo"0" >/proc/sys/net/ipv4/conf/all/Arp_ignore Echo"0" >/proc/sys/net/ipv4/conf/all/arp_announce Echo"Realserver stoped"       ;;*) echo"Usage: $ {start|stop}"Exit1Esacexit0

Here we set the virtual IP to: 192.168.5.200

③ Change the file permissions after saving the script file: chmod 755 realserver

④ Turn on Realserver services: Service Realserver start

3.4 Configuring the primary load Server

(1) Install keepalived related package

Yum Install-y keepalived

Under CentOS, it is easy to install packages with the Yum Install command, but only if your virtual machines are connected to the Internet;

(2) Editing the keepalived.conf configuration file

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

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

③ re-editing the keepalived configuration file: VI keepalived.conf

Global_defs {router_id Lvs_devel # Set the ID of the LVS, which should be unique within a network} vrrp_instance vi_1 {state Master #指定Keepalived的角色, master Main, BAC Kup to prepareInterfaceeth1 #指定Keepalived的角色, master-based, backup for standby virtual_router_id51#虚拟路由编号, the master must be consistent priority100#定义优先级, the higher the number, the higher the priority, the primary Dr must be larger than the standby Dr Advert_int1#检查间隔, default is 1s authentication {auth_type PASS auth_pass1111} virtual_ipaddress {192.168.5.200 #定义虚拟IP (VIP) for 192.168.2.33, multiple sets, one} per line} # define the VIP and Portvirtual_server for the LVS that provide services externally192.168.5.200 3306{Delay_loop6# Set Health check time in seconds Lb_algo WRR # set load scheduling algorithm for WLC lb_kind DR # set LVS implementation load mechanism, have n At, TUN, Dr three mode Nat_mask255.255.255.0Persistence_timeout0protocol TCP Real_server192.168.5.122 3306{# Specifies the IP address of the real Server1 weight3# Configure node weights, the larger the number the higher the weight tcp_check {connect_timeout10Nb_get_retry3Delay_before_retry3Connect_port 3306}} real_server192.168.5.1233306{# Specify the IP address of the real server2 weight3# Configure node weights, the larger the number the higher the weight tcp_check {connect_timeout10Nb_get_retry3Delay_before_retry3Connect_port 3306          }       }  }

(3) Open keepalived Service

Service keepalived Start
3.5 Configuring from a load server

The load server is roughly the same as the primary load server, except that you need to change the following two in the keepalived configuration file:

(1) Change state from master to backup

(2) Change priority from 100 to 99

Vrrp_instance vi_1 {State      BACKUP # changed here    to BACKUP interface eth1      virtual_router_id Wuyi Priority      99 # Here instead of 99, Master priority is    Advert_int 1      authentication {          auth_type PASS          auth_pass 1111      }      Virtual_ IPAddress {          192.168.80.200      }  }  

3.6 Verification Test

Log on to MySQL on 192.168.5.120

Mysql-h192.168.1.225-utest-ptest-p8066–ddbtest

Load balancing for MySQL with lvs+keepalived

Related Article

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.