CENTOS6.3 using Keepalived to build double-MySQL+ double-machine hot standby


Previous blog post describes how to configure MYSQLand each other.
See http://www.centoscn.com/MySQL/2015/1030/6351.html
Shows you how to tie the former implement Keepalived double-machine hot standby
System environment: CentOS 6.3 x64
MySQL version: MySQL-5.6.10
Keepalived version: keepalived-1.2.7
MySQL-VIP:192.168.7.253
MySQL-master1:192.168.7.201
MySQL-master2:192.168.7.249
First turn off iptables and SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
—————
SELINUX=disabled
—————
Note: If the line needs to open the iptables, need to add a rule to pass keepalived VRRP
# iptables -A INPUT -p vrrp -j ACCEPT
1. the keepalived in MySQL-master1:192.168.7.201 Server installation and configuration
Compile and install the actual native kernel version shall prevail
# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
# tar zxvf keepalived-1.2.7.tar.gz
# cd keepalived-1.2.7
# ./configure –prefix=/usr/local/keepalived –with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64
# make && make install
Set keepalived boot scripts
# 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 keepalived on
Create a new configuration file, default keepalived starts to search for configuration files in/etc/keepalived directory
# mkdir /etc/keepalived
# vi /etc/keepalived/keepalived.conf
——————
global_defs {
notification_email {
lzyangel@126.com
}
# When the main and backup device is changed, notify by mail
notification_email_from lzyangel@126.com
smtp_server stmp.126.com
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1{
# Define in the initialize state equipment
state BACKUP
# Note the network adapter interface
interface eth0
virtual_router_id 51
# Priorities, another to 90
priority 100
advert_int 1
# Did not take the initiative to seize resources
nopreempt
authentication {
# Authentication methods, which can be either PASS or AH authentication method
auth_type PASS
# Authentication password
auth_pass 1111
}
virtual_ipaddress {
# Virtual IP address, as the state changes and delete
192.168.7.253
}
}
virtual_server 192.168.7.253 3306 {
# Check for real_server every 2 seconds
delay_loop 2
# LVS algorithm
lb_algo wrr
Model # LVS
lb_kind DR
# Session time
persistence_timeout 60
protocol TCP
real_server 192.168.7.201 3306 {
# Weight
weight 3
# Detect script that is executed after the service down
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# Connection timeout
connect_timeout 10
# Number of reconnection
nb_get_retry 3
# Reconnect time interval
delay_before_retry 3
# Health check port
connect_port 3306
}
}
}
———————-
Write scripts to perform testing services down after
# vi /etc/rc.d/keepalived.sh
————-
#!/bin/sh
/etc/init.d/keepalived stop
————-
# chmod +x /etc/rc.d/keepalived.sh
Note: this script is used for notify_down option to the configuration file of the above, keepalived real_server service state is checked using the notify_down option.
When real_server service failure will trigger the script.
We can see, the script command:
Through a process of pkill keepalived forces kill keepalived, enables MySQL Auto shift.
In addition, we don’t have to worry about two MySQL data at updates, because the keepalived configuration on each MySQL only native MySQL IP+VIP, rather than two MySQL IP+VIP.
The above script is in a test environment in order to achieve a VIP switch function, production environment we recommend using a script
———————————
#!/bin/bash
# Environment variables
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
# Suspend execution for 5 seconds, to prevent artificial normal database restart
sleep 5
# Mysql_id (survival = 1 die =0)
mysql_id=’ps -C mysqld –noheader |wc -l’
Mysql_id die # judgment, then restart MySQL, if you still cannot start the MySQL to kill keepaliaved process switch VIP
if [ $mysql_id -eq 0 ];then
/etc/init.d/mysqld restart
sleep 5
if [ $mysql_id -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
———————————
Start the keepalived
# /etc/init.d/keepalived start
View the connection status
# ps -aux | grep keepalived
Returns:
—————
root 1387 1 0 21:13 ? 00:00:00 keepalived -D
root 1390 1387 0 21:13 ? 00:00:00 keepalived -D
root 1391 1387 0 21:13 ? 00:00:00 keepalived -D
root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived
——————–
Test
See VIP information
# ip addr
————————–
…………
inet 192.168.7.201/24 brd 192.168.7.255 scope global eth0
inet 192.168.7.253/32 scope global eth0
inet6 fe80::20c:29ff:feb2:9199/64 scope link
valid_lft forever preferred_lft forever
————————–
Note: If the master/slave switch occurs when the VIP, VIP does not close automatically, from the normal open, resulting in access to VIP, resulting in conflicting situations, you can manually enter the following command to remove the VIP address
# IP addr del virtual IP dev eth0
But this approach is only a temporary solution, when manually after you remove the VIP, restart keepalived open VIP services will not function properly, you need to restart the server to recover.
View the VRRP communications
# tcpdump vrrp
————————-
…..
18:10:56.365730 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
18:10:57.366825 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
18:10:58.367914 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
————————-
Note: If the primary keepalived hang, where the log will switch to keeplived information
To find a PC LAN, and then ping MySQL VIP, VIP is able to ping the access MySQL
Stop the MySQL service, keepalived health check program will trigger our scripts, to kill the keepalived process
# service mysqld stop
# ps -aux | grep keepalived
Returns no results
2. the keepalived in MySQL-master2:192.168.7.249 Server installation and configuration
Compile and install the actual native kernel version shall prevail
# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
# tar zxvf keepalived-1.2.7.tar.gz
# cd keepalived-1.2.7
# ./configure –prefix=/usr/local/keepalived –with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64
# make && make install
Set keepalived boot scripts
# 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 keepalived on
Create a new configuration file, default keepalived starts to search for configuration files in/etc/keepalived directory
# mkdir /etc/keepalived
# vi /etc/keepalived/keepalived.conf
——————
global_defs {
notification_email {
lzyangel@126.com
}
notification_email_from lzyangel@126.com
smtp_server stmp.126.com
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1{
# Two configured here are the BACKUP
state BACKUP
# Note the network adapter interface
interface eth0
virtual_router_id 51
# Priorities, another 100 to
priority 90
advert_int 1
# Did not take the initiative to seize resources
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.7.253
}
}
virtual_server 192.168.7.253 3306 {
# Check for real_server every 2 seconds
delay_loop 2
# LVS algorithm
lb_algo wrr
Model # LVS
lb_kind DR
# Session time
persistence_timeout 60
protocol TCP
real_server 192.168.7.249 3306 {
weight 3
# Detect script that is executed after the service down
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# Connection timeout
connect_timeout 10
# Number of reconnection
nb_get_retry 3
# Reconnect time interval
delay_before_retry 3
# Health check port
connect_port 3306
}
}
}
———————-
Write scripts to perform testing services down after
# vi /etc/rc.d/keepalived.sh
————-
#!/bin/sh
/etc/init.d/keepalived stop
————-
# chmod +x /etc/rc.d/keepalived.sh
Start the keepalived
# /etc/init.d/keepalived start
View the connection status
# ps -aux | grep keepalived
Returns:
—————
root 1387 1 0 21:13 ? 00:00:00 keepalived -D
root 1390 1387 0 21:13 ? 00:00:00 keepalived -D
root 1391 1387 0 21:13 ? 00:00:00 keepalived -D
root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived
——————–
At this point the configuration is complete, Web MySQL-VIP:192.168.7.253 virtual address needs to be configured only in the background
Physically connected the master database, is subject to which master before MySQL services.
When Master1:192.168.7.201 MySQL port is blocked or the server shuts down, keepalived automatically jump to Master2:192.168.7.249.
Because of the synchronization of two database data, user access to the MySQL-VIP:192.168.7.253 virtual address, so the site transferred to the data connection will be seamless and transparent Master2 service
To achieve double-machine hot standby + data synchronization feature. Real time availability guaranteed site database.
Note: when a master server hang after the restoration, you also need to open the MYSQL service and keepalived service, will guarantee that another server if you hang a seamless transfer.
It is recommended that MySQL and keeplived sets the service startup.
# chkconfig mysqld on
# chkconfig keepalived on
———-大功告成————

Leave a Reply

Your email address will not be published. Required fields are marked *