部署FastDFS

1、什麼是 FastDFS?

FastDFS 是一個開源的輕量級分佈式文件系統。它解決了大數據量存儲和負載均衡等問 題。特別適合以中小文件(建議範圍:文件大小爲 4KB-500MB)爲載體的在線服務,如 相冊網站、視頻網站等等。在 UC 基於 FastDFS 開發向用戶提供了:網盤,社區,廣告 和應用下載等業務的存儲服務。 nginx

FastDFS 是一款開源的輕量級分佈式文件系統純 C 實現,支持 Linux、FreeBSD 等 UNIX 系統類 google FS,不是通用的文件系統,只能經過專有 API 訪問,目前提供了 C、 Java 和 PHP API 爲互聯網應用量身定作,解決大容量文件存儲問題,追求高性能和高 擴展性 FastDFS 能夠看作是基於文件的 key value pair 存儲系統,稱做分佈式文件存 儲服務更爲合適。
FastDFS 服務端有三個角色:跟蹤服務器(tracker)、存儲服務器(storage)和客戶 端(client)。 git

Tracker:跟蹤服務器,主要作調度工做,起負載均衡的做用。在內存中記錄集羣中
全部存儲組和存儲服務器的狀態信息,是客戶端和數據服務器交互的樞紐。不記錄文 件索引信息,佔用的內存量不多。Tracker 是 FastDFS 的協調者,負責管理全部的 storage 和 group,每一個 storage 在啓 動後會鏈接 Tracker,告知本身所屬的 group 等信息,並保持週期性的心跳,tracker 根據 storage 的心跳信息,創建 grou 與 storage 的映射表 。
group :組, 也可稱爲卷。 同組內服務器上的文件是徹底相同的 ,同一組內的 storage 之間是對等的, 文件上傳、 刪除等操做能夠在任意一臺 storage 上進行 。
Storage:存儲服務器(又稱:存儲節點或數據服務器),文件和文件屬性(meta
data)都保存到存儲服務器上。Storage 直接利用 OS 的文件系統調用管理文件。 Storage 以組(卷,group 或 volume)爲單位組織,一個 group 內包含多臺 storage 機 器,數據互爲備份,存儲空間以 group 內容量最小的 storage 爲準,因此建議 group 內的多個 storage 儘可能配置相同,以避免形成存儲空間的浪費。
client:客戶端,做爲業務請求的發起方,經過專有接口,使用 TCP/IP 協議與跟蹤器
服務器或存儲節點進行數據交互。FastDFS 向使用者提供基本文件訪問接口,好比 upload、download、append、delete 等,以客戶端庫的方式提供給用戶使用。github

上傳機制:
部署FastDFSvim

  1. 客戶端想上傳文件首相向 Tracker 發送請求獲取 storage 的 IP 地址和端口
  2. Tracker 收到請求後查看 storage 剩餘多少空間
  3. Storage 會將 IP 地址和端口返回給 Tracker
  4. Tracker 再將 IP 地址和端口返回給客戶端
  5. 客戶端獲得 IP 地址和端口後即可以直接向 storage 直接上傳文件
  6. storage 在保存圖片的同時,會向 Tracker 進行彙報,告訴 Tracker 它當前是否還 留有剩餘空間,以及剩餘空間大小。
  7. 上傳成功後,storage 會將上傳成功生成的文件 ID 和保存位置一同返回給客戶端,客 戶端可經過此訊息的位置查看到文件

下載機制:
部署FastDFS瀏覽器

  1. 客戶端經過上傳獲得的文件 ID 和保存位置向 Tracker 詢問下載文件的 storage
  2. Tracker 查看 storage 獲得空閒的 storage 信息
  3. Tracker 返回一臺可用的 storage
  4. Client 直接和 storage 通信完成文件下載

2、部署FastDFS

FastDFS 是一名叫餘慶的國內工程師用 c 語言編寫的一款開源的分佈式文件系統 咱們能夠經過在 github 上經過他的地址 進行下載 須要的安裝包。本次實驗中咱們須要的安裝包有 libfastcommon ,FastDFS ,fastdfsnginx-module
環境以下:
部署FastDFS
因爲我這個是test環境,因此什麼都沒有,剛開始須要安裝git來獲取命令
一、安裝git(若是有可忽略):服務器

