FastDFS FastDFS简介 FastDFS 是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
FastDFS 服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS 同时对文件的 meta data 进行管理。所谓文件的 meta data 就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的 key 为 width,value 为 1024。文件 meta data 是文件属性列表,可以包含多个键值对。
跟踪器和存储节点都可以由一台多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少
FastDFS 系统结构如下图所示:
FastDFS安装 安装所需要的依赖包在GitHub上 https://github.com/happyfish100 本次系统环境采用的 Ubuntu 20.04的版本,FastDFS所有项目拉取的是最新的
安装依赖组件 1 root@k8s-master-12:~# apt-get -y install git gcc g++ make automake autoconf libtool pcre2-utils libpcre2-dev zlib1g zlib1g-dev openssl libssh-dev wget vim
存放路径
说明
位置
所有安装包
/usr/local/src
数据存储位置
/app/data/dfs
安装libfastcommon 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # 拉取最新libfastcommon代码 root@k8s-master-12:/usr/local/src# git clone https://github.com/happyfish100/libfastcommon.git --depth 1 Cloning into 'libfastcommon'... remote: Enumerating objects: 170, done. remote: Counting objects: 100% (170/170), done. remote: Compressing objects: 100% (162/162), done. remote: Total 170 (delta 18), reused 52 (delta 2), pack-reused 0 Receiving objects: 100% (170/170), 309.06 KiB | 238.00 KiB/s, done. Resolving deltas: 100% (18/18), done.# 编译安装 root@k8s-master-12:/usr/local/src/libfastcommon# ./make.sh -j 2 && ./make.sh install ...... ray_allocator.lo sorted_ array.lo -lm -ldl -lpthread mkdir -p /usr/lib mkdir -p /usr/lib mkdir -p /usr/include/fastcommon install -m 755 libfastcommon.so /usr/lib install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_ func.h pthread_func.h ini_ file_reader.h _ os_define.h sockopt.h sched_ thread.h http_func.h md5.h local_ ip_func.h avl_ tree.h ioevent.h ioevent_loop.h fast_ task_queue.h fast_ timer.h locked_timer.h process_ ctrl.h fast_mblock.h connection_ pool.h fast_mpool.h fast_ allocator.h fast_buffer.h skiplist.h multi_ skiplist.h flat_skiplist.h skiplist_ common.h system_info.h fast_ blocked_queue.h php7_ ext_wrapper.h id_ generator.h char_converter.h char_ convert_loader.h common_ blocked_queue.h multi_ socket_client.h skiplist_ set.h uniq_skiplist.h fc_ list.h locked_list.h json_ parser.h buffered_file_ writer.h server_id_ func.h fc_queue.h sorted_ queue.h fc_memory.h shared_ buffer.h thread_pool.h fc_ atomic.h array_allocator.h sorted_ array.h /usr/include/fastcommon# 编译完后不知道成没成功的话 用echo $? 输出如果是0的话 代表成功非0 都是错误的 root@k8s-master-12:/usr/local/src/libfastcommon# echo $? 0
安装libserverframe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 root@k8s-master-12:/usr/local/src# git clone https://github.com/happyfish100/libserverframe.git --depth 1 Cloning into 'libserverframe'... remote: Enumerating objects: 84, done. remote: Counting objects: 100% (84/84), done. remote: Compressing objects: 100% (74/74), done. remote: Total 84 (delta 9), reused 23 (delta 1), pack-reused 0 Unpacking objects: 100% (84/84), 137.19 KiB | 185.00 KiB/s, done. root@k8s-master-12:/usr/local/src# cd libserverframe/ root@k8s-master-12:/usr/local/src/libserverframe# ./make.sh -j 2 && ./make.sh install ...... mkdir -p /usr/lib mkdir -p /usr/lib mkdir -p /usr/include/sf/idempotency/common mkdir -p /usr/include/sf/idempotency/server mkdir -p /usr/include/sf/idempotency/client install -m 755 libserverframe.so /usr/lib cp -f sf_types.h sf_ global.h sf_define.h sf_ nio.h sf_service.h sf_ func.h sf_util.h sf_ configs.h sf_proto.h sf_ cluster_cfg.h sf_ sharding_htable.h sf_ connection_manager.h sf_ serializer.h sf_binlog_ index.h sf_file_ writer.h sf_binlog_ writer.h sf_ordered_ writer.h sf_buffered_ writer.h sf_iov.h sf_ shared_mbuffer.h /usr/include/sf cp -f idempotency/common/idempotency_ types.h /usr/include/sf/idempotency/common cp -f idempotency/server/server_types.h idempotency/server/server_ channel.h idempotency/server/request_htable.h idempotency/server/channel_ htable.h idempotency/server/server_handler.h idempotency/server/request_ metadata.h /usr/include/sf/idempotency/server cp -f idempotency/client/client_types.h idempotency/client/receipt_ handler.h idempotency/client/client_channel.h idempotency/client/rpc_ wrapper.h /usr/include/sf/idempotency/client root@k8s-master-12:/usr/local/src/libserverframe# echo $? 0
安装FastDFS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master-12:/usr/local/src# git clone https://github.com/happyfish100/fastdfs.git --depth 1 Cloning into 'fastdfs'... remote: Enumerating objects: 226, done. remote: Counting objects: 100% (226/226), done. remote: Compressing objects: 100% (208/208), done. remote: Total 226 (delta 45), reused 107 (delta 10), pack-reused 0 Receiving objects: 100% (226/226), 3.95 MiB | 1.53 MiB/s, done. Resolving deltas: 100% (45/45), done. root@k8s-master-12:/usr/local/src# cd fastdfs/ root@k8s-master-12:/usr/local/src/fastdfs# ./make.sh -j 2 && ./make.sh install ... mkdir -p /usr/include/fastdfs cp -f ../common/fdfs_define.h ../common/fdfs_ global.h ../common/mime_file_ parser.h ../common/fdfs_http_ shared.h ../tracker/tracker_types.h ../tracker/tracker_ proto.h ../tracker/fdfs_shared_ func.h ../tracker/fdfs_server_ id_func.h ../storage/trunk_ mgr/trunk_shared.h tracker_ client.h storage_client.h storage_ client1.h client_func.h client_ global.h fdfs_client.h /usr/include/fastdfs if [ ! -f /etc/fdfs/client.conf ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf; fi root@k8s-master-12:/usr/local/src/fastdfs# echo $? 0 # 拷贝配置文件到/etc/fdfs目录下 root@k8s-master-12:/usr/local/src/fastdfs# cp conf/http.conf /etc/fdfs/ root@k8s-master-12:/usr/local/src/fastdfs# cp conf/mime.types /etc/fdfs/
安装fastdfs-nginx-module 1 2 3 4 5 6 7 8 9 root@k8s-master-12:/usr/local/src# git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1 Cloning into 'fastdfs-nginx-module'... remote: Enumerating objects: 12, done. remote: Counting objects: 100% (12/12), done. remote: Compressing objects: 100% (12/12), done. remote: Total 12 (delta 0), reused 7 (delta 0), pack-reused 0 Unpacking objects: 100% (12/12), 24.09 KiB | 129.00 KiB/s, done.# 拷贝配置文件 root@k8s-master-12:/usr/local/src# cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
安装nginx 安装依赖组件 1 root@k8s-master-12:/usr/local/src# apt-get -y install zlib1g-dev libpcre3 libpcre3-dev openssl libssl-dev gcc make
编译安装nginx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # 下载nginx源码包 root@k8s-master-12:/usr/local/src# wget https://nginx.org/download/nginx-1.20.2.tar.gz root@k8s-master-12:/usr/local/src# tar xf nginx-1.20.2.tar.gz root@k8s-master-12:/usr/local/src# cd nginx-1.20.2/ root@k8s-master-12:/usr/local/src/nginx-1.20.2# ./configure --prefix=/app/nginx --user=nginx --group=nginx \ --error-log-path=/app/nginx/logs/error.log \ --http-log-path=/app/nginx/logs/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_ module \ --with-stream \ --with-http_v2_ module \ --with-http_auth_ request_module \ --with-http_ stub_status_ module \ --with-http_gzip_ static_module \ --with-http_ flv_module \ --with-http_ mp4_module \ --with-http_ realip_module \ --with-stream_ realip_module \ --with-stream_ ssl_module \ --with-file-aio \ --with-threads \ --add-module=/usr/local/src/fastdfs-nginx-module/src/ ... Configuration summary + using threads + using system PCRE library + using system OpenSSL library + using system zlib library nginx path prefix: "/app/nginx" nginx binary file: "/app/nginx/sbin/nginx" nginx modules path: "/app/nginx/modules" nginx configuration prefix: "/app/nginx/conf" nginx configuration file: "/app/nginx/conf/nginx.conf" nginx pid file: "/var/run/nginx/nginx.pid" nginx error log file: "/app/nginx/logs/error.log" nginx http access log file: "/app/nginx/logs/access.log" nginx http client request body temporary files: "client_ body_temp" nginx http proxy temporary files: "proxy_ temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_ temp" nginx http scgi temporary files: "scgi_temp root@k8s-master-12:/usr/local/src/nginx-1.20.2# make -j 2&& make install # 添加nginx的环境变量 root@k8s-master-12:/usr/local/src/nginx-1.20.2# echo 'export PATH=/app/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh root@k8s-master-12:/usr/local/src/nginx-1.20.2# source /etc/profile.d/nginx.sh
配置FastDFS tracker配置 现在开始tracker配置文件的修改
1 2 3 4 5 6 7 8 9 root@k8s-master-12:~# vim /etc/fdfs/tracker.conf disabled = false bind_addr = 0.0.0.0 # 监听地址 port = 22122 # tracker服务器端口(默认22122,一般不修改) connect_ timeout = 5 network_timeout = 60 base_ path = /app/data/dfs # 存储日志和数据的根目录 max_connections = 10240 ...
storage配置 1 2 3 4 5 6 root@k8s-master-12:~# vim /etc/fdfs/storage.conf port=23000 # storage服务端口(默认23000,一般不修改) base_path=/app/data/dfs # 数据和日志文件存储根目录 store_ path0=/app/data/dfs # 第一个存储目录 tracker_server=172.18.0.186:22122 # tracker服务器IP和端口 http.server_ port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
client配置 1 2 3 4 root@k8s-master-12:~# vim /etc/fdfs/client.conf#需要修改的内容如下 base_path=/app/data/dfs tracker_ server=172.18.0.186:22122 #tracker服务器IP和端口
配置nginx访问 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 root@k8s-master-12:~# vim /etc/fdfs/mod_fastdfs.conf tracker_ server=172.18.0.186:22122 #tracker服务器IP和端口 url_have_ group_name=true store_ path0=/app/data/dfs# 配置nginx root@k8s-master-12:~# cd /data/nginx/conf/ root@k8s-master-12:/data/nginx/conf~# vim nginx.conf user nginx; worker_processes 1; worker_ cpu_affinity 0001; #worker_ cpu_affinity 00000001; #error_ log logs/error.log;#error_log logs/error.log notice; #error_ log logs/error.log info;#pid logs/nginx.pid; worker_rlimit_ nofile 65535; events { use epoll; worker_connections 100000; multi_accept on; } http { include mime.types; default_type application/octet-stream; log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"status":"$status"}'; access_log /data/nginx/logs/access.log access_json; include /data/nginx/conf/conf.d/*.conf; sendfile on; tcp_nopush on; server_tokens off; client_max_body_size 1024m; #keepalive_timeout 0; keepalive_timeout 3600; server { listen 80 default_server; server_name _; return 444; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } root@k8s-master-12:/data/nginx/conf~# mkdir conf.d root@k8s-master-12:/data/nginx/conf~# cd conf.d root@k8s-master-12:/data/nginx/conf/conf.d~# vim download.conf server { listen 80; server_name download.xxxxx.com; location ~* /\.(html|htm|jsp|php|js)$ { deny all; } location /group1/M00 { root /data/fastdfs; ngx_fastdfs_module; } } # 配置好后检查nginx配置文件是否有语法错误 root@k8s-master-12:/data/nginx/conf/conf.d~# nginx -t ngx_http_fastdfs_set pid=46925 nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok nginx: configuration file /data/nginx/conf/nginx.conf test is successful # nginx配置文件没问题后需要修改fastdfs的trackerd启动文件 root@k8s-master-12:/data/nginx/conf/conf.d~# vim /usr/lib/systemd/system/fdfs_trackerd.service [Unit] Description=FastDFS trackerd service After=network-online.target [Service] Type=forking PIDFile=/data/fastdfs/data/fdfs_trackerd.pid # 这块需要改成实际的路径 ExecStart=/usr/bin/fdfs_ trackerd /etc/fdfs/tracker.conf start ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop # No artificial start/stop timeout TimeoutSec=0 # Disable OOM kill by Linux kernel OOMScoreAdjust=-1000 [Install] WantedBy=multi-user.target # 修改storaged的service文件 root@k8s-master-12:/data/nginx/conf/conf.d~# vim /usr/lib/systemd/system/fdfs_ storaged.service [Unit] Description=FastDFS storaged service After=network-online.target [Service] Type=forking PIDFile=/data/fastdfs/data/fdfs_storaged.pid # 这块需要改成实际的路径 ExecStart=/usr/bin/fdfs_ storaged /etc/fdfs/storage.conf start ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop # No artificial start/stop timeout TimeoutSec=0 # Disable OOM kill by Linux kernel OOMScoreAdjust=-1000 [Install] WantedBy=multi-user.target root@k8s-master-12:/data/nginx/conf/conf.d~# systemctl daemon-reload root@k8s-master-12:/data/nginx/conf/conf.d~# systemctl start fdfs_ trackerd root@k8s-master-12:/data/nginx/conf/conf.d~# systemctl start fdfs_storaged # fastdfs服务启动后测试上传文件是否能成功 root@k8s-master-12:/data/nginx/conf/conf.d~# fdfs_ upload_file /etc/fdfs/client.conf /root/1111.png group1/M00/00/00/rBIAumQcGTaADraUAAjAKPA75ys481.png
验证fastdfs 通过上传命令成功后的返回信息后,拿到之前配置好的域名或者ip+端口+group1/M00/00/00/rBIAumQcGTaADraUAAjAKPA75ys481.png访问验证