Lvs+Keepalived+MySQL Cluster架設高可用負載均衡Mysql集羣

-------------------------------------node

1、前言mysql

2、MySQL Cluster基本概念linux

3、環境sql

4、配置數據庫

1.LB-Master及LB-Backup配置vim

2.MGM配置bash

3.SQL節點(SQL1和SQL2)配置服務器

4.數據節點(NDB1和NDB2)配置socket

5、查看狀態tcp

6、測試

-------------------------------------

 

1、前言

     MySQL Cluster 是MySQL適合於分佈式計算環境的高實用、高冗餘版本。它採用了NDB Cluster存儲引擎,容許在1個Cluster中運行多個MySQL服務器。在MyQL5.0 及以上的二進制版本中、以及與最新的Linux版本兼容的RPM中提供了該存儲引擎。MySQL Cluster 是一種技術,該技術容許在無共享的系統中部署「內存中」數據庫的 Cluster。經過無共享體系結構,系統可以使用廉價的硬件,並且對軟硬件無特殊要求。此外,因爲每一個組件有本身的內存和磁盤,不存在單點故障。MySQL Cluster由一組計算機構成,每臺計算機上均運行着多種進程,包括MySQL服務器,NDB Cluster的數據節點,管理服務器,以及(可能)專門的數據訪問程序。

 

2、MySQL Cluster基本概念 

    NDB是一種「內存中」的存儲引擎,它具備可用性高和數據一致性好的特色。MySQL  Cluster可以使用多種故障切換和負載平衡選項配置NDB存儲引擎,但在Cluster 級別上的存儲引擎上作這個最簡單。MySQL Cluster的NDB存儲引擎包含完整的數據集,僅取決於Cluster自己內的其餘數據。 

    目前,MySQL Cluster的Cluster部分可獨立於MySQL服務器進行配置。在MySQL Cluster中,Cluster

的每一個部分被視爲1個節點。 

    管理(MGM)節點:這類節點的做用是管理MySQL  Cluster內的其餘節點,如提供配置數據、啓動並中止節點、運行備份等。因爲這類節點負責管理其餘節點的配置,應在啓動其餘節點以前首先啓動這類節點。MGM節點是用命令「ndb_mgmd」啓動的。 

    數據(NDB)節點:這類節點用於保存Cluster的數據。數據節點的數目與副本的數目相關,是片斷的倍數。例如,對於兩個副本,每一個副本有兩個片斷,那麼就有4個數據節點。不過沒有必要設置多個副本。數據節點是用命令「ndbd」啓動的。 

    客戶(SQL)節點:這是用來訪問Cluster數據的節點。對於MySQL Cluster,客戶端節點是使用NDB Cluster 存儲引擎的傳統MySQL服務器。一般,SQL節點是使用命令「mysqld -ndbcluster」啓動的,或將「ndbcluster」添加到「my.cnf」後使用「mysqld」啓動。 

    註釋:在不少狀況下,術語「節點」用於指計算機,但在討論MySQL Cluster時,它表示的是進程。在單臺計算機上能夠有任意數目的節點,爲此,咱們採用術語「Cluster主機」。 

    管理服務器(MGM節點)負責管理Cluster配置文件和Cluster日誌。Cluster中的每一個節點從管理服務器檢索配置數據,並請求肯定管理服務器所在位置的方式。當數據節點內出現新的事件時,節點將關於這類事件的信息傳輸到管理服務器,而後將這類信息寫入Cluster日誌。

 

3、環境

系統:CentOS6.4 32位   9臺

