lab environment
CPU name | IP | VIP | Serve | Active and standby |
KA_LV_MYSQL_01 | 192.168.30.130 | 192.168.30.100 | keepalived、LVS、MySQL | MASTER |
KA_LV_MYSQL_02 | 192.168.30.131 | BACKUP |
Software version:
[root@KA_LV_MYSQL_01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@KA_LV_MYSQL_01 ~]# keepalived -v
Keepalived v1.2.13 (03/19,2015)
[root@KA_LV_MYSQL_01 ~]# mysql -V
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
[root@KA_LV_MYSQL_01 ~]# ipvsadm -v
ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)
Install:
Configure the local YUM source and install it directly using the software in the CD image.
[root@KA_LV_MYSQL_01 ~]# yum install -y mysql mysql-server keepalived ipvsadm
Configure MySQL master-slave:
[root@KA_LV_MYSQL_01 ~]# cp /etc/my.cnf{,.bak}
[root@KA_LV_MYSQL_01 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-master-bin
server-id= 1
binlog-ignore-db=mysql #Ignore these libraries
binlog-ignore-db=user
binlog-ignore-db=information_schema
It's worth noting: there are three ways to ignore which databases need to be ignored:
1. Specify using binlog- do -db, binlog-ignore- db parameters in /etc/my.cnf on the master node . 2. When using grant to specify permissions, specify the database 3. Use replicate - do -db=dbname to limit the database on the slave
[root@KA_LV_MYSQL_01 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 754
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication client,replication slave on *.* to root@'192.168.30.%' identified by '123456';
mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option;
mysql> flush privileges;
mysql> show binary logs;
+-------------------------+-----------+
| Log_name | File_size |
+-------------------------+-----------+
| mysql-master-bin.000001 | 19726 |
| mysql-master-bin.000002 | 765307 |
| mysql-master-bin.000003 | 125 |
| mysql-master-bin.000004 | 276 |
+-------------------------+-----------+
4 rows in set (0.00 sec)
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)
mysql> show binary logs;
+-------------------------+-----------+
| Log_name | File_size |
+-------------------------+-----------+
| mysql-master-bin.000001 | 106 |
+-------------------------+-----------+
1 row in set (0.00 sec)
Configure mysql slave node
[root @ KA_LV_MYSQL_02 ~] # vim /etc/my.cnf
[mysqld]
datadir = / var / lib / mysql
socket = / var / lib / mysql / mysql.sock
log-bin = mysql-slave-bin
server-id = 2
mysql> change master to
-> master_host='192.168.30.130',
-> master_user='root',
-> master_password='123456',
-> master_log_file='mysql-master-bin.000001',
-> master_port=3306,
-> master_log_pos=106;
Query OK, 0 rows affected (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.30.130
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 258
Relay_Master_Log_File: mysql-master-bin.000001
Slave_IO_Running: Yes #这两个Yes说明mysql主从配置成功
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 414
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
ERROR:
No query specified
Configure keepalived:
[root@KA_LV_MYSQL_01 ~]# cp /etc/keepalived/keepalived.conf{,.bak}
[root @ KA_LV_MYSQL_01 ~] # vim / etc / keepalived / keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA_LV_MYSQL_01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.30.100 } } virtual_server 192.168.30.100 3306 { delay_loop 6 lb_something wlc lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.30.130 3306 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.30.131 3306 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
Just modify the following two places on KA_LV_MYSQL_02.
router_id KA_LV_MYSQL_02
priority 100
Configure LVS
[root@KA_LV_MYSQL_01 network-scripts]# cp ifcfg-lo ifcfg-lo:1
[root@KA_LV_MYSQL_01 network-scripts]# vim ifcfg-lo:1
DEVICE=lo:1
IPADDR=192.168.30.100
NETMASK=255.255.255.255
#NETWORK=
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=192.168.30.100
ONBOOT=yes
NAME=loopback
[root@KA_LV_MYSQL_01 network-scripts]# service network restart
[root@KA_LV_MYSQL_01 ~]# service ipvsadm restart && service keepalived restart
[root@KA_LV_MYSQL_01 network-scripts]# chkconfig ipvsadm on
[root@KA_LV_MYSQL_01 network-scripts]# chkconfig keepalived on
[root@KA_LV_MYSQL_01 network-scripts]# ipvsadm -A -t 192.169.30.100:3306 -s wlc
[root@KA_LV_MYSQL_01 network-scripts]# ipvsadm -a -t 192.168.30.100:3306 -r 192.168.30.130 -g
Destination already exists
[root@KA_LV_MYSQL_01 network-scripts]# ipvsadm -a -t 192.168.30.100:3306 -r 192.168.30.131 -g
Destination already exists
[root@KA_LV_MYSQL_01 network-scripts]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.30.100:3306 wlc persistent 50
-> 192.168.30.130:3306 Local 3 0 0
-> 192.168.30.131:3306 Route 3 0 0
TCP 192.169.30.100:3306 wlc
Just do the corresponding operations on KA_LV_MYSQL_02.
test:
Login to MySQL on KA_LV_MYSQL_01
Log in to MySQL on KA_LV_MYSQL_02 and view
Manually stop MySQL on KA_LV_MYSQL_02 and check the log on KA_LV_MYSQL_01 as follows. It can be seen that when MySQL is stopped, keepalived can automatically remove the faulty node, and when the faulty node recovers, it will rejoin the cluster.
[root@KA_LV_MYSQL_01 network-scripts]# tail /var/log/messages
Apr 25 08:12:43 KA_LV_MYSQL_01 Keepalived_vrrp[70845]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.100
Apr 25 08:12:48 KA_LV_MYSQL_01 Keepalived_vrrp[70845]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.100
Apr 25 08:16:15 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: TCP connection to [192.168.30.131]:3306 failed !!!
Apr 25 08:16:15 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Removing service [192.168.30.131]:3306 from VS [192.168.30.100]:3306
Apr 25 08:16:15 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Remote SMTP server [127.0.0.1]:25 connected.
Apr 25 08:16:15 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: SMTP alert successfully sent.
Apr 25 08:18:45 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: TCP connection to [192.168.30.131]:3306 success.
Apr 25 08:18:45 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Adding service [192.168.30.131]:3306 to VS [192.168.30.100]:3306
Apr 25 08:18:45 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Remote SMTP server [127.0.0.1]:25 connected.
Apr 25 08:18:45 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: SMTP alert successfully sent.
Manually turn off keepalived on KA_LV_MYSQL_01, and check the logs of the active and standby nodes. It can be seen that KA_LV_MYSQL_02 has become the master and has taken over the VIP.
[root@KA_LV_MYSQL_01 ~]# tail /var/log/messages
Apr 25 09:09:49 KA_LV_MYSQL_01 Keepalived[70843]: Stopping Keepalived v1.2.13 (03/19,2015)
Apr 25 09:09:49 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Removing service [192.168.30.130]:3306 from VS [192.168.30.100]:3306
Apr 25 09:09:49 KA_LV_MYSQL_01 Keepalived_vrrp[70845]: VRRP_Instance(VI_1) sending 0 priority
Apr 25 09:09:49 KA_LV_MYSQL_01 Keepalived_healthcheckers[70844]: Removing service [192.168.30.131]:3306 from VS [192.168.30.100]:3306
Apr 25 09:09:49 KA_LV_MYSQL_01 Keepalived_vrrp[70845]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 25 09:09:49 KA_LV_MYSQL_01 kernel: IPVS: __ip_vs_del_service: enter
[root@KA_LV_MYSQL_02 ~]# tail /var/log/messages
Apr 25 09:09:50 KA_LV_MYSQL_02 Keepalived_vrrp[6325]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 25 09:09:51 KA_LV_MYSQL_02 Keepalived_vrrp[6325]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 25 09:09:51 KA_LV_MYSQL_02 Keepalived_vrrp[6325]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 25 09:09:51 KA_LV_MYSQL_02 Keepalived_vrrp[6325]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.100
Apr 25 09:09:51 KA_LV_MYSQL_02 Keepalived_healthcheckers[6324]: Netlink reflector reports IP 192.168.30.100 added
Apr 25 09:09:56 KA_LV_MYSQL_02 Keepalived_vrrp[6325]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.100
The LVS cluster on KA_LV_MYSQL_02 is normal.
[root@KA_LV_MYSQL_02 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.30.100:3306 wlc persistent 50
-> 192.168.30.130:3306 Route 3 0 0
-> 192.168.30.131:3306 Local 3 0 0