一,查看本地centos的版本
[root@localhost lib]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: [email protected]
二,fastdfs的下载与安装
请参见 centos8安装fastdfs6.06集群方式一之:软件下载与安装
地址: https://www.cnblogs.com/architectforest/p/12388972.html
请参见 centos8安装fastdfs6.06集群方式三之:storage的安装/配置/运行
地址: https://www.cnblogs.com/architectforest/p/12389154.html
三,两台tracker上,生成fastdfs启动所需的默认的配置文件
[root@localhost fastdfs-6.06]# cd /etc/fdfs/
[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
说明:无须生成storage.conf文件,这两台tracker不做为storage
四,两台tracker上,安装nginx
说明:tracker上不需要安装fastdfs-nginx-module
1,解压nginx:
[root@localhost source]# tar -zxvf nginx-1.17.8.tar.gz
2,nginx配置,http_stub_status_module 模块
[root@localhost fdfs]# cd /usr/local/source/nginx-1.17.8/
[root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module
3,编译安装nginx
[root@localhost nginx-1.17.8]# make && make install
4,检查安装是否成功?
[root@localhost nginx-1.17.8]# ls /usr/local/soft/ | grep nginx
nginx
查看指定的编译参数是否起作用?
[root@localhost fdfs]# /usr/local/soft/nginx/sbin/nginx -V
nginx version: nginx/1.17.8
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)
configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module
五,两台tracker上,配置并启动fastdfs
1,创建tracker服务所需的目录
[root@localhost fdfs]# mkdir /data/fastdfs
[root@localhost fdfs]# mkdir /data/fastdfs/tracker
[root@localhost fdfs]# chmod 777 /data/fastdfs/tracker
2,配置tracker服务,
修改 tracker.conf 文件
[root@localhost fdfs]# vi /etc/fdfs/tracker.conf
只修改base_path一项的值为我们在上面所创建的目录即可
base_path = /data/fastdfs/tracker
3,启动 tracker 服务
[root@localhost fdfs]# /etc/init.d/fdfs_trackerd start
4,检查tracker服务启动是否成功
[root@localhost fdfs]# ps auxfww | grep fdfs
root 15067 0.0 0.0 12320 964 pts/0 S+ 15:14 0:00 | | \_ grep --color=auto fdfs
root 15026 0.0 0.1 90160 5940 ? Sl 15:13 0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
说明:能看到 fdfs_trackerd,表示tracker服务启动成功
5,检查tracker服务是否已绑定端口 22122?
[root@localhost dest]# netstat -anp | grep 22122
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 15026/fdfs_trackerd
说明: 22122端口是在/etc/fdfs/tracker.conf中定义的,
如下:
# the tracker server port
port = 22122
六,两台tracker上,配置client.conf,配置fastdfs的客户端使用的配置文件
1,配置client.conf
[root@localhost fdfs]# vi /etc/fdfs/client.conf
以下两项配置用到的tracker目录和服务器地址端口
base_path = /data/fastdfs/tracker
tracker_server = 172.17.0.2:22122
tracker_server = 172.17.0.3:22122
说明:两台tracker上的client.conf配置相同
2,从客户端的配置可以看到:客户端只需要了解tracker_server的信息
Tracker server作用也正是负载均衡和调度
3, Storage server作用是文件存储,客户端上传的文件最终存储在 Storage 服务上
七,用client.conf上传文件测试
说明:这一步等待四台storage server配置完成后再进行
1,从tacker上传一个文件
[root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt
group1/M00/00/00/rBEABF5aTEeAXHF4AAAABHf4XZU792.txt
注意返回的是group1
我们应该可以group1下面的两台机器均找到此txt文件:
storage1上
[root@d5d19e99e782 docker_tmp]# ls /data/fastdfs/storage/data/00/00
rBEABF5aTEeAXHF4AAAABHf4XZU792.txt
storage2上
[root@f201111d0698 docker_tmp]# ls /data/fastdfs/storage/data/00/00
rBEABF5aTEeAXHF4AAAABHf4XZU792.txt
2,如果想指定上传到某个group怎么办?例如:指定上传到group2
[root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt 172.17.0.6:23000
group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt
说明:指定group2中任一台的ip和端口即可
查看效果:
storage3上:
[root@494ac47d63f8 fdfs]# ls /data/fastdfs/storage/data/00/00
rBEABl5aUAqAXLCZAAAABHf4XZU043.txt
storage4上
[root@59fa1efff362 fdfs]# ls /data/fastdfs/storage/data/00/00
rBEABl5aUAqAXLCZAAAABHf4XZU043.txt
八,两台tracker上,配置并启动nginx
说明:这一步等待四台storage server配置完成后再进行
使用nginx做upstream负载均衡的原因:可以通过一个地址访问后端的多个group
1,文件上传完成后,从浏览器访问各个storage的nginx即可:
例如:
http://172.17.0.4/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt
http://172.17.0.5/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt
http://172.17.0.6/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt
http://172.17.0.7/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt
说明:各台storage server的ip地址后面跟着上传时所返回的地址
注意:只能访问各台机器所在的group,
如果想通过统一的ip地址进行访问,
需要在nginx中通过upstream访问到后端的机器
此nginx应运行在tracker上
2,配置nginx.conf
[root@0268c2dc2bf6 ~]# vi /usr/local/soft/nginx/conf/nginx.conf
内容:
添加 upstream到后端的storage
upstream fdfs_group1 {
server 172.17.0.4:80 weight=1 max_fails=2 fail_timeout=30s;
server 172.17.0.5:80 weight=1 max_fails=2 fail_timeout=30s;
} upstream fdfs_group2 {
server 172.17.0.6:80 weight=1 max_fails=2 fail_timeout=30s;
server 172.17.0.7:80 weight=1 max_fails=2 fail_timeout=30s;
}
针对带有group的url进行处理
location /group1 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group1;
expires 30d;
} location /group2 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group2;
expires 30d; }
3,重启服务后测试:
[root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx -s stop
[root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx
在浏览器中访问:
http://172.17.0.2/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt
http://172.17.0.2/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt