FastDFS分布式存储

loviezhang 2020-05-30

FastDFS分布式存储

fastdfs安装

主机名IP地址
fastdfs0110.0.1.130
fastdfs0210.0.1.131
fastdfs0310.0.1.132
web0110.0.1.7
#所有服务器操作相同
[ ~]# yum -y install make cmake gcc gcc-c++
[ ~]# git clone https://github.com/happyfish100/libfastcommon.git
[ ~]# mv libfastcommon /usr/local/src/
[ ~]# cd /usr/local/src/
[ src]# cd libfastcommon/
[ libfastcommon]# ./make.sh 
[ libfastcommon]# ./make.sh install
[ src]# yum install gettext gettext-devel libXft libXft-devel libXpm libXpm-devel autoconf libxtst-devel zlib-devel libpng-devel  glib-devel
[ src]# wget https://github.s3.amazonaws.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
[ src]# cd libevent-2.0.20-stable/
[ libevent-2.0.20-stable]# ./configure && make && make install 
[ src]# git clone https://github.com/happyfish100/fastdfs.git
[ src]# cd fastdfs/
[ fastdfs]# ./make.sh
[ fastdfs]# ./make.sh install

配置Fastdfs跟踪服务器Tracker(fastdfs01)

#fastdfs01服务器
[ init.d]# cd /etc/fdfs/
[ fdfs]# cp tracker.conf.sample tracker.conf
[ fdfs]# 
[ fdfs]# vim tracker.conf
disabled=false               //启用配置文件
port=22122                   //tracker 的端口号,一般采用 22122 这个默认端口
base_path=/fastdfs/tracker   //tracker 的数据文件和日志目录
store_lookup=0              //采取轮巡方式选择要上传的组进行存储,默认2 选择最大空闲空间的组
[ /]# mkdir -p /fastdfs/tracker
[ /]# /etc/init.d/fdfs_trackerd start
[ tracker]# netstat -lntup
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      8604/fdfs_trackerd

配置Fastdfs存储服务器Storage(fastdfs01、fastdfs02)

#fastdfs01、fastdfs02服务器配置相同
[ fastdfs]# cd /etc/fdfs/
[ fdfs]# vim storage.conf
disabled=false                          //启用配置文件
group_name=group1                       //组名(第一组为 group1,第二组为 group2)
port=23000                              //storage 的端口号,同一个组的 storage 端口号必须相同
base_path=/fastdfs/storage              //设置 storage 的日志目录
store_path0=/fastdfs/storage            //存储路径
store_path_count=1                      //存储路径个数,需要和 store_path 个数匹配
tracker_server=192.168.10.200:22122     //tracker 服务器的 IP 地址和端口
tracker_server=192.168.1.201:22122      //多个 tracker 直接添加多条配置
http.server_port=8888                   //设置 http 端口号
[ fdfs]# mkdir -p /fastdfs/storage
[ fdfs]# /etc/init.d/fdfs_storaged start

配置客户端(web01)

#web01服务器
[ src]# cd /etc/fdfs/
[ fdfs]# cp client.conf.sample client.conf
[ fdfs]# vim client.conf
base_path=/fastdfs/client
tracker_server=10.0.1.130:22122
[ fdfs]# mkdir -p /fastdfs/client

#测试
[ ~]# pwd
/root
[ ~]# ls
1.png
[ ~]# fdfs_upload_file /etc/fdfs/client.conf 1.png 
group1/M00/00/00/CgABg11rmL6AY4HpAABAwsjutas743.png
[ ~]#

在各个存储节点安装nginx

fastdfs-nginx-module
FastDFS 通过Tracker服务器,将文件放在Storage服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。
假设Tracker服务器将文件上传到了A机器上,上传成功后文件ID已经返回给客户端。此时FastDFS存储集群机制会将这个文件同步到同组存储B机器上,在文件还没有复制完成的情况下,客户端如果用这个文件ID在B机器上取文件,就会出现文件无法访问的错误。
而fastdfs-nginx-module可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。

[ ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[ ~]# yum clean all
[ ~]# yum makecache 
[ ~]# yum -y install nginx
[ ~]# git clone https://github.com/happyfish100/fastdfs-nginx-module.git
[ ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[ ~]# tar xf nginx-1.16.1.tar.gz 
[ ~]# yum -y install pcre-devel openssl openssl-devel
[ ~]# cd nginx-1.16.1/
[ nginx-1.16.1]#  ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt=‘-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC‘ --with-ld-opt=‘-Wl,-z,relro -Wl,-z,now -pie‘ --add-module=/root/fastdfs-nginx-module/src/
[ nginx-1.16.1]#  make
[ nginx-1.16.1]# mv /usr/sbin/nginx /usr/sbin/nginx.bak
[ nginx-1.16.1]# cd objs/
[ objs]# cp nginx /usr/sbin/nginx
[ ~]# cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[ ~]# vim /etc/fdfs/mod_fastdfs.conf 
tracker_server=10.0.1.130:22122
url_have_group_name = true
store_path0=/fastdfs/storage

[ ~]# cd /usr/local/src/fastdfs/conf/
[ conf]# cp http.conf mime.types /etc/fdfs/
[ conf]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
[ conf]# vim /etc/nginx/conf.d/default.conf 
server {
   listen 8888;
   server_name localhost;
   location /group1/M00 {
   ngx_fastdfs_module;
}
}
[ conf]# systemctl restart nginx

# 浏览器访问
http://10.0.1.131:8888/group1/M00/00/00/CgABg11rmL6AY4HpAABAwsjutas743.png

在跟踪节点服务器上安装Nginx

[ ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[ ~]# yum -y install nginx
[ ~]# vim /etc/nginx/conf.d/default.conf 
upstream fastdfs {
  server 10.0.1.131:8888;
  server 10.0.1.132:8888;
}
server {
  listen 80;
  server_name localhost;
  location /group1/M00 {
  proxy_pass http://fastdfs;
  }

#浏览器访问
http://10.0.1.130/group1/M00/00/00/CgABg11rmL6AY4HpAABAwsjutas743.png

相关推荐

huhuhuemail / 0评论 2019-02-16