FastDFS learning

FastDFS is an open source lightweight distributed file system, to manage files, it features include file storage, file synchronization, file access (such as file upload and download file), can solve the problem of large capacity storage and load balancing, especially suitable for to file as a carrier of online services, such as photo album website, video web site, etc.

Upload process

  1. The client asks the tracker to upload to the storage without additional parameters
  2. Tracker returns an available storage
  3. The client communicates with the storage to upload files

Download process

[picture archived failure outside the chain, the source station might be hotlinking prevention mechanism, proposed to directly upload picture preserved (img – 9 d7ss0gm – 1619694000452) (images2015.cnblogs.com/blog/380802…)”

  1. The client asks the tracker which storage the file was downloaded from, using the file id (group name and file name).
  2. Tracker returns an available storage
  3. The client communicates with the storage to download the file

The term is introduced

  • TrackerServer is a tracking server, which mainly does scheduling work and load balancing on access. Records the status of the storage server and connects the Client and storage Server.
  • Storage Server Storage Server where files and meta data are stored
  • Group A group, also called a volume. The files on the servers in the same group are identical
  • The file id consists of two parts, the group name and the file name
  • Meta Data file attributes (key Value pairs)

Synchronization mechanism

  • The storage servers in the same group are peers. Files can be uploaded or deleted on any storage server.
  • File synchronization is performed between storage servers in the same group in push mode, that is, the source server synchronizes files to the target server. (Add a storage server and synchronize all existing data (including source data and backup data) from the existing storage server to the new storage server.)
  • Source data needs to be synchronized. Backup data does not need to be synchronized again. Otherwise, a loop is generated.

The characteristics of

  • Linear dilatability is high
  • High performance for concurrent file access
  • Special API for file access methods
  • Hardware costs are low
  • Save only one copy of the same file content

FastDFS versus mogileFS

indicators FastDFS mogileFs
System simplicity Terse has only two roles tracker and storage Typically, there are three roles: tracker, storage, and mysql DB, which stores file information
The system performance High (no database is used, file synchronization is point-to-point without tracker transfer) High (use mysql to store indexes southwest west, and file synchronization is scheduled and forwarded by tracker)
System stability C language development, can support high concurrency, high load Perl language development, high concurrency and high load support general
RAID way Grouping (group redundancy), flexibility Dynamic redundancy, moderate flexibility
Communication protocol Private protocol, download files support HTTP HTTP
Technical documentation More detailed less
File attached properties support Does not support
Keep one copy of the same document support Does not support
File offsets are supported when downloading files support Does not support

Install FastDFS

1. Download and install libfastCommon

Libfastcommon is a public C function library extracted from FastDFS and FastDHT.

1) download libfastcommon

Wget # https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gzCopy the code

(2) extract

# tar -zxvf v1.0.7.tar. gz # CD libfastcommon-1.0.7Copy the code

③ Compile and install

# ./make.sh
# ./make.sh install
Copy the code

(4) libfastcommon. So the installation to the/usr/lib64 / libfastcommon. So, but FastDFS main program set the lib directory is/usr/local/lib, so you need to create soft links.

# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so 
Copy the code

Download and install FastDFS

1) download FastDFS

Wget # https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gzCopy the code

(2) extract

# tar -zxvf v5.05.tar. gz # CD fastdfs-5.05Copy the code

③ Compile and install

# ./make.sh
# ./make.sh install
Copy the code

4 Default installation mode Corresponding files and directories after installation A. Service script:

/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_tracker
Copy the code

B. Configuration files (these three are sample configuration files provided by the author) :

/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample
Copy the code

C. The command tool is in the /usr/bin/ directory:

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 
Copy the code

⑤ The bin directory of the FastDFS service script is /usr/local/bin, but the actual command is installed in /usr/bin/.

Two ways:

Change /usr/local/bin in /etc/init.d/fdfs_tracker to /usr/bin in /etc/init.d/fdfs_tracker.

