FastDFS安裝和簡介詳細總結

1、fastDFS簡介

1 FastDFS是用c語言編寫的一款開源的分散式文件系統。
2 FastDFS為互聯網量身訂製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,
3 使用FastDFS很容易搭建一套高性能的文件伺服器集群提供文件上傳、下載等服務。

2、fastDFS架構

1 FastDFS架構包括 Tracker server和Storage server。
2     客戶端請求Tracker server進行文件上傳、下載,通過Tracker server調度最終由Storage server完成文件上傳和下載。
3     Tracker server作用是負載均衡和調度,
4         通過Tracker server在文件上傳時可以根據一些策略找到Storage server提供文件上傳服務。
5         可以將tracker稱為追蹤伺服器或調度伺服器。
6     Storage server作用是文件存儲,
7         客戶端上傳的文件最終存儲在Storage伺服器上,
8         Storage server沒有實現自己的文件系統而是利用作業系統 的文件系統來管理文件。
9         可以將storage稱為存儲伺服器。

如下圖:

3、Tracker 集群

1 FastDFS集群中的Tracker server可以有多台,Tracker server之間是相互平等關係同時提供服務,
2 Tracker server不存在單點故障。
3 客戶端請求Tracker server採用輪詢方式,如果請求的tracker無法提供服務則換另一個tracker。

4、Storage集群

 1 Storage集群採用了分組存儲方式。
 2 storage集群由一個或多個組構成,集群存儲總容量為集群中所有組的存儲容量之和。
 3 一個組由一台或多台存儲伺服器組成,組內的Storage server之間是平等關係,
 4 不同組的Storage server之間不會相互通訊,同組內的Storage server之間會相互連接進行文件同步,
 5 從而保證同組內每個storage上的文件完全一致的。
 6 一個組的存儲容量為該組記憶體儲伺服器容量最小的那個,由此可見組記憶體儲伺服器的軟硬體配置最好是一致的。
 7 採用分組存儲方式的好處是靈活、可控性較強。
 8 比如上傳文件時,可以由客戶端直接指定上傳到的組也可以由tracker進行調度選擇。
 9 一個分組的存儲伺服器訪問壓力較大時,可以在該組增加存儲伺服器來擴充服務能力(縱向擴容)。
10 當系統容量不足時,可以增加組來擴充存儲容量(橫向擴容)

5、Storage狀態收集

1 Storage server會連接集群中所有的Tracker server,定時向他們報告自己的狀態,
2 包括磁碟剩餘空間、文件同步狀況、文件上傳下載次數等統計資訊。

6、文件上傳流程

1 客戶端上傳文件後存儲伺服器將文件ID返回給客戶端,
2 此文件ID用於以後訪問該文件的索引資訊。
3 文件索引資訊包括:組名,虛擬磁碟路徑,數據兩級目錄,文件名。

 1  組名:
 2     文件上傳後所在的storage組名稱,在文件上傳成功後有storage伺服器返回,需要客戶端自行保存。 
 3 虛擬磁碟路徑:
 4     storage配置的虛擬路徑,與磁碟選項store_path*對應。
 5     如果配置了store_path0則是M00,如果配置了store_path1則是M01,以此類推。
 6 數據兩級目錄:
 7     storage伺服器在每個虛擬磁碟路徑下創建的兩級目錄,用於存儲數據文件。
 8 文件名:
 9     與文件上傳時不同。
10     是由存儲伺服器根據特定資訊生成,
11     文件名包含:源存儲伺服器IP地址、文件創建時間戳、文件大小、隨機數和文件拓展名等資訊。

7、項目架構圖

8、安裝FastDFS(需要一天的時間)

1 安裝fastDFS之前需要依賴環境:
2 FastDFS使用c語言開發,需要使用gcc編譯,需要安裝gcc。
3 需要安裝fastdfs依賴類庫:libevent

8.1、安裝fastDFS依賴環境

8.1.1、安裝gcc

命令:命令:yum -y install gcc

8.1.2、安裝libevent

命令:yum –y install libevent libevent-devel

8.1.3、安裝libfastcommon依賴環境
 1 第一步:導入libfastcommon到usr/local/Hadoop/libfastcommonV1.0.7.tar.gz
 2     使用rz命令導入
 3 第二步:解壓
 4     tar -zxvf libfastcommonV1.0.7.tar.gz
 5 第三步:編譯
 6     命令:./make.sh
 7 第四步:安裝
 8     命令:./make.sh install
 9 第五步:拷貝usr/lib64目錄下:libfastcommon.so文件到usr/lib下
10     拷貝命令:cp usr/lib64/ libfastcommon.so /usr/lib
11     注意:
12         libfastcommon安裝好後會自動將庫文件拷貝至/usr/lib64下,
13         由於FastDFS程式引用usr/lib目錄所以需要將/usr/lib64下的庫文件拷貝至/usr/lib下。

