fastdfs集群安裝與配置

永遠十八歲 2021-08-15 17:06:01 阅读数:75

本文一共[544]字,预计阅读时长:1分钟~
fastdfs 集群 配置

FastDFS安裝與配置

一、Why FastDFS?

單機架構:

代碼與各種靜態文件放在同一臺主機,通過目錄結構直接訪問靜態資源。代碼與文件耦合度高。

分層架構:

使用獨立圖片服務器,單機擴展性差。代錶軟件:nfs

分布式文件系統:

擴展性强、高可用性。

缺點:需要的服務器資源多。

二、What is FastDFS?

開源、輕量、分布式文件系統。適用於中小文件(4KB ~~ 500MB)。

純C實現,支持Linux、FreeBSD等類UNIX

三、相關概念

1、tracker server

  • 調度器
  • 負責維持集群信息,這些信息由storage node報告生成
  • 本地不需要持久存儲數據
  • storage node需要周期性向tracker發送心跳信息

2、storage server

  • 實現文件上傳、下載、文件同步

  • 以group為單元進行組織

  • 任何一個storage server都應該屬於某個group,一個group應該包含至少兩個storage server.

  • 在同一個group內部,各storage server的數據互相冗餘。

  • 存儲空間以最小storage server空間為准。

3、base_path 基路徑

  • FastDFS基路徑,不會自動創建,需事先建好。
  • $base_path/data:數據文件存儲比特置,包括PID、節點狀態信息、數據同步文件
  • $base_path/logs:日志文件存儲比特置
  • $base_path/data/sync:storage server存放文件上傳、删除等更改操作binglog的路徑。

4、store_path 存儲路徑

  • 通常應對應一塊硬盤或RAID盤
  • 支持多存儲路徑,最多256個
  • 多個store_path時,順序不可隨意調整(文件ID中包含store_path順序號)

5、文件ID

  • 由storage server生成的訪問文件的憑證
  • 包括group名稱(group1)、存儲路徑順序號(M00)、兩級目錄的文件名(00/00)、文件名
  • 文件名:由源頭storage server ip 、創建時的時間戳、文件大小、文件的校驗碼和一個隨機數進行hash計算之後生成
  • 示例:group1/M00/01/32/wKgCaFx1982ENxLWAAAAAA0e5-o982.txt

四、制作rpm包並安裝

項目地址:

gitee.com/fastdfs100/…

github.com/happyfish10…

FastDFS v5.12、libfastcommon V1.0.43 及以上版本

github.com/happyfish10…

github.com/happyfish10…

tracker server:

主機名 IP地址
tracker1 192.168.100.10

storage server:

所屬組 主機名 IP地址
group1 storage11 192.168.100.11
group1 storage12 192.168.100.12
group2 storage21 192.168.100.21
group2 storage22 192.168.100.22

1 安裝包組

~]# yum groupinstall -y "Development Tools" -y
複制代碼

2 制作libfastcommon包並安裝

[[email protected] ~]# mkdir -pv rpmbuild/{SOURCES,SPECS}
[[email protected] ~]# tar xf libfastcommon-1.0.48.tar.gz 
[[email protected] ~]# cp libfastcommon-1.0.48/libfastcommon.spec rpmbuild/SPECS/
[[email protected] ~]# cd rpmbuild/SPECS/
[[email protected] SPECS]# rpmbuild -bb libfastcommon.spec 
# 制作成功的rpm包比特置:/root/rpmbuild/RPMS/x86_64/,debug包無需安裝
[[email protected] ~]# ls rpmbuild/RPMS/x86_64/
libfastcommon-1.0.48-1.el7.x86_64.rpm libfastcommon-debuginfo-1.0.48-1.el7.x86_64.rpm libfastcommon-devel-1.0.48-1.el7.x86_64.rpm
複制代碼

安裝libfastcommon

[[email protected] ~]# mkdir rpms
[[email protected] ~]# cp rpmbuild/RPMS/x86_64/libfastcommon-devel-1.0.48-1.el7.x86_64.rpm rpms/
[[email protected] ~]# cp rpmbuild/RPMS/x86_64/libfastcommon-1.0.48-1.el7.x86_64.rpm rpms/
[[email protected] ~]# rpm -ivh rpms/*.rpm
Preparing... ################################# [100%]
package libfastcommon-1.0.48-1.el7.x86_64 is already installed
package libfastcommon-devel-1.0.48-1.el7.x86_64 is already installed
複制代碼

3 制作fastdfs安裝包

[[email protected] ~]# wget https://github.com/happyfish100/fastdfs/archive/refs/tags/V5.12.tar.gz
[[email protected] ~]# cp fastdfs-5.12/fastdfs.spec rpmbuild/SPECS/
# 注意:需修改fastdfs.spec文件中FDFSVersion為5.12,原值為5.0.12,制作rpm包時會報找不到fastdfs-5.0.12目錄
[[email protected] ~]# cd rpmbuild/SPECS/
[[email protected] SPECS]# rpmbuild -bb fastdfs.spec
# 注意:制作fastdfs安裝包前,必須先安裝libfastcommon包(libfastcommon為fastdfs依賴包)
複制代碼

各包說明:

包名 用途
fastdfs-server 提供tracker及storage
fastdfs-5.12 server及client
fastdfs-tool 提供fastdfs各種工具
libfdfsclient 客戶端動態庫
libfdfsclient-devel 客戶端開發包

