Mysql雙主 keepalived+lvs實現mysql高可用性

MySQL複製

可以保證數據的冗餘的同時能夠作讀寫分離來分擔系統壓力,若是是主主複製還能夠很好的避免主節點的單點故障。可是MySQL主主複製存在一些問題沒法知足咱們的實際須要:未提供統一訪問入口來實現負載均衡,若是其中master宕掉的話須要手動切換到另一個master,而不能自動進行切換。html

這篇文章下面要介紹如何經過LVS+Keepalived的方式來是實現MySQL的高可用性,同時解決以上問題。前端

Keepalived和LVS介紹

Keepalivedmysql

是一個基於VRRP(虛擬路由冗餘協議)可用來實現服務高可用性的軟件方案,避免出現單點故障。Keepalived通常用來實現輕量級高可用性,且不須要共享存儲,通常用於兩個節點之間,常見有LVS+Keepalived、Nginx+Keepalived組合。linux

LVS算法

(Linux Virtual Server)是一個高可用性虛擬的服務器集羣系統。本項目在1998年5月由章文嵩博士成立,是中國國內最先出現的自由軟件項目之一。LVS主要用於多服務器的負載均衡,做用於網絡層。LVS構建的服務器集羣系統中,前端的負載均衡層被稱爲Director Server;後端提供服務的服務器組層被稱爲Real Server。經過下圖能夠大體瞭解LVS的基礎架構。sql

LVS有三種工做模式,分別是DR(Direct Routing 直接路由)、TUN(Tunneling IP隧道)、NAT(Network Address Translation 網絡地址轉換)。其中TUN模式可以支持更多的Real Server,但須要全部服務器支持IP隧道協議;DR也能夠支持至關的Real Server,但須要保證Director Server虛擬網卡與物理網卡在同一網段;NAT擴展性有限,沒法支持更多的Real Server,由於全部的請求包和應答包都須要Director Server進行解析再生,影響效率。 同時,LVS負載均衡有10中調度算法,分別是rr、wrr、lc、wlc、lblc、lblcr、dh、sh、sed、nq(詳細介紹本文不在說明)數據庫

本文中將利用LVS實現MySQL的讀寫負載均衡,Keepalived避免節點出現單點故障。​​​vim

LVS+Keepalived配置

環境準備後端

LVS1:172.30.8.192服務器

​LVS2:172.30.8.193

MySQL Server1:172.30.8.190

MySQL Server2:172.30.8.191

VIP:172.30.8.200

OS: CentOS 6.5

 

 

Mysql安裝及雙主配置

MySQL:5.1.73

本文爲了方便直接採用的yum安裝方式安裝將不在介紹

全局配置(即MAster,Slave都需配置)

1)修改配置文件

#vim /etc/my.cnf
添加
log-bin=mysql-bin
server-id=190    

 修改完須要重啓數據庫

 注:server-id是惟一id通常都採用ip末尾

2)分配複製權限,主庫和從庫均須要執行

mysql> grant replication client,replication slave on *.* to root@'172.30.8.%' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

2)清空日誌文件,主從庫都是默認開啓二進制日誌文件

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     26636 |
| mysql-bin.000002 |   1069399 |
| mysql-bin.000003 |     26636 |
| mysql-bin.000004 |   1069399 |
| mysql-bin.000005 |       536 |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

 須要注意的是,若是不想清空日誌文件的話,須要記錄當前master的log_file和log_pos,並在下面啓用複製操做時指定這兩個參數或者在slave的配置文件指定。

Slave配置

1)啓用複製
讓slave鏈接master並開始重作master二進制日誌中的事件

mysql> change master to  master_host='172.30.8.190',master_user='root',master_password='1231234',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=107;

master_log_pos若是是日誌的開始位置值能夠爲0;master_log_file是初始日誌文件。若是master日誌沒有被清空,這裏就是當前master的日誌信息

要注意的是,默認狀況下,會同步該用戶下全部的DB,若是想限定哪些DB,有3種思路

  1. 在master上的/etc/my.inf中經過參數binlog-do-db、binlog-ignore-db設置須要同步的數據庫。
  2. 在執行grant分配權限操做的時候,限定數據庫
  3. 在slave上限定數據庫使用replicate-do-db=dbname

