Linux学习笔记---Fastdfs安装配置介绍总结

FastDFS简介

1FastDFS是一个轻量级的开源分布式文件系统

2FastDFS主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡

3FastDFS实现了软件方式的RAID,可以使用廉价的IDE硬盘进行存储

4、支持存储服务器在线扩容

5、支持相同内容的文件只保存一份,节约磁盘空间

6FastDFS只能通过Client API访问,不支持POSIX访问方式

7FastDFS特别适合大中型网站使用,用来存储资源文件(如:图片、文档、音频、视频等等)

    FastDFS是一个开源的轻量级分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

FastDFS是由C语言编写,轻量级开源的分布式文件系统,在跨境通等B2C商城的项目之中作为图片服务器使用,用来存储商家的Logo,商品的图片等图片资源,github地址: https://github.com/happyfish100/fastdfs

 

FastDFS中有三个角色.Tracker Storage Client.见名知意

Tracker:

翻译过来是追踪者,本身并不是作为存储的角色,而是作为调度者的角色而存在的,也有负载均衡的理念在里面.Tracker管理所有的StorageGroup.

Storage:

Storage是存储的角色,每个Storage启动时会连接Tracker告知自身所属的Group并且报纸周期心跳Tracker则以此来建立GroupStorage的映射信息.同一个Group中的Storage中内容相同,互为备份.GroupStorage的容量以当前组中最小的为准,所以配置的时候要注意最好是设置成相同的大小,避免空间浪费.

Client:

 这个就不都说了,一看名字就知道是客户端.和Redis之类的中间件一样都需要一个客户端来访问.

说了那么多 画一下FastDFS的架构图吧.

如图所示Client访问Tracker,请求对应的文件地址,Tracker中包含了Storage所属的分组信息,并且通过对应的分组去访问分组中的某个Storage获取到对应的文件信息.

TrackerStorage都可以搭建集群,这样带来的好处是,可以避免单点故障,让服务可以可靠地提供.Group众多的情况下可以提升系统的QPS.特别适合中小型文件的存储,一般在4KB到500MB之间.

 

FastDFS对外提供文件的访问接口,如upload,download,append,delete等,通过客户端库的方式提供.

 

文件上传概述:

文件上传分为选择Tracker,选择Group,选择Storage,生成Field,选择两级目录,生成文件名这么几个步骤.现在互联网项目,为了保证服务可靠性,都会搭建集群,所以按照这种方式来总结.

选择Tracker:

Tracker集群中所有的Tracker地位都是对等的,客户端上传文件时会任意选择一个Tracker.

选择Group:

Tracker收到上传请求之后,会分配一个Group来存储文件,提供的规则有:轮询所有的Group,指定一个Group,负载均衡(剩余空间多的优先)

选择Storage:

分配好Group之后,Tracker会在Group中选择一个Storage,提供的规则有:轮询所有的Storage,根据ip排序,根据Storage优先级排序. 在选定好了Storage之后客户端向Storage发送写入文件请求,Storage为文件分配一个数据存储目录,提供的规则有:存储目录轮询,负载均衡

生成Field:

选择好存储目录之后,Storage为文件分配一个Field,由Storage的ip + 文件创建的时间戳 + 文件大小 + 文件crc32校验后+一个随机数拼接而成.再将这个二进制串进行Base64编码转换成String.

选择两级目录:生成field之后,每个存储目录下会有两级256*256的子目录,Storage会按照field进行第一次hash,路由到第一级子目录,再进行第二次hash,存储到对应的子目录下.

生成文件名:

当文件存储完成后,会为其创建一个文件名,创建规则为:Group + 存储目录 + 两级子目录 + field + 文件拓展名拼接而成

案例实践:

目的:当Client(客户端)访问Tracker, (上传或下载)请求获取对应的图片或文件地址,Tracker中包含了Storage所属的分组信息,并且通过对应的分组去访问分组中的某个Storage获取到对应的文件信息. 主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡的问题。

实验环境:

系统及IP地址

所需安装的软件

Centos7.5      192.168.1.10

Tracker server1

Centos7.5      192.168.1.20

Tracker server2

Centos7.5      192.168.1.30

