IDC

面试官:如何实现文件的大规模分布式存储?

作者:admin 2021-04-21 我要评论

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

 

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址:

https://github.com/sunshinelyz/mykit-delay

写在前面

在生产环境中,往往需要对FastDFS实现高可用,进而实现文件的大规模分布式存储。那么,如何实现FastDFS的高可用呢?今天,我们就基于CentOS 8服务器来搭建FastDFS高可用集群环境。

服务器版本

我们在服务器的命令行输入如下命令来查看服务器的内核版本。

  1. [root@localhost lib]# cat /etc/redhat-release  
  2. CentOS Linux release 8.1.1911 (Core)  

可以看到,集群中每台服务器的内核版本为:release 8.1.1911 (Core)。

服务器规划

这里,我们总共规划了6台服务器,分别为:2台tracker服务器,4台storage服务器,其中2台storage服务器为group1,两台storage服务器为group2。具体如下所示。

  • tracker服务器

tranker1:192.168.175.101

tracker2:192.168.175.102

  • storage服务器

storage1:192.168.175.103 group1

storage2:192.168.175.104 group1

storage3:192.168.175.105 group2

storage4:192.168.175.106 group2

环境准备

下载FastDFS

在每台服务器上执行如下命令下载FastDFS。

  1. [root@localhost source]# wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz 
  2. [root@localhost source]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz 
  3. [root@localhost source]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz 

安装环境依赖

在每台服务器上执行如下命令安装FastDFS所依赖的环境。

  1. [root@localhost dest]# yum install gcc gcc-c++ 
  2. [root@localhost dest]# yum install libtool zlib zlib-devel openssl openssl-devel 
  3. [root@localhost dest]# yum -y install pcre pcre-devel libevent libevent-devel perl unzip net-tools wget 

安装FastDFS

安装libfastcommon

在每台服务器上依次执行如下命令。

(1)解压libfastcommon的压缩包

  1. [root@localhost source]# tar -zxvf V1.0.43.tar.gz 

(2)编译并安装编译并安装

  1. [root@localhost source]# cd libfastcommon-1.0.43/ 
  2. [root@localhost libfastcommon-1.0.43]# ./make.sh && ./make.sh install 

(3)检查执行的结果,看安装是否成功

  1. [root@localhost libfastcommon-1.0.43]# ls /usr/lib64|grep libfastcommon 
  2. libfastcommon.so 
  3.  
  4. [root@localhost libfastcommon-1.0.43]# ls /usr/lib|grep libfastcommon 
  5. libfastcommon.so 

安装fastdfs

在每台服务器上依次执行如下命令。

(1)解压fastdfs

  1. [root@localhost source]# tar -zxvf V6.06.tar.gz 

(2)安装fastdfs

  1. [root@localhost source]# cd fastdfs-6.06/ 
  2. [root@localhost fastdfs-6.06]# ./make.sh && ./make.sh install 

(3)检查fastdfs是否安装成功

  1. [root@localhost fastdfs-6.06]# ls /usr/bin|grep fdfs 
  2. fdfs_appender_test 
  3. fdfs_appender_test1 
  4. fdfs_append_file 
  5. fdfs_crc32 
  6. fdfs_delete_file 
  7. fdfs_download_file 
  8. fdfs_file_info 
  9. fdfs_monitor 
  10. fdfs_regenerate_filename 
  11. fdfs_storaged 
  12. fdfs_test 
  13. fdfs_test1 
  14. fdfs_trackerd 
  15. fdfs_upload_appender 
  16. fdfs_upload_file 

安装部署tracker服务

复制tracker的配置文件

在两台tracker服务器上,依次执行如下命令。

  1. [root@localhost fastdfs-6.06]# cd /etc/fdfs/ 
  2. [root@localhost fdfs]# cp client.conf.sample client.conf 
  3. [root@localhost fdfs]# cp tracker.conf.sample tracker.conf 

注意:无须生成storage.conf文件,这两台tracker不做为storage。

安装Nginx

在两台tracker服务器上,依次执行如下命令。

注意:tracker上不需要安装fastdfs-nginx-module

(1)解压Nginx

  1. [root@localhost source]# tar -zxvf nginx-1.17.8.tar.gz 