2)開啓slave

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

 3)確認Slave是否和Mater成功通訊。若是 Slave_IO_Running和Slave_SQL_Running都是yes,則證實配置成功

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.30.8.190
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1042
               Relay_Log_File: mysqld-relay-bin.000020
                Relay_Log_Pos: 510
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            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: 1042
              Relay_Log_Space: 811
              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

 至此mysql主從配置完畢,若是雙主則操做相反便可。

附加:

1)mysql修改密碼

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password("1231234") where user="root";
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 2)受權遠程鏈接

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1231234' WITH GRANT OPTION;

 同步測試

1)Master建立數據庫

mysql> create database jingzi;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jingzi             |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

 2)slave查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jingzi             |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

 經過以上驗證,能夠看到主服務器上的修改可以正常同步到從服務器。

 

Keepalived安裝及LVS安裝(如下操做在lvs服務器主+從上操做)

Keepalived安裝

keepalived下載地址

須要安裝如下軟件包

#yum install -y kernel-devel openssl openssl-devel gcc* (若是後邊編譯報錯缺乏什麼包安裝什麼包就ok了)

 安裝

#wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

#tar -zxvf keepalived-1.2.13.tar.gz​

​#cd keepalived-1.2.13

#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.5.1.el6.x86_64/

默認狀況下keepalived啓動時會去/etc/keepalived目錄下找配置文件,將須要的配置文件拷貝到指定位置​

# mkdir /etc/keepalived

#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/etc/keepalived/keepalived.conf  /etc/keepalived/

​#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

​#chkconfig keepalived on

LVS安裝

ipvsadm下載地址

須要安裝如下軟件包

#yum install -y libnl* popt*​ kernel-headers   popt-static

 查看是否加載lvs模塊

#modprobe -l |grep ipvs​

 解壓安裝

#ln -s /usr/src/kernels/2.6.32-431.5.1.el6.x86_64/ /usr/src/linux

​#tar -zxvf ipvsadm-1.26.tar.gz

​#make && make install​

 LVS安裝完成,查看當前LVS集羣

#ipvsadm -ln​

至此keppalived+lvs安裝完成接下來咱們進行配置

 

​LVS+Keepalived配置(如下操做是在lvs-1上執行lvs-2相似)

配置Keepalived

修改keepalived配置文件並添加如下代碼

[root@lvs-1 ~]# vim /etc/keepalived/keepalived.conf 

global_defs {
    router_id LVS_1  # 設置lvs的id,在一個網絡內應該是惟一的
}
vrrp_instance VI_1 {
    state MASTER   #指定Keepalived的角色,MASTER爲主,BACKUP爲備
    interface eth0  #虛擬ip所在網
    virtual_router_id 51  #虛擬路由編號,主備要一致
    priority 100  #定義優先級,數字越大,優先級越高,主DR必須大於備用DR
    advert_int 1  #檢查間隔,默認爲1s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.30.8.200  #定義虛擬IP(VIP)爲172.30.8.200,可多設,每行一個
    }
}
# 定義對外提供服務的LVS的VIP以及port
virtual_server 172.30.8.200 3306 {
    delay_loop 6 # 設置健康檢查時間,單位是秒
    lb_algo wlc # 設置負載調度的算法爲wlc 基於權重的調度算法
    lb_kind DR # 設置LVS實現負載的機制,有NAT、TUN、DR三個模式
    nat_mask 255.255.255.0
    persistence_timeout 50 會話保持時間   (爲了實驗效果能夠註釋掉該選項)
    protocol TCP
    real_server 172.30.8.190 3306 {  # 指定real server1的IP地址
        weight 3   # 配置節點權值,數字越大權重越高
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }
    real_server 172.30.8.191 3306{  # 指定real server2的IP地址
        weight 3  # 配置節點權值,數字越大權重越高
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
     }
 }
 

 注意lvs-2操做相似只需修改如下選項便可

 

1.state MASTER   #指定Keepalived的角色,MASTER爲主,BACKUP爲備   
改成

state BACKUP #指定Keepalived的角色,MASTER爲主,BACKUP爲備 2.priority 100  #定義優先級,數字越大,優先級越高,主DR必須大於備用DR
改成
priority 99  #定義優先級,數字越大,優先級越高,主DR必須大於備用DR

 

 配置LVS(此腳本須要添加到lvs-1,Mysql-master,Mysql-slave)