[root@tracker1 ~]# yum -y install git
[root@tracker1 ~]# git config --global user.name test
[root@tracker1 ~]# git config --global user.email test@test.com

二、安裝依賴 libfastcommon(不論是tracker仍是storage或client都須要安裝這個依賴)app

#在github主頁上覆制下載地址
[root@tracker1 ~]# git clone  https://github.com/happyfish100/libfastcommon.git
[root@tracker1 ~]# cd libfastcommon/
#因爲做者已經寫好了安裝腳本,因此不須要像往常同樣去進行安裝,直接運行腳本便可
[root@tracker1 libfastcommon]# ./make.sh  && ./make.sh install

三、安裝DastDFS(不論是tracker仍是storage或client都須要安裝)負載均衡

#安裝方法基本上是大同小異
[root@tracker1 ~]# git clone https://github.com/happyfish100/fastdfs.git
[root@tracker1 ~]# cd fastdfs/
[root@tracker1 fastdfs]# ./make.sh && ./make.sh install

四、配置tracker1tcp

[root@tracker1 fastdfs]# cd /etc/fdfs/
[root@tracker1 fdfs]# mkdir -p /data/tracker-fdfs
[root@tracker1 fdfs]# cp tracker.conf.sample tracker.conf
[root@tracker1 fdfs]# sed -i "s/bind_addr =/bind_addr =192.168.171.134/g" tracker.conf
[root@tracker1 fdfs]# sed -i "s/base_path = \/home\/yuqing\/fastdfs/base_path = \/data\/tracker-fdfs/g" tracker.conf
[root@tracker1 fdfs]# /etc/init.d/fdfs_trackerd start 
Reloading systemd:                                         [  OK  ]
Starting fdfs_trackerd (via systemctl):                    [  OK  ]
[root@tracker1 fdfs]# netstat -anput | grep 22122
tcp        0      0 192.168.171.134:22122   0.0.0.0:*               LISTEN      41213/fdfs_trackerd
#tracker2配置相同,只需把監聽IP更改便可

五、配置storage1分佈式

[root@storage1 ~]# cd /etc/fdfs/
[root@storage1 fdfs]# cp storage.conf.sample storage.conf
[root@storage1 fdfs]# sed -i "s/bind_addr =/bind_addr =192.168.171.140/g" storage.conf
[root@storage1 fdfs]# sed -i "s/base_path = \/home\/yuqing\/fastdfs/base_path = \/data\/storage-fdfs\/base/g" storage.conf
[root@storage1 fdfs]# sed -i "s/store_path0 = \/home\/yuqing\/fastdfs/store_path0 = \/data\/storage-fdfs\/store/g" storage.conf
[root@storage1 fdfs]# mkdir -p /data/storage-fdfs/base
[root@storage1 fdfs]# mkdir -p /data/storage-fdfs/store
[root@storage1 fdfs]# sed -i "s/tracker_server = 192.168.209.121:22122/tracker_server = 192.168.171.134:22122/g" storage.conf
[root@storage1 fdfs]# sed -i "s/tracker_server = 192.168.209.122:22122/tracker_server = 192.168.171.135:22122/g" storage.conf
[root@storage1 ~]# cd fastdfs/conf/
[root@storage1 conf]# cp mime.types http.conf /etc/fdfs/
#安裝nginx模塊,只需在storage上面部署便可
[root@storage1 ~]# git clone https://github.com/happyfish100/fastdfs-nginx-module.git
[root@storage1 ~]# cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@storage1 ~]# vim /etc/fdfs/mod_fastdfs.conf
base_path=/data/storage-fdfs/store
tracker_server=192.168.171.134:22122
tracker_server=192.168.171.135:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path0=/data/storage-fdfs/store
[root@storage1 ~]# yum -y install openssl-devel pcre-devel 
[root@storage1 ~]# tar zxf nginx-1.14.0.tar.gz 
[root@storage1 ~]# cd nginx-1.14.0/
[root@storage1 nginx-1.14.0]# ./configure --add-module=/root/fastdfs-nginx-module/src && make && make install 
[root@storage1 nginx-1.14.0]# cd /usr/local/nginx/conf/
[root@storage1 conf]# vim nginx.conf
#找到server模塊,在下方寫入location
server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location /group1/M00 {
                root /data/storage-fdfs/store;
                ngx_fastdfs_module;
}
[root@storage1 ~]# /usr/local/nginx/sbin/nginx          # 啓動服務
[root@storage1 fdfs]# /etc/init.d/fdfs_storaged start 
Reloading systemd:                                         [  肯定  ]
Starting fdfs_storaged (via systemctl):                    [  肯定  ]
[root@storage1 fdfs]# netstat -anput | grep 23000
tcp        0      0 192.168.171.140:23000   0.0.0.0:*               LISTEN      54817/fdfs_storaged 
[root@storage1 ~]# netstat -anput | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      59420/nginx: master 
tcp        0      0 192.168.171.140:23000   192.168.171.143:46280   ESTABLISHED 60985/fdfs_storaged