Nginx proxy     client

Centos7.5      192.168.1.40

storage server group1

Centos7.5      192.168.1.50

storage server group2

创建数据存储位置  /storage/fastdfs

安装编译环境(所有节点)

 

1mkdir  /storage/fastdfs –p

2yum -y install git gcc gcc-c++ make autoconf automake libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim

安装libfastcommon (所有节点)

Libfastcommon包含了fastdfs运行所需的要的一些基础库

[root@localhost ~]# tar  zxf  libfastcommon.tar.gz  

[root@localhost ~]# cd  libfastcommon/

[root@localhost libfastcommon]# ./make.sh   && ./make.sh install

安装Fastdfs (所有节点)

[root@localhost ~]#  tar  zxf  fastdfs.tar.gz

[root@localhost ~]#  cd  fastdfs/

[root@localhost fastdfs]#  ./make.sh   && ./make.sh   install

[root@localhost fastdfs]#  cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

[root@localhost fastdfs]# cp /etc/fdfs/storage.conf.sample  /etc/fdfs/storage.conf

[root@localhost fastdfs]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

[root@localhost fastdfs]# cp   conf/http.conf     /etc/fdfs/

[root@localhost fastdfs]# cp conf/mime.types   /etc/fdfs/

部署安装storage server

安装nginx

[root@localhost ~]# tar zxf   fastdfs-nginx-module.tar.gz

[root@localhost ~]# cd fastdfs-nginx-module/

[root@localhost fastdfs-nginx-module]# cp src/mod_fastdfs.conf  /etc/fdfs/

[root@localhost ~]#  useradd  nginx

[root@localhost ~]#  tar  zxf  nginx-1.14.0.tar.gz  

[root@localhost ~]# cd nginx-1.14.0/  

[root@localhost nginx-1.14.0]# ./configure   --add-module=/root/fastdfs-nginx-module/src

[root@localhost nginx-1.14.0]# make  && make install 

配置tracker server(1.10and1.20除个别配置不同外)

1.10 root@localhost ~]# vim /etc/fdfs/tracker.conf

bind_addr=192.168.1.10    #trackerIP地址

port=22122   #工作端口号

base_path=/storage/fastdfs/  #数据和日志路径

1.20 root@localhost ~]# vim /etc/fdfs/tracker.conf

bind_addr=192.168.1.20   #trackerIP地址

port=22122   #工作端口号

base_path=/storage/fastdfs/  #数据和日志路径

配置storage  server1.40and1.50上更改配置文件)

[root@localhost ~]# vim  /etc/fdfs/storage.conf

    group_name=group1   第一台storage为group1  第二台storage为group2

bind_addr=192.168.1.40

port=23000  #默认端口,不做修改

base_path=/storage/fastdfs/  #数据和日志目录地址

store_path0=/storage/fastdfs/  #第一个存储目录和basr_path路径相同

tracker_server=192.168.1.10:22122

tracker_server=192.168.1.20:22122

http.server_port=8888 #http访问文件的端口

 

[root@localhost ~]# vim  /etc/fdfs/mod_fastdfs.conf

base_path=/storage/fastdfs/tracker_server=192.168.1.10:22122tracker_server=192.168.1.20:22122storage_server_port=23000group_name=group1  #第二台storage组名为group2url_have_group_name = true  #group有多个的时候需要改为true,以组名去访问store_path0=/storage/fastdfs/group_count = 2[group1]group_name=group1storage_server_port=23000store_path_count=1store_path0=/storage/fastdfs/[group2]group_name=group2storage_server_port=23000store_path_count=1store_path0=/storage/fastdfs/

Nginx主配置文件修改1.40 and1.50都需要做

[root@localhost ~]# vim  /usr/local/nginx/conf/nginx.conf

在原来的server上添加

   server {

        listen    8888;

        server_name localhost;

        location ~/group[0-9]/M00/ {

                 ngx_fastdfs_module;

        }

}

启动服务

启动tracker server(两台都要启动)

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

 

 

启动storage server

[root@localhost ~]# /usr/local/nginx/sbin/nginx

[root@localhost ~]# /etc/init.d/fdfs_storaged  start

 

 