%s+/usr/local/bin+/usr/bin # vim fdfs_storaged Use the find and replace command to make uniform changes :%s+/usr/local/bin+/usr/bin

  

/usr/local/bin = /usr/local/bin = /usr/local/bin

# ln -s /usr/bin/fdfs_trackerd   /usr/local/bin
# ln -s /usr/bin/fdfs_storaged   /usr/local/bin
# ln -s /usr/bin/stop.sh         /usr/local/bin
# ln -s /usr/bin/restart.sh      /usr/local/bin
Copy the code

3. Configure the FastDFS Tracker

① Go to /etc/fdfs, copy the FastDFS sample configuration file tracker.conf.sample, and name it tracker.conf.

# cd /etc/fdfs
# cp tracker.conf.sample tracker.conf
# vim tracker.conf
Copy the code

② Edit tracker.conf. The ones marked in red need to be modified.

Is the configuration file invalid? False Disabled =false # Port to provide services port=22122 # Tracker Data and log directory address (the root directory must exist, subdirectories will be created automatically) base_path=/ ZHJ /fastdfs/ Tracker # HTTP service port http.server_port=8080Copy the code

③ Create the base data directory of tracker, that is, the directory corresponding to base_path

# mkdir -p /zhj/fastdfs/tracker
Copy the code

4. Enable tracing port on the firewall (default 22122)

-c INPUT -m state --state NEW -m TCP -p TCP --dport 22122 -j ACCEPT  # service iptables restartCopy the code

(5) start the Tracker

The first successful startup will create directories data and logs under/ZHJ/FDFSDFS /tracker/ (configured base_path).

Fdfs_trackerd start can also be started in this way, if the soft link is created above, then use this way: # service fdfs_trackerd startCopy the code

Check whether the FastDFS Tracker service is successfully started. If port 22122 is being monitored, the Tracker service is successfully installed.

# netstat -unltp|grep fdfs
Copy the code

Close the Tracker command:

# service fdfs_trackerd stop
Copy the code

⑥ Set the Tracker to boot

/etc/init.d/fdfs_trackerd start / /etc/init.d/fdfs_trackerd startCopy the code

⑦ Tracker server directory and file structure

After the Tracker service is successfully started, the data and logs directories are created in base_path. The directory structure is as follows:

${base_path} | __data | | __storage_groups. Dat: storage grouping information | | __storage_servers. Dat: storage server list | __logs | | __trackerd. Log: Tracker Server log fileCopy the code

4, FastDFS Storage (Storage)

① Go to the /etc/fdfs directory, copy the FastDFS storage sample configuration file storage.conf.sample, and name it storage.conf

# cd /etc/fdfs
# cp storage.conf.sample storage.conf# vim storage.conf
Copy the code

(2) edit storage. Conf

The ones marked in red need to be modified. The other ones are the default ones.

Is the configuration file invalid? Group_name =group1 # Port =23000 # Heartbeat interval, Heart_beat_interval =30 # Storage Data and log directory address (the root directory must exist, Subdirectories are automatically generated) base_path=/ ZHJ /fastdfs/storage # Storage server supports multiple paths for storing files. The number of base paths for storing files is set here. Usually, only one directory is configured. Store_path_count =1 # Configure store_path_count paths one by one. The index number is based on 0. If store_path0 is not configured, it is the same path as base_path. Store_path0 =/ ZHJ /fastdfs/file # Fastdfs uses two levels of directories to store files. Set the number of directories for storing files. # If this parameter is set to N (e.g. : 256), the storage Server will automatically create N * N subdirectories under store_path for storing files when it is first run. Subdir_count_per_path =256 # tracker_server list, will actively connect to tracker_server # Write a line tracker_Server =127.0.0.1(host IP):22122# The time period (default is all day) during which system synchronization is allowed. It is used to avoid problems with peak synchronization. Server_port =8080 sync_start_time=00:00 synC_end_time =23:59Copy the code

