MySQL master-slave replication with lvs+keepalived single-point write read load balancer High availability experiment "go"

Source: Internet
Author: User

First, the environment
Master (Host a): 192.168.1.1
Slave (Host B): 192.168.1.2
W-VIP (write): 192.168.1.3
R-VIP (Read): 192.168.1.4
Client (test): 192.168.1.100

Operating system version: CentOS release 6.4
MySQL database version: 5.6.14
Keepalived version: 1.2.7
LVS Version: 1.26

All environments are virtual machines

Second, design ideas

1. Server A and B, synchronize the data through the MySQL slave process.
2. Enable two virtual Ip:w-vip/r-vip via keepalived, one responsible for writing, one responsible for reading, and the realization of read and write separation.
3. When both A and B are present, W-VIP forwards the request to the host A,R-VIP and forwards the request to A and B for load balancing.
4. When host A is abnormal, B takes over the service, W-VIP/R-VIP at this time drift to host B, at this time the two virtual IP is Host B, to achieve high availability
5. When Host B is abnormal, R-VIP will kick the B out and the other unchanged

Third, the structure diagram

Iv. Software Installation
The following software is installed on both master and slave hosts:
1. mysql installation (slightly)
2. keepalived Installation
Yum Install keepalived
2. LVS Installation
Yum Install Ipvsadm

Five, configuration
1. Configure master-slave replication for MySQL (slightly)
2. Configure keepalived
The configuration on Master
Vi/etc/keepalived/keepalived.conf

[Plain]View Plaincopy
  1. ! Configuration File for Keepalived
  2. Global_defs {
  3. router_id mysql-ha
  4. }
  5. Vrrp_instance Vi_1 {
  6. State BACKUP
  7. Interface eth1
  8. VIRTUAL_ROUTER_ID 90
  9. Priority 100
  10. Advert_int 1
  11. Notify_master "/usr/local/mysql/bin/remove_slave.sh"
  12. Nopreempt
  13. Authentication {
  14. Auth_type PASS
  15. Auth_pass 1111
  16. }
  17. virtual_ipaddress {
  18. 192.168.1.3 Label Eth1:1
  19. 192.168.1.4 label Eth1:2
  20. }
  21. }
  22. Virtual_server 192.168.1.3 6603 {
  23. Delay_loop 2
  24. Lb_algo WRR
  25. Lb_kind DR
  26. Persistence_timeout 60
  27. Protocol TCP
  28. Real_server 192.168.1.1 6603 {
  29. Weight 3
  30. notify_down/usr/local/mysql/bin/mysql.sh
  31. Tcp_check {
  32. Connect_timeout 10
  33. Nb_get_retry 3
  34. Delay_before_retry 3
  35. Connect_port 6603
  36. }
  37. }
  38. }
  39. Virtual_server 192.168.1.4 6603 {
  40. Delay_loop 2
  41. Lb_algo WRR
  42. Lb_kind DR
  43. Persistence_timeout 60
  44. Protocol TCP
  45. Real_server 192.168.1.1 6603 {
  46. Weight 1
  47. notify_down/usr/local/mysql/bin/mysql.sh
  48. Tcp_check {
  49. Connect_timeout 10
  50. Nb_get_retry 3
  51. Delay_before_retry 3
  52. Connect_port 6603
  53. }
  54. }
  55. Real_server 192.168.1.2 6603 {
  56. Weight 3
  57. Tcp_check {
  58. Connect_timeout 10
  59. Nb_get_retry 3
  60. Delay_before_retry 3
  61. Connect_port 6603
  62. }
  63. }
  64. }


Keepalived configured as a service and boot up

[Plain]View Plaincopy
    1. cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
    2. cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
    3. cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
    4. Chkconfig--add keepalived
    5. Chkconfig--level 345 keepalived on


vi/usr/local/mysql/bin/remove_slave.sh

[Plain]View Plaincopy
    1. #!/bin/bash
    2. User=u1
    3. password=12345
    4. Log=/usr/local/mysql/log/remove_slave.log
    5. echo "' Date '" >> $log
    6. /USR/LOCAL/MYSQL/BIN/MYSQL-U$USER-P$PASSWORD-E "Set global read_only=off;reset master;stop Slave;change master to Mast er_host= ' localhost '; ">> $log
    7. /bin/sed-i ' s#read-only#\ #read-only# '/etc/my.cnf

vi/usr/local/mysql/bin/mysql.sh

[Plain]View Plaincopy
    1. #!/bin/bash
    2. /etc/init.d/keepalived stop


Configuration on the slave
Vi/etc/keepalived/keepalived.conf

[Plain]View Plaincopy
  1. ! Configuration File for Keepalived
  2. Global_defs {
  3. router_id mysql-ha
  4. }
  5. Vrrp_instance Vi_1 {
  6. State BACKUP
  7. Interface eth1
  8. VIRTUAL_ROUTER_ID 90
  9. Priority 99
  10. Advert_int 1
  11. Notify_master "/usr/local/mysql/bin/remove_slave.sh"
  12. Authentication {
  13. Auth_type PASS
  14. Auth_pass 1111
  15. }
  16. virtual_ipaddress {
  17. 192.168.1.3 Label Eth1:1
  18. 192.168.1.4 label Eth1:2
  19. }
  20. }
  21. Virtual_server 192.168.1.3 6603 {
  22. Delay_loop 2
  23. Lb_algo WRR
  24. Lb_kind DR
  25. Persistence_timeout 60
  26. Protocol TCP
  27. Real_server 192.168.1.2 6603 {
  28. Weight 3
  29. notify_down/usr/local/mysql/bin/mysql.sh
  30. Tcp_check {
  31. Connect_timeout 10
  32. Nb_get_retry 3
  33. Delay_before_retry 3
  34. Connect_port 6603
  35. }
  36. }
  37. }
  38. Virtual_server 192.168.1.4 6603 {
  39. Delay_loop 2
  40. Lb_algo WRR
  41. Lb_kind DR
  42. Persistence_timeout 60
  43. Protocol TCP
  44. Real_server 192.168.1.2 6603 {
  45. Weight 3
  46. notify_down/usr/local/mysql/bin/mysql.sh
  47. Tcp_check {
  48. Connect_timeout 10
  49. Nb_get_retry 3
  50. Delay_before_retry 3
  51. Connect_port 6603
  52. }
  53. }
  54. }

