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
- ! Configuration File for Keepalived
- Global_defs {
- router_id mysql-ha
- }
- Vrrp_instance Vi_1 {
- State BACKUP
- Interface eth1
- VIRTUAL_ROUTER_ID 90
- Priority 100
- Advert_int 1
- Notify_master "/usr/local/mysql/bin/remove_slave.sh"
- Nopreempt
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.3 Label Eth1:1
- 192.168.1.4 label Eth1:2
- }
- }
- Virtual_server 192.168.1.3 6603 {
- Delay_loop 2
- Lb_algo WRR
- Lb_kind DR
- Persistence_timeout 60
- Protocol TCP
- Real_server 192.168.1.1 6603 {
- Weight 3
- notify_down/usr/local/mysql/bin/mysql.sh
- Tcp_check {
- Connect_timeout 10
- Nb_get_retry 3
- Delay_before_retry 3
- Connect_port 6603
- }
- }
- }
- Virtual_server 192.168.1.4 6603 {
- Delay_loop 2
- Lb_algo WRR
- Lb_kind DR
- Persistence_timeout 60
- Protocol TCP
- Real_server 192.168.1.1 6603 {
- Weight 1
- notify_down/usr/local/mysql/bin/mysql.sh
- Tcp_check {
- Connect_timeout 10
- Nb_get_retry 3
- Delay_before_retry 3
- Connect_port 6603
- }
- }
- Real_server 192.168.1.2 6603 {
- Weight 3
- Tcp_check {
- Connect_timeout 10
- Nb_get_retry 3
- Delay_before_retry 3
- Connect_port 6603
- }
- }
- }
Keepalived configured as a service and boot up
[Plain]View Plaincopy
- cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
- cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
- cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
- Chkconfig--add keepalived
- Chkconfig--level 345 keepalived on
vi/usr/local/mysql/bin/remove_slave.sh
[Plain]View Plaincopy
- #!/bin/bash
- User=u1
- password=12345
- Log=/usr/local/mysql/log/remove_slave.log
- echo "' Date '" >> $log
- /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
- /bin/sed-i ' s#read-only#\ #read-only# '/etc/my.cnf
vi/usr/local/mysql/bin/mysql.sh
[Plain]View Plaincopy
- #!/bin/bash
- /etc/init.d/keepalived stop
Configuration on the slave
Vi/etc/keepalived/keepalived.conf
[Plain]View Plaincopy
- ! Configuration File for Keepalived
- Global_defs {
- router_id mysql-ha
- }
- Vrrp_instance Vi_1 {
- State BACKUP
- Interface eth1
- VIRTUAL_ROUTER_ID 90
- Priority 99
- Advert_int 1
- Notify_master "/usr/local/mysql/bin/remove_slave.sh"
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.3 Label Eth1:1
- 192.168.1.4 label Eth1:2
- }
- }
- Virtual_server 192.168.1.3 6603 {
- Delay_loop 2
- Lb_algo WRR
- Lb_kind DR
- Persistence_timeout 60
- Protocol TCP
- Real_server 192.168.1.2 6603 {
- Weight 3
- notify_down/usr/local/mysql/bin/mysql.sh
- Tcp_check {
- Connect_timeout 10
- Nb_get_retry 3
- Delay_before_retry 3
- Connect_port 6603
- }
- }
- }
- Virtual_server 192.168.1.4 6603 {
- Delay_loop 2
- Lb_algo WRR
- Lb_kind DR
- Persistence_timeout 60
- Protocol TCP
- Real_server 192.168.1.2 6603 {
- Weight 3
- notify_down/usr/local/mysql/bin/mysql.sh
- Tcp_check {
- Connect_timeout 10
- Nb_get_retry 3
- Delay_before_retry 3
- Connect_port 6603
- }
- }
- }
Keepalived configured as a service and boot up
[Plain]View Plaincopy
- cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
- cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
- cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
- Chkconfig--add keepalived
- Chkconfig--level 345 keepalived on
vi/usr/local/mysql/bin/remove_slave.sh
[Plain]View Plaincopy
- #!/bin/bash
- User=u1
- password=12345
- Log=/usr/local/mysql/log/remove_slave.log
- echo "' Date '" >> $log
- /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
- /bin/sed-i ' s#read-only#\ #read-only# '/etc/my.cnf
vi/usr/local/mysql/bin/mysql.sh
[Plain]View Plaincopy
- #!/bin/bash
- /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
- #!/bin/bash
- # Description:config Realserver lo and apply Noarp
- sns_vip=192.168.1.3
- sns_vip2=192.168.1.4
- Source/etc/rc.d/init.d/functions
- Case "$" in
- Start
- Ifconfig lo:0 $SNS _vip netmask 255.255.255.255 broadcast $SNS _VIP
- Ifconfig lo:1 $SNS _vip2 netmask 255.255.255.255 broadcast $SNS _VIP2
- /sbin/route add-host $SNS _vip Dev lo:0
- /sbin/route add-host $SNS _vip2 Dev lo:1
- 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
- Ifconfig lo:1 Down
- Route del $SNS _VIP >/dev/null 2>&1
- Route del $SNS _vip2 >/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: $ {start|stop}"
- Exit 1
- Esac
- Exit 0
[Plain]View Plaincopy
- chmod 755/usr/local/bin/lvs_real.sh
- 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"