拓撲結構:mysql
實驗規劃:redis
把後端的mysql作成「一主多從」複製架構,主的mysql將提供「寫」操做,從的mysql將提供「讀」操做,兩臺服務器作lvs-keepalived實現高可用,正常狀況下,一臺服務器得到對mysql「寫」操做的vip,另外一臺服務器得到對mysql"讀"操做的vip,當用戶請求鏈接「寫」vip操做時,將被lvs調度到主的mysql上,當用戶請求鏈接「讀」vip操做時,將被lvs調度到從的mysql上。sql
實驗環境:數據庫
ipvsadm-1.26-4.el6.x86_64vim
keepalived-1.2.13-4.el6.x86_64後端
Linux centos-server 2.6.32-504.el6.x86_64centos
mysql-5.1
bash
首先配置主從複製:
服務器
配置主mysql [root@centos-server ~]# vim/etc/my.cnf #修改my.cnf [mysqld] port = 3306 binlog-do-db=dragon #同步的數據庫 replicate-ignore-db=mysql #不一樣步的數據庫 replicate-ignore-db=information_schema server-id= 1 #server-id要惟一 log-bin=mysql-bin mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000037 | 106 | dragon | | +------------------+----------+--------------+------------------+ 1 row in set (0.08 sec) mysql> grant replication slave on *.* to 'back'@'192.168.150.%' identified by 'qwe123'; 1 row in set (0.01 sec) #添加用戶"back",SLAVE用於登陸本機複製數據庫日誌文件 [root@centos-server ~]# cd /usr/local/mysql/var/ [root@centos-server var]# tar zcf dragon.tar.gz dragon/ scp dragon.tar.gz root@192.168.150.137:/usr/local/mysql/var/ #把數據庫複製到SLAVE上 配置從mysql: 修改my.cnf的server-id爲2 [root@drbd1 ~]# cd /usr/local/mysql/var/ [root@drbd1 var]#tar xf dragon.tar.gz #先解壓縮複製過來的數據庫文件 [root@drbd1 var]#mysql -uroot -pqwe123 mysql> slave stop; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='192.168.150.128',master_user='back',masaster_log_pos=106; #使用用戶"back"複製MASTER的"mysql-bin.000025"日誌 Query OK, 0 rows affected (0.03 sec) mysql> slave start; Query OK, 0 rows affected (0.01 sec) mysql> exit [root@drbd1 var]# service mysqld restart #重啓SLAVE的mysql [root@drbd1 var]# mysql -uroot -pqwe123 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.150.128 Master_User: back Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000037 Read_Master_Log_Pos: 106 Relay_Log_File: drbd1-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000037 Slave_IO_Running: Yes Slave_SQL_Running: Yes
在從mysql配置提供「讀」操做的vip,而且禁止apr響應和添加路由條目:架構
[root@centos-server ~]# vim /etc/sysctl.conf net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.default.arp_ignore=1 net.ipv4.conf.default.arp_announce=2 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.lo.arp_announce=2 [root@centos-server ~]# sysctl -p [root@centos-server ~]# ifconfig lo:0 192.168.150.201/32 [root@centos-server ~]# route add -host 192.168.150.201 dev lo:0
在從mysql配置提供「寫」操做的vip,而且禁止apr響應和添加路由條目:
[root@centos-server ~]# vim /etc/sysctl.conf net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.default.arp_ignore=1 net.ipv4.conf.default.arp_announce=2 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.lo.arp_announce=2 [root@centos-server ~]# sysctl -p [root@centos-server ~]# ifconfig lo:0 192.168.150.200/32 [root@centos-server ~]# route add -host 192.168.150.200 dev lo:0
配置LVS調度:
[root@centos-server ~]# sysctl -w net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.all.send_redirects = 0 [root@centos-server ~]# sysctl -w net.ipv4.conf.default.send_redirects=0 net.ipv4.conf.default.send_redirects = 0 [root@centos-server ~]# sysctl -w net.ipv4.conf.eth3.send_redirects=0 net.ipv4.conf.eth2.send_redirects = 0 [root@centos-server ~]# ipvsadm -A -t 192.168.150.200:3306 -s rr [root@centos-server ~]# ipvsadm -a -t 192.168.150.200:3306 -r 192.168.150.129 -g -w 1 [root@centos-server ~]# ipvsadm -A -t 192.168.150.201:3306 -s rr [root@centos-server ~]# ipvsadm -a -t 192.168.150.201:3306 -r 192.168.150.128 -g -w 1 [root@centos-server ~]# service ipvsadm save
配置keepalived實現高可用:
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 220.181.12.11 smtp_connect_timeout 30 router_id haproxy_DEVEL } vrrp_instance VI_1 { state mastert #另外一端keepalived配置爲slave interface eth3 #掛載vip的本地網卡 virtual_router_id 51 priority 100 #另外一配置文件爲「80」 advert_int 1 authentication { auth_type PASS auth_pass 1234 } virtual_ipaddress { 192.168.150.200/24 dev eth3 } } virtual_server 192.168.150.200/24 3306 { #後端mysql的健康檢測 delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.150.128 3306 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } vrrp_instance VI_2 { state slave #另外一端keepalived配置爲「master」 interface eth3 virtual_router_id 52 priority 80 #另外一端keepalived配置爲「100」 advert_int 1 authentication { auth_type PASS auth_pass 1234 } virtual_ipaddress { 192.168.150.201/24 dev eth3 } } virtual_server 192.168.150.201/24 3306 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.150.129 3306 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } [root@centos-server ~]# service keepalived restart #兩端keepalived重啓
鏈接vip測試:
請求「寫」操做的vip時,可以被正常調度
[root@drbd2 var]# mysql -uback -pqwe123 -h 192.168.150.200 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 161 Server version: 5.1.55-log Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
請求「讀」操做時,可以被正常調度
[root@drbd2 var]# mysql -uread -pqwe123 -h 192.168.150.201 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.1.55-log Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
查看lvs當前處理的調度:
[root@centos-server ~]# ipvsadm -Lnc IPVS connection entries pro expire state source virtual destination TCP 14:56 ESTABLISHED 192.168.150.129:53464 192.168.150.200:3306 192.168.150.128:3306 TCP 00:46 NONE 192.168.150.129:0 192.168.150.200:3306 192.168.150.128:3306 [root@centos-server ~]# ipvsadm -Lnc IPVS connection entries pro expire state source virtual destination TCP 01:25 FIN_WAIT 192.168.150.128:49274 192.168.150.201:3306 192.168.150.129:3306 TCP 14:41 ESTABLISHED 192.168.150.128:49275 192.168.150.201:3306 192.168.150.129:3306
keepalived實現故障轉移:
停掉其中一臺HA的網卡 [root@centos-server ~]# ifdown eth3
虛擬IP被轉移