(2)nginx配置,http_stub_status_module 模块

  1. [root@localhost fdfs]# cd /usr/local/source/nginx-1.17.8/ 
  2. [root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module  

(3)编译安装Nginx

  1. [root@localhost nginx-1.17.8]# make && make install 

(4)检查安装是否成功

  1. [root@localhost nginx-1.17.8]# ls /usr/local/soft/ | grep nginx 
  2. nginx 

(5)查看指定的编译参数是否起作用

  1. [root@localhost fdfs]# /usr/local/soft/nginx/sbin/nginx -V 
  2. nginx version: nginx/1.17.8 
  3. built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)  
  4. configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module  

配置并启动FastDFS

在两台tracker上,配置并启动FastDFS。

(1)创建tracker服务所需的目录

  1. [root@localhost fdfs]# mkdir /data/fastdfs 
  2. [root@localhost fdfs]# mkdir /data/fastdfs/tracker 
  3. [root@localhost fdfs]# chmod 777 /data/fastdfs/tracker 

(2)配置tracker服务,修改 tracker.conf 文件

  1. [root@localhost fdfs]# vi /etc/fdfs/tracker.conf 

只修改base_path一项的值为我们在上面所创建的目录即可

  1. base_path = /data/fastdfs/tracker 

(3)启动 tracker 服务

  1. [root@localhost fdfs]# /etc/init.d/fdfs_trackerd start 

(4)检查tracker服务启动是否成功

  1. [root@localhost fdfs]# ps auxfww | grep fdfs 
  2. root      15067  0.0  0.0  12320   964 pts/0    S+   15:14   0:00  |   |           \_ grep --color=auto fdfs 
  3. 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

  1. [root@localhost dest]# netstat -anp | grep 22122 
  2. tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      15026/fdfs_trackerd  

说明:22122端口是在/etc/fdfs/tracker.conf中定义的,如下所示:

  1. # the tracker server port 
  2. port = 22122 

配置client.conf

两台tracker上,配置client.conf,配置fastdfs的客户端使用的配置文件。

(1)配置client.conf

  1. [root@localhost fdfs]# vi /etc/fdfs/client.conf 

以下两项配置用到的tracker目录和服务器地址端口

  1. base_path = /data/fastdfs/tracker 
  2. tracker_server = 192.168.175.101:22122 
  3. tracker_server = 192.168.175.102:22122 

说明:两台tracker上的client.conf配置相同

(2)从客户端的配置可以看到:客户端只需要了解tracker_server的信息。tracker server作用也正是负载均衡和调度

(3)Storage server作用是文件存储,客户端上传的文件最终存储在 Storage 服务上

测试文件

用client.conf上传文件测试。