Nginx代理配置:(在1.30服务器上先安装nginx反向代理)

[root@localhost nginx-1.15.4]# vim /usr/local/nginx/conf/nginx.conf    upstream fdfs_group1 { #设置group1的服务器      server 192.168.1.40:8888 weight=1 max_fails=2 fail_timeout=30s;    }    upstream fdfs_group2 {
    server 192.168.1.50:8888 weight=1 max_fails=2 fail_timeout=30s;    }#下边添加两个locationlocation ~ /group1 {           proxy_pass http://fdfs_group1;        }        location ~ /group2 {            proxy_pass http://fdfs_group2;       }启动nginx[root@localhost nginx-1.15.4]# /usr/local/nginx/sbin/nginx 
client测试(用proxy主机测试)
[root@localhost nginx-1.15.4]# vim /etc/fdfs/client.conf
base_path=/storage/fastdfs
tracker_server=192.168.1.10:22122
tracker_server=192.168.1.20:22122

 

上传一张照片:

 

[root@localhost ~]# fdfs_upload_file  /etc/fdfs/client.conf   1234.jpg  

group2/M00/00/00/wKgBMl2fBk2AYtenAABpddRbZZY691.jpg

[root@localhost ~]# fdfs_upload_file  /etc/fdfs/client.conf  66.jpeg

group2/M00/00/00/wKgBMl2fBmaAQfWuAAGFe_SFgSA74.jpeg

访问图片

 

 

storage服务器上可以查看到图片 位置:(1.50

[root@localhost ~]# cd /storage/fastdfs/

[root@localhost fastdfs]# cd data/

[root@localhost data]# cd 00

[root@localhost 00]# cd 00

[root@localhost 00]# ls

wKgBMl2e9AWAD3C4AABpddRbZZY131.jpg

wKgBMl2fBk2AYtenAABpddRbZZY691.jpg

文件下载(客户端上)

[root@localhost ~]# fdfs_download_file  /etc/fdfs/client.conf group2/M00/00/00/wKgBMl2fBmaAQfWuAAGFe_SFgSA74.jpeg xiazai.png

文件删除:

[root@localhost ~]# fdfs_delete_file    /etc/fdfs/client.conf group2/M00/00/00/wKgBMl2fBk2AYtenAABpddRbZZY691.jpg

 

 

以下是Linux FastDFS安装配置的步骤: 1.下载FastDFS安装包并解压缩: ```shell wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz tar -zxvf V5.11.tar.gz ``` 2.安装libfastcommon: ```shell cd fastdfs-5.11/libfastcommon/ ./make.sh ./make.sh install ``` 3.安装FastDFS: ```shell cd ../fastdfs/ ./make.sh ./make.sh install ``` 4.配置Tracker服务器: ```shell cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf vi /etc/fdfs/tracker.conf ``` 在tracker.conf中修改base_path和http.server_port参数,例如: ``` base_path=/data/fastdfs/tracker http.server_port=8080 ``` 5.配置Storage服务器: ```shell cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf vi /etc/fdfs/storage.conf ``` 在storage.conf中修改base_path、store_path0、tracker_server参数,例如: ``` base_path=/data/fastdfs/storage store_path0=/data/fastdfs/storage tracker_server=192.168.36.100:22122 tracker_server=192.168.36.103:22122 ``` 6.配置客户端: ```shell cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf vi /etc/fdfs/client.conf ``` 在client.conf中修改base_path和tracker_server参数,例如: ``` base_path=/data/fastdfs/client tracker_server=192.168.36.100:22122 tracker_server=192.168.36.103:22122 ``` 7.配置Nginx: ```shell cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak vi /usr/local/nginx/conf/nginx.conf ``` 在http节点下添加如下配置: ``` location /group1/M00 { root /data/fastdfs/storage/data; ngx_fastdfs_module; } ``` 8.启动Tracker和Storage服务器: ```shell /etc/init.d/fdfs_trackerd start /etc/init.d/fdfs_storaged start ``` 9.测试FastDFS: ```shell echo "Hello, FastDFS!" > test.txt /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.txt ``` 以上是Linux FastDFS安装配置的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值