系統環境
1.系統環境:
# uname -smr
Linux 2.6.32-504.23.4.el6.centos.plus.x86_64 x86_64
# cat /etc/issue
CentOS release 6.6 (Final)
2.MogileFS規劃:
數據庫 (MySQL):192.168.10.241 (儲存MogileFS元數據)
管理節點(tarckers):192.168.10.200:7001 (負責MogileFS的調度,管理)
存儲節點(storage1):192.168.10.220:7500 (存儲文件)
存儲節點(storage2):192.168.10.231:7500 (存儲文件)
反向代理服務器(Nginx):192.168.10.231:8080 (爲MogileFS的Tracker提供反向代理服務)
1.特性
1).工作於應用層:無需特殊的核心組件; 2).單點:三大組件(tracker,mogstore,database)皆可實現高可用; 3).自動文件複製:複製的最小單位不是文件,而是class;基於不同的class,文件可以被自動的複製到多個有足夠存儲空間的存儲節點上; 4).傳輸中立,無特殊協議:可以通過NFS或HTTP協議進行通信; 5).簡單的命名空間:文件通過一個給定的key來確定,是一個全局的命名空間;沒有目錄,基於域實現文件隔離; 6).不共享數據:無需通過昂貴的SAN來共享磁盤,每個存儲節點只需維護自己所屬的存儲設備(device)
2.MogileFS架構(如果數據量較大可以對MySQL做主從,並使用amoeba實現讀寫分離)
Tracker:MogileFS的核心,是一個調度器;服務進程爲mogilefsd;可以做負載均衡調度;
主要職責有:
數據刪除;
數據複製;
監控:故障後生成新的數據副本;
查詢;
Database:Tracker訪問Database,返回用戶可用的Storage Node及文件的存放位置;
mogstored:數據存儲的位置,通常是一個HTTP(WebDAV)服務器,用於數據的創建、刪除、獲取等;不可做負載均衡調度;
3. 組成
MogileFS由3部分組成:
server:主要包括mogilefsd和mogstored兩個應用程序。
mogilefsd實現的是tracker,它通過數據庫來保存元數據信息,包括站點domain、class、host等;
mogstored是存儲節點(store node),它其實是個WebDAV服務,默認監聽在7500端口,接受客戶端的文件存儲請求。
Utils(工具集):主要是MogileFS的一些管理工具,例如mogadm等;
在MogileFS安裝完後,要運行mogadm工具將所有的store node註冊到mogilefsd的數據庫裏,mogilefsd會對這些節點進行管理和監控;
客戶端API:MogileFS的客戶端API很多,例如Perl、PHP、Java、Python等,用這個模塊可以編寫客戶端程序,實現文件的備份管理功能等;
4. 安裝部署MogileFS(在192.168.10.220和192.168.100.231執行相同操作)
1. 安裝MogileFS分佈式文件系統 yum install perl perl-YAML cpan -I Danga::Socket cpan -I IO::AIO cpan -I Net::Netmask cpan -I Perlbal cpan -I DBD::mysql cpan -I IO::WrapTie tar xf MogileFS-Client-1.17.tar.gz cd MogileFS-Client-1.17 perl Makefile.PL make && make install tar xf MogileFS-Server-2.72.tar.gz perl Makefile.PL make make install cpan -I MogileFS::Utils 2. MogileFS不能以root用戶啓動,必須爲Mogilefs創建用戶 useradd mogilefs 3. 增大打開文件數,並且重啓服務器(默認1024) vim /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 4. 準備存儲設備 mkdir /etc/mogilefs mkdir -p /data/mogdata/dev1 將添加一塊磁盤掛載至/data/mogdata/dev1 chown -R mogilefs /data/mogdata/dev1 5. 爲MogileFS提供配置文件 # vim /etc/mogilefs/mogilefsd.conf #MogileFS(調度節點或管理節點)的配置文件 daemonize = 1 db_dsn = DBI:mysql:mogilefs:host=192.168.10.241;port=3306;mysql_connect_timeout=5 db_user = mogilefs db_pass = mogilefs trackers = 192.168.10.241 conf_port = 7001 listener_jobs = 5 node_timeout = 5 rebalance_ignore_missing = 1 query_jobs = 10 delete_jobs = 1 replicate_jobs = 5 reaper_jobs = 1 # vim /etc/mogilefs/mogilefs.conf #該文件在MogileFS的家目錄下 trackers = 192.168.10.220:7001 # vim /etc/mogilefs/mogstored.conf #MogileFS的Storage(存儲節點)的配置文件 maxconns = 10000 httplisten=0.0.0.0:7500 mgmtlisten=0.0.0.0:7501 docroot=/data/mogdata chown -R mogilefs /etc/mogilefs/ 6. 部署數據庫,參照以前文章,配置數據庫,保存MogileFS的元數據 創建數據庫 mysql> CREATE DATABASE mogilefs; mysql> GRANT ALL ON mogilefs.* TO 'mogilefs'@'%' IDENTIFIED BY 'mogilefs'; mysql> FLUSH PRIVILEGES; mysql> quit 初始化Mogilefs存儲元數據的數據庫(執行一次即可) $ mogdbsetup --dbhost=192.168.10.241 --dbname=mogilefs --dbuser=mogilefs --dbpassword=mogilefs --yes 啓動Trackers $ mogilefsd 啓動Storage $ mogstored -d
5. 向MogileFS的Trackers節點添加host(主機),device(設備),domain(域),class(文件的最小複製單位)
添加節點 $ mogadm --trackers=192.168.10.220:7001 host add 192.168.10.220 --ip=192.168.10.220 --status=alive $ mogadm --trackers=192.168.10.220:7001 host add 192.168.10.231 --ip=192.168.10.231 --status=alive $ mogadm host list # 查看已添加節點 添加設備 $ mogadm device add 192.168.10.220 1 $ mogadm device add 192.168.10.231 2 $ mogadm device list # 查看已添加設備 在192.168.10.231 創建設備 mkdir -p /data/mogdata/dev2 chown -R mogilefs /data/mogdata/dev2 $ mogadm device list 192.168.10.220 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 0.021 9.200 9.222 100 192.168.10.231 [2]: alive used(G) free(G) total(G) weight(%) dev2: alive 16.371 9.725 26.096 100 添加domain(域): $ mogadm domain add images $ mogadm domain list domain class mindevcount replpolicy hashtype -------------------- -------------------- ------------- ------------ ------- images default 2 MultipleHosts() NONE 添加class(文件類別) $ mogadm class add images upload1 --mindevcount=2 # 在域images中添加類別upload和upload2,最小文件複製份數爲2 $ mogadm class add images upload2 --mindevcount=2 測試文件 $ mogtool --trackers=192.168.10.220:7001 --domain=images inject /home/mogilefs/1.jpeg "1.jpeg" $ mogtool --trackers=192.168.10.220:7001 --domain=images inject /home/mogilefs/2.jpg "/2.jpg" $ moglistkeys --domain=images /2.jpg 1.jpeg 查看文件信息 $ mogfileinfo --domain=images --key='fish.jpg' - file: fish.jpg class: default devcount: 2 domain: images fid: 15 key: fish.jpg length: 50040 - http://192.168.10.220:7500/dev1/0/000/000/0000000015.fid - http://192.168.10.231:7500/dev2/0/000/000/0000000015.fid
6. 通過Python腳本實現MogileFS文件的遷移,也可以結合Rsync實現備份:
#!/usr/bin/python # Author:zhooukanggen # Dump File of MogileFS import subprocess import time def MogileFS_Dump(): moglistkeys = '/usr/local/bin/moglistkeys --trackers=127.0.0.1:7001 --domain=common_raw' Rest = subprocess.Popen([moglistkeys],stdout=subprocess.PIPE,shell=True).communicate()[0].split('\n') for i in Rest: status = subprocess.call(['/usr/local/bin/mogfetch','--trackers=127.0.0.1:7001','--domain=common_raw','--key=' + i,'--file=./' + i]) time.sleep(1) if status == 0: print "Success" else: print "Fail" MogileFS_Dump() #導出文件 def MogileFS_Upload(): Upload = subprocess.Popen(['ls -1 /home/web/mogilefs'],stdout=subprocess.PIPE,shell=True).communicate()[0].split('\n')[:-1] for i in Upload: status = subprocess.call(['/usr/local/bin/mogupload','--trackers=192.168.10.220:7001','--domain=images','--key=' + i,'--file=/home/web/mogilefs/' + i]) time.sleep(5) if status == 0: print "Success" else: print "Fail" MogileFS_Upload() #導入文件
7. 使用nginx作爲MogileFS的反向代理
1. 安裝nginx # wget # wget # tar xf nginx-1.8.0.tar.gz # tar xf nginx_mogilefs_module-1.0.4.tar.gz # cd nginx-1.8.0 ./configure \ --prefix=/usr/local/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/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre --with-debug \ --add-module=../nginx_mogilefs_module-1.0.4 # make && make install 2. 配置nginx作爲MogileFS的反向代理 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; tcp_nopush on; keepalive_timeout 65; fastcgi_cache_path /var/tmp/nginx/fcgi levels=1:2 keys_zone=fcgicache:10m inactive=5m; server { listen 8080; server_name localhost; location / { root html; index index.html index.htm; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /images/ { mogilefs_tracker 192.168.10.220:7001; mogilefs_domain images; mogilefs_noverify on; mogilefs_pass { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } } } 3. 啓動nginx # /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
8. 驗證:
檢查MogileFS $ mogadm check Checking trackers... 192.168.10.220:7001 ... OK Checking hosts... [ 1] 192.168.10.220 ... OK [ 2] 192.168.10.231 ... OK Checking devices... host device size(G) used(G) free(G) use% ob state I/O% ---- ------------ ---------- ---------- ---------- ------ ---------- ----- [ 1] dev1 9.222 0.040 9.182 0.43% writeable 0.0 [ 2] dev2 26.096 16.389 9.707 62.80% writeable N/A ---- ------------ ---------- ---------- ---------- ------ total: 35.318 16.429 18.889 46.52% 獲取詳細狀態 $ mogstats -c /etc/mogilefs/mogilefsd.conf Fetching statistics... (all) Statistics for devices... device host files status ---------- ---------------- ------------ ---------- dev1 192.168.10.220 107 alive dev2 192.168.10.231 107 alive ---------- ---------------- ------------ ---------- Statistics for file ids... Max file id: 619 Statistics for files... domain class files size (m) fullsize (m) -------------------- ----------- ---------- ----------- ------------- images default 107 17 35 -------------------- ----------- ---------- ----------- ------------- Statistics for replication... domain class devcount files -------------------- ----------- ---------- ---------- images default 2 107 -------------------- ----------- ---------- ---------- Statistics for replication queue... status count -------------------- ------------ -------------------- ------------ Statistics for delete queue... status count -------------------- ------------ -------------------- ------------ Statistics for general queues... queue status count --------------- -------------------- ------------ --------------- -------------------- ------------ done 列出hots $ mogadm host list 192.168.10.220 [1]: alive IP: 192.168.10.220:7500 192.168.10.231 [2]: alive IP: 192.168.10.231:7500 查看domain $ mogadm domain list domain class mindevcount replpolicy hashtype -------------------- -------------------- ------------- ------------ ------- images default 2 MultipleHosts() NONE images upload1 2 MultipleHosts() NONE images upload2 2 MultipleHosts() NONE 查看host信息$ mogadm class list domain class mindevcount replpolicy hashtype -------------------- -------------------- ------------- ------------ ------- images default 2 MultipleHosts() NONE images upload1 2 MultipleHosts() NONE images upload2 2 MultipleHosts() NONE 查看設備信息 $ mogadm device list 192.168.10.220 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 0.039 9.183 9.222 100 192.168.10.231 [2]: alive used(G) free(G) total(G) weight(%) dev2: alive 16.389 9.707 26.096 100 添加文件 $ mogupload --domain=images --key='1.jpg' --file='/home/mogilefs/2.jpg' 查看key爲1.jpg的信息 $ mogfileinfo --domain=images --key='1.jpg' - file: 1.jpg class: default devcount: 2 domain: images fid: 619 key: 1.jpg length: 279696 - http://192.168.10.220:7500/dev1/0/000/000/0000000619.fid - http://192.168.10.231:7500/dev2/0/000/000/0000000619.fid 導出文件 $ mogfetch --domain=images --key='1.jpg' --file='./test.jpg'
通過瀏覽器訪問1.jpg
使用將host(192.168.10.231)的狀態修改爲down,並通過瀏覽器訪問1.jpg
$ mogadm host modify 192.168.10.231 --ip=192.168.10.231 --status=down $ mogadm host list 192.168.10.220 [1]: alive IP: 192.168.10.220:7500 192.168.10.231 [2]: down IP: 192.168.10.231:7500
注意 :
1.當Sys-Syscal 使用 Sys-Syscall-0.25.tar.gz 版本 會導致副本就只有一份(自身的bug),通過查找資料當Sys-Syscall模塊需要使用 Sys-Syscall-0.23.tar.gz 副本就會恢復正常
2.增大MogileFS用戶最大打開文件數,重啓服務器,如果不增大最大打開文件數,啓動Mogstored就會報錯
ERROR: Need to be root to increase max connections.