(1)从tacker上传一个文件

  1. [root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt             

注意返回的是group1,我们可以group1下面的两台机器均找到此txt文件:

  • storage1上
  1. [root@d5d19e99e782 docker_tmp]# ls /data/fastdfs/storage/data/00/00 
  2. rBEABF5aTEeAXHF4AAAABHf4XZU792.txt 
  • storage2上
  1. [root@f201111d0698 docker_tmp]# ls /data/fastdfs/storage/data/00/00 
  2. rBEABF5aTEeAXHF4AAAABHf4XZU792.txt 

(2)指定group上传文件

如果想指定上传到某个group怎么办?例如:指定上传到group2

  1. [root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt 192.168.175.105:23000 
  2. group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 

说明:指定group2中任一台的ip和端口即可。

(3)查看效果

  • storage3上
  1. [root@494ac47d63f8 fdfs]# ls /data/fastdfs/storage/data/00/00 
  2. rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 
  • storage4上
  1. [root@59fa1efff362 fdfs]# ls /data/fastdfs/storage/data/00/00 
  2. rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 

安装部署storage服务

生成默认配置文件

四台storage上:生成启动fastdfs默认的配置文件。

  1. [root@localhost fastdfs-6.06]# cd /etc/fdfs/ 
  2. [root@localhost fdfs]# cp storage.conf.sample storage.conf 
  3. [root@localhost fdfs]# cp client.conf.sample client.conf 

说明:不需要生成tracker.conf,因为storage上不再运行tracker服务

安装Nginx

四台storage上:安装nginx及fastdfs-nginx-module

(1)解压nginx

  1. [root@localhost source]# tar -zxvf nginx-1.17.8.tar.gz 

(2)解压fastdfs-nginx-module

  1. [root@localhost source]# tar -zxvf V1.22.tar.gz 

(3)修改config文件,把/usr/local 替换成 /usr

  1. [root@localhost source]# cd fastdfs-nginx-module-1.22/ 
  2. [root@localhost fastdfs-nginx-module-1.22]# cd src 
  3. [root@localhost src]# vi config 

(4)Nginx配置,添加fastdfs-nginx-module和http_stub_status_module 模块

  1. [root@localhost fdfs]# cd /usr/local/source/nginx-1.17.8/ 
  2. [root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/ 

(5)编译安装nginx

  1. [root@localhost nginx-1.17.8]# make && make install 

(6)检查安装是否成功

  1. [root@localhost nginx-1.17.8]# ls /usr/local/soft/ | grep nginx 
  2. nginx 

(7)查看指定的编译参数是否起作用

  1. [root@localhost fdfs]# /usr/local/soft/nginx/sbin/nginx -V 
  2. nginx version: nginx/1.17.8 
  3. built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)  
  4. configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/ 

配置并启动storage服务

四台storage上:配置并启动storage服务

(1)创建storage服务所需的目录

  1. [root@localhost fdfs]# mkdir /data/fastdfs/storage 
  2. [root@localhost fdfs]# chmod 777 /data/fastdfs/storage/ 

(2)配置storage服务

编辑storage的配置文件:

  1. [root@localhost fdfs]# vi /etc/fdfs/storage.conf 

各配置项包括:

  1. group_name = group1 
  2. #配置base_path为上面所创建的storage目录 
  3. base_path = /data/fastdfs/storage 
  4. #store_path :存储所在的目录,可以设置多个,注意从0开始 
  5. store_path0 = /data/fastdfs/storage 
  6. #tracker_server的ip和端口 
  7. tracker_server = 192.168.175.101:22122 
  8. tracker_server = 192.168.175.102:22122  
  9. #指定http服务的端口 
  10. http.server_port = 80 

配置的不同之处:

  1. 192.168.175.103   group_name = group1 
  2. 192.168.175.104   group_name = group1 
  3. 192.168.175.105   group_name = group2 
  4. 192.168.175.106   group_name = group2 

(3)启动storage服务

  1. [root@localhost fdfs]# /etc/init.d/fdfs_storaged start 
  2. 正在启动 fdfs_storaged (via systemctl):                   [  确定  ] 

(4)检查storage服务启动是否成功

  1. [root@localhost fdfs]# ps auxfww | grep fdfs 
  2. root      15630  0.0  0.0  12320   972 pts/0    S+   15:46   0:00  |   |           \_ grep --color=auto fdfs 
  3. root      15026  0.0  0.1 155696  6964 ?        Sl   15:13   0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf 
  4. root      15573  2.7  1.7 150736 66292 ?        Sl   15:45   0:02 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf 

说明:看到fdfs_storaged,表示storage服务启动成功

(5)检查storage服务是否已绑定到端口:23000

  1. [root@localhost fdfs]# netstat -anp | grep 23000 
  2. tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      15573/fdfs_storaged  

说明:23000 端口是在配置文件 /etc/fdfs/storage.conf中定义的,如下:

  1. # the storage server port 
  2. port = 23000 

配置fastdfs-nginx-module

四台存储服务器上:配置fastdfs-nginx-module

(1)生成配置文件

  1. [root@localhost nginx-1.17.8]# cp /usr/local/source/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/ 

(2)编辑配置文件

  1. [root@localhost nginx-1.17.8]# vi /etc/fdfs/mod_fastdfs.conf 

配置以下几项

  1. group_name=group1 
  2. connect_timeout=10 
  3. tracker_server=192.168.175.101:22122 
  4. tracker_server=192.168.175.102:22122 
  5. url_have_group_name = true 
  6. store_path0=/data/fastdfs/storage 
  7. group_count = 2 
  8.  
  9. [group1] 
  10.  
  11. group_name=group1 
  12. storage_server_port=23000 
  13. store_path_count=1 
  14. store_path0=/data/fastdfs/storage 
  15.  
  16. [group2] 
  17.  
  18. group_name=group2 
  19. storage_server_port=23000 
  20. store_path_count=1 
  21. store_path0=/data/fastdfs/storage 

说明:最上面的group_name:当机器属于group1这组时,值为group1;当机器属于group2这组时,值为group2。

说明:url_have_group_name = true。注意:这一项不要漏掉,会导致nginx不正常工作

(3)复制另两个web访问用到配置文件到fdfs配置目录下:

  1. [root@d5d19e99e782 /]# cp /usr/local/source/fastdfs-6.06/conf/http.conf /etc/fdfs/ 
  2. [root@d5d19e99e782 /]# cp /usr/local/source/fastdfs-6.06/conf/mime.types /etc/fdfs/ 

配置Nginx

四台存储服务器上:配置nginx

编辑nginx的配置文件:

  1. [root@localhost conf]# vi /usr/local/soft/nginx/conf/nginx.conf 

在server listen 80 的这个server配置下面,

增加一个location

  1. location ~/group([0-9]) { 
  2.     root  /data/fastdfs/storage/data; 
  3.     ngx_fastdfs_module; 
  4.  

启动nginx

(1)启动Nginx

  1. [root@localhost storage]# /usr/local/soft/nginx/sbin/nginx 

(2)检查nginx是否已成功启动

  1. [root@localhost storage]# ps auxfww | grep nginx 
  2. root      24590  0.0  0.0  12320   980 pts/0    S+   16:44   0:00  |   |           \_ grep --color=auto nginx 
  3. root      24568  0.0  0.0  41044   428 ?        Ss   16:44   0:00  \_ nginx: master process /usr/local/soft/nginx/sbin/nginx 
  4. nobody    24569  0.0  0.1  74516  4940 ?        S    16:44   0:00      \_ nginx: worker process 

配置tracker服务

配置tracker服务

说明:这一步等待四台storage server配置完成后再进行。使用n=Nginx做upstream负载均衡的原因:可以通过一个地址访问后端的多个group

(1)文件上传完成后,从浏览器访问各个storage的Nginx即可:

例如:

  1. http://192.168.175.103/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt 
  2. http://192.168.175.104/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt 
  3. http://192.168.175.105/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 
  4. http://192.168.175.106/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 

说明:各台storage server的ip地址后面跟着上传时所返回的地址。注意:只能访问各台机器所在的group,

  • 如果想通过统一的ip地址进行访问
  • 需要在Nginx中通过upstream访问到后端的机器
  • 此Nginx应运行在tracker上

(2)配置nginx.conf

  1. [root@0268c2dc2bf6 ~]# vi /usr/local/soft/nginx/conf/nginx.conf 

内容:

添加 upstream到后端的storage。

  1. upstream fdfs_group1 { 
  2.                 server 192.168.175.103:80 weight=1  max_fails=2 fail_timeout=30s; 
  3.                 server 192.168.175.104:80 weight=1  max_fails=2 fail_timeout=30s; 
  4.    } 
  5.  
  6.    upstream fdfs_group2 { 
  7.                 server 192.168.175.105:80 weight=1 max_fails=2 fail_timeout=30s; 
  8.                 server 192.168.175.106:80 weight=1 max_fails=2 fail_timeout=30s; 
  9.    } 

针对带有group的url进行处理

  1. location /group1 { 
  2.     proxy_next_upstream http_502 http_504 error timeout invalid_header; 
  3.     proxy_pass http://fdfs_group1; 
  4.     expires 30d; 
  5.   
  6.  location /group2 { 
  7.      proxy_next_upstream http_502 http_504 error timeout invalid_header; 
  8.      proxy_pass http://fdfs_group2; 
  9.      expires 30d; 
  10.  

(3)重启测试

  1. [root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx -s stop 
  2. [root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx 

在浏览器中访问:

  1. http://192.168.175.101/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt 
  2. http://192.168.175.101/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 

本文转载自微信公众号「冰河技术」,可以通过以下二维码关注。转载本文请联系冰河技术公众号。 


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/x7LO_DGiBGXgGr2vC53oUw

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 面试官:如何实现文件的大规模分布式存

    面试官:如何实现文件的大规模分布式存

  • 潮数推出构建数据库安全防线全面解决方

    潮数推出构建数据库安全防线全面解决方

  • 程序员修神之路--缓存架构不够好,系统

    程序员修神之路--缓存架构不够好,系统

  • 如何应对云存储与SAN的挑战

    如何应对云存储与SAN的挑战

腾讯云代理商
海外云服务器