一、分散式檔案系統介紹
分散式檔案系統:Distributed file system, DFS,又叫做網路檔案系統:Network File System。一種允許檔案通過網路在多臺主機上分享的檔案系統,可讓多機器上的多使用者分享檔案和儲存空間。
特點:在一個分享的磁碟檔案系統中,所有節點對資料儲存區塊都有相同的訪問權,在這樣的系統中,訪問許可權就必須由客戶端程式來控制。分散式檔案系統可能包含的功能有:透通的資料複製與容錯。
分散式檔案系統是被設計用在區域網。而分散式資料儲存,則是泛指應用分散式運算技術的檔案和資料庫等提供資料儲存服務的系統。
決定因素:資料的儲存方式、資料的讀取速率、資料的安全機制。
發展歷史:大致分為三個發展階段,網路檔案系統(1980s)、共享SAN檔案系統(1990s)、物件導向的並行檔案系統(2000s)。
二、FastDFS分散式系統架構介紹
FastDFS:是一個開源的輕量級分散式檔案系統,功能包括:檔案儲存、檔案同步、檔案訪問(檔案上傳、檔案下載)等,解決了大容量儲存和負載均衡的問題。特別適合中小檔案(建議範圍:4KB < file_size <500MB),對以檔案為載體的線上服務,如相簿網站、視訊網站等。
FastDFS是為網際網路應用量身定做的分散式檔案系統,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高效能等指標。和現有的類Google FS分散式檔案系統相比,FastDFS的架構和設計理念有其獨到之處,主要體現在輕量級、分組方式和對等結構三個方面。
FastDFS架構圖
FastDFS服務端有兩個角色:跟蹤器(tracker)和儲存節點(storage)。跟蹤器主要做排程工作,在訪問上起負載均衡的作用。
FastDFS模組介紹
1)tracker server:跟蹤伺服器,用來排程來自客戶端的請求,且在記憶體中記錄所有儲存組和儲存伺服器的資訊狀態。
2)storage server:儲存伺服器,用來儲存檔案(data)和檔案屬性(metadata)。
3)client:客戶端,業務請求發起方,通過專用介面基於TCP協議與tracker server和storage server進行互動。
4)group:組,也可稱為卷,同組內上的檔案是完全相同的。
5)檔案標識:包括兩部分,組名(group)和檔名(含路徑)
6)檔案相關屬性:鍵值對(Key Value Pair)方式
7)檔名:與原檔名並不相同。由storage server根據特定資訊生成,並且可逆,檔名包含:源儲存伺服器的IP地址、檔案建立時間戳、檔案大小、隨機數和副檔名等。
FastDFS由跟蹤伺服器(Tracker Server)、儲存伺服器(Storage Server)和客戶端(Client)構成,其中:
跟蹤伺服器Tracker Server
主要做排程工作,起到均衡的作用;負責管理所有的 storage server和 group,每個 storage 在啟動後會連線 Tracker,告知自己所屬 group 等資訊,並保持週期性心跳。tracker根據storage的心跳資訊,建立group==>[storage serverlist]的對映表。
Tracker需要管理的元資訊很少,會全部儲存在記憶體中;另外tracker上的元資訊都是由storage彙報的資訊生成的,本身不需要持久化任何資料,這樣使得tracker非常容易擴充套件,直接增加tracker機器即可擴充套件為tracker cluster來服務,cluster裡每個tracker之間是完全對等的,所有的tracker都接受stroage的心跳資訊,生成後設資料資訊來提供讀寫服務。
儲存伺服器Storage Server
主要提供容量和備份服務;以 group 為單位,每個 group 內可以有多臺 storage server,資料互為備份。以group為單位組織儲存能方便的進行應用隔離、負載均衡、副本數定製(group內storage server數量即為該group的副本數),比如將不同應用資料存到不同的group就能隔離應用資料,同時還可根據應用的訪問特性來將應用分配到不同的group來做負載均衡;缺點是group的容量受單機儲存容量的限制,同時當group內有機器壞掉時,資料恢復只能依賴group內地其他機器,使得恢復時間會很長。
group內每個storage的儲存依賴於本地檔案系統,storage可配置多個資料儲存目錄,比如有10塊磁碟,分別掛載在/data/disk1-/data/disk10,則可將這10個目錄都配置為storage的資料儲存目錄。storage接受到寫檔案請求時,會根據配置好的規則選擇其中一個儲存目錄來儲存檔案。為了避免單個目錄下的檔案數太多,在storage第一次啟動時,會在每個資料儲存目錄裡建立2級子目錄,每級256個,總共65536個檔案,新寫的檔案會以hash的方式被路由到其中某個子目錄下,然後將檔案資料作為本地檔案儲存到該目錄中。
客戶端Client
主要是上傳下載資料的伺服器,也就是我們自己的專案所部署在的伺服器。每個客戶端伺服器都需要安裝Nginx
基本架構如下圖所示。
FastDFS的儲存策略
為了支援大容量,儲存節點(伺服器)採用了分卷(或分組)的組織方式。儲存系統由一個或多個卷組成,卷與卷之間的檔案是相互獨立的,所有卷的檔案容量累加就是整個儲存系統中的檔案容量。一個卷可以由一臺或多臺儲存伺服器組成,一個卷下的儲存伺服器中的檔案都是相同的,卷中的多臺儲存伺服器起到了冗餘備份和負載均衡的作用。
在卷中增加伺服器時,同步已有的檔案由系統自動完成,同步完成後,系統自動將新增伺服器切換到線上提供服務。當儲存空間不足或即將耗盡時,可以動態新增捲。只需要增加一臺或多臺伺服器,並將它們配置為一個新的卷,這樣就擴大了儲存系統的容量。
FastDFS的上傳過程
FastDFS向使用者提供基本檔案訪問介面,比如upload、download、append、delete等,以客戶端庫的方式提供給使用者使用。
Storage Server會定期的向Tracker Server傳送自己的儲存資訊。當Tracker Server Cluster中的Tracker Server不止一個時,各個Tracker之間的關係是對等的,所以客戶端上傳時可以選擇任意一個Tracker。
當Tracker收到客戶端上傳檔案的請求時,會為該檔案分配一個可以儲存檔案的group,當選定了group後就要決定給客戶端分配group中的哪一個storage server。當分配好storage server後,客戶端向storage傳送寫檔案請求,storage將會為檔案分配一個資料儲存目錄。然後為檔案分配一個fileid,最後根據以上的資訊生成檔名儲存檔案。檔名的格式如下:
FastDFS的檔案同步
寫檔案時,客戶端將檔案寫至group內一個storage server即認為寫檔案成功,storage server寫完檔案後,會由後臺執行緒將檔案同步至同group內其他的storage server。
每個storage寫檔案後,同時會寫一份binlog,binlog裡不包含檔案資料,只包含檔名等元資訊,這份binlog用於後臺同步,storage會記錄向group內其他storage同步的進度,以便重啟後能接上次的進度繼續同步;進度以時間戳的方式進行記錄,所以最好能保證叢集內所有server的時鐘保持同步。
storage的同步進度會作為後設資料的一部分彙報到tracker上,tracke在選擇讀storage的時候會以同步進度作為參考。
客戶端上傳檔案後儲存與伺服器將檔案ID返回給客戶端,此檔案ID用於以後訪問該檔案的索引資訊。檔案索引資訊包括:組名,虛擬磁碟路徑,資料兩級目錄,檔名。
路徑格式:
FastDFS的檔案下載
客戶端uploadfile成功後,會拿到一個storage生成的檔名,接下來客戶端根據這個檔名即可訪問到該檔案。
跟upload file一樣,在downloadfile時客戶端可以選擇任意tracker server。tracker傳送download請求給某個tracker,必須帶上檔名資訊,tracke從檔名中解析出檔案的group、大小、建立時間等資訊,然後為該請求選擇一個storage用來服務讀請求。
FastDFS效能方案
三、Centos下部署FastDFS分散式儲存操作記錄
1)伺服器資訊(centos6.9)
描述 ip地址 主機名稱 分組 跟蹤伺服器1 192.168.10.200 Fastdfs_tracker_t1 跟蹤伺服器2 192.168.10.201 Fastdfs_tracker_t2 儲存伺服器1 192.168.10.202 Fastdfs_storage_s1 group1 儲存伺服器2 192.168.10.203 Fastdfs_storage_s2 group2 儲存伺服器3 192.168.10.204 Fastdfs_storage_s3 group3 客戶機1 192.168.10.205 Fastdfs_client
2)FastDFS安裝(所有tracker伺服器、storage伺服器、客戶端機器都要有如下操作。這裡以Fastdfs_tracker_t1伺服器操作為例)
1)編譯和安裝所需的依賴包 [root@Fastdfs_tracker_t1 ~]# yum install make cmake gcc gcc-c++ 2)安裝libfastcommon [root@Fastdfs_tracker_t1 ~]# cd /usr/local/src/ [root@Fastdfs_tracker_t1 src]# wget https://codeload.github.com/happyfish100/libfastcommon/tar.gz/V1.0.7 -O libfastcommon-1.0.7.tar.gz [root@Fastdfs_tracker_t1 src]# tar zxf libfastcommon-1.0.7.tar.gz [root@Fastdfs_tracker_t1 src]# cd libfastcommon-1.0.7 [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ./make.sh [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ./make.sh install libfastcommon 預設安裝到了/usr/lib64/libfastcommon.so和/usr/lib64/libfdfsclient.so 因為FastDFS主程式設定的lib目錄是/usr/local/lib,所以需要建立軟連結 [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so [root@Fastdfs_tracker_t1 libfastcommon-1.0.7]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so 3)安裝FastDFS 先提前安裝依賴 [root@Fastdfs_tracker_t1 ~]# yum install gettext gettext-devel libXft libXft-devel libXpm libXpm-devel autoconf libxtst-devel zlib-devel libpng-devel glib-devel [root@Fastdfs_tracker_t1 ~]# cd /usr/local/src/ [root@Fastdfs_tracker_t1 src]# wget https://github.s3.amazonaws.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz [root@Fastdfs_tracker_t1 src]# tar -zvxf libevent-2.0.20-stable.tar.gz [root@Fastdfs_tracker_t1 src]# cd libevent-2.0.20-stable [root@Fastdfs_tracker_t1 libevent-2.0.20-stable]# ./configure && make && make install 接著編譯安裝FastDFS(編譯前要確保已經成功安裝了 libfastcommon)。百度雲盤下載:https://pan.baidu.com/s/1x_CTcRqZr_BTptXF9kHOng 提取密碼:9ku2 [root@Fastdfs_tracker_t1 src]# pwd /usr/local/src [root@Fastdfs_tracker_t1 src]# ls FastDFS_v5.05.tar.gz FastDFS_v5.05.tar.gz [root@Fastdfs_tracker_t1 src]# tar -zvxf FastDFS_v5.05.tar.gz [root@Fastdfs_tracker_t1 src]# cd FastDFS [root@Fastdfs_tracker_t1 FastDFS]# ./make.sh [root@Fastdfs_tracker_t1 FastDFS]# ./make.sh install 採用預設安裝的方式安裝,安裝後的相應檔案與目錄: 服務指令碼在: /etc/init.d/fdfs_storaged /etc/init.d/fdfs_trackerd 配置檔案在(樣例配置檔案): /etc/fdfs/client.conf.sample /etc/fdfs/storage.conf.sample /etc/fdfs/tracker.conf.sample 命令工具在/usr/bin/目錄下的: fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_crc32 fdfs_delete_file fdfs_download_file fdfs_file_info fdfs_monitor fdfs_storaged fdfs_test fdfs_test1 fdfs_trackerd fdfs_upload_appender fdfs_upload_file stop.sh restart.sh 因為FastDFS服務指令碼設定的bin目錄是/usr/local/bin,但實際命令安裝在/usr/bin,可以進入/user/bin 目錄使用以下命令檢視 fdfs 的相關命令: [root@Fastdfs_tracker_t1 ~]# cd /usr/bin/ [root@Fastdfs_tracker_t1 bin]# ls | grep fdfs 因此需要修改FastDFS服務指令碼中相應的命令路徑,也就是把/etc/init.d/fdfs_storaged和/etc/init.d/fdfs_trackerd 兩個指令碼中的/usr/local/bin修改成/usr/bin: [root@Fastdfs_tracker_t1 bin]# vim /etc/init.d/fdfs_trackerd 使用查詢替換命令進統一修改::%s+/usr/local/bin+/usr/bin [root@Fastdfs_tracker_t1 bin]# vim /etc/init.d/fdfs_storaged 使用查詢替換命令進統一修改::%s+/usr/local/bin+/usr/bin
3)配置FastDFS跟蹤器Tracker(兩臺機器Fastdfs_tracker_t1和Fastdfs_tracker_t2都需要操作)
1)複製 FastDFS 跟蹤器樣例配置檔案,並重新命名 [root@Fastdfs_tracker_t1 ~]# cd /etc/fdfs/ [root@Fastdfs_tracker_t1 fdfs]# cp tracker.conf.sample tracker.conf 2)編輯跟蹤器配置檔案 [root@Fastdfs_tracker_t1 fdfs]# vim /etc/fdfs/tracker.conf // 修改的內容如下(其它引數保留預設配置) ...... disabled=false //啟用配置檔案 port=22122 //tracker 的埠號,一般採用 22122 這個預設埠 base_path=/fastdfs/tracker //tracker 的資料檔案和日誌目錄 # the method of selecting group to upload files # 0: round robin # 1: specify group # 2: load balance, select the max free space group to upload file store_lookup=0 //採取輪巡方式選擇要上傳的組進行儲存,預設2 選擇最大空閒空間的組 3)建立基礎資料目錄(參考基礎目錄 base_path 配置) [root@Fastdfs_tracker_t1 fdfs]# mkdir -p /fastdfs/tracker 4)關閉iptables防火牆 [root@Fastdfs_tracker_t1 fdfs]# /etc/init.d/iptables stop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 溫馨提示: 如果iptables防火牆中開啟的話,則需要開啟跟蹤器埠(預設為 22122) [root@Fastdfs_tracker_t1 fdfs]# vim /etc/sysconfig/iptables ## FastDFS Tracker Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT [root@Fastdfs_tracker_t1 fdfs]# service iptables restart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5)啟動Tracker服務 [root@Fastdfs_tracker_t1 fdfs]# /etc/init.d/fdfs_trackerd start Starting FastDFS tracker server: 初次成功啟動,會在/fastdfs/tracker 目錄下建立 data、logs 兩個目錄;可以檢視tracker是否啟動成功: [root@Fastdfs_tracker_t1 fdfs]# ls /fastdfs/tracker/ data logs [root@Fastdfs_tracker_t1 fdfs]# netstat -unltp|grep fdfs tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 10469/fdfs_trackerd [root@Fastdfs_tracker_t1 fdfs]# lsof -i:22122 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME fdfs_trac 10469 root 5u IPv4 214098 0t0 TCP *:22122 (LISTEN) [root@Fastdfs_tracker_t1 fdfs]# tail -100f /fastdfs/tracker/logs/trackerd.log 關閉Tracker [root@Fastdfs_tracker_t2 fdfs]# /etc/init.d/fdfs_trackerd stop 設定FastDFS跟蹤器開機啟動 [root@Fastdfs_tracker_t1 fdfs]# chkconfig fdfs_trackerd on [root@Fastdfs_tracker_t1 fdfs]# chkconfig --list|grep fdfs_trackerd fdfs_trackerd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Fastdfs_tracker_t1 fdfs]# vim /etc/rc.d/rc.local [root@Fastdfs_tracker_t1 fdfs]# cat /etc/rc.d/rc.local ....... /etc/init.d/fdfs_trackerd start
4)配置FastDFS儲存服務(三臺機器Fastdfs_storage_s1、Fastdfs_storage_s2、Fastdfs_storage_s3都需要操作)
1)複製FastDFS儲存器樣例配置檔案,並重新命名。 [root@Fastdfs_storage_s1 FastDFS]# cd /etc/fdfs/ [root@Fastdfs_storage_s1 fdfs]# cp storage.conf.sample storage.conf [root@Fastdfs_storage_s1 fdfs]# vim /etc/fdfs/storage.conf //修改的內容如下(其它引數保留預設配置): ....... disabled=false //啟用配置檔案 group_name=group1 //組名(第一組為 group1,第二組為 group2) port=23000 //storage 的埠號,同一個組的 storage 埠號必須相同 base_path=/fastdfs/storage //設定 storage 的日誌目錄 store_path0=/fastdfs/storage //儲存路徑 store_path_count=1 //儲存路徑個數,需要和 store_path 個數匹配 tracker_server=192.168.10.200:22122 //tracker 伺服器的 IP 地址和埠 tracker_server=192.168.1.201:22122 //多個 tracker 直接新增多條配置 http.server_port=8888 //設定 http 埠號 2)建立基礎資料目錄(參考基礎目錄 base_path 配置) [root@Fastdfs_storage_s1 fdfs]# mkdir -p /fastdfs/storage 4)關閉iptables防火牆 [root@Fastdfs_storage_s1 fdfs]# /etc/init.d/iptables stop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 溫馨提示: 如果iptables防火牆中開啟的話,則需要開啟23000埠 [root@Fastdfs_tracker_t1 fdfs]# vim /etc/sysconfig/iptables ## FastDFS Tracker Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT [root@Fastdfs_storage_s1 fdfs]# service iptables restart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5)啟動 Storage [root@Fastdfs_storage_s1 fdfs]# /etc/init.d/fdfs_storaged start Starting FastDFS storage server: 初次成功啟動,會在/fastdfs/storage 目錄下建立資料目錄 data 和日誌目錄 logs)各節點啟動動, 使用 tail -f /fastdfs/storage/logs/storaged.log 命令監聽儲存節點日誌,可以看到儲存節點連結到跟蹤器, 並提示哪一個為 leader 跟蹤器。同時也會看到同一組中的其他節點加入進來的日誌資訊。 檢視23000埠監聽情況: [root@Fastdfs_storage_s1 fdfs]# netstat -unltp|grep fdfs tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 28347/fdfs_storaged [root@Fastdfs_storage_s1 fdfs]# lsof -i:23000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME fdfs_stor 28347 root 5u IPv4 211801 0t0 TCP *:inovaport1 (LISTEN) [root@Fastdfs_storage_s1 fdfs]# tail -f /fastdfs/storage/logs/storaged.log mkdir data path: FD ... mkdir data path: FE ... mkdir data path: FF ... data path: /fastdfs/storage/data, mkdir sub dir done. [2018-02-26 14:27:46] INFO - file: storage_param_getter.c, line: 191, use_storage_id=0, id_type_in_filename=ip, storage_ip_changed_auto_adjust=1, store_path=0, reserved_storage_space=10.00%, use_trunk_file=0, slot_min_size=256, slot_max_size=16 MB, trunk_file_size=64 MB, trunk_create_file_advance=0, trunk_create_file_time_base=02:00, trunk_create_file_interval=86400, trunk_create_file_space_threshold=20 GB, trunk_init_check_occupying=0, trunk_init_reload_from_binlog=0, trunk_compress_binlog_min_interval=0, store_slave_file_use_link=0 [2018-02-26 14:27:46] INFO - file: storage_func.c, line: 254, tracker_client_ip: 192.168.10.202, my_server_id_str: 192.168.10.202, g_server_id_in_filename: -905271104 [2018-02-26 14:27:51] INFO - local_host_ip_count: 2, 127.0.0.1 192.168.10.202 [2018-02-26 14:27:51] INFO - file: tracker_client_thread.c, line: 310, successfully connect to tracker server 192.168.10.201:22122, as a tracker client, my ip is 192.168.10.202 [2018-02-26 14:27:51] INFO - file: tracker_client_thread.c, line: 1235, tracker server 192.168.10.201:22122, set tracker leader: 192.168.10.201:22122 [2018-02-26 14:27:52] INFO - file: tracker_client_thread.c, line: 310, successfully connect to tracker server 192.168.10.200:22122, as a tracker client, my ip is 192.168.10.202 ....... 關閉storage [root@Fastdfs_storage_s1 fdfs]# /etc/init.d/fdfs_storaged stop 設定storage開機啟動 [root@Fastdfs_storage_s1 fdfs]# chkconfig fdfs_storaged on [root@Fastdfs_storage_s1 fdfs]# chkconfig --list|grep fdfs_storaged fdfs_storaged 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Fastdfs_storage_s1 fdfs]# vim /etc/rc.d/rc.local ...... /etc/init.d/fdfs_storaged start 所有Storage節點都啟動之後,可以在任一 Storage 節點上使用如下命令檢視叢集資訊: [root@Fastdfs_storage_s1 fdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf [2018-02-26 14:30:22] DEBUG - base_path=/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 server_count=2, server_index=0 tracker server is 192.168.10.200:22122 group count: 3 Group 1: group name = group1 disk total space = 50268 MB disk free space = 45029 MB trunk free space = 0 MB storage server count = 1 active server count = 1 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 192.168.10.202 ip_addr = 192.168.10.202 ACTIVE http domain = version = 5.05 join time = 2018-02-26 14:27:07 up time = 2018-02-26 14:27:07 total storage = 50268 MB free storage = 45029 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 0 connection.max_count = 0 total_upload_count = 0 success_upload_count = 0 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 0 success_upload_bytes = 0 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 0 success_file_open_count = 0 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 0 success_file_write_count = 0 last_heart_beat_time = 2018-02-26 14:29:52 last_source_update = 1970-01-01 08:00:00 last_sync_update = 1970-01-01 08:00:00 last_synced_timestamp = 1970-01-01 08:00:00 Group 2: group name = group2 disk total space = 49086 MB disk free space = 44696 MB trunk free space = 0 MB storage server count = 1 active server count = 1 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 192.168.10.203 ip_addr = 192.168.10.203 ACTIVE http domain = version = 5.05 join time = 2018-02-26 14:27:09 up time = 2018-02-26 14:27:09 total storage = 49086 MB free storage = 44696 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 0 connection.max_count = 0 total_upload_count = 0 success_upload_count = 0 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 0 success_upload_bytes = 0 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 0 success_file_open_count = 0 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 0 success_file_write_count = 0 last_heart_beat_time = 2018-02-26 14:30:15 last_source_update = 1970-01-01 08:00:00 last_sync_update = 1970-01-01 08:00:00 last_synced_timestamp = 1970-01-01 08:00:00 Group 3: group name = group3 disk total space = 49086 MB disk free space = 44962 MB trunk free space = 0 MB storage server count = 1 active server count = 1 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 192.168.10.204 ip_addr = 192.168.10.204 ACTIVE http domain = version = 5.05 join time = 2018-02-26 14:27:10 up time = 2018-02-26 14:27:10 total storage = 49086 MB free storage = 44962 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 0 connection.max_count = 0 total_upload_count = 0 success_upload_count = 0 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 0 success_upload_bytes = 0 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 0 success_file_open_count = 0 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 0 success_file_write_count = 0 last_heart_beat_time = 2018-02-26 14:29:55 last_source_update = 1970-01-01 08:00:00 last_sync_update = 1970-01-01 08:00:00 last_synced_timestamp = 1970-01-01 08:00:00
5)檔案上傳測試(在客戶機192.168.10.205上測試即可,也可以直接在Fastdfs_tracker伺服器如192.168.10.200上測試,只要在安裝fastdfs_client機器上測試就行)
1)修改客戶端配置檔案 [root@Fastdfs_client ~]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf [root@Fastdfs_client ~]# vim /etc/fdfs/client.conf //修改的內容如下(其它引數保留預設配置): ...... base_path=/fastdfs/tracker tracker_server=192.168.10.200:22122 tracker_server=192.168.10.201:22122 2)建立基礎資料目錄(參考基礎目錄 base_path 配置) [root@Fastdfs_client ~]# mkdir -p /fastdfs/tracker 3)上傳(可以通過Xftp5上傳)一張圖片(比如test.png)到客戶機192.168.10.205目錄/usr/local/src/,用作上傳測試 [root@Fastdfs_client src]# pwd /usr/local/src [root@Fastdfs_client src]# ll test.png -rw-r--r--. 1 root root 165712 Feb 26 14:53 test.png 然後使用FastDFS上傳命令,將測試圖片上傳到FastDFS儲存裡。 [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.png group1/M00/00/00/wKgKylqTsFCAdeEuAAKHUIZ3K9s287.png 如上能返回檔案ID號,說明上傳成功。由返回的資訊可知,test.png圖片被上傳到group1下的資料目錄下了。 到group1組內機器(即Fastdfs_storage_s1)的資料目錄下可找到剛才上傳的檔案: [root@Fastdfs_storage_s1 ~]# ll /fastdfs/storage/data/00/00/ total 164 -rw-r--r--. 1 root root 165712 Feb 26 14:59 wKgKylqTsFCAdeEuAAKHUIZ3K9s287.png 同理,再上傳一張圖片進行測試 [root@Fastdfs_client ~]# cd /usr/local/src/ [root@Fastdfs_client src]# ll haha.png -rw-r--r--. 1 root root 12331 Feb 26 16:40 haha.png [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/haha.png group2/M00/00/00/wKgKy1qTyCKAFGzSAAAwK2jUNaY428.png 即haha.png圖片被上傳到group2組內機器的資料目錄下了 [root@Fastdfs_storage_s2 ~]# ll /fastdfs/storage/data/00/00/ total 16 -rw-r--r--. 1 root root 12331 Feb 26 16:41 wKgKy1qTyCKAFGzSAAAwK2jUNaY428.png 再上傳一次 [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/haha.png group3/M00/00/00/wKgKzFqTyRaAeDLJAAAwK2jUNaY992.png [root@Fastdfs_storage_s3 ~]# ll /fastdfs/storage/data/00/00/ total 16 -rw-r--r--. 1 root root 12331 Feb 26 16:45 wKgKzFqTyRaAeDLJAAAwK2jUNaY992.png 這裡,我測試的時候,一個group組內只有一臺儲存伺服器,測試圖片上傳到group內,只上傳到一臺儲存伺服器上了。 正常來說,應該要在同一個group組內新增2臺以上的儲存伺服器,這樣圖片就是被上傳到多臺儲存伺服器上, 那麼當一臺儲存伺服器掛掉後,圖片還能從其他的儲存伺服器上讀出來。
6)在各個儲存節點上安裝Nginx(Fastdfs_storage_s1、Fastdfs_storage_s2、Fastdfs_storage_s3)
1)fastdfs-nginx-module 作用說明 FastDFS 通過Tracker伺服器,將檔案放在Storage伺服器儲存,但是同組儲存伺服器之間需要進入檔案複製,有同步延遲的問題。 假設Tracker伺服器將檔案上傳到了A機器上,上傳成功後檔案ID已經返回給客戶端。此時FastDFS儲存叢集機制會將這個檔案同步到同組儲存B機器上, 在檔案還沒有複製完成的情況下,客戶端如果用這個檔案ID在B機器上取檔案,就會出現檔案無法訪問的錯誤。 而fastdfs-nginx-module可以重定向檔案連線到源伺服器取檔案,避免客戶端由於複製延遲導致的檔案無法訪問錯誤。 (解壓後的fastdfs-nginx-module在nginx安裝時使用) 2)安裝nginx前,需要配置以下依賴 [root@Fastdfs_storage_s1 ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel 3)安裝nginx和fastdfs-nginx-module [root@Fastdfs_storage_s1 ~]# cd /usr/local/src/ [root@Fastdfs_storage_s1 src]# wget http://nginx.org/download/nginx-1.13.9.tar.gz 下載fastdfs-nginx-module地址:https://pan.baidu.com/s/1i6Jqsi5 提取密碼:g1ps [root@Fastdfs_storage_s1 src]# ll fastdfs-nginx-module_v1.16.tar.gz -rw-r--r--. 1 root root 17510 Feb 26 16:58 fastdfs-nginx-module_v1.16.tar.gz [root@Fastdfs_storage_s1 src]# ls nginx-1.13.9.tar.gz nginx-1.13.9.tar.gz [root@Fastdfs_storage_s1 src]# tar -zvxf nginx-1.13.9.tar.gz [root@Fastdfs_storage_s1 src]# tar -zvxf fastdfs-nginx-module_v1.16.tar.gz [root@Fastdfs_storage_s1 src]# vim /usr/local/src/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/" 千萬注意:這個路徑修改是很重要的,不然在 nginx 編譯的時候會報錯的! 緊接著進行nginx的編譯安裝操作 [root@Fastdfs_storage_s1 src]# cd nginx-1.13.9 [root@Fastdfs_storage_s1 nginx-1.13.9]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src [root@Fastdfs_storage_s1 nginx-1.13.9]# make && make install 4)複製 fastdfs-nginx-module 原始碼中的配置檔案到/etc/fdfs 目錄,並修改 [root@Fastdfs_storage_s1 src]# cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ [root@Fastdfs_storage_s1 src]# vim /etc/fdfs/mod_fastdfs.conf ...... connect_timeout=10 base_path=/tmp tracker_server=192.168.10.200:22122 tracker_server=192.168.10.201:22122 storage_server_port=23000 group_name=group1 url_have_group_name = true store_path0=/fastdfs/storage group_count = 3 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/fastdfs/storage [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/fastdfs/storage [group3] group_name=group3 storage_server_port=23000 store_path_count=1 store_path0=/fastdfs/storage 特別注意: 第二組內的Storage伺服器(這裡指Fastdfs_storage_s2)的mod_fastdfs.conf配置與第一組配置只有group_nam不同,即: group_name=group2 第三組內的Storage伺服器(這裡指Fastdfs_storage_s3)的mod_fastdfs.conf 配置與第一組配置只有group_name不同,即: group_name=group3 5)複製 FastDFS 的部分配置檔案到/etc/fdfs 目錄 [root@Fastdfs_storage_s1 src]# cd /usr/local/src/FastDFS/conf [root@Fastdfs_storage_s1 conf]# cp http.conf mime.types /etc/fdfs/ 6)在/fastdfs/storage 檔案儲存目錄下建立軟連線,將其連結到實際存放資料的目錄 [root@Fastdfs_storage_s1 conf]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00 7)配置 Nginx,簡潔版 nginx 配置樣例 [root@Fastdfs_storage_s1 src]# cd /usr/local/nginx/conf/ [root@Fastdfs_storage_s1 conf]# cp nginx.conf nginx.conf.bak [root@Fastdfs_storage_s1 conf]# cat nginx.conf user www; //如果url訪問圖片時出現404,可以將這個修改為"user root" worker_processes 8; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; ###### ## set access log format ###### log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_cookie" $host $request_time'; ####### ## http setting ####### sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m; proxy_temp_path /var/www/cache/tmp; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; # client_header_timeout 600s; client_body_timeout 600s; # client_max_body_size 50m; client_max_body_size 100m; #允許客戶端請求的最大單個檔案位元組數 client_body_buffer_size 256k; #緩衝區代理緩衝請求的最大位元組數,可以理解為先儲存到本地再傳給使用者 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; ## includes vhosts include vhosts/*.conf; } [root@Fastdfs_storage_s1 conf]# mkdir vhosts [root@Fastdfs_storage_s1 conf]# cd vhosts/ [root@Fastdfs_storage_s1 vhosts]# vim fastdfs.conf [root@Fastdfs_storage_s1 vhosts]# cat fastdfs.conf server { listen 8888; server_name localhost; location /group1/M00 { //注意,Fastdfs_storage_s2機器內此處修改為/group2/M00,Fastdfs_storage_s3機器內此處修改為/group3/M00 #alias /fastdfs/storage/data; ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } [root@Fastdfs_storage_s1 vhosts]#useradd www [root@Fastdfs_storage_s1 vhosts]# mkdir -p /var/www/cache [root@Fastdfs_storage_s1 vhosts]# /usr/local/nginx/sbin/nginx -t [root@Fastdfs_storage_s1 vhosts]# /usr/local/nginx/sbin/nginx [root@Fastdfs_storage_s1 vhosts]# /usr/local/nginx/sbin/nginx -s reload [root@Fastdfs_storage_s1 vhosts]# lsof -i:8888 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 31543 root 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31617 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31618 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31619 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31620 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31621 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31622 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31623 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) nginx 31624 www 10u IPv4 218910 0t0 TCP *:ddi-tcp-1 (LISTEN) +++++++++++++++++++++++++++++++++++++++++++特別注意++++++++++++++++++++++++++++++++++++++++++++++++++ A、8888 埠值是要與/etc/fdfs/storage.conf 中的 http.server_port=8888 相對應, 因為 http.server_port 預設為 8888,如果想改成 80,則要對應修改過來。 B、Storage 對應有多個 group 的情況下,訪問路徑帶 group 名,如/group1/M00/00/00/xxx, 對應的 Nginx 配置為: location ~/group([0-9])/M00 { ngx_fastdfs_module; } C、如查下載時如發現老報 404,將 nginx.conf 第一行 user www 修改為 user root 後重新啟動!這個要特別注意!!!!! ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8)設定Nginx開機啟動 [root@Fastdfs_storage_s1 ~]# vim /etc/rc.d/rc.local ........ /usr/local/nginx/sbin/nginx 9)關閉iptables防火牆 [root@Fastdfs_storage_s1 conf]# /etc/init.d/iptables stop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 溫馨提示: 如果開啟了iptables防火牆功能,則需要開通8888埠訪問 [root@Fastdfs_storage_s1 ~]# vim /etc/sysconfig/iptables ## FastDFS Tracker Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT [root@Fastdfs_storage_s1 ~]# service iptables restart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10)通過瀏覽器訪問測試時上傳的檔案(對應上傳到FastDFS儲存裡的路徑) 在客戶端機器上傳圖片 [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/haha.png group1/M00/00/00/wKgKylqT3OCAUrqYAAAwK2jUNaY262.png [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.png group2/M00/00/00/wKgKy1qT3P-Ae-vQAAKHUIZ3K9s459.png [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.png group3/M00/00/00/wKgKzFqT3SWAaunZAAKHUIZ3K9s849.png 然後訪問圖片的url http://192.168.10.202:8888/group1/M00/00/00/wKgKylqT3OCAUrqYAAAwK2jUNaY262.png http://192.168.10.203:8888/group2/M00/00/00/wKgKy1qT3P-Ae-vQAAKHUIZ3K9s459.png http://192.168.10.204:8888/group3/M00/00/00/wKgKzFqT3SWAaunZAAKHUIZ3K9s849.png
7)在各個跟蹤節點伺服器上安裝Nginx(Fastdfs_tracker_t1、Fastdfs_tracker_t2)
在tracker上安裝的nginx主要為了提供 http 訪問的反向代理、負載均衡以及快取服務
1)安裝編譯 Nginx 所需的依賴包 [root@Fastdfs_tracker_t1 ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel 2)上傳 ngx_cache_purge-2.3.tar.gz和nginx-1.13.9.tar.gz到/usr/local/src,解壓 [root@Fastdfs_tracker_t1 ~]# cd /usr/local/src/ [root@Fastdfs_tracker_t1 src]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz [root@Fastdfs_tracker_t1 src]# wget http://nginx.org/download/nginx-1.13.9.tar.gz [root@Fastdfs_tracker_t1 src]# tar -zvxf ngx_cache_purge-2.3.tar.gz [root@Fastdfs_tracker_t1 src]# tar -zvxf nginx-1.13.9.tar.gz [root@Fastdfs_tracker_t1 src]# cd nginx-1.13.9 [root@Fastdfs_tracker_t1 nginx-1.13.9]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_cache_purge-2.3 && make && make install 3)配置 Nginx,設定負載均衡以及快取 [root@Fastdfs_tracker_t1 conf]# cp nginx.conf nginx.conf.conf [root@Fastdfs_tracker_t1 conf]# cat nginx.conf user www; worker_processes 8; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; ###### ## set access log format ###### log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_cookie" $host $request_time'; ####### ## http setting ####### sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m; proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; # client_header_timeout 600s; client_body_timeout 600s; # client_max_body_size 50m; client_max_body_size 100m; client_body_buffer_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; ## includes vhosts include vhosts/*.conf; } [root@Fastdfs_tracker_t1 conf]# mkdir vhosts [root@Fastdfs_tracker_t1 vhosts]# cat lb.conf //如果group組內有多個儲存伺服器,則這裡的負載就起作用了。 upstream fdfs_group1 { server 192.168.10.202:8888 weight=1 max_fails=2 fail_timeout=30s; } upstream fdfs_group2 { server 192.168.10.203:8888 weight=1 max_fails=2 fail_timeout=30s; } upstream fdfs_group3 { server 192.168.10.204:8888 weight=1 max_fails=2 fail_timeout=30s; } server{ listen 8000; server_name localhost; access_log /usr/local/nginx/logs/lb_access.log main; error_log /usr/local/nginx/logs/lb_error.log; location /group1/M00 { proxy_pass http://fdfs_group1; } location /group2/M00 { proxy_pass http://fdfs_group2; } location /group3/M00 { proxy_pass http://fdfs_group3; } #設定清除快取的訪問許可權 location ~/purge(/.*) { allow 127.0.0.1; allow 114.242.193.201; allow 192.168.10.0/24; deny all; proxy_cache_purge mycache $host$1$is_args$args ; } } 按以上 nginx 配置檔案的要求,建立對應的快取目錄 [root@Fastdfs_tracker_t1 conf]# useradd www [root@Fastdfs_tracker_t1 conf]# ulimit 65535 [root@Fastdfs_tracker_t1 conf]# mkdir -p /fastdfs/cache/nginx/proxy_cache [root@Fastdfs_tracker_t1 conf]# mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp [root@Fastdfs_tracker_t1 conf]# /usr/local/nginx/sbin/nginx -t [root@Fastdfs_tracker_t1 conf]# /usr/local/nginx/sbin/nginx [root@Fastdfs_tracker_t1 conf]# /usr/local/nginx/sbin/nginx -s reload 4)關閉iptables防火牆 [root@Fastdfs_tracker_t1 conf]# /etc/init.d/iptables stop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 溫馨提示: 如果iptables防火牆中開啟的話,則需要開啟8000埠 [root@Fastdfs_tracker_t1 fdfs]# vim /etc/sysconfig/iptables ## FastDFS Tracker Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT [root@Fastdfs_tracker_t1 conf]# service iptables restart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5)設定Nginx開機啟動 [root@Fastdfs_storage_s1 ~]# vim /etc/rc.d/rc.local ........ /usr/local/nginx/sbin/nginx 6)檔案訪問測試 在客戶端機器上傳圖片(或使用上面測試中已經上傳過的圖片路徑進行訪問測試) [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.jpg group1/M00/00/00/wKgKylqUxBOAFo8hAAKHUIZ3K9s443.jpg [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/haha.png group2/M00/00/00/wKgKy1qUxBeAT0PxAAAwK2jUNaY575.png [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.jpg group3/M00/00/00/wKgKzFqUxBmATFurAAKHUIZ3K9s282.jpg 然後訪問圖片的url http://192.168.10.200:8000/group1/M00/00/00/wKgKylqUxBOAFo8hAAKHUIZ3K9s443.jpg http://192.168.10.201:8000/group1/M00/00/00/wKgKylqUxBOAFo8hAAKHUIZ3K9s443.jpg http://192.168.10.200:8000/group2/M00/00/00/wKgKy1qUxBeAT0PxAAAwK2jUNaY575.png http://192.168.10.201:8000/group2/M00/00/00/wKgKy1qUxBeAT0PxAAAwK2jUNaY575.png http://192.168.10.200:8000/group3/M00/00/00/wKgKzFqUxBmATFurAAKHUIZ3K9s282.jpg http://192.168.10.201:8000/group3/M00/00/00/wKgKzFqUxBmATFurAAKHUIZ3K9s282.jpg 以上是簡單配置,多次測試可以發現,每一個Tracker中的 Nginx都單獨對後端的Storage儲存伺服器組做了負載均衡, 但整套FastDFS 叢集如果想對外提供統一的檔案訪問地址,還需要對兩個Tracker中的Nginx進行HA叢集。 即對Fastdfs_tracker_t1(192.168.10.200)和Fastdfs_tracker_t2(192.168.10.201)做keepalived+nginx的高可用。 192.168.10.200 Master 192.168.10.201 Slave 192.168.10.210 VIP 也就是說整套FastDFS 叢集對外提供統一的檔案訪問(VIP)地址192.168.10.210. Keepalived+Nginx 主從部署記錄可以參考:http://www.cnblogs.com/kevingrace/p/6138185.html,部署記錄這裡就不介紹了
8)fdfs常用命令
1)上傳檔案命令:fdfs_upload_file 不僅可以上傳圖片檔案,也可以上傳其他檔案。 [root@Fastdfs_client src]# echo "hahaha" > test.txt [root@Fastdfs_client src]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.txt group3/M00/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 上傳成功後,返回一串帶有組和路徑標識的字串,這是檔案的id,然後到group3組內的儲存伺服器上檢視這個檔案的id資訊 [root@Fastdfs_storage_s3 ~]# ll /fastdfs/storage/data/00/00/ total 84 -rw-r--r--. 1 root root 7 Feb 27 11:02 wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt [root@Fastdfs_storage_s3 ~]# cat /fastdfs/storage/data/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt hahaha [root@Fastdfs_storage_s3 ~]# md5sum /fastdfs/storage/data/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 5a6d311c0d8f6d1dd03c1c129061d3b1 /fastdfs/storage/data/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 2)下載檔案命令:fdfs_download_file 在客戶端機器上將上面上傳到Fastdfs儲存裡文檔案下載下來 [root@Fastdfs_client ~]# cd /mnt/ [root@Fastdfs_client mnt]# /usr/bin/fdfs_download_file /etc/fdfs/client.conf group3/M00/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt [root@Fastdfs_client mnt]# ls wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt [root@Fastdfs_client mnt]# cat wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt hahaha [root@Fastdfs_client mnt]# md5sum wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 5a6d311c0d8f6d1dd03c1c129061d3b1 wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 下載後可以對比檔案的md5值。 3)檢視檔案資訊命令:fdfs_file_info [root@Fastdfs_client ~]# /usr/bin/fdfs_file_info /etc/fdfs/client.conf group3/M00/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt source storage id: 0 source ip address: 192.168.10.204 file create timestamp: 2018-02-27 11:02:02 file size: 7 file crc32: 3909919332 (0xE90CA264) 4)刪除檔案命令:fdfs_delete_file [root@Fastdfs_client ~]# /usr/bin/fdfs_delete_file /etc/fdfs/client.conf group3/M00/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt 刪除之後沒有資訊提示,我們再進行下載和查詢,結果如下: [root@Fastdfs_client mnt]# /usr/bin/fdfs_download_file /etc/fdfs/client.conf group3/M00/00/00/wKgKzFqUyiqAabn8AAAAB-kMomQ229.txt [2018-02-27 14:21:08] ERROR - file: tracker_proto.c, line: 48, server: 192.168.10.204:23000, response status 2 != 0 download file fail, error no: 2, error info: No such file or directory 到group3組內的儲存伺服器上檢視,發現沒有這個檔案的id資訊 [root@Fastdfs_storage_s3 ~]# ll /fastdfs/storage/data/00/00/ [root@Fastdfs_storage_s3 ~]# 5)追加檔案內容命令:fdfs_upload_appender 和 fdfs_append_file 先使用命令上傳一個appender檔案 [root@Fastdfs_client src]# echo "123456" > haha.txt [root@Fastdfs_client src]# /usr/bin/fdfs_upload_appender /etc/fdfs/client.conf /usr/local/src/haha.txt group1/M00/00/00/wKgKylqU_myEa194AAAAACXX7BE787.txt 到group1組內的儲存伺服器上檢視這個檔案 [root@Fastdfs_storage_s1 ~]# ll /fastdfs/storage/data/00/00/ total 12 -rw-r--r--. 1 root root 7 Feb 27 14:45 wKgKylqU_myEa194AAAAACXX7BE787.txt [root@Fastdfs_storage_s1 ~]# cat /fastdfs/storage/data/00/00/wKgKylqU_myEa194AAAAACXX7BE787.txt 123456 然後進行內容追加 [root@Fastdfs_client src]# echo "abcdefgh" > heihei.txt [root@Fastdfs_client src]# /usr/bin/fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKgKylqU_myEa194AAAAACXX7BE787.txt /usr/local/src/heihei.txt 再次到group1組內的儲存伺服器上檢視這個檔案內容 [root@Fastdfs_storage_s1 ~]# cat /fastdfs/storage/data/00/00/wKgKylqU_myEa194AAAAACXX7BE787.txt 123456 abcdefgh 或者將檔案下載到本地檢視 [root@Fastdfs_client src]# cd /mnt/ [root@Fastdfs_client mnt]# /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgKylqU_myEa194AAAAACXX7BE787.txt [root@Fastdfs_client mnt]# ls wKgKylqU_myEa194AAAAACXX7BE787.txt [root@Fastdfs_client mnt]# cat wKgKylqU_myEa194AAAAACXX7BE787.txt 123456 abcdefgh 6)監控伺服器狀態命令:fdfs_monitor [root@Fastdfs_client src]# /usr/bin/fdfs_monitor /etc/fdfs/client.conf [2018-02-27 14:49:06] DEBUG - base_path=/fastdfs/tracker, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 server_count=2, server_index=0 tracker server is 192.168.10.200:22122 group count: 3 Group 1: group name = group1 disk total space = 50268 MB disk free space = 44987 MB trunk free space = 0 MB storage server count = 1 active server count = 1 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 192.168.10.202 ip_addr = 192.168.10.202 ACTIVE http domain = ........ ........ 7)移除故障節點示例 如果一個group組內有多個節點,其中一個節點的上的fastdfs出了問題,需要將它從組中移除,可根據下面的命令操作: a)停掉fastdfs上的storage服務 # /etc/init.d/fdfs_storaged stop # lsof -i :23000 //確保storage服務停止 b)將問題節點(比如group1組內的節點storage-node3,ip為192.168.10.23)從組中移除 # /usr/bin/fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.10.23 c)移除成功,使用fdfs_monitor命令進行監控 # /usr/bin/fdfs_monitor /etc/fdfs/client.conf 通過監控,可以發現移除之後,storage-node3節點的狀態為DELETED。 d)當storage-node3上的fastdfs修復後,可以再把它加入到fastdfs組中,加入的操作只需要把storage-node3上的fastdfs服務正常啟動就好, 因為配置檔案已經做了配置,如果服務正常的話,修復後的節點可以自動加入到fastdfs組中 啟動fastdfs_storaged服務 # /etc/init.d/fdfs_storaged stop # lsof -i :23000 使用fdfs_monitor命令進行監控 # /usr/bin/fdfs_monitor /etc/fdfs/client.conf 修復後,再次監控,可以看到,此時storage-node3已經變為ACTIVE狀態了。