一、FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。python
二、FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。nginx
三、FastDFS服务端有两个角色:c++
(1)跟踪器(tracker):跟踪器主要作调度工做;
(2)存储节点(storage):在访问上起负载均衡的做用。git
存储节点存储文件,完成文件管理的全部功能:就是这样的存储、同步和提供存取接口,FastDFS同时对文件的metadata进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value)方式表示。github
四、为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,全部卷的文件容量累加就是整个存储系统中的文件容量。一个卷能够由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的做用。vim
(1)在卷中增长服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务;服务器
(2)当存储空间不足或即将耗尽时,能够动态添加卷。只须要增长一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量;app
(3)FastDFS中的文件标识分为两个部分:卷名和文件名,两者缺一不可。负载均衡
准备两台虚拟机,一台为 DFS端,一台为 storage端。 |
主机名 | IP地址 | 主要软件 |
---|---|---|---|
tracker | 192.168.50.139 | libfastcommon、fastd | |
storage | 192.168.50.140 | libfastcommon |
一、安装环境包:tcp
yum -y install libevent libevent-devel perl make gcc zlib zlib-devel pcre pcre-devel gcc-c++ openssl-devel
二、安装libfastcommon:
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz tar zxf V1.0.38.tar.gz -C /opt/ cd /opt/libfastcommon-1.0.38/ ./make.sh && ./make.sh install
三、建立软链接:
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz tar zxf V5.11.tar.gz -C /opt/ cd /opt/fastdfs-5.11/ ./make.sh && ./make.sh install [root@localhost fdfs]# ls client.conf.sample //客户端上传配置文件(模板) storage.conf.sample //文件存储服务器配置文件(模板) tracker.conf.sample //负责均衡调度服务器配置文件(模板) 复制配置文件: cp tracker.conf.sample tracker.conf cp storage.conf.sample storage.conf cp client.conf.sample client.conf
一、tracker监控端(能够有多个),创建数据、日志存放目录:
mkdir -m 755 -p /opt/fastdfs
二、修改 tracker配置文件:
vim /etc/fdfs/tracker.conf 增改如下内容: port=22122 base_path=/opt/fastdfs //tracker存储data和log的跟路径,必须提早建立好 http.server_port=8080 //tracker服务器上启动http服务进程(没装忽略)
三、开启服务:
fdfs_trackerd /etc/fdfs/tracker.conf start //启动服务 netstat -atnp | grep 22122 //查看端口是否正常开启 tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 43776/fdfs_trackerd
四、设置开启自启动:
vim /etc/rc.local #末行添加: fdfs_trackerd /etc/fdfs/tracker.conf start
#storage数据存储端(能够有多个) #创建数据、日志存放目录 mkdir -m 777 -p /opt/fastdfs #修改storage配置文件 vim /etc/fdfs/storage.conf group_name=group1 #默认组名,根据实际状况修改 port=23000 #storge默认23000,同一个组的storage端口号必须一致 base_path=/opt/fastdfs #storage日志文件的根路径 store_path_count=1 #与下面路径个数相同,默认为1 store_path0=/opt/fastdfs #提供的存储路径(默认与日志文件存放在一块儿) tracker_server=192.168.50.140:22122 #本身的tracker服务器IP(重点!!!) http.server_port=80(http访问文件的端口默认为8888,nginx中配置的监听端口保持一致) #开启服务 fdfs_storaged /etc/fdfs/storage.conf start netstat -atnp | grep 23000 tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 40430/fdfs_storaged #开机自启tracker vim /etc/rc.local #末行添加 fdfs_storaged /etc/fdfs/storage.conf start #检查是否与tracker关联成功 fdfs_monitor /etc/fdfs/storage.conf Storage 1: id = 192.168.50.140 ip_addr = 192.168.50.140 (storage) ACTIVE
#解压nginx安装包 tar zxf nginx-1.12.0.tar.gz -C /opt/ #下载fastdfs-nginx-module安装包 wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz tar zxf V1.20.tar.gz -C /opt/ #编译安装nginx ./configure \ --prefix=/usr/local/nginx \ --add-module=/opt/fastdfs-nginx-module-1.20/src/ #fastdfs-nginx-module模块 make && make instal
l
cd fastdfs-nginx-module-1.20/src cp mod_fastdfs.conf /etc/fdfs/ #修改fastdfs-nginx-module模块配置文件mod-fasts.conf vim mod_fastdfs.conf base_path=/opt/fastdfs #存放数据文件、日志的路径 tracker_server=192.168.50.140:22122 #tracker端的地址(重点!!!) url_have_group_name = true #url是否包含group名称 storage_server_port=23000 #须要和storage配置的相同 store_path_count=1 #存储路径个数,须要和store_path个数匹配 store_path0=/opt/fastdfs #文件存储的位置 #修改nginx配置文件 vim /usr/local/nginx/conf/nginx.conf #空行处添加 location ~/M00 { root /opt/fastdfs/data; ngx_fastdfs_module; } #拷贝fastdfs解压目录中的http.conf和mime.types cd /opt/fastdfs-5.11/conf/ cp mime.types http.conf /etc/fdfs/
一、上传文件:
/usr/bin/fdfs_upload_file <config_file> <local_filename>
二、下载文件:
/usr/bin/fdfs_download_file <config_file> <file_id> [local_filename]
三、删除文件:
/usr/bin/fdfs_delete_file <config_file> <file_id>