传统web站点部署大部分都是基于动静分离的方式,静态图片存放于一台nfs存储。可是这种架构有一个问题就是nfs存储是单点,并且若是图片量过大的时候,则nfs会成为性能短板。基于此能够采用分布式存储的替代方案来替代nfs。
fastdfs不但能够存储图片,还能够存储其余非结构化数据,其特性是能够存放海量的中小容量文件,并且性能和容量也不会成为瓶颈。前端
fastdfs基于以下两个组件组成:nginx
上传文件步骤git
此处采用4台服务器进行配置,一台既为tracker节点,也为storage节点,其他3台都为storage节点。
github
下载源码包制做成rpm包
安装的话能够经过编译安装,不过此处由于要安装4台服务器,因此我从github克隆下来源码制做成源码包进行分发安装,提升效率。web
git clone https://github.com/happyfish100/libfastcommon.git git clone https://github.com/happyfish100/fastdfs.git
mv libfastcommon libfastcommon-1.0.36 tar zcf libfastcommon libfastcommon-1.0.36.tar.gz libfastcommon libfastcommon-1.0.36/* mv fastdfs fastdfs fastdfs fastdfs-5.0.11 tar zcf fastdfs-5.0.11.tar.gz fastdfs-5.0.11/*
mkdir /root/rpmbuild/{SOURCES,SPECS} mv libfastcommon libfastcommon-1.0.36.tar.gz /root/rpmbuild/SOURCES/ mv /root/libfastcommon-1.0.36/libfastcommon.spec /root/rpmbuild/SPECS/ mv fastdfs-5.0.11.tar.gz /root/rpmbuild/SOURCES/ mv /root/fastdfs-5.0.11/fastdfs.spec /rpm/rpmbuild/SPECS/
rpmbuild -bb libfastcommon.spec rpmbuild -bb fastdfs.spec
制做完成以后会在rpmbuild目录下生成一些目录,以下:
其中RPMS目录为制做好的源码包
算法
先安装libfastcommon,由于fastdfs依赖它,而后再安装fastdfs便可,而后拷贝到其余服务器进行安装。服务器
配置tracker节点
安装完成以后会在/etc目录下生成fdfs目录,其中文件以下,其中为各类配置文件:
配置tracker.conf架构
base_path=/data/fastdfs #修改工做路径为本身建立路径 port=22122 #监听端口为221222端口# # the method of selecting group to upload files #指定文件上传方式时上传到哪一个组# # 0: round robin #轮询选择# # 1: specify group #指定组# # 2: load balance, select the max free space group to upload file #根据负载,哪一个组剩余空间最大上传到哪一个组# store_lookup=2 # which group to upload file #若是指定组,此处指定上传到哪一个组# # when store_lookup set to 1, must set store_group to the group name store_group=group2 # which storage server to upload file #上传到组内哪一个服务器# # 0: round robin (default) #轮询# # 1: the first server order by ip address #ip小的服务器# # 2: the first server order by priority (the minimal) #根据服务器优先级# # Note: if use_trunk_file set to true, must set store_server to 1 or 2 store_server=0 # which path(means disk or mount point) of the storage server to upload file #上传到服务器哪一个硬盘# # 0: round robin #轮询# # 2: load balance, select the max free space path to upload file #根据剩余空间# store_path=0 # which storage server to download file #从组内哪一个服务器请求数据# # 0: round robin (default) #轮询# # 1: the source storage server which the current file uploaded to #最开始上传到哪一个服务器# download_server=0
其余参数无需过多修改。
配置storage节点app
group_name=group1 #指定组名,此处四台服务器,两台为group1,两台为group2,组成镜像组# port=23000 #指定工做端口# store_path_count=1 #指定数据存储路径,若是有多个则按照实际数量写,此处只有一个,则写1# store_path0=/data/storage #数据存储路径# tracker_server=192.168.11.200:22122 #tracker节点ip和端口# upload_priority=10 #数据上传优先级#
service fdfs_storaged start service fdfs_trackerd start
上传文件进行测试分布式
base_path=/data/fastdfs #client日志存储路径# tracker_server=192.168.11.200:22122 #tracker节点ip和端口#
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --with-debug --add-module=../fastdfs-nginx-module/src/
[Unit] Description=nginx server after=network.target remote-fs.target nss-lookup.target [service] type=forking pidfile=/run/nginx.pid execstartpre=/usr/bin/rm -rf /run/nginx.pid execstartpre=/usr/sbin/nginx -t execstart=/usr/bin/nginx execreload=/bin/kill -s HUP $MAINPID killsignal=SIGQUIT timeoutstopsec=5 killmode=process privatetmp=true [install] wantedby=multi-user.target
base_path=/data/fastdfs #指定工做目录# tracker_server=192.168.11.200:22122 #指定tracker的ip地址# group_name=group2 #指定当前服务器所属组# url_have_group_name = false #此项若是为true,则访问文件须要在前面添加group信息,不然直接写M00便可# store_path0=/data/storage #指定存储路径# log_filename=/data/fastdfs/logs/mod_fastdfs.log #指定log日志文件路径#
location /M00 { alias /data/storage/data; ngx_fastdfs_module; }
如今文件只是可以分布式上传到不一样的服务器节点了,可是若是要访问的话还须要在本地安装nginx,不过nginx须要打fastdfs-nginx-module补丁才能使用。编译成功以后本地配置nginx的反向代理到指定的工做目录便可经过nginx访问到指定资源。不过生产环境中fastdfs中文件的上传下载都是经过前端的程序来自动写入和请求的。