要拷貝的文件如下:

 

 

 ****************************************************手動安裝fastDFS*************************START******************************

8.2、安裝fastDFS

 1 第一步:導入fastDFS安裝包
 2     使用rz命令導入
 3 
 4 第二步:解壓
 5     tar -zxvf FastDFS_v5.05.tar.gz
 6 
 7 第三步:編譯安裝
 8     編譯命令:./make.sh
 9     安裝命令:./make.sh install
10 
11 第四步:拷貝FastDFS/conf下面所有配置文件到etc/fdfs/
12     拷貝命令:[root@localhost conf]# cp * /etc/fdfs/
13 
14     [root@localhost fdfs]# ll
15     total 104
16     -rw-r--r--. 1 root root 23981 May 13 18:34 anti-steal.jpg
17     -rw-r--r--. 1 root root  1461 May 13 18:34 client.conf
18     -rw-r--r--. 1 root root   858 May 13 18:34 http.conf
19     -rw-r--r--. 1 root root 31172 May 13 18:34 mime.types
20     -rw-r--r--. 1 root root  7829 May 13 18:34 storage.conf
21     -rw-r--r--. 1 root root  7829 May 13 18:31 storage.conf.sample
22     -rw-r--r--. 1 root root   105 May 13 18:34 storage_ids.conf
23     -rw-r--r--. 1 root root  7102 May 13 18:34 tracker.conf
24     -rw-r--r--. 1 root root  7102 May 13 18:31 tracker.conf.sample

8.3、安裝tracker

 1 安裝tracker,由於tracker運行程式就是fasfdfs,fastDFS安裝成功,只需要修改etc/fdfs/tracker.conf配置文件即可。
 2 
 3 第一步:修改base_path存儲基本路徑
 4     base_path=/home/fastdfs //fastdfs需要手動創建,fastdfs在home目錄下
 5 
 6 第二步:修改存在組
 7     store_group=group1
 8     
 9 第三步:在home目錄下創建fastdfs
10     創建命令:mkdir fastdfs
11     
12 第三步:測試啟動tracker
13     測試是否安裝成功
14     運行命令:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
15     
16     drwx------. 4 mine   mine   4096 May  5 02:22 mine
17     [root@localhost home]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
18     [root@localhost home]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
19     waiting for pid [9077] exit ...
20     starting ...

8.4、安裝storage

 1 由於storage和tracker運行的都是fastDFS程式,
 2 每一台伺服器都部署一台fasfDFS,
 3 一台伺服器是tracker,一台是storage。
 4 此機器:tracker和fastDFS都是使用同一個程式。只需要配置storage配置文件即可。
 5 
 6 配置storage.conf
 7     第一步:配置組名
 8         group_name=group1 //必須和tracker的組名相同。
 9         
