拓撲圖如下:
雙主模型數據庫:
dataserver1.9527du.com(192.168.60.22) dataserver2.9527du.com(192.168.60.88)
負責調度數據庫的haproxy
haproxy.9527du.com(192.168.60.130)
分佈式存儲節點:
mogilefs1.9527du.com(192.168.60.129) mogilefs2.9527du.com(192.168.60.128)
負責調度tracker的nginx
nginx.9527du.com(192.168.60.40)
在haproxy.9527du.com和nginx.9527du.com 這兩臺主機,安裝keepalived,把keepalived做成雙主工作模型。
流動IP爲:
192.168.60.130(haproxy使用) 192.168.60.40(nginx使用)
正常情況下,192.168.60.130地址配置在,haproxy.9527du.com該主機上。
192.168.60.40地址配置在,nginx.9527du.com主機上。
當其中的一臺主機菪機,另一臺主機就處於master狀態,VIP就會在處於master狀態的主機配置上並啓動相應的服務,這樣就解決了兩個調度節點的單點故障問題。
調度原理:
當客戶端想獲取存在分佈式文件系統上的圖片時,客戶端就向nginx發送請求(nginx監聽在80端口),nginx把請求交給mogilefs模塊處理,根據調度算法把請求分發給tracker,tracker(mogilefsd)就會訪問數據庫獲取獲取請求的數據的元數據,向haproxy(haproxy監聽在3306端口)發送訪問數據庫的請求。haproxy根據調度算法會把tracker的連接數據庫的請求分發到上游服務器backup server.
使用多個tracker調度(mogilefsd)節點來調度mogstored,並使用nginx進行調度tracker,保證了tracker的冗餘。
把兩臺數據庫搭建成雙主工作模型,並使用haproxy進行調度,保證了存儲文件元數據的數據庫不會成爲單點問題處在。
這樣我們的分佈式文件存儲系統就有了很好的冗餘。
一、安裝數據庫
使用的數據庫軟件爲:
mariadb-5.5.36-linux-i686.tar.gz
<一>、在server1.9527du.com(192.168.60.22)這臺主機安裝數據庫軟件;
1、爲初始化數據做準備工作
(1)、添加初始化數據庫使用到的mysql用戶
說明:爲了安全方面的考慮,建議添加一個系統用戶mysql
[root@dataserver1 /]# useradd -r -s /sbin/nologin mysql [root@dataserver1 /]# id mysql uid=102(mysql) gid=104(mysql) groups=104(mysql)
(2)、創建數據目錄,並把數據目錄的屬主屬組修改爲:mysql
[root@dataserver1 /]# mkdir -pv /mydata/mysql [root@dataserver1 mydata]# chown mysql:mysql ./mysql/ [root@dataserver1 mydata]# ll -d ./mysql/ drwxr-xr-x 2 mysql mysql 4096 Sep 30 14:23 ./mysql/
(3)、把數據庫軟件解壓到指定目錄下
[root@dataserver1 admin]# tar -xf mariadb-5.5.36-linux-i686.tar.gz -C /usr/local/
(4)、爲了維護方便,建議不要直接修改目錄名稱,創建一個軟鏈接
[root@dataserver1 admin]# cd /usr/local/ [root@dataserver1 local]# ln -sv mariadb-5.5.36-linux-i686 mysql create symbolic link `mysql' to `mariadb-5.5.36-linux-i686' [root@dataserver1 local]# ll mysql lrwxrwxrwx 1 root root 25 Sep 30 14:30 mysql -> mariadb-5.5.36-linux-i686
(5)、由於初始化數據庫,要以mysql的身份執行一些腳本來完成數據庫的初始化,所以最好把解壓後的數據庫軟件的屬主修改爲mysql
[root@dataserver1 local]# cd mysql/ [root@dataserver1 mysql]# chown mysql:mysql -R ./* [root@dataserver1 mysql]# ll ./ total 216 drwxr-xr-x 2 mysql mysql 4096 Sep 30 14:29 bin -rw-r--r-- 1 mysql mysql 17987 Feb 24 2014 COPYING -rw-r--r-- 1 mysql mysql 26545 Feb 24 2014 COPYING.LESSER drwxr-xr-x 3 mysql mysql 4096 Sep 30 14:29 data drwxr-xr-x 2 mysql mysql 4096 Sep 30 14:29 docs drwxr-xr-x 3 mysql mysql 4096 Sep 30 14:29 include -rw-r--r-- 1 mysql mysql 8694 Feb 24 2014 INSTALL-BINARY drwxr-xr-x 3 mysql mysql 4096 Sep 30 14:29 lib ...
2、初始化數據庫
因爲,指定數據庫的數據目錄的位置和初始化數據庫的用戶
[root@dataserver1 mysql]# ./scripts/mysql_install_db --datadir=/mydata/mysql/ --user=mysql Installing MariaDB/MySQL system tables in '/mydata/mysql/' ... OK Filling help tables... OK ......
說明:
從上述輸出信息,數據庫已經初始化成功。
3、爲啓動數據庫做準備工作;
(1)、按照需要爲數據庫提供配置文件
[root@dataserver1 mysql]# cp support-files/my-large.cnf /etc/my.cnf
(2)、修改my.cnf,指定初始化數據庫時,給的數據目錄路徑。
[root@dataserver1 mysql]# vim /etc/my.cnf thread_concurrency = 2 datadir = /mydata/mysql/
(3)、提供LSB風格的啓動腳本
[root@dataserver1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@dataserver1 mysql]# chmod u+x /etc/init.d/mysqld [root@dataserver1 mysql]# ll /etc/init.d/mysqld -rwxr-xr-x 1 root root 11844 Sep 30 14:40 /etc/init.d/mysqld
4、啓動數據庫並測試
(1)、啓動數據庫服務
[root@dataserver1 mysql]# service mysqld start Starting MySQL... [ OK ]
(2)、連接數據
[root@dataserver1 mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. 。。。。。。 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
說明:
從上述信息,得知數據庫已經安裝成功
5、授權訪問數據庫
由於,初始化數據庫會默認創建很多用戶,且這些用戶都是沒有密碼的。
所以,刪除不需要的用戶,給保留的用戶添加密碼
(1)、安裝數據庫時,默認創建的用戶
mysql> select user,host,password from mysql.user; +------+------------------------+----------+ | user | host | password | +------+------------------------+----------+ | root | localhost | | | root | dataserver1.9527du.com | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | dataserver1.9527du.com | | +------+------------------------+----------+ 6 rows in set (0.00 sec)
(2)、刪除不需要有用戶
mysql> drop user 'root'@'dataserver1.9527du.com'; mysql> drop user 'root'@'::1'; mysql> drop user ''@'localhost'; mysql> drop user ''@'dataserver1.9527du.com';
(3)、給用戶添加密碼
mysql> set password for 'root'@'localhost' = password('root'); Query OK, 0 rows affected (0.00 sec) mysql> set password for 'root'@'127.0.0.1' = password('root'); Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to 'admin'@'%.%.%.%' identified by 'admin'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
(4)、修改後,數據庫的用戶列表
mysql> select user,host,password from mysql.user; +-------+-----------+-------------------------------------------+ | user | host | password | +-------+-----------+-------------------------------------------+ | root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | admin | %.%.%.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +-------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec)
<二>、在dataserver2.9527du.com(192.168.60.88)主機也是一樣安裝配置。
。。。。。。
二、把dataserver1.9527du.com和dataserver2.9527du.com兩臺主機的數據庫配置成雙主工作模型。
說明:
在 MariaDB數據的主主複製模型中。兩個節點都要啓動二進制日誌和中繼日誌。 因爲雙主模型的複製架構,兩個節點都可讀可寫。
<一>、修改兩臺主機的/etc/my.cnf配置文件
1、修改dataserver2.9527du.com(192.168.60.88)的配置文件
binlog_format=mixed --------> 二進制日誌使用混合模式 log-bin=server2-bin --------> 啓動二進制日誌 relay-log=server2-relay --------> 啓動中繼日誌 server-id = 12 --------> 注意:兩臺主機的server-id要求不一樣。會了避免循環複製。 auto-increment-offset = 2 -------> 設置自動增長時的偏移量 auto-increment-increment = 2 ------> 每次增長2
說明:
有些表使用了自動增長字段,爲了避免重複複製來錯誤的。
2、修改dataserver1.9527du.com(192.168.60.22)的配置文件
log-bin=server1-bin binlog_format=mixed relay-log=server1-relay server-id = 11 auto-increment-offset = 1 auto-increment-increment = 2
<二>、把兩臺服務器配置成雙主工作模型。
其實雙主模型就是兩臺主機互爲主從。
1、所dataserver1.9527du.com當做主(master)服務器,dataserver2.9527du.com當做從(slave)服務器
(1)、查看dataserver1.9527du.com 的二進制日誌的位置
mysql> show master status; +--------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | server1-bin.000001 | 245 | | | +--------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
(2)、在dataserver2.9527du.com 主機配置複製的各種參數
mysql> change master to master_host='192.168.60.22',master_user='rep',master_password='rep',master_log_file='server1-bin.000001',master_log_pos=245; Query OK, 0 rows affected (0.01 sec)
參數說明:
master_host 主服務器的地址 master_user 連接到主服務器,且有: REPLICATION SLAVE 和 REPLICATION CLIENT 權限的用戶。 master_password 連接到主服務器的密碼 master_log_file 指定從那個二進制日誌開始複製 master_log_pos 指定從二進制日誌的那個事件開始複製
(3)、啓動slave前查看從服務器的狀態,是否報錯
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.60.22 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: server1-bin.000001 Read_Master_Log_Pos: 245 Relay_Log_File: server2-relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: server1-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No 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: 245 Relay_Log_Space: 245 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 1 row in set (0.00 sec)
(4)、啓動複製操作
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
說明:
執行上述指令,就會同時啓動:IO_THREAD、SQL_THREAD 兩個複製工作線程。也可以使用分別啓動/停止某個線程。
mysql> start|stop slave IO_THREAD|SQL_THREAD
(5)、查看從服務器的運行狀況狀況
主要是查看兩個複製工作線程。
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.60.22 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: server1-bin.000001 Read_Master_Log_Pos: 245 Relay_Log_File: server2-relay.000002 Relay_Log_Pos: 531 Relay_Master_Log_File: server1-bin.000001 Slave_IO_Running: Yes ---->負責到主服務器請求二進制日誌事件的IO線程也沒有錯誤 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: 245 Relay_Log_Space: 823 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 11 1 row in set (0.00 sec)
2、所dataserver2.9527du.com當做主(master)服務器,dataserver1.9527du.com當做從(slave)服務器
(1)、查看dataserver1.9527du.com 的二進制日誌的位置
mysql> show master status; +--------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | server2-bin.000001 | 337 | | | +--------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
(2)、在dataserver1.9527du.com 主機配置複製的各種參數
mysql> change master to master_host='192.168.60.88',master_user='rep',master_password='rep',master_log_file='server2-bin.000001',master_log_pos=337; Query OK, 0 rows affected (0.02 sec)
(3)、啓動slave前查看從服務器的狀態,是否報錯
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.60.88 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: server2-bin.000001 Read_Master_Log_Pos: 337 Relay_Log_File: server1-relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: server2-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No 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: 337 Relay_Log_Space: 245 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 1 row in set (0.00 sec)
(4)、啓動複製操作
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
(5)、查看從服務器的運行狀況狀況
主要是查看兩個複製工作線程。
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.60.88 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: server2-bin.000001 Read_Master_Log_Pos: 337 Relay_Log_File: server1-relay.000002 Relay_Log_Pos: 531 Relay_Master_Log_File: server2-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: 337 Relay_Log_Space: 823 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 12 1 row in set (0.00 sec)
<三>、測試雙主模型的數據庫是否工作OK?
1、查看兩臺數據庫的所擁有的數據庫是否一樣的?
(1)、查看server1.9627du.com(192.168.60.22)主機有那些數據庫
[root@dataserver1 mysql]# hostname; mysql -uroot -h127.0.0.1 -p dataserver1.9527du.com Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. 。。。。。。 mysql> create database testdb; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.00 sec)
(2)、查看server2.9627du.com(192.168.60.88)主機有那些數據庫
[root@dataserver2 mysql]# hostname; mysql -uroot -h127.0.0.1 -p dataserver2.9527du.com Enter password: 。。。。。。 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.00 sec)
說明:
從上述結果來看,兩臺數據庫服務器所擁有的數據庫都是一樣的。
2、測試刪除數據庫兩臺主機的數據是否能夠同步
(1)、在dataserver2.9527du.com 主機刪除 testdb 數據庫。
mysql> drop database testdb; Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
(2)、在:server1.9627du.com(192.168.60.22)查看是否還有:testdb數據庫
[root@dataserver1 mysql]# mysql -uroot -h127.0.0.1 -p Enter password: 。。。。。。 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
說明:
數據庫的主從架構已經搭建成功。
三、配置:haproxy負載均衡兩臺MariDB服務器
haproxy接收連接數據庫的請求,按照調度算法把用戶請求分發到後端數據庫服務器。
由於客戶端與數據庫服務器交互使用的協議是mysql協議(應用層協議),而不是http協議。所以要修改haproxy的工作模型爲:TCP。
1、配置文件如下:
defaults mode tcp ------> 指定haproxy工作在tcp模型 log global #option httplog option dontlognull #option http-server-close #option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 listen stats ---------> 配置haproxy的狀態頁 mode http ---------> 設置haproxy的工作模型爲http不從默認繼承。 bind 0.0.0.0:8888 stats enable stats uri /haproxyadmin?stats stats realm Haproxy\ Statistics stats auth admin:admin stats admin if TRUE #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend mysql bind *:3306 ------> haproxy 監聽的端口和地址。 mode tcp ------> 設置haproxy的工作模型 default_backend mysqlserver ------> 調用定義的backup server服務器組。 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend mysqlserver -------> 定義backup server 服務器組 balance leastconn bash-type consistent -------> 定義調度方法。使用laeastconn調度方法,非常適合mysql數據庫服務這種長連接。 -------> 爲了提供後端數據庫服務器的緩存命中率和不因爲某臺數據庫服務器故障而導致緩存全部失效使用“一致性哈唏”。指定hash-type 爲:consistent server dbsrv1 192.168.60.22:3306 check port 3306 -------> 配置 backup server是什麼 server dbsrv2 192.168.60.88:3306 check port 3306
2、啓動
(1)、啓動haproxy服務
[root@haproxy haproxy]# service haproxy start Starting haproxy: [ OK ]
(2)、查看 haproxy的監聽端口
[root@haproxy haproxy]# netstat -anptl | grep haproxy tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 3115/haproxy -----> haproxy的管理接口 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3115/haproxy
3、從遠程訪問haproxy代理,創建數據庫,是否能夠調度雙主模型的數據庫服務器
(1)、從遠程連接數據庫,地址指向:haproxy代理監聽的地址
D:\>mysql -uadmin -h192.168.60.130 -p Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or \g. 。。。。。。 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
說明:
從上述可以看出,haproxy已經能夠正確代理,兩臺MariDB數據庫服務器了。
(2)、測試創建數據庫
mysql> create database testdb; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.00 sec)
查看兩臺數據庫服務器
[root@dataserver2 ~]# hostname;mysql -uroot -h127.0.0.1 -p -e 'show databases;' dataserver2.9527du.com Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+
[root@dataserver1 mysql]# hostname; mysql -uroot -h127.0.0.1 -p -e 'show databases;'; dataserver1.9527du.com Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+
說明:
Haproxy已經能夠調度兩臺數據庫服務器了,且兩臺數據庫也能夠保持數據的同步。
(3)、測試刪除數據庫:
D:\>mysql -uadmin -h192.168.60.130 -p Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or \g. 。。。。。。 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement mysql> drop database testdb; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
查看兩臺 MariDB 數據庫服務器
[root@dataserver1 mysql]# hostname; mysql -uroot -h127.0.0.1 -p -e 'show databases;'; dataserver1.9527du.com Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
[root@dataserver2 ~]# hostname;mysql -uroot -h127.0.0.1 -p -e 'show databases;' dataserver2.9527du.com Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
說明:
從上述測試得出結論,haproxy 負載均衡數據庫已經搭建成功!!!
四、配置分佈式文件系統mogilefs
mogilefs分佈式文件系統是有主控節點(tracker)的。
當存儲數據時,使用mogilefs的客戶端向主控節點(tracker)發起存儲數據的請求,主控節點就會檢索數據庫,查看Storage Nodes節點的未使用空間信息,tracker就會告訴客戶端應該把數據存儲在Storage Nodes的哪個位置,客戶端再去連接Storage Nodes進行數據的存儲。
這就是mogilefs分佈式文件系統存儲數據的簡單流程。
當mogilefs客戶端取數據時,首先客戶端向tracker發起獲取數據的請求,tracker就去查數據庫,因爲數據的元數據(如:某文件存儲在Storage Node的那個位置)是存儲在數據庫的。tracker查詢到文件的存儲位置就把這些信息,發送給客戶端。客戶端再去聯繫 Storage Nodes 取數據的。
MogileFS(是perl語言開發的) 的組件:
1、Tracker: Mogilefsd進程,其實它就是一個調度器。 其實現的功能包括:Replication,Deletion,Query,Monitor等等; 2、Storage Nodes: mogstored 進程;文件實際存儲的位置. 3、MySQL 節點:用戶於爲tracker存儲元數據信息;存儲mogilefs的名稱空間及文件名;
提供兩個Tracker節點,從而實現調度器的冗餘。
<一>、配置 Storage Nodes
1、在mogilefs1.9527du.com(192.168.60.129)主機配置 Storage Node,也就是配置mogstored 進程的工作特徵。
說明:
mogilefs對存儲空間沒有要求。可以是文件可以是分區等。這裏使用分區
(1)、準備好配置分佈式文件系統所需要的存儲空間。
創建掛載點
[root@mogilefs1 ~]# mkdir /mogilefs
編輯/etc/fstab配置文件,實現開機自動掛載/dev/sda3分區
[root@mogilefs1 ~]# vim /etc/fstab /dev/sda3 /mogilefs ext4 defaults 0 0
掛載測試
[root@mogilefs1 ~]# mount -a [root@mogilefs1 ~]# mount | grep sda3 /dev/sda3 on /mogilefs type ext4 (rw)
查看大小
[root@mogilefs1 ~]# df -h | grep sda3 /dev/sda3 1019M 34M 934M 4% /mogilefs
(2)、設置 Mogstored 的存儲目錄
說明:
當往mogilefs分佈式存儲中存儲數據時,負責存儲數據的進程是:mogstored ,而該進程的屬主是:mogilefs.根據linux的文件訪問控制模型,mogstored進程的屬主要有存儲空間的讀寫權限纔可以存儲數據的。所以要修改存儲空間的屬主屬組爲:mogilefs.
創建存儲目錄
[root@mogilefs1 ~]# mkdir -pv /mogilefs/mogdata/dev1
修改目錄的屬主屬組都爲:mogilefs
[root@mogilefs1 ~]# chown -R mogilefs:mogilefs /mogilefs/mogdata/ [root@mogilefs1 ~]# ll /mogilefs/ total 20 drwx------ 2 root root 16384 Oct 1 10:13 lost+found drwxr-xr-x 2 mogilefs mogilefs 4096 Oct 1 10:20 mogdata
(3)、提供mogstored工作的配置文件如下
[root@mogilefs1 ~]# vim /etc/mogilefs/mogstored.conf maxconns = 10000 httplisten = 0.0.0.0:7500 ----> 可以通過瀏覽器訪問存儲節點的文件 mgmtlisten = 0.0.0.0:7501 ----> mogstored 監聽的地址 docroot = /mogilefs/mogdata -----> 存儲設備的根位置。
2、在mogilefs2.9527du.com(192.168.60.128)主機配置 Storage Node,也就是配置mogstored 進程的工作特徵。
(1)、準備好配置分佈式文件系統所需要的存儲空間。
創建掛載點
[root@mogilefs2wq ~]# mkdir /mogilefs
編輯/etc/fstab配置文件,實現開機自動掛載/dev/sda3分區
[root@mogilefs2wq ~]# vim /etc/fstab /dev/sda3 /mogilefs ext4 defaults 0 0
掛載測試
root@mogilefs2wq ~]# mount -a [root@mogilefs2wq ~]# mount | grep sda3 /dev/sda3 on /mogilefs type ext4 (rw)
查看大小
[root@mogilefs2wq ~]# df -h | grep sda3 /dev/sda3 1019M 34M 934M 4% /mogilefs
創建存儲目錄
[root@mogilefs2wq ~]# mkdir /mogilefs/mogdata/dev2
修改目錄的屬主屬組都爲:mogilefs
[root@mogilefs2wq ~]# chown mogilefs:mogilefs /mogilefs/mogdata/ [root@mogilefs2wq ~]# ll /mogilefs/ total 20 drwx------ 2 root root 16384 Oct 1 10:19 lost+found drwxr-xr-x 2 mogilefs mogilefs 4096 Oct 1 10:23 mogdata
(3)、提供mogstored工作的配置文件如下
[root@mogilefs2wq ~]# vim /etc/mogilefs/mogstored.conf maxconns = 10000 httplisten = 0.0.0.0:7500 mgmtlisten = 0.0.0.0:7501 ----> mogstored 監聽的地址 docroot = /mogilefs/mogdata -----> 存儲設備的根位置。
<二>、配置調度器(tracker)也就是:mogilefsd 進程的工作特徵並初化Mogilefsd存儲元數據所需要的數據庫。
1、在兩個節點:mogilefs1.9527du.com和mogilefs2.9527du.com設置:mogilefsd進程的工作特徵如下所述:
[root@mogilefs1 ~]# vim /etc/mogilefs/mogilefsd.conf db_dsn = DBI:mysql:mogilefs:host=192.168.60.130 # 數據庫類型:所使用的數據庫:host=數據庫服務器所在的主機IP地址 db_user = moguser -------> 連接數據庫使用的用戶 db_pass = moguser -------> 連接數據庫使用的密碼 # IP:PORT to listen on for mogilefs client requests listen = 0.0.0.0:7001 -----> 監聽的地址和端口。
2、初始化mogilefsd進程所需要保存文件元數據的表。
(1)、設置mogilefsd使用的數據庫用戶並授權
連接數據庫
[root@haproxy /]# hostname ; mysql -uroot -h192.168.60.130 -p haproxy.9527du.com Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. 。。。。 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
創建數據庫,用來保存存儲在modilefs分佈式文件系統的文件元數據的。
mysql> create database mogilefs;
授權
mysql> grant all on mogilefs.* to 'moguser'@'192.168.60.%' identified by 'moguser'; Query OK, 0 rows affected (0.01 sec)
刷新授權表讓它立即生效
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
(2)、初始化mogilefsd使用的表
說明:前面已經使用haproxy負載均衡兩臺數據庫服務器了。這裏就可以直接連接到haproxy調度器,實現初始化工作。
[root@mogilefs1wq ~]# mogdbsetup --dbhost=192.168.60.130 --dbname=mogilefs --dbrootuser=root --dbrootpass=root --dbuser=moguser --dbpass=moguser --type=MySQL This will attempt to setup or upgrade your MogileFS database. It won't destroy existing data. Run with --help for more information. Run with --yes to shut up these prompts. Continue? [N/y]: y Create/Upgrade database name 'mogilefs'? [Y/n]: y Grant all privileges to user 'moguser', connecting from anywhere, to the mogilefs database 'mogilefs'? [Y/n]: y
說明:
已經完成初始化,mogilefsd進程所需要表。
3、啓動測試
啓動mogilefsd進程
[root@mogilefs1 /]# service mogilefsd start Starting mogilefsd [ OK ]
查看調度器tracker監聽的端口
root@mogilefs1 /]# netstat -anptl | grep mogilefsd tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 25900/mogilefsd tcp 0 0 192.168.60.129:60212 192.168.60.130:3306 ESTABLISHED 25908/mogilefsd [de tcp 0 0 192.168.60.129:60213 192.168.60.130:3306 ESTABLISHED 25921/mogilefsd [jo tcp 0 0 192.168.60.129:60211 192.168.60.130:3306 ESTABLISHED 25902/mogilefsd [mo
啓動mogstored進程,也就是存儲節點。
[root@mogilefs1 /]# service mogstored start Starting mogstored [ OK ]
查看存儲監聽的端口
[root@mogilefs1 /]# netstat -anptl | grep mogstored tcp 0 0 0.0.0.0:7500 0.0.0.0:* LISTEN 25972/mogstored tcp 0 0 0.0.0.0:7501 0.0.0.0:* LISTEN 25972/mogstored
(2)、啓動mogilefs2.9527du.com主機的:mogilefsd和mogstored進程:
啓動mogilefsd進程
[root@mogilefs2wq ~]# service mogilefsd start Starting mogilefsd [ OK ]
查看調度器tracker監聽的端口
[root@mogilefs2wq ~]# netstat -anptl | grep mogilefs tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 2976/mogilefsd tcp 0 0 192.168.60.128:46369 192.168.60.130:3306 ESTABLISHED 2984/mogilefsd [del tcp 0 0 192.168.60.128:46367 192.168.60.130:3306 ESTABLISHED 2978/mogilefsd [mon tcp 0 0 192.168.60.128:46368 192.168.60.130:3306 ESTABLISHED 2997/mogilefsd [job
啓動mogstored進程,也就是存儲節點。
[root@mogilefs2wq ~]# service mogstored start Starting mogstored [ OK ] [root@mogilefs2wq ~]# netstat -anptl | grep mogstored tcp 0 0 0.0.0.0:7500 0.0.0.0:* LISTEN 3036/mogstored tcp 0 0 0.0.0.0:7501 0.0.0.0:* LISTEN 3036/mogstored
說明:
啓動兩個節點,所有工作進程都正常。
從上面查看兩個節點的:mogilefsd 進程監聽的端口,也可判斷:haproxy調度兩臺雙主架構的數據庫服務器都工作正常。
<三>、整合存儲節點的存儲資源。
也就是告訴mogilefs的控制節點(調度節點tracker)有那些存儲節點(Storage Nodes),以及存儲節點的存儲設備。
這樣,mogilefs的中心節點(mogilefsd進程)就可以調度管理這些存儲節點的資源了。
1、添加主機
(1)、在nogilefs2.9527du.com節點添加存儲節點主機。
[root@mogilefs2wq mogilefs]# mogadm --trackers=192.168.60.128:7001 host add mogilefs1 --ip=192.168.60.129 --status=alive [root@mogilefs2wq mogilefs]# mogadm --trackers=192.168.60.128:7001 host add mogilefs2 --ip=192.168.60.128 --status=alive
(2)、查看添加的主機是否成功
在mogilefs2.9527du.com使用【mogadm】命令查看
[root@mogilefs2wq mogilefs]# mogadm --trackers=192.168.60.128:7001 host list mogilefs1 [1]: alive IP: 192.168.60.129:7500 mogilefs2 [2]: alive IP: 192.168.60.128:7500
說明:
從上面可以看出,已經成功添加。
在mogilefs1.9527du.com使用【mogadm】命令查看
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.129:7001 host list mogilefs1 [1]: alive IP: 192.168.60.129:7500 mogilefs2 [2]: alive IP: 192.168.60.128:7500
說明:
從上面可以看出,已經成功添加。
2、添加主機的存儲到兩個節點的調度器trackers(mogilefsd)上。
(1)、添加存儲設備
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.129:7001,192.168.60.128:7001 device add mogilefs1 1 --status=alive [root@mogilefs1 ~]# mogadm --trackers=192.168.60.129:7001,192.168.60.128:7001 device add mogilefs2 2 --status=alive
說明:
上面的:1 表示:dev1. 在mogstored.conf的配置文件中的根:docroot = /mogilefs/mogdata 下一定要有該目錄。該目錄也就是mogstord存儲數據的目錄。
(2)、查看添加的存儲是否成功
檢測調度器
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.129:7001,192.168.60.128:7001 check Checking trackers... ----------------> 檢測兩個調度器都OK!!! 192.168.60.129:7001 ... OK 192.168.60.128:7001 ... OK Checking hosts... -----------------> 檢測兩臺存儲節點都OK!!! [ 1] mogilefs1 ... OK [ 2] mogilefs2 ... OK Checking devices... -----------------> 檢測兩臺存儲節點的存儲設備都OK!!! host device size(G) used(G) free(G) use% ob state I/O% ---- ------------ ---------- ---------- ---------- ------ ---------- ----- [ 1] dev1 0.944 0.033 0.911 3.46% writeable 0.0 [ 2] dev2 0.944 0.033 0.911 3.45% writeable 0.0 ---- ------------ ---------- ---------- ---------- ------ total: 1.888 0.065 1.822 3.46%
列出調度器的設備有那些
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.128:7001,192.168.60.129:7001 device list mogilefs1 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 0.032 0.911 0.943 100 mogilefs2 [2]: alive used(G) free(G) total(G) weight(%) dev2: alive 0.032 0.911 0.943 100
說明:
從上述測試結果可以看出,mogilefs系統工作正常。
<四>添加域
mogilefs分佈式文件系統的存儲是平面化的,所以存儲的文件名稱是不允許同名的。
使用domain來模擬文件系統上的目錄。定義名稱空間,這樣就可以在不同的域中文件名是可以相同的。
1、給兩個調度節點(tracker)都添加同一個域(domain)。
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.128:7001,192.168.60.129:7001 domain add images
2、查看添加的域的信息
[root@mogilefs1 ~]# mogadm --trackers=192.168.60.128:7001,192.168.60.129:7001 domain list domain class mindevcount replpolicy hashtype -------------------- -------------------- ------------- ------------ ------- images default 2 MultipleHosts() NONE 域名 複製單元默認 複製單元(提供數據的冗餘) 複製策略
<五>、向搭建的存儲系統存儲數據和下載數據測試
1、通過mogilefs1.9527du.com節點的調度器tracker上傳圖片
[root@mogilefs1 ~]# mogupload --trackers=192.168.60.129:7001 --domain=images --key='/a.png' --file='/root/a.png'
(1)、在本節點查看
[root@mogilefs1 ~]# mogfileinfo --trackers=192.168.60.129:7001 --domain=images --key='/a.png' - file: /a.png class: default devcount: 2 domain: images fid: 10 key: /a.png length: 213132 - http://192.168.60.128:7500/dev2/0/000/000/0000000010.fid - http://192.168.60.129:7500/dev1/0/000/000/0000000010.fid
(2)、在另一個節點查看
[root@mogilefs2 ~]# mogfileinfo --trackers=192.168.60.128:7001 --domain=images --key='/a.png' - file: /a.png class: default devcount: 2 domain: images fid: 10 key: /a.png length: 213132 - http://192.168.60.128:7500/dev2/0/000/000/0000000010.fid - http://192.168.60.129:7500/dev1/0/000/000/0000000010.fid
說明:
通過mogilefs1.9527du.com的調度器上傳數據是沒有問題的。
2、通過mogilefs2.9527du.com節點的調度器tracker上傳圖片
(1)、在本節點上傳數據
[root@mogilefs2 ~]# mogupload --trackers=192.168.60.128 --domain=images --key='/b.png' --file='/root/b.png'
(2)、在本節點查看數據。
[root@mogilefs2 ~]# mogfileinfo --trackers=192.168.60.128 --domain=images --key='/b.png' - file: /b.png class: default devcount: 2 domain: images fid: 12 key: /b.png length: 77339 - http://192.168.60.128:7500/dev2/0/000/000/0000000012.fid - http://192.168.60.129:7500/dev1/0/000/000/0000000012.fid
(3)、在另一個節點查看
[root@mogilefs1 ~]# mogfileinfo --trackers=192.168.60.129:7001 --domain=images --key='/b.png' - file: /b.png class: default devcount: 2 domain: images fid: 12 key: /b.png length: 77339 - http://192.168.60.129:7500/dev1/0/000/000/0000000012.fid - http://192.168.60.128:7500/dev2/0/000/000/0000000012.fid
說明:
通過mogilefs2.9527du.com的調度器上傳數據是沒有問題的。
3、列出指定domain的文件
[root@mogilefs1 ~]# moglistkeys --trackers=192.168.60.129:7001,192.168.60.128:7001 --domain=images --key_prefix='/' /a.png /b.png
4、下載文件
(1)、聯繫mogilefs1.9527du.com節點的調度器tracker下載圖片
[root@mogilefs1 ~]# mogfetch --trackers=192.168.60.129:7001 --domain=images --key='/a.png' --file='/tmp/a.png'
查看下載的數據
[root@mogilefs1 ~]# ll /tmp/a.png -rw-r--r-- 1 root root 213132 Oct 2 10:13 /tmp/a.png
(2)、聯繫mogilefs2.9527du.com節點的調度器tracker下載圖片
[root@mogilefs2 ~]# mogfetch --trackers=192.168.60.128:7001 --domain=images --key='/a.png' --file='/tmp/a.png'
查看下載的數據
[root@mogilefs2 ~]# ll /tmp/a.png -rw-r--r-- 1 root root 213132 Oct 2 10:08 /tmp/a.png
說明:
已經把mogilefs分佈式文件系統存儲搭建成功!!!
五、搭建NGINX負載器來負載均衡兩個tracker調度器。
我們在瀏覽器地址欄:http://192.168.60.129:7500/dev1/0/000/000/0000000012.fid 就可以查看該文件了。
但是,在編寫html頁面鏈接該圖片資源是很不方便的。最好,訪問mogilefs分佈式存儲中的圖片時,像訪問網頁一樣,在後面鍵入:http://192.168.60.129/images/a.png 就可以訪問到存儲在mogilefs分佈式文件系統中的圖片數據了。NGINX的第三方模塊:
nginx_mogilefs_module-1.0.4.tar.gz支持這種功能。
工作原理:
當客戶端向NGINX發送請求images目錄下的資源時,NGINX會把請求交給:nginx_mogilefs_module 來處理。該模塊會把請求:uri轉化爲:key.然後使用該key向調度器tracker發請求,調度器tracker(mogilefsd進程)收到請求後,會去查詢數據庫。從數據庫中查詢去nginx_mogilefs_module請求的key對應的文件file存儲在存儲節點的那個位置,調度器把這些信息告訴nginx_mogilefs_module模塊,然後nginx_mogilefs_module就向聯繫存儲節點,請求數據。當nginx_mogilefs_module收到存儲節點響應的數據後。NGINX就把用戶請求的圖片響應給客戶端。
1、編譯NGINX讓它支持nginx_mogilefs_module模塊
因爲,NGINX不支持動態裝卸載模塊,所以要在編譯時指定模塊編譯進來。
但是,TNGINX是支持動態裝卸載模塊的。這像HTTPD服務器,用於什麼模塊編譯好使用指令裝載就可以使用了。
(1)、創建運行NGINX服務進程的用戶nginx,
[root@nginx admin]# useradd -r -s /sbin/nologin nginx [root@nginx admin]# id nginx uid=402(nginx) gid=402(nginx) groups=402(nginx)
(2)、編譯
解壓
root@nginx admin]# tar -xf nginx_mogilefs_module-1.0.4.tar.gz [root@nginx admin]# tar -xf nginx-1.6.1.tar.gz [root@nginx admin]# cd nginx-1.6.1
配置
[root@nginx nginx-1.6.1]# ./configure \ --prefix=/usr/local/nginx-1.6 \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/subsys/nginx.lock \ --user=nginx --group=nginx \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --with-pcre \ --add-module=../nginx_mogilefs_module-1.0.4 ------->使用該指令指定編譯時要把nginx_mogilefs_module模塊編譯進來
編譯安裝
[root@nginx nginx-1.6.1]# make & make install
2、啓動
[root@nginx ~]# service nginx start Starting nginx: [ OK ] [root@nginx ~]# netstat -anptl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5576/nginx
3、配置nginx調度mogilefs中的兩個tracker(mogilefsd進程)
upstream trackers { ------> 定義上游(後端)服務器(mogilefsd) server 192.168.60.129:7001; server 192.168.60.128:7001; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /image { --------> 對用戶請求的URI做訪問控制,當URI以image打頭的都由該location裏的指令處理。 mogilefs_tracker trackers; mogilefs_domain images; -------> 定義域domain,當nginx_mogilefs_module模塊把URI轉成key後就會向treaker調度器請求images域對應key的文件了。 -------> 因爲在mogilefs分佈式文件系統中查找數據是要指定域和key.因爲數據是存放在域中的。mogfileinfo --trackers=192.168.60.129:7001 --domain=images --key='/b.png' mogilefs_methods GET PUT DELETE; ----> 設置請求方法。 mogilefs_pass { ------> 調用定義的上游服務器組 proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } }
4、重啓動nginx
[root@nginx admin]# service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ] [root@nginx admin]# netstat -anptl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2460/nginx
訪問測試如下圖:
說明:
從上述訪問可以看出,nginx調度已經搭成功
六、提供nginx負載均衡器和haproxy負載均衡器的高可用。避免存儲系統存在單點故障。
分析兩個調度器所涉及的資源:
(1)、負責調度兩個tracker(mogiled)的NGINX調度器
nginx 進程
IP地址
(2)、負責高度兩臺數據庫服務器的haproxy調度器
mysqld 進程
IP地址
解決方案:
(1)、keepalived (2)、corosync + pacemaker
從功能都可以實現,但是keepalived比較容易實現,輕量級。且高可用的資源沒有涉及到存儲。
所以,這裏選擇keepalived實現。調度節點的高可用。
<一>、在nginx.9527du.com和haproxy.9527du.com主機都要安裝上:nginx和haproxy
1、在haproxy.9527du.com 編譯安裝nginx並測試
(1)、編譯安裝NGINX
在haproxy.9527du.com 編譯安裝nginx,如在nginx.9527du.com(192.168.60.40)安裝一樣。
(2)、從nginx.9527du.com複製已經測試成功的配置文件和啓動腳本到haproxy.9527du.com節點
[root@nginx admin]# scp -p /etc/init.d/nginx 192.168.60.130:/etc/init.d/ [email protected]'s password: nginx 100% 2525 2.5KB/s 00:00 [root@nginx admin]# scp -p /etc/nginx/nginx.conf 192.168.60.130:/etc/nginx/ [email protected]'s password: nginx.conf 100% 3089 3.0KB/s 00:00
(3)、啓動
[root@haproxy ~]# service nginx start Starting nginx: [ OK ] [root@haproxy ~]# netstat -anptl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5854/nginx
(4)、訪問測試
如下圖:
說明:
在haproxy.9527du.com(192.168.60.130)安裝的nginx也可以調度兩個tracker了。
2、在nginx.9527du.com(192.168.60.40)安裝配置haproxy 來調度兩臺數據庫服務器
(1)、從haproxy.9527du.com節點複製已經測試成功的配置文件到nginx.9527du.com(192.168.60.40)節點
[root@haproxy ~]# scp -p /etc/haproxy/haproxy.cfg 192.168.60.40:/etc/haproxy/ [email protected]'s password: haproxy.cfg 100% 2871 2.8KB/s 00:00
(2)、使用rsyslog記錄haproxy產生的日誌記錄日誌
[root@nginx ~]# vim /etc/rsyslog.conf local2.* /var/log/haproxy.log # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514
(3)、啓動
[root@nginx ~]# service rsyslog restart Shutting down system logger: [ OK ] Starting system logger: [ OK ] [root@nginx ~]# service haproxy start Starting haproxy: [ OK ] [root@nginx ~]# netstat -anptl | grep haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2653/haproxy tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2653/haproxy
(3)、訪問測試:
訪問haproxy的狀態頁面
D:\>mysql -uadmin -h192.168.60.40 -p Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 156 Server version: 5.5.36-MariaDB-log MariaDB Server 。。。。。。。。 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mogilefs | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
說明:
nginx.9527du.com(192.168.60.40)上的haproxy 已經能夠調度數據庫服務了。
<二>、在 haproxy.9527du.com(192.168.60.130)和nginx.9527du.com(192.168.60.40)配置keepalived 實現對 nginx調度器和haproxy調度器的高可用。
從而避免單點故障,導致存儲癱瘓。
1、配置keepalived
(1)、分佈式應用的要求,爲了保證系統能夠正常工作。
保證兩個節點的時間是同步的
[root@nginx ~]# hostname;date;ssh 192.168.60.80 'hostname;date' nginx.9527du.com Thu Oct 2 12:06:15 CST 2014 haproxy.9527du.com Thu Oct 2 12:06:39 CST 2014
(2)、配置文件如下:
[root@haproxy ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 192.168.60.80 smtp_connect_timeout 30 } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 2 weight -5 fall 2 rise 1 } vrrp_script chk_nginx { script "killall -0 nginx" interval 3 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 53 priority 110 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.60.130 } track_script { chk_haproxy chk_nginx } notify_master "/etc/init.d/haproxy start; /etc/init.d/nginx start" notify_fault "/etc/init.d/haproxy stop; /etc/init.d/nginx start" } vrrp_instance VI_3 { state BACKUP interface eth0 virtual_router_id 56 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.60.40 } track_script { chk_haproxy chk_nginx } notify_master "/etc/init.d/haproxy start; /etc/init.d/nginx start" notify_fault "/etc/init.d/haproxy stop; /etc/init.d/nginx start" }
(3)、複製配置文件到nginx.9527du.com節點。
[root@haproxy ~]# scp -p /etc/keepalived/keepalived.conf 192.168.60.90:/etc/keepalived/
(4)、修改nginx.9527du.com節點的配置文件,配置文件如下
[root@nginx ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 192.168.60.90 smtp_connect_timeout 30 } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 2 weight -5 fall 2 rise 1 } vrrp_script chk_nginx { script "killall -0 nginx" interval 3 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 53 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.60.130 } track_script { chk_haproxy chk_nginx } notify_master "/etc/init.d/haproxy start; /etc/init.d/nginx start" notify_fault "/etc/init.d/haproxy stop; /etc/init.d/nginx start" } vrrp_instance VI_3 { state MASTER interface eth0 virtual_router_id 56 priority 115 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.60.40 } track_script { chk_haproxy chk_nginx } notify_master "/etc/init.d/haproxy start; /etc/init.d/nginx start" notify_fault "/etc/init.d/haproxy stop; /etc/init.d/nginx start" }
2、啓動keepalived
(1)、在nginx.9527du.com節點先啓動keepalived服務
[root@nginx ~]# service keepalived start Starting keepalived: [ OK ]
(2)、查看資源
查看haproxy服務是否啓動
[root@nginx ~]# netstat -anptl | grep haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2188/haproxy tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2188/haproxy tcp 0 1 192.168.60.90:43245 192.168.60.22:3306 SYN_SENT 2188/haproxy
查看nginx服務是否啓動
[root@nginx ~]# netstat -anptl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2291/nginx
查看流動IP
[root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.40/32 scope global eth0 inet 192.168.60.130/32 scope global eth0
3、啓動haproxy.9527du.com(192.168.60.80)的keepalived
(1)、遠程方式啓動keepalived服務
[root@nginx ~]# ssh 192.168.60.80 'service keepalived start' Starting keepalived: [ OK ]
(2)、查看資源
查看haproxy服務是否啓動
[root@haproxy ~]# netstat -anptl | grep haproxy tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2193/haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2193/haproxy tcp 0 1 192.168.60.132:33174 192.168.60.22:3306 SYN_SENT 2193/haproxy tcp 0 1 192.168.60.132:45016 192.168.60.88:3306 SYN_SENT 2193/haproxy
查看nginx服務是否啓動
[root@haproxy ~]# netstat -anptl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2292/nginx
查看流動IP:192.168.60.130是否配置上?
[root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.130/32 scope global eth0
說明:
從上述信息得知,在兩個節點啓動keepalived都正常。
3、測試:
(1)、關閉haproxy.9527du.com(192.168.60.80)的keepaliced 查看VIP 192.168.60.130 是否會流轉到nginx.9527du.com(192.168.60.90)
關閉keepalived服務
[root@haproxy ~]# service keepalived stop Stopping keepalived:
查看是否放棄配置流動IP:192.168.60.130
[root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0
從上述可以看出VIP已經不在了
查看nginx.9527du.com
[root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.40/32 scope global eth0 inet 192.168.60.130/32 scope global eth0
從上面可以看出,haproxy.9527du.com的VIP已經流轉到該臺主機了。
(2)、查看對haproxy和nginx服務是否影響
[root@nginx ~]# netstat -anptl | grep -e "haproxy" -e "nginx" tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2188/haproxy tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2291/nginx tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2188/haproxy tcp 0 1 192.168.60.90:41282 192.168.60.88:3306 SYN_SENT 2188/haproxy
說明:
從上述可以看出,一切正常。
(3)、把haproxy.9527du.com的keepalived啓動,查看VIP:192.168.60.130 是配置上。
啓動
[root@haproxy ~]# service keepalived start Starting keepalived: [ OK ]
查看流轉IP
[root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.130/32 scope global eth0
查看haproxy和nginx服務是否正常
[root@haproxy ~]# netstat -anptl | grep -e "haproxy" -e "nginx" tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2292/nginx tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2193/haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2193/haproxy tcp 0 1 192.168.60.80:37754 192.168.60.88:3306 SYN_SENT 2193/haproxy tcp 0 1 192.168.60.80:55891 192.168.60.22:3306 SYN_SENT 2193/haproxy
4、再測試;
(1)、關閉nginx.9527du.com(192.168.60.90)的keepaliced 查看VIP 192.168.60.40 是否會流轉到haproxy.9527du.com(192.168.60.80)
關閉keepalived服務
[root@nginx ~]# service keepalived stop Stopping keepalived: [ OK ]
查看是流動IP:192.168.60.40 是否還在
[root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0
查看,haproxy.9527du.com 是否配置了VIP: 192.168.60.40
[root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.130/32 scope global eth0 inet 192.168.60.40/32 scope global eth0
查看對haproxy,nginx服務是否有影響
[root@haproxy ~]# netstat -anptl | grep -e "haproxy" -e "nginx" tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2292/nginx tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2193/haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2193/haproxy tcp 0 1 192.168.60.80:55991 192.168.60.22:3306 SYN_SENT 2193/haproxy tcp 0 1 192.168.60.80:37854 192.168.60.88:3306 SYN_SENT 2193/haproxy
說明:
從上述可以看出一切正常
(2)、把nginx.9527du.com的keepalived啓動起
啓動
[root@nginx ~]# service keepalived start Starting keepalived: [ OK ]
查看是否配置上流動IP:192.168.60.40
[root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.40/32 scope global eth0
查看:haproxy和nginx服務是否正常
[root@nginx ~]# netstat -anptl | grep -e "haproxy" -e "nginx" tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2188/haproxy tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2291/nginx tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2188/haproxy tcp 0 1 192.168.60.90:41483 192.168.60.88:3306 SYN_SENT 2188/haproxy tcp 0 1 192.168.60.90:44135 192.168.60.22:3306 SYN_SENT 2188/haproxy
說明:
從上述測試結果可以看出,keepalived高可用已經搭建成功
七、通過瀏覽訪問存儲在mogilefs中的圖片,來測試該存儲系統的冗餘能力?
1、測試數據的高可用是否成功,對獲取圖片數據是否影響
(1)、關閉dataserver1.9527du.com 主機的數據庫服務
[root@dataserver1 ~]# service mysqld stop Shutting down MySQL. [ OK ] [root@dataserver1 ~]# netstat -anptl | grep mysqld
訪問如下圖:
說明:
正常。
(2)、關閉dataserver2.9527du.com的數據庫服務
[root@dataserver1 ~]# service mysqld start Starting MySQL.. [ OK ] [root@dataserver2 ~]# service mysqld stop Shutting down MySQL. [ OK ] [root@dataserver2 ~]# netstat -anptl | grep mysqld
訪問測試:
如下圖。
說明:
訪問圖片一切正常。
[root@dataserver2 ~]# service mysqld start Starting MySQL.. [ OK ]
2、測試:調度器nginx和haproxy是否實現高可用
(1)、關閉nginx.9527du.com的keepalived(測試nginx調度器)
[root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.40/32 scope global eth0 [root@nginx ~]# service keepalived stop Stopping keepalived: [ OK ] [root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0
訪問測試:
如下圖:
說明:
keepalived高可用nginx節點沒有問題
[root@nginx ~]# service keepalived start Starting keepalived: [ OK ] [root@nginx ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.90/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.40/32 scope global eth0
(2)、關閉haproxy.9527du.com 的keepalived服務(測試haproxy調度器)
[root@haproxy ~]# service keepalived stop Stopping keepalived: [ OK ] [root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0
訪問測試:
說明:
keepalived高可用nginx節點沒有問題
[root@haproxy ~]# service keepalived start Starting keepalived: [ OK ] [root@haproxy ~]# ip add show | grep "[[:space:]]*inet[[:space:]]*[1][9]" inet 192.168.60.80/24 brd 192.168.60.255 scope global eth0 inet 192.168.60.132/24 brd 192.168.60.255 scope global eth1
3、測試mogilefs的tracker是否能夠提供高可用
(1)、停止 mogilefs1.9527du.com 的mogilefsd服務
[root@mogilefs1 ~]# service mogilefsd stop Stopping mogilefsd [ OK ] [root@mogilefs1 ~]# netstat -anptl | grep mogilefsd
訪問測試:
如下圖:
說明:
一切正常
[root@mogilefs1 ~]# service mogilefsd start Starting mogilefsd [ OK ] [root@mogilefs1 ~]# netstat -anptl | grep mogilefsd tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 2429/mogilefsd
(2)、測試停止 mogilefs2.9527du.com 的mogilefsd服務
[root@mogilefs2 ~]# service mogilefsd stop Stopping mogilefsd [ OK ] [root@mogilefs2 ~]# netstat -anptl | grep mogilefsd
訪問測試:
如下圖:
[root@mogilefs2 ~]# service mogilefsd start Starting mogilefsd [ OK ] [root@mogilefs2 ~]# netstat -anptl | grep mogilefsd tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 2528/mogilefsd
說明:
一切也正常。
tracker節點的高可用也沒有問題。
完成!!!