一.fastdfs环境搭建
下面准备 2 个环境模拟部署并测试
准备工作
主服务器:192.168.100.10
从服务器:192.168.100.11
步骤1. 了解 fastDFS 原理
这次是最简单的 storage 同步,不涉及多 tracker 多 group 的配置,这里简单讲一下 fastdfs 的原理 FastDFS 是余庆老师开发的一个开源的高性能分布式文件系统(DFS)。
它的主要功能包括:文件存储,文 件同步和文件访问,以及高容量和负载平衡。 FastDFS 系统有三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。 Tracker Server: 跟踪服务器,主要做调度工作,起到均衡的作用;负责管理所有的 storage server 和 group, 每个 storage 在启动后会连接 Tracker,告知自己所属 group 等信息,并保持周期性心跳。多个 Tracker 之 间是对等关系,不存在单点故障。 Storage Server: 存储服务器,主要提供容量和备份服务;以 group 为单位,每个 group 内可以有多台 storage server,组内的 storage server 上的数据互为备份。 Client:客户端,上传下载数据的服务器 模块之间的主要关系如下:
安装步骤:
1. 首先安装 fastdfs
2. 准备软件包
3.安装编译工具辅助库文件
yum whatprovides *******
yum install gcc libevent libenent-devel perl gcc-c++ -y
Yum install unzip -y
4.安装及配置 fastdfs
unzip libfastcommon-master.zip -d /usr/local tar -zxvf fastdfs-5.05.tar.gz
mv fastdfs-5.05 /usr/local/
cd libfastcommon-master/
cp -r /usr/local/fastdfs-5.05 /conf/* /etc/fdfs/
5. 自此 fastdfs 安装完成了,接下来配置 trackerd 和 storaged,并启动它们
6. 规划文件路径
Tracker : mkdir -p /opt/fastdfs/tracker
Storage : mkdir -p /opt/fastdfs/storage
Storage 存储路径 : mkdir -p /opt/fastdfs/storage/file
Client : mkdir -p /opt/fastdfs/client
Module-nginx : mkdir -p /opt/fastdfs/module-nginx
7.启动命令
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
8.修改配置文件
修改/etc/fdfs/tracker.conf 修改一处 path 即可 /opt/fastdfs/tracker
修改/etc/fdfs/storage.conf 修改 2 处
path /opt/fastdfs/storage /opt/fastdfs/storage/file
1 处 server 地址
9.上传下载删除测试
修改 client.Conf
[root@10 opt]# fdfs_test /etc/fdfs/client.conf upload /opt/test.jpg This is FastDFS client test program v5.05 Copyright © 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2021-08-22 14:28:52] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, 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 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=192.168.100.10, port=23000 group_name=group1, ip_addr=192.168.100.10, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg source ip address: 192.168.100.10 file timestamp=2021-08-22 14:28:52 file size=29858 file crc32=2885185840 example file url: http://192.168.100.10/group1/M00/00/00/wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKhkCmEh7qSASfnBAAB0oqv4cTA408_big.jpg source ip address: 192.168.100.10 file timestamp=2021-08-22 14:28:52 file size=29858 file crc32=2885185840 example file url: http://192.168.100.10/group1/M00/00/00/wKhkCmEh7qSASfnBAAB0oqv4cTA408_big.jpg ------------------------------------------------------------------------------------------------------------------------------------------------------ [root@11 fdfs]# fdfs_test /etc/fdfs/client.conf upload /opt/test.jpg This is FastDFS client test program v5.05 Copyright © 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2021-08-22 14:30:15] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=30, network_timeout=60,
tracker_server_count=1, 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 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=192.168.100.11, port=23000 group_name=group1, ip_addr=192.168.100.11, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKhkC2Eh7veAPwyNAAB0oqv4cTA280.jpg source ip address: 192.168.100.11 file timestamp=2021-08-22 14:30:15 file size=29858 file crc32=2885185840 example file url: http://192.168.100.11/group1/M00/00/00/wKhkC2Eh7veAPwyNAAB0oqv4cTA280.jpg storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKhkC2Eh7veAPwyNAAB0oqv4cTA280_big.jpg source ip address: 192.168.100.11 file timestamp=2021-08-22 14:30:15 file size=29858 file crc32=2885185840 example file url: http://192.168.100.11/group1/M00/00/00/wKhkC2Eh7veAPwyNAAB0oqv4cTA280_big.jpg [root@11 fdfs]# 删除测试 [root@10 00]# fdfs_test /etc/fdfs/client.conf delete group1 M00/00/00/wKhkCmEh7qSASfnBAAB0oqv4cTA408_big.jpg This is FastDFS client test program v5.05 Copyright © 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2021-08-22 14:33:54] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, 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 storage=192.168.100.10:23000 delete file success [root@10 00]# ll 总用量 36 -rw-r–r–. 1 root root 29858 8 月 22 14:28 wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg -rw-r–r–. 1 root root 49 8 月 22 14:28 wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg-m
10.由于现在还没有和 nginx 整合无法使用 http 下载。
11. FastDFS 和 nginx 整合
12. 安装 zlib pcre 包
./configure
Make
Make install
fastdfs-nginx-module 安装 解压文件,拷贝 src 下配置文件到./etc/fdfs/
13.安装 NGINX
解压 NGINX 进入目录:
./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module-master/src
Make
Mke install
修改配置文件
Cd /usr/local/nginx/conf/nginx.cong
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root html; index index.html index.htm; }
location ~/group[1-3]/M00
{
ngx_fastdfs_module;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#error_page 500 502 503 504 /50x.html;
location = /50x.html { root html;
}
14. 测试
[root@11 sbin]# ./nginx -V nginx version: nginx/1.8.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module-master/src [root@11 sbin]# ./nginx -t ngx_http_fastdfs_set pid=15248 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 启动进程 [root@11 sbin]# ./nginx ngx_http_fastdfs_set pid=15251 [root@11 sbin]# ps -ef | grep fdfs root 8388 1 0 14:19 ? 00:00:00 fdfs_trackerd /etc/fdfs/tracker.conf root 8400 1 0 14:21 ? 00:00:00 fdfs_storaged /etc/fdfs/storage.conf root 15255 7782 0 15:20 pts/1 00:00:00 grep --color=auto fdfs [root@11 sbin]# ps -ef | grep nginx root 15252 1 0 15:19 ? 00:00:00 nginx: master process ./nginx nobody 15253 15252 0 15:19 ? 00:00:00 nginx: worker process
root 15257 7782 0 15:20 pts/1 00:00:00 grep --color=auto nginx [root@11 sbin]# 访问测试
15.相关 tracker 配置文件详解
16.修改两台的参数测试 2 个 storage 同步:
主机目前文件数 example file url: http://192.168.100.10/group1/M00/00/00/wKhkCmEiIo-AYKJLAAEQF2scZcE975_big.jpg [root@10 00]# ll 总用量 380 -rw-r–r–. 1 root root 29858 8 月 22 14:28 wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg -rw-r–r–. 1 root root 49 8 月 22 14:28 wKhkCmEh7qSASfnBAAB0oqv4cTA408.jpg-m -rw-r–r–. 1 root root 32625 8 月 22 18:10 wKhkCmEiIoaAZGCDAAB_cdUYvFs176_big.jpg -rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIoaAZGCDAAB_cdUYvFs176_big.jpg-m -rw-r–r–. 1 root root 32625 8 月 22 18:10 wKhkCmEiIoaAZGCDAAB_cdUYvFs176.jpg -rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIoaAZGCDAAB_cdUYvFs176.jpg-m -rw-r–r–. 1 root root 69655 8 月 22 18:10 wKhkCmEiIo-AYKJLAAEQF2scZcE975_big.jpg -rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIo-AYKJLAAEQF2scZcE975_big.jpg-m -rw-r–r–. 1 root root 69655 8 月 22 18:10 wKhkCmEiIo-AYKJLAAEQF2scZcE975.jpg
-rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIo-AYKJLAAEQF2scZcE975.jpg-m -rw-r–r–. 1 root root 54526 8 月 22 18:10 wKhkCmEiIoqAUErLAADU_nHm0tI849_big.jpg -rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIoqAUErLAADU_nHm0tI849_big.jpg-m -rw-r–r–. 1 root root 54526 8 月 22 18:10 wKhkCmEiIoqAUErLAADU_nHm0tI849.jpg -rw-r–r–. 1 root root 49 8 月 22 18:10 wKhkCmEiIoqAUErLAADU_nHm0tI849.jpg-m [root@10 00]#
17.修改 11 的 stroge 配置文件
退出进程
[root@11 fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf stop waiting for pid [8388] exit … pid [8388] exit. [root@11 fdfs]# fdfs_storaged /etc/fdfs/storage.conf stop waiting for pid [8400] exit … pid [8400] exit. [root@11 ]# vim client.conf tracker_server=192.168.100.10:22122 vim storage.conf tracker_server=192.168.100.10:22122
18.修改 10 主机的 tracker
19.重启服务测试
已经实现了自动同步
20.上传文件测试
测试成功
21.删除文件测试
两个 storge 都访问不到了。
22.查看群集信息
[root@10 fdfs]# fdfs_monitor /etc/fdfs/storage.conf [2021-08-22 19:53:17] DEBUG - base_path=/opt/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, 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=1, server_index=0 tracker server is 192.168.100.10:22122 group count: 1 Group 1: group name = group1 disk total space = 17394 MB disk free space = 15781 MB trunk free space = 0 MB storage server count = 2 active server count = 2 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 1 current trunk file id = 0 Storage 1: id = 192.168.100.10 ip_addr = 192.168.100.10 (10) ACTIVE http domain = version = 5.05 join time = 2021-08-22 14:21:10 up time = 2021-08-22 18:28:02 total storage = 17394 MB free storage = 15781 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 = 1 connection.max_count = 3 total_upload_count = 10 success_upload_count = 10 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 = 10 success_set_meta_count = 10 total_delete_count = 2 success_delete_count = 2 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 = 373368 success_upload_bytes = 373368 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 = 59814 success_sync_in_bytes = 59814 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 14 success_file_open_count = 14 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 14 success_file_write_count = 14 last_heart_beat_time = 2021-08-22 19:53:03 last_source_update = 2021-08-22 18:59:39 last_sync_update = 2021-08-22 18:28:26
last_synced_timestamp = 1970-01-01 08:00:00 (never synced) Storage 2: id = 192.168.100.11 ip_addr = 192.168.100.11 ACTIVE http domain = version = 5.05 join time = 2021-08-22 14:20:57 up time = 2021-08-22 18:28:27 total storage = 17394 MB free storage = 15785 MB upload priority = 8 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 = 1 connection.max_count = 1 total_upload_count = 2 success_upload_count = 2 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 = 2 success_set_meta_count = 2 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 = 59716 success_upload_bytes = 59716 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 = 343951 success_sync_in_bytes = 343951 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 20 success_file_open_count = 20 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 20 success_file_write_count = 20 last_heart_beat_time = 2021-08-22 19:52:58 last_source_update = 2021-08-22 14:30:14 last_sync_update = 2021-08-22 18:59:42 last_synced_timestamp = 2021-08-22 18:59:39 (0s delay)