Linux keepalived + LVS implements mysql slave database load balancing and keepalivedlvs
Prerequisites:
Reference link:
Http://www.osyunwei.com/archives/7464.html
Ps: the above are the main references for this operation. I am very grateful for the contributions made by the author of this article. The main purpose of my essay is
Describes some problems and supplements encountered during the deployment of the above materials.
Purpose:
1. Solves the functional defects of web read/write splitting and achieves load balancing and high availability of database slave Databases
2. Simplify the configuration of the web and main functional modules and use vip to take over
Lab environment:
Operating System: redhat6.4 64-bit # The current mainstream version should be above 6.4, mainly due to hardware requirements
DB version: mysql5.6 # There is nothing to say. Choosing a version depends on the application requirements.
Lvs HOST: 192.168.1.180 # deploy lvs + keepalived for VIP management and database read/write requests Distribution
Slave database IP: 192.168.20.182, 192.168.20.183 # mysql_slave one master and two slave
Testing Machine: 192.168.20.181 # used to test the functions of the entire architecture
VIP: 192.168.255.253 # virtual IP address for request Distribution
1. install and configure MySQL 20.182 in the environment (20.183 and)
Rpm-ivh
MySQL-server-5.6.16-1.el6.x86_64.rpm
MySQL-devel-5.6.16-1.el6.x86_64.rpm
MySQL-shared-5.6.16-1.el6.x86_64.rpm
MySQL-client-5.6.16-1.el6.x86_64.rpm
MySQL-shared-compat-5.6.16-1.el6.x86_64.rpm
The security of mysql5.6 is improved. After installation is complete, the following prompt is displayed:
You will find that password in '/root/. mysql_secret'
Cat/root/. mysql_secret
# The random password set for the root user at Mon Aug 31 08:28:44 2015 (local time): 06 tIwUud
After entering mysql with the PASSWORD: SET password = PASSWORD ('xxxxxx'); reset the password. Others are the same as normal mysql installation.
Master-slave database construction (omitted)
Well ...... I am not a lazy, but I am too much content on the Internet. I have no value to write when I click it. If I am interested in it, I will leave it empty.
You can search for them at will.
Network Environment configuration (all machines)
Because my production environment has a separate Cisco Firewall, the system firewalls of all my servers are disabled, including selinux, otherwise it will be too troublesome.
Some inexplicable errors may be caused by iptable and selinux.
1. Disable selinux:
Modify selinux = "" in the/etc/SELINUX/config file to disabled, and then restart.
If you do not want to restart the system, run the setenforce 0 command.
2. Disable iptables:
Service iptables stop chkconfig iptables off II. LVS + keepalive host creation (192.168.1.180). To simplify the process, do not manage the high availability configuration of the machine, 1. install LVSyum install ipvsadm # the Linux kernel of the current version has been integrated with lvs, this is the management module modprobe ip_vs # The load module executes ipvsadm to test whether the installation is successful 2. install keepalivedyum install gcc-c ++ make openssl-devel kernel-devel ncurses-devel
Download keeplived: http://www.keepalived.org/download.html
Cd/usr/local/src
Wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
Ps: due to the different installation time, my new production environment is centos6.5 and the test environment is redhat6.4, and my production has always been a redhat series, I have been using manual rpm and compilation and installation. Although it is difficult, I am able to train my hands-on skills and some unclear understanding of the system, in addition, yum itself is not very secure and is not as secure as manual installation. I suggest you use yum if it is not available, but manually uninstall it, this is my bottom line.
Tar-zxvf keepalived-1.2.19.tar.gz
Cd keepalived-1.2.19
./Configure # configuration. The following prompt must be displayed, indicating that the configuration is correct to continue installation
Use IPVS Framework: Yes
S sync daemon support: Yes
Use VRRP Framework: Yes
Make # Edit
Make install # install
Cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
Mkdir/etc/keepalived
Cp/usr/local/etc/keepalived. conf/etc/keepalived/
Cp/usr/local/sbin/keepalived/usr/sbin/
Cp/usr/local/etc/rc. d/init. d/keepalived/etc/rc. d/init. d/
Chmod + x/etc/rc. d/init. d/keepalived # Add execution permission
Chkconfig keepalived on # Set startup
Service keepalived start # start
Service keepalived stop # disable
Service keepalived restart # restart
Ps: the commands directly assisted by the original author are basically writable. Just follow the commands.
Ps2: References this will install perl-related environments, because his slave database status monitoring script is required, but I did not add it here, mainly considering the following situations
1. I have a separate database slave database status monitoring and warning script (see the previous two original articles)
2. keeplived itself detects port 3306. If a database exception occurs, it can be detected and processed.
3. I hope to continue to provide services even if there is a problem with slave Database Synchronization, because the online environment requires smooth service and I can accept the price of temporary non-synchronization.
3. Configure keepalived
Cp/etc/keepalived. conf/etc/keepalived. conf-bak
>/Etc/keepalived. conf # Clear the configuration file
Vi/etc/keepalived. conf # paste the following content directly.
==========================================! Configuration File for rule global_defs {router_id priority} vrrp_instance VI_1 {state MASTER interface eth0 priority 51 priority 100 priority 1 authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.255.253} virtual_server 192.168.255.253 3306 {limit 6 lb_algo wlc lb_kind DR # persistence_timeout 120 protocol TCP real_server 192.168.20.182 3306 {wei Ght 1 TCP_CHECK {connect_timeout 3 nb_get_retry 3 limit 3 connect_port 3306} real_server 192.168.1_183 3306 {weight 1 TCP_CHECK {connect_timeout 3 limit 3 limit 3 connect_port 3306 }}======== ================================================ # wp # Save and exit. Note: it is recommended that you copy the above content to the editor and check whether the brackets are complete and matched. This is very important. Iii. Database slave database configuration (192.168.20.182 and 183) 1. bind an LVS Virtual Server (VIP): 192.168.255.253 to lo: 0
# Add the following code for editing:
Vi/etc/rc. d/init. d/realserver ========================================== ==================
#! /Bin/sh
# Chkconfig:-80 90
# Description: realserver
# Mysql_vip start realserver
Mysql_vip = 192.168.255.253
./Etc/rc. d/init. d/functions
Case "$1" in
Start)
Ifconfig lo: 0 $ mysql_vip netmask 255.255.255.255 broadcast $ mysql_vip
/Sbin/route add-host $ mysql_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 $ mysql_vip>/dev/null 2> & 1
Echo "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: $0 {start | stop }"
Exit 1
Esac
Exit 0========================================================== ============================
Chmod + x/etc/rc. d/init. d/realserver # Add the script execution permission
Chkconfig realserver on # Add boot start
/Etc/rc. d/init. d/realserver start # enable. The stop parameter is disabled.
2. Configure to allow ARP forwarding vi/etc/sysctl. conf ================================
Net. ipv4.ip _ forward = 1 # modify the original configuration
# MYSQL-VIP-NewAdd # This is the new configuration
Net. ipv4.conf. lo. arp_ignore = 1
Net. ipv4.conf. lo. arp_announce = 2
Net. ipv4.conf. all. arp_ignore = 1
Net. ipv4.conf. all. arp_announce = 2=============================/Sbin/sysctl-p # Make the configuration take effect immediately
Iv. Server Load balancer Function Testing 1. LVS host (20.180:
Service keepalived start # start keepalived
Ipvsadm-Ln # Check The lvs running status
2. Database slave database (20.182, 20.183)
/Etc/rc. d/init. d/realserver start
VIP bound to lo: 0
3. Test the Server Load balancer Function
20.182: Execute service mysql stop
Run on 20.180 (LVS host)
Ipvsadm-Ln # Check The lvs running status
We can see that the 20.182 server has been removed. Similarly, after the server is restarted, it will be added to the service again.
4. VIP Database Function Test
Run the database connection command on the test server (1.181 ).
Mysql-uroot-pXXXXXX-h192.168.255.253
Or use the telnet 192.168.255.253 3306 command to try the connection. If you are prompted to enter the password, the connection is normal.
FAQ:
1. I can ping but telnet fails. Why?
A: Many people, including myself, have encountered this problem on the Internet. The VIP can be pinged, but the telnet and mysql connection commands cannot be accessed.
This problem has plagued me for a long time. The intermediate test was stopped for two months, because I could not find the cause, but it was a joke. The reason turned out to be: I didn't use a testing machine,
All my test commands are executed on the LVS host, while the LVS host is bound with a VIP and does not have the mysql service, so it can certainly be pinged, but no request is forwarded.
In this case, the service is definitely unavailable.
2. What are the troubles of some commands in the original documents?
A: For example, ipvsadm-L does not display a specific IP address on my server. It only displays localhost. At one time, I thought the service was unavailable and I had a connection with it, but it is okay to execute ipvsadm-Ln.
3. Is the database slave database status check script used?
A: I think this should be taken into consideration based on the specific requirements of the business system. What my system requires is continuous service provision, and the requirements for data consistency are not that high, so I only need to configure the synchronization status
Alarm, and ensure that the service can be handled in a timely manner, without the need to stop the service so harsh. In addition, the use of perl or shell scripts is actually no different.
OK! The whole solution is like this. Some steps are omitted in the middle, such as the establishment of the master-slave Database Service and the high availability of LVS. I have built these two services countless times before, I am too lazy to write more, and this individual service has already had countless strategies online, and the value of writing is not very great. Let's study it.
In addition, the level is limited. If there are some omissions and errors, please point out that I will modify them in time. If there is a better solution, you can share it.