Keepalived configured as a service and boot up

[Plain]View Plaincopy
    1. cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
    2. cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
    3. cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
    4. Chkconfig--add keepalived
    5. Chkconfig--level 345 keepalived on

vi/usr/local/mysql/bin/remove_slave.sh

[Plain]View Plaincopy
    1. #!/bin/bash
    2. User=u1
    3. password=12345
    4. Log=/usr/local/mysql/log/remove_slave.log
    5. echo "' Date '" >> $log
    6. /USR/LOCAL/MYSQL/BIN/MYSQL-U$USER-P$PASSWORD-E "Set global read_only=off;reset master;stop Slave;change master to Mast er_host= ' localhost '; ">> $log
    7. /bin/sed-i ' s#read-only#\ #read-only# '/etc/my.cnf


vi/usr/local/mysql/bin/mysql.sh

[Plain]View Plaincopy
    1. #!/bin/bash
    2. /etc/init.d/keepalived stop


3. Configuring LVS
Master is the same as the configuration on slave:
vi/usr/local/bin/lvs_real.sh

[Plain]View Plaincopy
  1. #!/bin/bash
  2. # Description:config Realserver lo and apply Noarp
  3. sns_vip=192.168.1.3
  4. sns_vip2=192.168.1.4
  5. Source/etc/rc.d/init.d/functions
  6. Case "$" in
  7. Start
  8. Ifconfig lo:0 $SNS _vip netmask 255.255.255.255 broadcast $SNS _VIP
  9. Ifconfig lo:1 $SNS _vip2 netmask 255.255.255.255 broadcast $SNS _VIP2
  10. /sbin/route add-host $SNS _vip Dev lo:0
  11. /sbin/route add-host $SNS _vip2 Dev lo:1
  12. echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  13. echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  14. echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  15. echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  16. Sysctl-p >/dev/null 2>&1
  17. echo "Realserver Start OK"
  18. ;;
  19. Stop
  20. Ifconfig lo:0 Down
  21. Ifconfig lo:1 Down
  22. Route del $SNS _VIP >/dev/null 2>&1
  23. Route del $SNS _vip2 >/dev/null 2>&1
  24. echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  25. echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  26. echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  27. echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  28. echo "Realserver stoped"
  29. ;;
  30. *)
  31. echo "Usage: $ {start|stop}"
  32. Exit 1
  33. Esac
  34. Exit 0
[Plain]View Plaincopy
    1. chmod 755/usr/local/bin/lvs_real.sh
    2. echo "/usr/local/bin/lvs_real.sh start" >>/etc/rc.local


V. Start of Master and slave
1. Start MySQL on master
Service MySQL Start
2. Start MySQL on the slave
Service MySQL Start
3. Start the Realserver script on master
/usr/local/bin/lvs_real.sh start
4. Start the Realserver script on the slave
/usr/local/bin/lvs_real.sh start
5. Start the keepalived on master
Service keepalived Start
6. Start the keepalived on the slave
Service keepalived Start


VI. Testing
1. See if LVS can load-balance the forwarding
Execute separately on master and slave:
Ipvsadm-ln

2. Verify connectivity on the client:
Ping 192.168.1.3
Ping 192.168.1.4
Mysql-u u1-p12345-p 6603-h 192.168.1.3-e "show variables like ' server_id '"
Mysql-u u1-p12345-p 6603-h 192.168.1.4-e "show variables like ' server_id '"

3. Stop MySQL on master, read IP no automatically switch to slave, see if IP is removed from master MySQL
Execute separately on master and slave:
Ipvsadm-ln
Execute on the client:
Mysql-u u1-p12345-p 6603-h 192.168.1.3-e "show variables like ' server_id '"
Mysql-u u1-p12345-p 6603-h 192.168.1.4-e "show variables like ' server_id '"

4. Stop keepalived on master to see if the read-write VIP will migrate to slave.
Execute separately on master and slave:
Ipvsadm-ln
Execute on the client:
Mysql-u u1-p12345-p 6603-h 192.168.1.3-e "show variables like ' server_id '"
Mysql-u u1-p12345-p 6603-h 192.168.1.4-e "show variables like ' server_id '"

5. Stop MySQL on slave and read if IP is removed from slave mysql
Execute separately on master and slave:
Ipvsadm-ln
Execute on the client:
Mysql-u u1-p12345-p 6603-h 192.168.1.3-e "show variables like ' server_id '"
Mysql-u u1-p12345-p 6603-h 192.168.1.4-e "show variables like ' server_id '"

6. Restart the master System to see if the switching process is normal

Vii. Reference
Http://wenku.baidu.com/link?url=17K0UDApzfK18A8F5Of9wl0lmGx6AD_ 0pq8gyvqmcsbmsz16xjt2ji88lpaeid2qecs7piuxkzqlyatttduejxwzey6joibfbmt3vgtfdus
Http://blog.chinaunix.net/uid-23500957-id-3781918.html
Http://blog.chinaunix.net/uid-23500957-id-3781919.html
Http://blog.chinaunix.net/uid-20639775-id-3337471.html

MySQL master-slave replication with lvs+keepalived single-point write read load balancer High availability experiment "go"

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.