③ Create a Storage basic data directory corresponding to the base_path directory

# mkdir -p/ZHJ /fastdfs/storage # this is the configured store_path0 path # mkdir -p/ZHJ /fastdfs/fileCopy the code

④ Open the memory port in the firewall (default 23000)

-c INPUT -m state --state NEW -m TCP -p TCP --dport 23000 -j ACCEPT  # service iptables restartCopy the code

(5) start the Storage

Make sure Tracker is enabled before starting Storage. If the initial startup succeeds, the data and logs directories are created in/ZHJ /fastdfs/storage.

# service fdfs_storaged start # service fdfs_storaged start # service fdfs_storaged startCopy the code

Check whether the Storage starts successfully. Port 23000 is being monitored.

# netstat -unltp|grep fdfs
Copy the code

To close the Storage command:

# service fdfs_storaged stop
Copy the code

To check whether the Storage and Tracker are communicating:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
Copy the code

⑥ Set the Storage startup

/etc/init.d/fdfs_storaged start: /etc/init.d/fdfs_storaged startCopy the code

All landowners Storage directory

After Storage is successfully started, data and logs directories are created under base_path to record Storage Server information.

In the store_path0 directory, N*N subdirectories are created:

5. File upload test

① Modify the client configuration file in the Tracker server

# cd /etc/fdfs
# cp client.conf.sample client.conf
# vim client.conf
Copy the code

You can modify the following configuration, other default.

Base_path =/ ZHJ /fastdfs/ Client # tracker_server=127.0.0.1(host IP):22122Copy the code

② Upload test

In Linux, run the following command to upload the namei.jpeg image

# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf namei.jpeg
Copy the code

Return after a successful upload file ID: group1 M00/00/00 / wKgz6lnduTeAMdrcAAEoRmXZPp870. Jpeg

The returned fileid is a combination of group, storage directory, two-level subdirectories, fileid, and file name extension (specified by the client to distinguish file types).

Install Nginx

The file was uploaded successfully, but we couldn’t download it. So Nginx is installed as a server to support Http access to files. The Nginx module that installs FastDFS later also requires an Nginx environment.

Nginx only needs to be installed on the server where the StorageServer resides to access files. Because it is a single machine here, TrackerServer and StorageServer are on the same server.

1, install nginx required environment

(1) the GCC installed

# yum install gcc-c++
Copy the code

② PCRE pcre-devel installation

# yum install -y pcre pcre-devel
Copy the code

(3) zlib installation

# yum install -y zlib zlib-devel
Copy the code

(4) the OpenSSL installation

# yum install -y openssl openssl-devel
Copy the code

2. Install Nginx

1) download nginx

C # wget - https://nginx.org/download/nginx-1.12.1.tar.gzCopy the code

(2) extract

# tar -zxvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1
Copy the code

3 Use the default Settings

# ./configure
Copy the code

(4) Compile and install

# make
# make install
Copy the code

(5) start the nginx

#./nginx -s stop #./nginx -s quit #./nginx -s reloadCopy the code

⑥ Set the boot

# vim/etc/rc. Local add a line: / usr/local/nginx/sbin/nginx# # set the execute permissions chmod 755 rc. LocalCopy the code

⑦ Check the nginx version and module

/usr/local/nginx/sbin/nginx -V
Copy the code

• Enable Nginx port on firewall (default 8080)

Once added, it can be accessed on the machine using port 8080.

-c INPUT -m state --state NEW -m TCP -p TCP --dport 8080 -j ACCEPT  # service iptables restartCopy the code

3. Access files

Simple test access to files

(1) to modify nginx. Conf

# vim/usr/local/nginx/conf/nginx. Conf add the following lines, M00 {alias/ZHJ /fastdfs/file/data location /group1/M00 {alias/ZHJ /fastdfs/file/data; } # restart nginx# / usr/local/nginx/sbin/nginx -s reloadCopy the code

