1醇锚,簡(jiǎn)介
FastDFS是一款類GoogleFS的開源分布式文件系統(tǒng)续膳,它用純C語(yǔ)言實(shí)現(xiàn)页畦,支持Linux胖替、FreeBSD、AIX等UNIX系統(tǒng)豫缨。它只能通過專有API對(duì)文件進(jìn)行存取訪問独令,不支持POSIX接口方式,不能mount使用好芭。準(zhǔn)確地講燃箭,GoogleFS以及FastDFS、mogileFS栓撞、HDFS遍膜、TFS等類GoogleFS都不是系統(tǒng)級(jí)的分布式文件系統(tǒng),而是應(yīng)用級(jí)的分布式文件存儲(chǔ)服務(wù)瓤湘。
FastDFS是為互聯(lián)網(wǎng)應(yīng)用量身定做的分布式文件系統(tǒng)瓢颅,充分考慮了冗余備份、負(fù)載均衡弛说、線性擴(kuò)容等機(jī)制挽懦,并注重高可用、高性能等指標(biāo)木人。和現(xiàn)有的類Google FS分布式文件系統(tǒng)相比信柿,F(xiàn)astDFS的架構(gòu)和設(shè)計(jì)理念有其獨(dú)到之處,主要體現(xiàn)在輕量級(jí)醒第、分組方式和對(duì)等結(jié)構(gòu)三個(gè)方面渔嚷。
FastDFS是由國(guó)人余慶所開發(fā),其項(xiàng)目地址:https://github.com/happyfish100 稠曼。
2形病,架構(gòu)
2.1,架構(gòu)圖
FastDFS服務(wù)端有兩個(gè)角色:跟蹤器(tracker)和存儲(chǔ)節(jié)點(diǎn)(storage)。跟蹤器主要做調(diào)度工作漠吻,在訪問上起負(fù)載均衡的作用量瓜。
2.2,模塊介紹
- tracker server:跟蹤服務(wù)器途乃,用來(lái)調(diào)度來(lái)自客戶端的請(qǐng)求绍傲,且在內(nèi)存中記錄所有存儲(chǔ)組和存儲(chǔ)服務(wù)器的信息狀態(tài)。
- storage server:存儲(chǔ)服務(wù)器耍共,用來(lái)存儲(chǔ)文件(data)和文件屬性(metadata)烫饼。
- client:客戶端,業(yè)務(wù)請(qǐng)求發(fā)起方划提,通過專用接口基于TCP協(xié)議與tracker server和storage server進(jìn)行交互枫弟。
- group:組,也可稱為卷鹏往,同組內(nèi)上的文件是完全相同的。
- 文件標(biāo)識(shí):包括兩部分骇塘,組名(group)和文件名(含路徑)
- 文件相關(guān)屬性:鍵值對(duì)(Key Value Pair)方式
- 文件名:與原文件名并不相同伊履。由storage server根據(jù)特定信息生成,并且可逆款违,文件名包含:源存儲(chǔ)服務(wù)器的IP地址唐瀑、文件創(chuàng)建時(shí)間戳、文件大小插爹、隨機(jī)數(shù)和文件擴(kuò)展名等哄辣。
3,部署安裝
3.1赠尾,安裝包準(zhǔn)備
執(zhí)行下面的shell力穗,下載安裝包,分別是libfastcommon基礎(chǔ)組件气嫁、fastdfs当窗、nginx、fastdfs-nginx-module寸宵。
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
wget http://nginx.org/download/nginx-1.11.8.tar.gz
wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
3.2崖面,安裝libfastcommon基礎(chǔ)組件
- 解壓:tar -zxvf V1.0.7.tar.gz
- 進(jìn)入解壓后目錄:cd libfastcommon-1.0.7/
- 編譯:./make.sh
- 安裝:sudo ./make.sh install
- 設(shè)置軟鏈接,否則無(wú)法啟動(dòng)
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
3.3梯影,安裝fastdfs
安裝
- 解壓: tar -zxvf V5.05.tar.gz
- 進(jìn)入解壓后目錄:cd fastdfs-5.05/
- 編譯:./make.sh
- 安裝:sudo ./make.sh install
- 設(shè)置軟鏈接巫员,否則無(wú)法啟動(dòng)
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so - 目錄規(guī)劃
/usr/bin存放有編譯出來(lái)的文件,包含fdfs_trackerd甲棍、fdfs_storaged简识、fdfs_monitor、fdfs_upload_file等
/etc/fdfs存放有配置文件
配置文件
- 復(fù)制一份tracker配置文件:
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf - 編輯:vim /etc/fdfs/tracker.conf
# the base path to store data and log files
base_path=/home/kevin/fastdfs/tracker/data-and-log
- 復(fù)制一份storage配置文件:cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
- 編輯:vim /etc/fdfs/storage.conf
# the base path to store data and log files
base_path=/home/kevin/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/kevin/fastdfs/storage/images-data0
store_path1=/home/kevin/fastdfs/storage/images-data1
store_path2=/home/kevin/fastdfs/storage/images-data2
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.1.32:22122
- 備注:storage (存儲(chǔ)節(jié)點(diǎn))服務(wù)部署,一般 storage 服務(wù)我們會(huì)單獨(dú)裝一臺(tái)機(jī)子财异,但是這里為了方便我們安裝在同一臺(tái)倘零。如果 storage 單獨(dú)安裝的話,那上面安裝的所有步驟都要在走一遍戳寸,只是到了編輯配置文件的時(shí)候呈驶,編輯的是 storage.conf 而已。
啟動(dòng)測(cè)試
tracker server
- 啟動(dòng) tracker 服務(wù):
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf - 重啟 tracker 服務(wù):
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart - 查看是否有tracker 進(jìn)程:
ps aux | grep tracker
storage server
- 啟動(dòng) storage 服務(wù):
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf疫鹊,首次啟動(dòng)會(huì)很慢袖瞻,因?yàn)樗趧?chuàng)建預(yù)設(shè)存儲(chǔ)文件的目錄 - 重啟 storage 服務(wù):
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart - 查看是否有 storage 進(jìn)程:
ps aux | grep storage
3.4,client測(cè)試
- 編輯/etc/fdfs/client.conf
# the base path to store log files
base_path=/home/kevin/fastdfs/client/data-and-log
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.1.32:22122
- 通過fdfs_test上傳的圖片拆吆,保存了兩張圖片
kevin@orange:~/fastdfs/temp$ wget http://img.tuku.cn/file_thumb/201504/m2015042221045424.jpg
kevin@orange:~/fastdfs/temp$ /usr/bin/fdfs_test /etc/fdfs/client.conf upload ./m2015042221045424.jpg
kevin@orange:~/fastdfs/storage/images-data0$ find . -name "*.jpg"
./data/00/00/wKgBIFmemtyAGcebAAAdgkSCZh4105.jpg
./data/00/00/wKgBIFmemtyAGcebAAAdgkSCZh4105_big.jpg
- 通過fdfs_upload_file上傳聋迎,保存一張?jiān)紙D片
kevin@orange:~/fastdfs/temp$ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf m2015042221045424.jpg
group1/M00/00/00/wKgBIFmenECAAOdbAAAdgkSCZh4066.jpg
kevin@orange:~/fastdfs/storage/images-data0$ find . -name "*.jpg" | xargs ls -l
-rw-r--r-- 1 root root 7554 8月 24 17:22 ./data/00/00/wKgBIFmemtyAGcebAAAdgkSCZh4105_big.jpg
-rw-r--r-- 1 root root 7554 8月 24 17:22 ./data/00/00/wKgBIFmemtyAGcebAAAdgkSCZh4105.jpg
-rw-r--r-- 1 root root 7554 8月 24 17:28 ./data/00/00/wKgBIFmenECAAOdbAAAdgkSCZh4066.jpg
- 即使我們現(xiàn)在知道圖片的訪問地址我們也訪問不了,因?yàn)槲覀冞€沒裝 FastDFS 的 Nginx 模塊
3.5枣耀,安裝nginx及其插件
安裝
- nginx插件的配置文件
# 解壓nginx的fastdfs插件文件
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
# 修改配置文件中的無(wú)效路徑霉晕,把local去掉
vim fastdfs-nginx-module/src/config
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改為
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
復(fù)制文件:cp fastdfs-5.05/conf/http.conf /etc/fdfs
復(fù)制文件:cp fastdfs-5.05/conf/mime.types /etc/fdfs
- 安裝 Nginx 依賴包(Ubuntu)
sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
通過命令,查看是否安裝某些包
dpkg -l | grep zlib創(chuàng)建nginx文件目錄
mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx生成配置捞奕,注意修改最后一行(--add-module參數(shù))牺堰,倒數(shù)第二行為ipv6模塊
tar -zxvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/local/nginx/nginx.pid \
--lock-path=/var/lock/nginx/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-ipv6 \
--add-module=/home/kevin/fastdfs/install/soft/fastdfs-nginx-module/src
編譯:make
安裝:sudo make install (也可以直接復(fù)制nginx文件到/etc/nginx/,避免直接install影響其他nginx環(huán)境)
復(fù)制配置文件:cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
確認(rèn)nginx的fastdfs插件是否安裝成功
root@orange:/usr/local/nginx/conf# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.11.8
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/local/nginx/nginx.pid --lock-path=/var/lock/nginx/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/home/kevin/fastdfs/install/soft/fastdfs-nginx-module/src
- 備注:--prefix=/usr/local/nginx說(shuō)明nginx目錄是/usr/local/nginx颅围,--add-module說(shuō)明nginx的插件
配置文件
- 編輯/etc/fdfs/mod_fastdfs.conf
# the base path to store log files
base_path=/home/kevin/fastdfs/fastdfs-nginx-module/data-and-log/
# FastDFS tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.1.32:22122
# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/kevin/fastdfs/storage/images-data0
store_path1=/home/kevin/fastdfs/storage/images-data1
store_path2=/home/kevin/fastdfs/storage/images-data2
- 編輯/usr/local/nginx/conf/nginx.conf
# 在http添加對(duì)其他配置文件的引用
include /usr/local/nginx/conf/conf.d/*.conf;
- 在/usr/local/nginx/conf/目錄下創(chuàng)建文件夾conf.d伟葫,并編輯fdfs.conf,完成nginx與fastdfs的對(duì)接
server {
listen 8888;
listen [::]:8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4院促,測(cè)試
地址:http://192.168.1.32:8888
上傳不同類型的文件
kevin@orange:~/fastdfs/temp$ fdfs_upload_file /etc/fdfs/client.conf meixi.jpg
group1/M00/00/00/wKgBIFmeyGWAJHacAACiyMy3SR8400.jpg
kevin@orange:~/fastdfs/temp$ fdfs_upload_file /etc/fdfs/client.conf gnuplot.pdf
group1/M00/00/00/wKgBIFmeyJiAFBM2ABhOlsdTgP0787.pdf
kevin@orange:~/fastdfs/temp$ fdfs_upload_file /etc/fdfs/client.conf my_multi.cnf
group1/M00/00/00/wKgBIFmeyLeARRxPAAAHX_E2eqU506.cnf
kevin@orange:~/fastdfs/temp$ fdfs_upload_file /etc/fdfs/client.conf tiger.txt
group1/M00/00/00/wKgBIFmeyLyAEhFzAAAFTfDtQ9M236.txt
- 通過wget下載測(cè)試
wget http://192.168.1.32:8888/group1/M00/00/00/wKgBIFmeyGWAJHacAACiyMy3SR8400.jpg
wget http://192.168.1.32:8888/group1/M00/00/00/wKgBIFmeyJiAFBM2ABhOlsdTgP0787.pdf
wget http://192.168.1.32:8888/group1/M00/00/00/wKgBIFmeyLeARRxPAAAHX_E2eqU506.cnf
wget http://192.168.1.32:8888/group1/M00/00/00/wKgBIFmeyLyAEhFzAAAFTfDtQ9M236.txt
- 通過瀏覽器直接訪問
5筏养,參考網(wǎng)址
- FastDFS常見命令
http://www.linuxidc.com/Linux/2014-10/107575.htm - 常見開源分布式存儲(chǔ)系統(tǒng)
http://www.charmingzhou.com/distributed.html - 分布式文件系統(tǒng)FastDFS架構(gòu)剖析
http://os.51cto.com/art/201205/335144.htm - 分布式文件系統(tǒng)-FastDFS
http://xinzong.blog.51cto.com/10018904/1834466