六、storage2配置

#以前的配置都相同
[root@storage2 ~]# scp  root@192.168.171.140:/etc/fdfs/mod_fastdfs.conf  /etc/fdfs/
[root@storage2 ~]# scp root@192.168.171.140:/etc/fdfs/storage.conf /etc/fdfs/
[root@storage2 ~]# vim /etc/fdfs/storage.conf
bind_addr =192.168.171.143
#安裝nginx
[root@storage2 ~]# scp root@192.168.171.140:/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/                    # 將主配文件複製過來
[root@storage2 ~]# /etc/init.d/fdfs_storaged start 
[root@storage2 ~]# /usr/local/nginx/sbin/nginx 
[root@storage2 ~]# netstat -anput | grep 23000
[root@storage2 ~]# netstat -anput | grep 80

七、client端配置

[root@client ~]# cd /etc/fdfs/
[root@client fdfs]# cp client.conf.sample client.conf
[root@client fdfs]# mkdir -p /data/client
[root@client fdfs]# sed -i "s/base_path = \/home\/yuqing\/fastdfs/base_path = \/data\/client/g" client.conf
[root@client fdfs]# sed -i "s/tracker_server = 192.168.0.196:22122/tracker_server = 192.168.171.134:22122/g" client.conf
[root@client fdfs]# sed -i "s/tracker_server = 192.168.0.197:22122/tracker_server = 192.168.171.145:22122/g" client.conf
#下載一張圖片用於測試
[root@client ~]# ls | grep test.jpg 
test.jpg
[root@client ~]# fdfs_upload_file /etc/fdfs/client.conf test.jpg           # 進行sha上傳
group1/M00/00/00/wKirjF59zZmATNnrAAFEF6rSzeI733.jpg
#上述返回的信息,訪問這張圖片時會使用
#下方是在storage1上查看的,表示圖片已經上傳到了storage上
[root@storage1 /]# ls /data/storage-fdfs/store/data/00/00/
wKirjF59zZmATNnrAAFEF6rSzeI733.jpg
#那麼怎麼肯定這張圖片就是咱們上傳的那張圖片,畢竟名稱都是不同的,可使用md5sum來進行分析一波
[root@storage1 /]# cd /data/storage-fdfs/store/data/00/00/
[root@storage1 00]# md5sum wKirjF59zZmATNnrAAFEF6rSzeI733.jpg 
369c05bf4fd335ea1a51838d209bf66a  wKirjF59zZmATNnrAAFEF6rSzeI733.jpg
[root@client ~]# md5sum test.jpg 
369c05bf4fd335ea1a51838d209bf66a  test.jpg
#能夠看到返回的信息識別碼是同樣的
[root@client ~]# fdfs_download_file /etc/fdfs/client.conf  group1/M00/00/00/wKirjF59zZmATNnrAAFEF6rSzeI733.jpg b.jpg       # 在終端上進行下載,若是最後不加上b.jpg的話,他下載下來名稱是wkir.........那一串
[root@client ~]# ls | grep b.jpg 
b.jpg

瀏覽器訪問sttstorage上的nginx+上傳圖片時返回的ID
http://192.168.171.140/group1/M00/00/00/wKirjF59zZmATNnrAAFEF6rSzeI733.jpg
測試成功則訪問到如下圖片
部署FastDFS

相關文章
相關標籤/搜索