Outside the chain picture archiving failure, the source station might be hotlinking prevention mechanism, proposed to directly upload picture preserved (img – GAZ2Q88z – 1619694000462) (images2017.cnblogs.com/blog/856154…)”

② Images uploaded before browser access, successful.

http://127.0.0.1 (host IP)/group1 M00/00/00 / wKgz6lnduTeAMdrcAAEoRmXZPp870 jpeg

FastDFS configures the Nginx module

1. Install and configure the Nginx module

1 Fastdfs-nginx-module module description

FastDFS uses the Tracker server to store files on the Storage server. However, files need to be replicated between Storage servers in the same group, resulting in synchronization delay.

Suppose the Tracker server uploads the file to 192.168.51.128, and the file ID is returned to the client.

In this case, the FastDFS storage cluster mechanism synchronizes the file to the same storage group 192.168.51.129. If the client uses the file ID to obtain the file from 192.168.51.129, the file cannot be accessed.

Fastdfs-nginx-module redirects the file link to the source server to retrieve the file, avoiding the file failure caused by replication delay on the client.

Download fastdfs-nginx-module and unzip it

There are some issues with the latest version of master and the current version of Nginx. Wget # # # extract at https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip Unzip 5 e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip # # rename the mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1 fastdfs-nginx-module-masterCopy the code

(3) to configure Nginx

Add modules to nginx

# # stopped first nginx service/usr/local/nginx/sbin/nginx -s stop into the unpack the package directory # CD/softpackages/nginx 1.12.1 / # #. To add modules/configure --add-module=.. /fastdfs-nginx-module-master/ SRC # make && make installCopy the code

④ Check the Nginx module

# /usr/local/nginx/sbin/nginx -V
Copy the code

If the following is present, the module was added successfully

⑤ Copy the fastdfs-nginx-module configuration file to /etc/fdfs and modify it

# cd /softpackages/fastdfs-nginx-module-master/src

# cp mod_fastdfs.conf /etc/fdfs/
Copy the code

Modify the following configuration, other defaults

Connect_timeout =10 # Tracker Server Tracker_Server =127.0.0.1(host IP address):22122 # StorageServer Default port Storage_server_port =23000 # If the uri of file ID contains /group**, set it to true url_have_group_name = true # storage_server_port=23000 # If the URI of file ID contains /group**, set it to true. Store_path0 =/ ZHJ /fastdfs/file must be the same as storage.confCopy the code

⑥ Copy part of the FastDFS configuration file to /etc/fdfs

Types: /etc/ fdfs.types: /etc/ fdfs.types: /etc/ fdfs.types: /etc/ fdfs.types: /etc/ fdfs.types: /etc/ fdfs.types: /etc/Copy the code

⑦ Configure nginx and modify nginx.conf

# vim /usr/local/nginx/conf/nginx.conf
Copy the code

Modify configuration, other defaults

Add the Fastdfs-nginx module to port 8080

location ~/group([0-9])/M00 {
    ngx_fastdfs_module;
}
Copy the code

Note:

Server_port =8080 in /etc/fdf/storage.conf If the port is changed to another port, you need to unify the port and enable the port on the firewall.

Location ~/group([0-9])/M00 if there are multiple groups

8. Create a soft connection in/ZHJ /fastdfs/file and link it to the directory where the data is stored.

# ln -s /zhj/fastdfs/file/data/ /zhj/fastdfs/file/data/M00 
Copy the code

Pet-name ruby start nginx

# /usr/local/nginx/sbin/nginx
Copy the code

If the print is as follows, the configuration is successful

⑩ Access it in the address bar.

If you can download the file, the installation is successful. Note that instead of using the nginx route directly in point 3, you can configure the Fastdfs-nginx-module module to redirect files to the source server.

http://127.0.0.1 (host IP)/group1 M00/00/00 / wKgz6lnduTeAMdrcAAEoRmXZPp870 jpeg

Final deployment structure diagram (stolen diagram) : The following structure can be used to set up the environment.