安裝fastdfs軟件包:

[[email protected] ~]# cd /root/rpmbuild/RPMS/x86_64
[[email protected] x86_64]# cp fastdfs-5.12-1.el7.x86_64.rpm fastdfs-server-5.12-1.el7.x86_64.rpm fastdfs-tool-5.12-1.el7.x86_64.rpm libfdfsclient-* /root/rpms/
[[email protected] x86_64]# cd /root/rpms/
[[email protected] rpms]# rpm -ivh fastdfs-* libfdfs*
Preparing... ################################# [100%]
package libfdfsclient-5.12-1.el7.x86_64 is already installed
package libfdfsclient-devel-5.12-1.el7.x86_64 is already installed
package fastdfs-tool-5.12-1.el7.x86_64 is already installed
package fastdfs-server-5.12-1.el7.x86_64 is already installed
package fastdfs-5.12-1.el7.x86_64 is already installed
複制代碼

五、配置tracker並啟動

提供配置文件:

~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
複制代碼

編輯配置文件,修改以下項:

base_path=/data/tracker/fastdfs
複制代碼

創建文件:

~]# mkdir -pv /data/tracker/fastdfs
複制代碼

啟動服務:

[[email protected] ~]# /etc/init.d/fdfs_trackerd start
複制代碼

查看服務:

[[email protected] ~]# /etc/init.d/fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd; bad; vendor preset: disabled)
Active: active (running) since Sat 2021-08-14 02:07:44 CST; 4min 27s ago
Docs: man:systemd-sysv-generator(8)
Process: 22916 ExecStop=/etc/rc.d/init.d/fdfs_trackerd stop (code=exited, status=0/SUCCESS)
Process: 22933 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/fdfs_trackerd.service
└─22938 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
Aug 14 02:07:44 tracker1 systemd[1]: Starting LSB: FastDFS tracker server...
Aug 14 02:07:44 tracker1 fdfs_trackerd[22933]: Starting FastDFS tracker server:
Aug 14 02:07:44 tracker1 systemd[1]: Started LSB: FastDFS tracker server.
複制代碼

六、配置storage

storage11

group_name=group1
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
複制代碼

storage12

group_name=group1
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
複制代碼

storage21

group_name=group2
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
複制代碼

storage22

group_name=group2
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
複制代碼

storage節點創建目錄:

~]# mkdir -pv /data/fastdfs/fdfs
複制代碼

啟動服務:

~]# /etc/init.d/fdfs_storaged start
複制代碼

七、測試

測試工具的使用需配置/etc/fdfs/client.conf

base_path=/data/fastdfs/client
tracker_server=192.168.100.10:22122
複制代碼

1、查看集群狀態

命令:fdfs_monitor

Usage:fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]

示例:

# 查看集群整體狀態
~]# fdfs_monitor /etc/fdfs/client.conf list group1
 # 查看group1狀態
~]# fdfs_monitor /etc/fdfs/client.conf list group1
複制代碼

2、上傳、下載文件測試

命令:fdfs_upload_file

Usage: fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]

# 上傳文件,上傳後storage server會返回文件的FID
[[email protected] ~]# fdfs_upload_file /etc/fdfs/client.conf fastdfs-5.12/conf/anti-steal.jpg
group1/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
複制代碼

命令:fdfs_download_file

Usage: fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>]

[[email protected] ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
複制代碼

查看是否是一個文件:

[[email protected] ~]# md5sum wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
2a5af127e0e1188e73012cd5cc69d0f4 wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
[[email protected] ~]# md5sum fastdfs-5.12/conf/anti-steal.jpg
2a5af127e0e1188e73012cd5cc69d0f4 fastdfs-5.12/conf/anti-steal.jpg
複制代碼

八、通過nginx訪問

fastdsf-nginx-module 1.20

nginx 1.18.0

主機上需已安裝FastDFS storage server和client libray.

FastDFS 版本需>=5.11

1、編譯安裝nginx

~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
~]# tar xf nginx-1.18.0.tar.gz
~]# yum -y install gcc redhat-rpm-config perl-devel perl-ExtUtils-Embed gd-devel libxml2-devel libxslt-devel openssl-devel openssl pcre pcre-devel gperftools
[[email protected] nginx-1.18.0]# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/fastdfs-nginx-module-1.20/src
[[email protected] nginx-1.18.0]# make -j 4 && make install
複制代碼

2、配置

創建nginx用戶

~]# useradd -r nginx
複制代碼

創建所需目錄並授權給nginx用戶

~]# mkdir -pv /var/lib/nginx/tmp/
~]# chown -R nginx.nginx /var/lib/nginx
複制代碼

nginx.conf添加以下配置:

location /M00 {
root /data/fastdfs/fdfs/data;
ngx_fastdfs_module;
}
複制代碼

/etc/fdfs/mod_fastdfs.conf修改:

base_path=/data/nginx/
tracker_server=192.168.100.10:22122
store_path0=/data/fastdfs/fdfs # 與當前節點storage.conf中store_path0保持一致
複制代碼

提供http.conf mime.type文件(fastdfs源碼包conf目錄下)

~]# scp http.conf mime.types [email protected]:/etc/fdfs/
複制代碼

啟動nginx

~]# nginx -c /etc/nginx/nginx.conf
複制代碼

訪問圖片:

http://192.168.100.11/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg

版权声明:本文为[永遠十八歲]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210815170542734R.html