一、简介
- FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。
- FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。
1.1、FastDFS的两个核心概念分别是:Tracker(跟踪器)、Storage(存储节点)
跟踪器Tracker主要做调度工作,相当于mvc中的controller的角色,在访问上起负载均衡的作用。跟踪器和存储节点都可以由一台或多台服务器构成,跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务,其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
跟踪器Tracker负责管理所有的Storage和group,每个Storage在启动后会连接Tracker,
二、tracker与storage安装
2.1、开发环境安装
[root@localhost ~]# cd /usr/local/
[root@localhost local]# yum groupinstall "Development Tools" "Server platform Development"
- 进入/usr/local:是为了将软件源码下载到此文件夹
2.2、libfastcommon下载并安装
[root@localhost local]# git clone https://github.com/happyfish100/libfastcommon.git
[root@localhost local]# cd libfastcommon/
[root@localhost libfastcommon]# ./make.sh
[root@localhost libfastcommon]# ./make.sh install
2.3、fastdfs下载并安装
[root@localhost local]# git clone https://github.com/happyfish100/fastdfs.git
[root@localhost local]# cd fastdfs/
[root@localhost fastdfs]# ./make.sh
[root@localhost fastdfs]# ./make.sh install
2.4、环境配置
2.4.1、创建文件夹
[root@localhost local]# mkdir -p /home/fdfs/tracker
[root@localhost local]# mkdir -p /home/fdfs/storage
[root@localhost local]# mkdir -p /home/fdfs/client
- 创建的文件夹用于在配置时使用
2.4.2、复制配置文件
[root@localhost libfastcommon]# cd /etc/fdfs/
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# ls
client.conf http.conf mod_fastdfs.conf storage.conf.sample tracker.conf
client.conf.sample mime.types storage.conf storage_ids.conf.sample tracker.conf.sample
2.5、修改tracker配置文件
- 文件目录:/etc/fdfs/
[root@localhost fdfs]# vim tracker.conf
- 修改内容:
# the base path to store data and log files
base_path=/home/fdfs/tracker
- base_path:配置tracker的log文件夹
2.5.1、启动tracker:
[root@localhost fdfs]# /etc/init.d/fdfs_trackerd start
2.6、配置storage
- 文件目录:/etc/fdfs/
[root@localhost fdfs]# vim storage.conf
- 修改内容
# the base path to store data and log files
base_path=/home/fdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/fdfs/storage/m0
store_path1=/home/fdfs/storage/m1
#store_path1=/home/yuqing/fastdfs2
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.58.215:22122
- base_path:指定tracker的log文件夹路径
- store_path0:指定文件的存储路径
- store_path1:指定文件的存储路径
- tracker_server:当前虚拟机对应的ip地址
2.6.1、启动tracker
/etc/init.d/fdfs_storaged start
2.7、配置client
- 文件目录:/etc/fdfs/
[root@localhost fdfs]# vim client.conf
- 修改内容:
# the base path to store log files
base_path=/home/fdfs/client
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.58.215:22122
- base_path:指定client对应的log文件夹地址
- tracker_server:当前虚拟机对应ip地址
2.8、测试上传
[root@localhost local]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/timg.jpg
group1/M01/00/00/wKg611s8l26AM9LFAAAjE0YaL3A308.jpg
[root@localhost local]#
- 说明:
- /usr/bin/fdfs_upload_file:执行文件上传命令
- /etc/fdfs/client.conf:执行上传文件对应的配置文件
- /usr/local/timg.jpg:上传的文件
- group1/M01/00/00/wKg611s8l26AM9LFAAAjE0YaL3A308.jpg:文件上传成功返回的文件id
三、配置网络
3.1、nginx插件配置
3.1.1、nginx插件下载
[root@localhost local]# git clone https://github.com/happyfish100/fastdfs-nginx-module
3.1.2、配置插件
[root@localhost local]# cd fastdfs-nginx-module/src/
[root@localhost local]# mod_fastdfs.conf /etc/fdfs/
3.1.3、修改配置文件
[root@localhost local]# vim /etc/fdfs/mod_fastdfs.conf
- 修改内容
# FastDFS tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.58.215:22122
# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
# path(disk or mount point) count, default value is 1
# must same as storage.conf
store_path_count=2
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/fdfs/storage/m0
store_path1=/home/fdfs/storage/m1
- 说明
- tracker_server:当前虚拟机对应的ip地址
- store_path_count:对应的设备数量
- store_path0:文件路径
3.2、安装nginx
3.2.1、nginx依赖库及nginx下载
[root@localhost local]# yum -y install pcre-devel zlib-devel
[root@localhost local]# yum -y install openssl openssl-devel
[root@localhost local]# wget http://nginx.org/download/nginx-1.8.1.tar.gz
[root@localhost local]# tar xf nginx-1.8.1.tar.gz
3.2.2、安装nginx
[root@localhost nginx-1.8.1]# cd nginx-1.8.1
[root@localhost nginx-1.8.1]# ./configure --prefix=/application/nginx/ --add-module=../fastdfs-nginx-module/src/
[root@localhost nginx-1.8.1]# make
[root@localhost nginx-1.8.1]# make install
- 说明
- prefix=/application/nginx/:软件的安装地址
- add-module=../fastdfs-nginx-module/src/:添加的插件地址(fastdfs-nginx-module地址为当前虚拟机中的位置,地址不对在make会出现错误)
3.2.3、配置nginx
vim /application/nginx/conf/nginx.conf
- 修改内容:
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4.4、复制fast中的配置文件
[root@localhost ~]# cd /usr/local/fastdfs/conf/
[root@localhost conf]# http.conf /etc/fdfs/
[root@localhost conf]# mime.types /etc/fdfs/
4.5、启动nginx配置
[root@localhost conf]# cp /application/nginx/sbin/nginx /etc/init.d/
[root@localhost conf]# /etc/init.d/nginx
- 将nginx的启动文件复制到启动文件夹
4.6、测试图片访问
[root@localhost conf]# cd /usr/local/
[root@localhost local]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/timg.jpg
group1/M00/00/00/wKg611s8mjqAERqfAAAjE0YaL3A708.jpg
4.7、配置防火墙端口:
- 端口打开:
[root@bogon local]# firewall-cmd --permanent --zone=public --add-port=8081/tcp
success
[root@bogon local]# firewall-cmd --reload
success