FastDFS Distributed File System
In traditional file systems, files are stored locally on the same machine. In contrast, distributed file systems store files across multiple machines that are connected via network and managed centrally. Both uploading and accessing files require interaction through a central management system.
FastDFS Architecture
FastDFS consists of two primary components: Tracker Server and Storage Server.
- Tracker Server: This acts as the coordination server responsible for managing communication between clients and storage nodes, providing load balancing and monitoring the operational status of storage servers. It serves as the bridge connecting clients and storage servers.
- Storage Server: These servers store actual files along with their metadata. Each storage server runs a thread that periodically reports its status to all trackers in the tracker cluster, including disk usage, file synchronization status, and statistics on upload/download counts.
- Group: A group refers to a cluster of multiple storage servers. When a file is uploaded to one server within a group, FastDFS automatically synchronizes it to all other servers in the same group, ensuring redundancy. Servers in different groups maintain separate data and do not communicate with each other.
- Tracker Cluster: A collection of tracker servers working together.
- Storage Cluster: Made up of several groups.
File Upload and Download Process
Upload Flow
- The client queries the tracker server to find an available storage server.
- The tracker returns the IP address and port number of a suitable storage server.
- The client connects directly to the specified storage server and uploads the file.
- After successful upload, the storage server provides a file identifier to the client, completing the process.
Download Flow
- The client asks the tracker server for the location of the requested file.
- The tracker responds with the IP address and port of a storage server holding the file.
- The client connects directly to the storage server to download the specified file.
- The download completes successfully.
Setting Up FastDFS
Acquiring Requried Packages
Visit https://github.com/happyfish100 to download the necessary packages. Download the source code as ZIP archives.
Installing Dependencies
Install required libraries:
sudo apt-get install build-essential
sudo apt-get install libtool
Install PCRE library (http://www.pcre.org/):
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
Install ZLIB library (http://www.zlib.net):
sudo apt-get install zlib1g-dev
Firewall Configuration
Enable or disable firewall as needed:
ufw enable
ufw disable
Create a directory named fastdfs on your server and place the downloaded files there. Alternatively, use wget to retrieve them directly.
Extract and compile:
First, install the basic FastDFS libraries:
libfastcommon
Navigate to the correct path and run:
./make && ./make install
If no errors occur, proceed with installing FastDFS itself.
After successful compilation, go to /etc/fdfs.
Two sample configuration files will be present:
storage.conf.sampletracker.conf.sample
These configurations support both tracker and storage roles depending on how you configure them.
Edit tracker.conf.sample, rename it to tracker.conf, and set:
base_path=/home/fastdfs
Other settings can remain unchanged.
For storage.conf.sample, rename it to storage.conf, and configure:
base_path=/home/fastdfs
store_path0=/home/fastdfs
tracker_server=xxx.xxxx.xxx.xxx:22122
Starting Services
Start the services using these commands:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
Once installed, if uploads succeed but access fails, install fastdfs-nginx-module-master.
Unzip and modify:
Edit config:
Replace /usr/local/ paths with /usr/ globally using this command:
:%s+/usr/local/+/usr/+g
Copy mod_fastdfs.conf to /etc/fdfs/:
sudo cp mod_fastdfs.conf /etc/fdfs/
If NGINX was installed via package managers like apt or yum, it's recommended to reinstall NGINX using binary installation so that modules can be compiled-in.
Download NGINX from http://nginx.org/download/
Installing NGINX Dependencies
Install necessary tools:
yum/apt install gcc
yum/apt install -y unzip zip
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
Compiling NGINX
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/fastdfs-nginx-module/src
Compilation and Installation
make && sudo make install
Copying Configuration Files
Copy the following files from the FastDFS source and module directories into /etc/fdfs/:
http.confmime.typesmod_fastdfs.conffromfastdfs-nginx-module-master/src
Configuring Files
Modify mod_fastdfs.conf:
url_have_group_name = true
tracker_server=192.168.197.125:22122
Update nginx.conf:
server {
listen 80;
server_name image.caicai.top;
location ~/group([0-9])/ {
ngx_fastdfs_module;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Managing NGINX Service
Start NGINX:
nginx
Stop NGINX:
nginx -s stop
Reload configuration:
nginx -s reload
With no errors reported, FastDFS setup is complete.