編寫LVS啓動腳本/etc/init.d/realserver

[root@lvs-1 ~]# vim /etc/init.d/realserver 

SNS_VIP=172.30.8.200
/etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo:0
       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
       route del $SNS_VIP >/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: $0 {start|stop}"
       exit 1
esac
exit 0

 

 將lvs腳本加入開機自啓動 

#chmod +x /etc/init.d/realserver
#echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local

 分別啓動LVS和keepalived

# service realserver start
# service keepalived start

注意此時網卡的變化,能夠看到虛擬網卡已經分配到了realserver上。

此時查看LVS集羣狀態,能夠看到集羣下有兩個Real Server,調度算法,權重等信息。ActiveConn表明當前Real Server的活躍鏈接數

[root@lvs-1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.30.8.200:3306 wlc
  -> 172.30.8.190:3306            Route   3      0          0         
  -> 172.30.8.191:3306            Route   3      0   

 測試驗證

關閉MySQL Server2

[root@mysql-2 ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

 在LVS1查看/var/log/messages中關於keepalived日誌,LVS1檢測到了MySQL Server2宕機,同時LVS集羣自動剔除了故障節點

May 18 23:52:26 localhost Keepalived_healthcheckers[5909]: TCP connection to [172.30.8.191]:3306 failed !!!
May 18 23:52:26 localhost Keepalived_healthcheckers[5909]: Removing service [172.30.8.191]:3306 from VS [172.30.8.200]:3306

 重新啓動MySQL Server2後自動將故障節點自動加入LVS集羣

May 18 23:53:50 localhost Keepalived_healthcheckers[5909]: TCP connection to [172.30.8.191]:3306 success.
May 18 23:53:50 localhost Keepalived_healthcheckers[5909]: Adding service [172.30.8.191]:3306 to VS [172.30.8.200]:3306

 關閉LVS1上的Keepalived(模擬宕機操做),查看LVS1上的日誌,能夠看到Keepalived移出了LVS1上的VIP

May 18 23:54:28 localhost Keepalived[5908]: Stopping Keepalived v1.2.13 (05/17,2017)
May 18 23:54:28 localhost Keepalived_vrrp[5910]: VRRP_Instance(VI_1) sending 0 priority
May 18 23:54:28 localhost Keepalived_vrrp[5910]: VRRP_Instance(VI_1) removing protocol VIPs.
May 18 23:54:28 localhost Keepalived_healthcheckers[5909]: Netlink reflector reports IP 172.30.8.200 removed
May 18 23:54:28 localhost Keepalived_healthcheckers[5909]: Removing service [172.30.8.190]:3306 from VS [172.30.8.200]:3306
May 18 23:54:28 localhost Keepalived_healthcheckers[5909]: Removing service [172.30.8.191]:3306 from VS [172.30.8.200]:3306

 同時查看LVS2上日誌,能夠看到LVS2成爲了Master,並接管了VIP

May 18 23:54:29 localhost Keepalived_vrrp[5187]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 18 23:54:30 localhost Keepalived_vrrp[5187]: VRRP_Instance(VI_1) Entering MASTER STATE
May 18 23:54:30 localhost Keepalived_vrrp[5187]: VRRP_Instance(VI_1) setting protocol VIPs.
May 18 23:54:30 localhost Keepalived_healthcheckers[5186]: Netlink reflector reports IP 172.30.8.200 added
May 18 23:54:30 localhost Keepalived_vrrp[5187]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.30.8.200
May 18 23:54:35 localhost Keepalived_vrrp[5187]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.30.8.200

在LVS2上查看LVS集羣狀態,一切正常

[root@lvs-2 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.30.8.200:3306 wlc
  -> 172.30.8.190:3306            Route   3      0          0         
  -> 172.30.8.191:3306            Route   3      1          0  

 

總結

  • MySQL主主複製是集羣的基礎,組成Server Array,其中每一個節點做爲Real Server。
  • LVS服務器提供了負載均衡的做用,將用戶請求分發到Real Server,一臺Real Server故障並不會影響整個集羣的。
  • Keepalived搭建主備LVS服務器,避免了LVS服務器的單點故障,出現故障時能夠自動切換到正常的節點。
相關文章
相關標籤/搜索