FastDFS是一個開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等。
其工作流程如下:
1、tracker:是一個調度器,Fastdfs進程就是Fastdfs進程程序,trackers做了很多工作,replication(節點複製),deletion(文件刪除),reaper(存儲失敗重製),monitor(健康檢測)等等,這個是基於時間的eventbasesd父進程/消息總線來管理所有來治癒客戶端的交互(requesting operations to be performed)包括將請求負載平衡到多個‘query workers’中,然後讓Fastdfs的子進程去處理。
2、group:實際文件存放位置,存儲節點是一個文件存儲節點服務器,用來做刪除,存放,重命名等事情,讀取可用web服務來實現,任何web服務都可以。
實驗環境4臺服務器node1,node2,node3,node4
node1:client,tracker,store
group1:node1,node2
group2:node3,node4
1、依賴
yum -y groupinstall "Development tools"
2、包下載
[root@node4 fastdfs]# git clone https://github.com/happyfish100/libfastcommon.git
[root@node4 fastdfs]# git clone https://github.com/happyfish100/fastdfs.git
3、libfastcommon依賴
[root@node2 fastdfs]# cd libfastcommon/
[root@node2 libfastcommon]# ./make.sh
[root@node2 libfastcommon]# ./make.sh install
4、fastdfs安裝
[root@node1 fastdfs]# cp -r fastdfs /www/server/source/fastdfs-5.0.12
[root@node1 fastdfs]# ln -sv /www/server/source/fastdfs-5.0.12 /www/server/fastdfs
[root@node1 fastdfs]# cd /www/server/source/fastdfs-5.0.12
[root@node1 fastdfs-5.0.12]# ./make.sh
[root@node1 fastdfs-5.0.12]# ./make.sh install
[root@node1 fastdfs-5.0.12]# cp conf/{http.conf,mime.types,anti-steal.jpg} /etc/fdfs/
5、存儲目錄
[root@node1 fastdfs-5.0.12]# mkdir /www/data/fastdfs/{tracker,store,client}/{data,base} -pv
6、tracker配置文件:
[root@node1 fastdfs]# cd /etc/fdfs/
[root@node1 fdfs]# cp tracker.conf.sample tracker.conf
[root@node1 fdfs]# vim tracker.conf
base_path=/www/data/fastdfs/tracker/base #臨時數據 注意權限
#選組
# 0: round robin
# 1: specify group
# 2: load balance, select the max free space group to upload file
store_lookup=2
# which group to upload file
# when store_lookup set to 1, must set store_group to the group name
store_group=group2 只有指定組才生效
#選組中的節點
# 0: round robin (default)
# 1: the first server order by ip address
# 2: the first server order by priority (the minimal)
# Note: if use_trunk_file set to true, must set store_server to 1 or 2
store_server=0
# which path(means disk or mount point) of the storage server to upload file
# 0: round robin
# 2: load balance, select the max free space path to upload file
store_path=0
# 讀
# 0: round robin (default)
# 1: the source storage server which the current file uploaded to
download_server=0
#預留百分10
reserved_storage_space = 10%
run_by_group=
run_by_user=
allow_hosts=*
7、啓動trackerd
[root@node1 fdfs]# /etc/init.d/fdfs_trackerd start
Reloading systemd: [ OK ]
Starting fdfs_trackerd (via systemctl): [ OK ]
[root@node1 fdfs]# ss -lntup|grep 22122
tcp LISTEN 0 128 *:22122 *:* users:(("fdfs_trackerd",pid=30355,fd=5))
8、storage配置
[root@node1 fdfs]# cp storage.conf.sample storage.conf
[root@node1 fdfs]# vim storage.conf
base_path=/www/data/fastdfs/store/base
group_name=group1 #組號
store_path_count=1 #存儲個數
store_path0=/www/data/fastdfs/store/data #存儲位置
#store_path1=/home/yuqing/fastdfs2
tracker_server=192.168.1.201:22122
node3,node4的組id
group_name=group2
9、啓動 store
[root@node1 fdfs]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl): [ OK ]
[root@node1 fdfs]# ss -lntup|grep fs
tcp LISTEN 0 128 *:23000 *:* users:(("fdfs_storaged",pid=30850,fd=5))
文件存儲目錄結構
10、客戶端配置
[root@node1 fdfs]# cp client.conf.sample client.conf
[root@node1 fdfs]# vim client.conf
base_path=/www/data/fastdfs/client/base
tracker_server=192.168.1.200:22122
11、上傳文件
[root@node1 ~]# fdfs_upload_file /etc/fdfs/client.conf a.jpg
group1/M00/00/00/wKgByFsvVkKANyY1AAAQhCu9IQw489.jpg
[root@node1 ~]# fdfs_upload_file /etc/fdfs/client.conf a.jpg
group2/M00/00/00/wKgBylsvVkmAFdD7AAAQhCu9IQw868.jpg
配置nginx代理
1、編譯nginx
name: nginx
linkname: nginx
version: 1.14.0
download: http://nginx.org/download/nginx-{version}.tar.gz
cmds:
- id nginx || useradd -r -M -s /sbin/nologin nginx
yum:
- yum -y groupinstall "Development tools"
- yum -y install pcre-devel openssl-devel zlib-devel
uncompress: tar xf nginx-{version}.tar.gz
uncompress_dir: nginx-{version}
compile: ./configure --prefix={app_source_path}
--conf-path={app_source_path}/conf/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_v2_module
--with-http_dav_module
--with-http_stub_status_module
--with-threads
--with-file-aio
--with-http_gzip_static_module
--with-stream
--add-module=../nginx-http-auth-digest-master
--add-module=../fastdfs-nginx-module-master/src
&& make && make install
depends:
- name: nginx-http-auth-digest
version: ''
download: 'https://github.com/atomx/nginx-http-auth-digest/archive/master.zip -O nginx-http-auth-digest-master.zip'
uncompress: unzip -o nginx-http-auth-digest-master.zip
uncompress_dir: ''
compile: ''
- name: nginx-fastdfs
version: ''
download: 'https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip -O nginx-fastdfs.zip'
uncompress: unzip -o nginx-fastdfs.zip
uncompress_dir: ''
compile: ''
init:
cmds:
- cat {app_source_path}/conf/nginx.conf|egrep 'include conf\.d/\*\.conf' || sed -i '$s#}#\tinclude conf.d/*.conf;\n}#' {app_source_path}/conf/nginx.conf
- '[ -d {app_source_path}/conf/conf.d ] || mkdir {app_source_path}/conf/conf.d'
- echo 'export PATH={app_link_path}/sbin:$PATH'>/etc/profile.d/nginx.sh
systemctl:
path: /etc/systemd/system/mynginx.service
content:
Unit:
Description: The Nginx Server
After: network.target remote-fs.target nss-lookup.target
Service:
Type: forking
PIDFILE: '/var/run/nginx.pid'
ExecStart: '{app_link_path}/sbin/nginx'
ExecReload: '{app_link_path}/sbin/nginx -s reload'
ExecStop: '{app_link_path}/sbin/nginx -s stop'
PrivateTmp: true
Install:
WantedBy: multi-user.target
2、配置nginx
[root@node1 src]# cp /root/fs/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
[root@node1 fdfs]# vim mod_fastdfs.conf
base_path=/www/data/fastdfs/client/base
group_name=group1
tracker_server=192.168.1.200:22122
store_path0=/www/data/fastdfs/store/data
log_filename=/www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
url_have_group_name = false #說明在反代時候不用加group
3、日誌配置
[root@node1 fdfs]# touch /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
[root@node1 fdfs]# chown -R nginx.nginx /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
4、nginx配置
location /M00 {
alias /www/data/fastdfs/store/data/data/;
ngx_fastdfs_module;
}
5、日誌
[root@node1 fdfs]# cat /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
[2018-06-24 17:31:47] INFO - fastdfs apache / nginx module v1.15, response_mode=proxy, base_path=/www/data/fastdfs/client/base, url_have_group_name=0, group_name=group1, storage_server_port=23000, path_count=1, store_path0=/www/data/fastdfs/store/data, connect_timeout=2, network_timeout=30, tracker_server_count=1, if_alias_prefix=, local_host_ip_count=3, anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0, token_check_fail content_type=, token_check_fail buff length=0, load_fdfs_parameters_from_tracker=1, storage_sync_file_max_delay=86400s, use_storage_id=0, storage server id count=0, flv_support=1, flv_extension=flv