軟件包(能夠去mysql官網下載http://dev.mysql.com/downloads/cluster/):

MGM:

MySQL-Cluster-gpl-management-7.1.31-1.el6.i686.rpm

MySQL-Cluster-gpl-tools-7.1.31-1.el6.i686.rpm

SQL節點:

MySQL-Cluster-gpl-client-7.1.31-1.el6.i686.rpm

MySQL-Cluster-gpl-server-7.1.31-1.el6.i686.rpm

NDB節點:

MySQL-Cluster-gpl-storage-7.1.31-1.el6.i686.rpm

拓撲圖:

wKiom1OFwSTzvSc9AAGGtl899xs417.jpg

IP規劃:

wKioL1OFw-Gwy2L8AAGxWiLB-iU586.jpg

 

4、配置(請先卸載掉與mysql有關的全部RPM包)

1.LB-Master及LB-Backup配置

(1)LB-Master及LB-Backup安裝keepalived和ipvsadm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# yum groupinstall "Additional Development"         //安裝開發工具
# yum groupinstall "Development tools"
# tar -zxvf keepalived-1.2.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/keepalived-1.2.1
# ./cnfigure
Keepalived configuration
------------------------
Keepalived version       : 1.2.1
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto 
Use IPVS Framework       : No     // 配置出現錯誤
IPVS  sync  daemon support : No
Use VRRP Framework       : Yes
Use Debug flags          : No
解決方法:
# yum install kernel-devel ipvsadm
# ln -s /usr/src/kernels/2.6.32-358.el6.i686/ /usr/src/linux
# ./cnfigure                    //再次配置環境
# make                          //編譯
# make install                  //安裝
   
# cd /usr/local/etc             //keepalived默認安裝路徑
# ll
drwxr-xr-x. 3 root root 4096 May 24 00:37 keepalived
drwxr-xr-x. 3 root root 4096 May 24 00:29 rc.d
drwxr-xr-x. 2 root root 4096 May 24 00:29 sysconfig
    
配置以系統方式service啓動
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ 
# cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/

(2)LB-Master及LB-Backup的keepalived主配置文檔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# cat /etc/keepalived/keepalived.conf
#guration File for keepalived
#global define
global_defs {
         router_id HaMySQL_1
         }
vrrp_sync_group VGM {
         group {
         VI_MYSQL
         }
}
vrrp_instance VI_MYSQL {
         state MASTER              //LB-Backup 設置爲BACKUP
         interface eth0
         lvs_sync_daemon_inteface eth0
         virtual_router_id 55
         priority 100              //LB-Backup 設置爲90
         advert_int 5
         authentication {
                 auth_type PASS
                 auth_pass 123456
         }
         virtual_ipaddress {
                 192.168.2.200 /24  dev eth0
         }
}
##########  LVS  MySQL Start   ###########
virtual_server 192.168.2.200 3306 {
         delay_loop 6
         lb_algo rr
         lb_kind DR
         persistence_timeout 6
         protocol TCP
 
         real_server 192.168.2.50 3306 {
                 weight 100
                 TCP_CHECK {
                         connect_timeout 3
                         nb_get_retry 3
                         delay_before_retry 3
                         connect_port 3306
                 }
         }
         real_server 192.168.2.60 3306 {
                 weight 100
                 TCP_CHECK {
                         connect_timeout 3
                         nb_get_retry 3
                         delay_before_retry 3
                         connect_port 3306
                 }
         }
}
##########  LVS MySQL END   #############

2.MGM配置

(1)安裝管理節點

1
2
3
# rpm -ivh MySQL-Cluster-gpl-management-7.1.31-1.el6.i686.rpm
# rpm -ivh MySQL-Cluster-gpl-tools-7.1.31-1.el6.i686.rpm 
# mkdir /etc/mysql-cluster

(2)配置管理節點

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# vim /etc/mysql-cluster/config.ini     //添加如下內容
[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
# TCP/IP options:
[tcp default]
portnumber=2202
# Management process options:
[ndb_mgmd]
id =1
hostname =192.168.2.10
datadir= /var/lib/mysql-cluster
# Options for data node
[ndbd]
id =2
hostname =192.168.2.30
datadir= /var/lib/mysql
[ndbd]
id =3
hostname =192.168.2.40
datadir= /var/lib/mysql
[mysqld]
id =4
hostname =192.168.2.50
[mysqld]
id =5
hostname =192.168.2.60

(3)啓動管理節點

1
2
3
4
5
# mkdir /var/lib/mysql-cluster
# ndb_mgmd -f /etc/mysql-cluster/config.ini 
MySQL Cluster Management Server mysql-5.1.73 ndb-7.1.31
# netstat -tupln
tcp     0     0    0.0.0.0:1186   0.0.0.0:*   LISTEN     17629 /ndb_mgmd

3.數據節點(NDB1和NDB2)配置

(1)安裝數據節點

1
2
# rpm -ivh MySQL-Cluster-gpl-storage-7.1.31-1.el6.i686.rpm
# mkdir /var/lib/mysql

(2)配置數據節點

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# vim /etc/my.cnf
[mysqld]
datadir= /var/lib/mysql
socket= /var/lib/mysql/mysql .sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
[mysqld_safe]
log-error= /var/log/mysqld .log
pid- file = /var/run/mysqld/mysqld .pid
 
[mysql_cluster]
ndb-connectstring=192.168.2.10

(3)啓動數據節點

1
2
3
4
5
6
# ndbd --initial       //NDB1
2014-05-28 00:32:17 [ndbd] INFO     -- Angel connected to  '192.168.2.10:1186'
2014-05-28 00:32:17 [ndbd] INFO     -- Angel allocated nodeid: 2
# ndbd --initial       //NDB2
2014-05-28 00:33:08 [ndbd] INFO     -- Angel connected to  '192.168.2.10:1186'
2014-05-28 00:33:08 [ndbd] INFO     -- Angel allocated nodeid: 3

4.SQL節點(SQL1和SQL2)配置

網卡及屏蔽ARP設置

在現有網卡基礎上添加一塊lo:0網卡

wKioL1OF0K-jcWeNAADMjF9kAk4318.jpg

1
2
3
4
# vim /etc/sysctl.conf                  //添加如下兩行
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
# sysctl -p

 

(1)安裝SQL節點

1
2
# rpm -ivh MySQL-Cluster-gpl-client-7.1.31-1.el6.i686.rpm
# rpm -ivh MySQL-Cluster-gpl-server-7.1.31-1.el6.i686.rpm //如出錯,卸載與mysql有關的rpm包

(2)配置SQL節點

1
2
3
4
5
6
7
8
9
# The MySQL server 
[mysqld]
port = 3306
socket= /var/lib/mysql/mysql .sock
ndbcluster
default-storage-engine=NDBCLUSTER
skip-name-resolve
[mysql_cluster]
ndb-connectstring=192.168.2.10

(3)啓動SQL節點

1
2
3
4
5
6
# service mysql start        //SQL1
Starting MySQL.. SUCCESS! 
# service mysql start        //SQL2,如沒法啓動,執行pkill -9 mysql再啓動
Starting MySQL SUCCESS! 
# netstat -tupln |grep mysql
tcp     0     0   0.0.0.0:3306        0.0.0.0:*     LISTEN      3475 /mysqld

(4)配置遠程鏈接(SQL1和SQL2配置一致)

1
2
3
# mysql
mysql> grant all on *.* to  'nuo' @ '%'  identified by  '123' ;
mysql> flush privileges;

 

啓動順序必定要遵循:MGM→NDB→SQL

 

5、查看狀態

1.查看MGM狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id =2    @192.168.2.30  (mysql-5.1.73 ndb-7.1.31, Nodegroup: 0, *)
id =3    @192.168.2.40  (mysql-5.1.73 ndb-7.1.31, Nodegroup: 0)
 
[ndb_mgmd(MGM)] 1 node(s)
id =1    @192.168.2.10  (mysql-5.1.73 ndb-7.1.31)
 
[mysqld(API)]   2 node(s)
id =4    @192.168.2.50  (mysql-5.1.73 ndb-7.1.31)
id =5    @192.168.2.60  (mysql-5.1.73 ndb-7.1.31)

2.查看LB-Master狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# service keepalived start
Starting keepalived:                                       [  OK  ]
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host 
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
     link /ether  00:0c:29:22:3d:01 brd ff:ff:ff:ff:ff:ff
     inet 192.168.2.20 /24  brd 192.168.2.255 scope global eth0
     inet 192.168.2.200 /24  scope global secondary eth0
     inet6 fe80::20c:29ff:fe22:3d01 /64  scope link 
        valid_lft forever preferred_lft forever
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.200:mysql rr persistent 6
   -> 192.168.2.50:mysql           Route   100    0          0         
   -> 192.168.2.60:mysql           Route   100    0          0

3.查看LB-Backup狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# service keepalived start
Starting keepalived:                                       [  OK  ]
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host 
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
     link /ether  00:0c:29:27:e1:98 brd ff:ff:ff:ff:ff:ff
     inet 192.168.2.21 /24  brd 192.168.2.255 scope global eth0
     inet6 fe80::20c:29ff:fe27:e198 /64  scope link 
        valid_lft forever preferred_lft forever
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.200:mysql rr persistent 6
   -> 192.168.2.50:mysql           Route   100    0          0         
   -> 192.168.2.60:mysql           Route   100    0          0

 

6、測試

  1. 客戶端1(CentOS6.4 32位,IP:192.168.2.80/24)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# yum install mysql
# mysql -h 192.168.2.200 -u nuo -p
Enter password:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ndbinfo            |
test                |
+--------------------+
mysql> create database t;
mysql> use t;
mysql> create table t2( id  int);
mysql> insert into t2 values(10);
mysql> insert into t2 values(20);

2.LB-Master查看鏈接狀態

1
2
3
4
5
6
7
8
9
10
11
12
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.200:mysql rr persistent 6
   -> 192.168.2.50:mysql           Route   100    1          0         
   -> 192.168.2.60:mysql           Route   100    0          0         
# ipvsadm -lcn
IPVS connection entries
pro expire state        source              virtual            destination
TCP 14:48  ESTABLISHED 192.168.2.80:49993 192.168.2.200:3306 192.168.2.50:3306
TCP 00:54  NONE        192.168.2.80:0     192.168.2.200:3306 192.168.2.50:3306

3.客戶端2(CentOS6.4 32位,IP:192.168.2.81/24)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# yum install mysql
# mysql -h 192.168.2.200 -u nuo -p
Enter password:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ndbinfo            |
| t                  |
test                |
+--------------------+
mysql> use t;
mysql>  select  * from t2;
+------+
id    |
+------+
|   20 |
|   10 |
+------+

4.LB-Master查看鏈接狀態

1
2
3
4
5
# ipvsadm -lcn
IPVS connection entries
pro expire state        source              virtual            destination
TCP 00:36  NONE        192.168.2.81:0     192.168.2.200:3306 192.168.2.60:3306
TCP 14:18  ESTABLISHED 192.168.2.81:42435 192.168.2.200:3306 192.168.2.60:3306

 

總結:在客戶端1上插入數據後,服務器顯示客戶端1鏈接的是SQL1(192.168.2.50),在客戶端2上進行查詢,能查詢到客戶端1輸入的數據,服務器顯示客戶端2鏈接的是SQL2(192.168.2.60),因此,數據是同步的,而且是一致性的。

相關文章
相關標籤/搜索