10     第二步:配置基本存儲路徑base_path
11         base_path=/home/fastdfs
12     
13     第三步:配置文件存儲路徑
14         store_path0=/home/fastdfs/storage_path //storage_path需要手動創建
15         #store_path1=/home/yuqing/fastdfs2
16         如果有多個存儲路徑,就可以依次往下順延。
17         
18     第四步:創建文件存儲路徑storage_path
19         /home/fastdfs此目錄下面創建
20         命令:mkdir storage_path
21         
22         [root@localhost fastdfs]# mkdir storage_path
23         [root@localhost fastdfs]# ll
24         total 12
25         drwxr-xr-x. 2 root root 4096 May 13 18:45 data
26         drwxr-xr-x. 2 root root 4096 May 13 18:43 logs
27         drwxr-xr-x. 2 root root 4096 May 13 18:54 storage_path
28     
29     第五步:修改連接tracker伺服器地址
30         tracker_server=192.168.66.66:22122
31         
32     第六步:啟動storage測試
33         啟動命令:
34         [root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
35         [root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
36         waiting for pid [9132] exit ...
37         starting ...

8.5、測試圖片上傳

 1 Tracker和storage都已經安裝完成,使用命令測試文件上傳:
 2 FastDFS提供一個文件上傳命令:usr/bin/fdfs_test 測試文件上傳
 3 測試上傳需要連接tracker伺服器,連接storage伺服器。
 4 因此需要指定一個配置文件:client.conf配置文件,通過Client.conf連接tracker伺服器。
 5 
 6 第一步:修改Client.conf
 7     # the base path to store log files
 8     base_path=/home/fastdfs
 9 
10     # tracker_server can ocur more than once, and tracker_server format is
11     #  "host:port", host can be hostname or ip address
12     tracker_server=192.168.66.66:22122
13 
14 第二步:測試命令:
15     在 /etc/fdfs 目錄下執行下面的測試命令:
16     /usr/bin/fdfs_test client.conf upload anti-steal.jpg
17     
18     解析:
19         使用測試程式fdfs_test 的命令 upload命令上傳anti-steal.jpg圖片。
20     * example file url: http://192.168.66.66/group1/M00/00/00/wKhCQlc1uJOAYKOhAABdrZgsqUU338_big.jpg
21     * storage_upload_slave_by_filename
22     * group_name=group1, remote_filename=M00/00/00/wKhCQlc1uJOAYKOhAABdrZgsqUU338_big.jpg
23     * source ip address: 192.168.66.66
24     * file timestamp=2016-05-13 19:20:51
25     * file size=23981
26     * file crc32=2553063749
27     * example file url: http://192.168.66.66/group1/M00/00/00/wKhCQlc1uJOAYKOhAABdrZgsqUU338_big.jpg
28 
29 思考:
30     圖片已經上傳成功,返回圖片地址?瀏覽器是否能訪問?
31     瀏覽器訪問任何資源都必須遵循http協議。
32     Nginx作為http伺服器,通過nginx訪問分散式圖片伺服器。
33     
34 也就是說:nginx訪問fastDFS。需要nginx和FASTDFS整合。

9、整合nginx和FASTDFS

FastDFS-nginx-module整合nginx和fastDFS

9.1、安裝FastDFS-nginx-module

1 第一步:導入FastDFS-nginx-module到usr/local/Hadoop
2     fastdfs-nginx-module_v1.16.tar.gz
3 第二步:解壓fastdfs-nginx-module_v1.16.tar.gz
4     命令:tar –zxvf fastdfs-nginx-module_v1.16.tar.gz
5 第三步:修改/home/canglang/fasfDFS-common/fastdfs-nginx-module/src/Config
6     修改路徑

 1  注意:
 2     去掉錯誤路徑,usr/local/include改為usr/include
 3         
 4 第四步:拷貝fastdfs-nginx-module/src/mod_fastdfs.conf到etc/fdfs
 5     在/home/canglang/fasfDFS-common/fastdfs-nginx-module/src目錄下執行
 6     cp mod_fastdfs.conf /etc/fdfs/
 7     
 8 第五步:修改mod_fastdfs.conf
 9     * base_path=/home/fastdfs/storage_path //此處必須是存儲路徑
10     * tracker_server=192.168.66.66:22122
11     * group_name=group1 //和前面配置組名保持一致
12     * store_path0=/home/fastdfs/storage_path //M00
13     * url_have_group_name = true //允許使用組名
14     
15 第六步:拷貝usr/lib64目錄下庫文件libfdfsclient.so
16     拷貝usr/lib64/libfdfsclient.so 庫文件 到 usr/lib目錄下。
17     拷貝命令:cp /usr/lib64/libfdfsclient.so /usr/lib

9.2、整合

 1 需要把module添加nginx中。通過設置安裝參數方式添加模組。
 2 首先在var目錄下創建temp目錄,在temp目錄下創建nginx目錄
 3 參數設置:在nginx的解壓目錄下執行
 4     ./configure \
 5     --prefix=/home/canglang/nginx/nginx-install \
 6     --pid-path=/var/run/nginx/nginx.pid \
 7     --lock-path=/var/lock/nginx.lock \
 8     --error-log-path=/var/log/nginx/error.log \
 9     --http-log-path=/var/log/nginx/access.log \
10     --with-http_gzip_static_module \
11     --http-client-body-temp-path=/var/temp/nginx/client \
12     --http-proxy-temp-path=/var/temp/nginx/proxy \
13     --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
14     --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
15     --http-scgi-temp-path=/var/temp/nginx/scgi \
16     --add-module=/home/canglang/fastdfs-nginx-module/src
17 
18 需要從新編譯安裝:
19     在nginx的解壓目錄下執行
20     Make 
21     Make install
22     
23 修改nginx配置文件nginx.conf配置文件:
24     http {
25         include       mime.types;
26         default_type  application/octet-stream;  
27         sendfile        on;
28         keepalive_timeout  65;
29         server {
30             listen       80;
31             server_name  192.168.66.66;
32             location /group1/M00/ {
33                 #root   html;
34                 #index  index.html index.htm;
35                 ngx_fastdfs_module;
36             }
37             error_page   500 502 503 504  /50x.html;
38             location = /50x.html {
39                 root   html;
40             }
41         }
42     }

9.3、重啟服務

1 fastDFS:
2 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
3 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
4 nginx:
5 cd /home/canglang/nginx/nginx-install/sbin/
6 ./nginx start
7 關閉防火牆 

****************************************************手動安裝fastDFS*************************END******************************

 

10、圖片伺服器(使用安裝版本)

 1 直接在虛擬機中導入即可
 2 選擇:I moved it –不需要進行設置,使用原來mac地址。
 3 選擇:I copy it --? 生成新MAC地址。生成新的網卡eth1
 4     需要eth1網卡修改為eth0網卡:
 5     修改:/etc/udev/rules.d/70-persistent-net.rules
 6     打開:上面配置文件,發現有2個網卡;
 7     #SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:b5:e3:64", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
 8     SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:b5:e3:66", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
 9 修改網卡配置:
10     vim /etc/sysconfig/network-scripts/ifcfg-eth0
11     DEVICE=eth0
12     TYPE=Ethernet
13     *UUID=bb6c530a-6983-4a14-9883-7f077f2172bc //刪除
14     ONBOOT=no
15     NM_CONTROLLED=yes
16     BOOTPROTO=none
17     *HWADDR=00:0c:29:b5:e3:66
18     IPADDR=192.168.66.66
19     PREFIX=24
20     GATEWAY=192.168.66.2
21     DEFROUTE=yes
22     IPV4_FAILURE_FATAL=yes
23     IPV6INIT=no
24     NAME="System eth0"
25     DNS1=219.141.140.10

11、設置

11.1、設置桌面啟動

1 第一次導入是以黑窗口的方式啟動的
2     用戶名:root
3     密碼:itcast
4 cd /etc
5 vim inittab
6 將最下面的一行中的3改成5,保存
7 reboot重啟

11.2、設置網路

1 右上角 小電腦圖標--右鍵--設置網路即可

11.3、設置連接fastdfs是ip

 1 fastDFS安裝完成後會在/etc/fdfs下生成配置文件
 2 
 3 anti-steal.jpg
 4 client.conf ---客戶端的配置文件
 5 client.conf.sample
 6 http.conf
 7 mime.types
 8 mod_fastdfs.conf ---nginx訪問fastDFS的配置文件
 9 storage.conf ---存儲的配置文件
10 storage.conf.sample
11 storage_ids.conf
12 tracker.conf ---調度者的配置文件
13 tracker.conf.sample

11.4、修改ip

1 storage.conf
2 mod_fastdfs.conf
3 client.conf
4 這三個文件中都需要修改ip
5 tracker_server=192.168.254.67:22122

11.5、查找圖片存儲路徑

1 storage.conf配置文件中store_path0=/home/fastdfs/store_path0
2 這是路徑:/home/fastdfs/store_path0/data/00/00

11.6、重啟

1 fastdfs安裝完成後會在/usr/bin目錄下生成一個啟動命令
2 啟動:
3     /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
4     執行/usr/bin/目錄下的fdfs_trackerd啟動命令,載入/etc/fdfs/下面的tracker.conf配置文件
5     
6     /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
7     執行/usr/bin/目錄下的fdfs_storaged情動命令,載入/etc/fdfs/下面的storage.conf配置文件

12、測試圖片上傳

12.1、上傳

1 cd /etc/fdfs/
2 /usr/bin/fdfs_test client.conf upload anti-steal.jpg
3 執行/usr/bin/目錄下的fdfs_test測試命令,載入client.conf配置文件,upload  圖片名稱

12.2、訪問

12.3、查看圖片上傳到路徑

1 cd /home/fastdfs/store_path0/data/00/00

13、設置開機自動啟動

 

更換電腦後切換fastDFS中配置的ip

 1 圖片測試上傳和配置文件的路徑:/etc/fdfs
 2     storage.conf     tracker_server=192.168.66.66:22122
 3     client.conf     tracker_server=192.168.66.66:22122
 4 
 5 重啟服務:
 6     /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
 7     /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
 8 
 9 測試圖片上傳:
10     在 /etc/fdfs 目錄下執行下面的測試命令:    /usr/bin/fdfs_test client.conf upload anti-steal.jpg
11     會返回一個全路徑: http://192.168.174.129/group1/M00/00/00/wKiugVtAcVCAIZxsAABdrZgsqUU567_big.jpg
12 
13 
14 整合nginx後 
15     修改/etc/fdfs   mod_fastdfs.conf    tracker_server=192.168.66.66:22122
16     修改nginx.con配置文件中的服務ip
17 
18 啟動nginx:
19     /home/canglang/nginx/nginx-install/sbin
20     ./nginx
21 
22 訪問:http://192.168.174.129/group1/M00/00/00/wKiugVtAcVCAIZxsAABdrZgsqUU567_big.jpg
1 nginx啟動時報這個錯:
2     [canglang@localhost sbin]$ sudo ./nginx
3     ngx_http_fastdfs_set pid=2807
4     [canglang@localhost sbin]$ nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
5 解決方案:
6     修改/conf/nginx.conf配置文件
7     將#pid logs/nginx.conf的注釋放開
8     修改成自己定的路徑就行

 

Tags: