fastDFS分佈式文件系統搭建

fastDFS概述

 FastDFS是一個開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等。
 FastDFS爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。

優勢

(1)海量的存儲:主從型分佈式存儲,存儲空間方便拓展,
(2)fastDFS對文件內容做hash處理,避免出現重複文件
(3)然後fastDFS結合Nginx集成, 提供網站效率

組成結構

1、跟蹤器(tracker):跟蹤器主要做調度工作;
2、存儲節點(storage):在訪問上起負載均衡的作用

fastDFS分佈式文件系統搭建

實驗環境

tracker服務器 192.168.13.128(libfastcommon、fastd、)
storage+Nginx服務器 192.168.13.129(libfastcommon)

1,在所有節點服務器上安裝libfastcommon服務

[root@tracker ~]# yum -y install libevent  \   ##安裝環境必要的組件
libevent-devel perl  make gcc zlib  zlib-devel \
pcre pcre-devel  gcc-c++  openssl-devel
[root@tracker ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz
##從網站中下載libfastcommon壓縮包
[root@tracker ~]# tar zxvf V1.0.38.tar.gz -C /opt/  ##解壓
[root@tracker ~]# cd /opt/libfastcommon-1.0.38/
[root@tracker libfastcommon-1.0.38]# ./make.sh && ./make.sh install  ##編譯安裝
##建立軟連接便於系統識別
[root@tracker libfastcommon-1.0.38]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@tracker libfastcommon-1.0.38]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@tracker libfastcommon-1.0.38]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

2,在所有節點服務器上安裝fastDFS服務

[root@storage ~]# wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
##從網站中下載壓縮包
[root@storage ~]# tar zxf V5.11.tar.gz -C /opt/  ##解壓
[root@storage ~]# cd /opt/fastdfs-5.11/
[root@storage fastdfs-5.11]# ./make.sh && ./make.sh install   ##編譯安裝
[root@storage fastdfs-5.11]# cd /etc/fdfs/              ##複製配置文件模板
[root@storage fdfs]# cp tracker.conf.sample tracker.conf
[root@storage fdfs]# cp storage.conf.sample storage.conf
[root@storage fdfs]# cp client.conf.sample client.conf

3,配置tracker服務器

[root@tracker fdfs]# mkdir -m 755 -p /opt/fastdfs  ##創建數據文件,日誌文件存放目錄
[root@tracker fdfs]# vim /etc/fdfs/tracker.conf   ##修改tracker配置文件
port=22122         //tracker服務默認端口22122即可
base_path=/opt/fastdfs      //tracker存儲data和log的跟路徑,必須提前創建好
http.server_port=8080 //tracker服務器上啓動http服務進程,沒裝忽略
[root@tracker fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf start ##開啓服務
[root@tracker fdfs]# netstat -natp | grep 22122
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      2678/fdfs_trackerd  
[root@tracker fdfs]# vim /etc/rc.local ##設置開機自啓
##末行添加
fdfs_trackerd   /etc/fdfs/tracker.conf start 
[root@tracker fdfs]# systemctl stop firewalld.service    ##關閉防火牆
[root@tracker fdfs]# setenforce 0

4,配置storage服務器

[root@storage fdfs]# mkdir -m 755 -p /opt/fastdfs  ##創建數據和日誌文件目錄
[root@storage fdfs]# vim /etc/fdfs/storage.conf   ##修改storage配置文件
group_name=group1        //默認組名,根據實際情況修改
port=23000       //storge默認23000,同一個組的storage端口號必須一致
base_path=/opt/fastdfs          //storage日誌文件的根路徑
store_path_count=1              //與下面路徑個數相同,默認爲1
store_path0=/opt/fastdfs       //提供的存儲路徑(默認與日誌文件存放在一起)
tracker_server=192.168.13.128:22122      //自己的tracker服務器IP(重點!!!)
http.server_port=80  //http訪問文件的端口默認爲8888,nginx中配置的監聽端口保持一致
[root@storage fdfs]# fdfs_storaged /etc/fdfs/storage.conf start  ##開啓服務
[root@storage fdfs]# netstat -ntap | grep 23000
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      2687/fdfs_storaged  
[root@storage fdfs]# echo "fdfs_storaged   /etc/fdfs/storage.conf start" >> /etc/rc.local 
##開機自啓
[root@storage fdfs]# systemctl stop firewalld.service   ##關閉防火牆
[root@storage fdfs]# setenforce 0
[root@storage fdfs]# fdfs_monitor /etc/fdfs/storage.conf  ##檢查與tracker關聯是否成功
Storage 1:
id = 192.168.13.129
ip_addr = 192.168.13.129 (storage)  ACTIVE
...

5,在storage服務器上安裝Nginx

##這裏爲了減少虛擬機開啓的數量,就在storage端進行nginx的安裝
[root@storage fdfs]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
##下載模塊壓縮包
[root@storage fdfs]# tar zxf V1.20.tar.gz -C /opt/   ##解壓
[root@storage fdfs]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/
Password for root@//192.168.100.3/LNMP-C7:  
[root@storage fdfs]# cd /mnt/
[root@storage mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/   ##解壓
[root@storage mnt]# cd /opt/nginx-1.12.2
[root@storage nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--add-module=/opt/fastdfs-nginx-module-1.20/src/          ##fastdfs-nginx-module模塊
[root@storage nginx-1.12.2]# make && make install

##可能的報錯##
/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory
##解決方案##
修改fastdfs-nginx-module-1.20/src/config文件,然後重新配置編譯安裝即可
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

[root@storage nginx-1.12.2]# cd ../fastdfs-nginx-module-1.20/src/
[root@storage src]# cp mod_fastdfs.conf /etc/fdfs/
[root@storage src]# cd /etc/fdfs/
[root@storage fdfs]# vim mod_fastdfs.conf  ##配置fastdfs-nginx-module模塊
#檢查一下配置
base_path=/opt/fastdfs         //存放數據文件、日誌的路徑
tracker_server=192.168.13.128:22122    //tracker端的地址(重點!!!)
url_have_group_name = true  //url是否包含group名稱
storage_server_port=23000           //需要和storage配置的相同
store_path_count=1              //存儲路徑個數,需要和store_path個數匹配
store_path0=/opt/fastdfs               //文件存儲的位置
[root@storage fdfs]# vim /usr/local/nginx/conf/nginx.conf  ##配置Nginx配置文件
#空行處添加,不做這步可能會導致報錯
location ~/M00 {
                                root /opt/fastdfs/data;
                                ngx_fastdfs_module;
}
[root@storage fdfs]# cd /opt/fastdfs-5.11/conf/
[root@storage conf]# cp mime.types http.conf /etc/fdfs/ 
##拷貝fastdfs解壓目錄中的http.conf和mime.types

6,修改client客戶端(此處在storage服務器上使用)

[root@storage ~]# vim /etc/fdfs/client.conf
#檢查以下配置
base_path=/opt/fastdfs     ##tracker服務器文件路徑
tracker_server=192.168.13.128:22122            ##tracker服務器IP地址和端口號
http.tracker_server_port=8080 
##tracker服務器的http端口號,必須和tracker的設置對應起來

7,fastDFS常用命令

上傳文件:

/usr/bin/fdfs_upload_file  <config_file> <local_filename>

下載文件:

/usr/bin/fdfs_download_file <config_file> <file_id> [local_filename]

刪除文件:

/usr/bin/fdfs_delete_file <config_file> <file_id>

謝謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章