Loading... # FastDFS 集群 ![1564986976390.png][1] ## 架构 六台服务器,tracker节点ip:131,132,storage节点:181,182对应group1,191,192对应group2 ## 单机配置 首先在六台服务器上分别配置单机FastDFS环境: [FastDFS搭建及使用](https://www.princelei.club/archives/243.html) ## Tracker集群搭建 在131,132上分别启动tracker ``` /etc/init.d/fdfs_trackerd start ``` 查看端口 ``` netstat -unltp |grep fdfs ``` 启动日志 ``` tail -100f trackerd.log ``` ## Storage集群搭建 修改配置文件```/etc/fdfs/storage.conf``` 修改tracker_server 的ip地址,多个 tracker 直接添加多条配置 ``` tracker_server=192.168.150.131:22122 tracker_server=192.168.150.132:22122 ``` 修改192,192的group_name ``` group_name=group1 -> group_name=group2 ``` 启动Storage服务 /etc/init.d/fdfs_storaged start fdfsMonitor ``` /usr/bin/fdfs_monitor /etc/fdfs/storage.conf ``` 查看端口:`netstat -unltp | grep fdfs` 使用FastDFS中的Monitor查看:在所有的Storage启动成功后执行下述命令 ``` /usr/bin/fdfs_monitor /etc/fdfs/storage.conf ``` ## 测试FastDfs集群 测试文件上传 ``` fdfs_upload_file /etc/fdfs/client.conf tmp.sh ``` 修改tracker.conf文件中group的负载策略 修改配置文件中的store_lookup,这个属性的可选值有0,1,2。分别代表: ``` # 0: 轮询 # 1: 指定卷,通过卷名指定访问某一个卷 通过store_group配置指定 # 2: 访问空闲磁盘空间较大的。是默认的负载策略。 ``` ## Nginx集群搭建 FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。假设 Tracker 服务器将文件上传到了 S1,上传成功后文件 ID已经返回给客户端。 此时 FastDFS 存储集群机制会将这个文件同步到同组存储 S2,在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 S2 上取文件,就会出现文件无法访问的错误。 而 fastdfs-nginx-module 可以重定向文件连接到源服务器(S1)取文件,避免客户端由于复制延迟导致的文件无法访问错误。 ### Storage节点Nginx反向代理 在每个Storage节点安装配置Nginx ### 修改配置文件mod_fastdfs.conf ``` connect_timeout=10 # 181、182 对应 group 1 # 191、192 对应 group 2 group_name= tracker_server=192.168.150.131:22122 tracker_server=192.168.150.132:22122 group_count = 2 # group settings for group # 1 # since v1.14 # when support multi-group, uncomment following section [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/var/data/fastdfs-storage/store # store_path1=/home/yuqing/fastdfs1 # group settings for group # 2 # since v1.14 # when support multi-group, uncomment following section as neccessary [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/var/data/fastdfs-storage/store ``` ### 检查Nginx配置文件 端口80必须和Storage服务器中的/etc/fdfs/storage.conf配置文件中的http.server_port=80一致。 ``` listen 80; location ~ /group([0-9])/M00 { # add_header Content-Disposition "attachment;filename=$arg_attname"; ngx_fastdfs_module; } ``` 测试访问文件 报错 ``` ERROR - file: /root/fastdfs-nginx-module/src//common.c, line: 709, expect parameter token or ts in url, uri: / group1/M00/00/00/wKiWtV1H4eKAKE4YAAABAE3E6HQ3627.gif ``` 检查防盗链系统 vi /etc/fdfs/http.conf ``` http.anti_steal.check_token= ``` ### Tracker节点 负载均衡反向代理 在每个Tracker节点安装配置Nginx 在 tracker 上安装的 nginx 主要为了提供 http 访问的反向代理、负载均衡以及缓存服务。 #### Nginx缓存 ##### 传并解压ngx_cache_purge-2.3.tar.gz ``` cd /root tar -zxf ngx_cache_purge-2.3.tar.gz ``` ##### 编译安装nginx ``` ./configure --prefix=/usr/local/tengine --add-module=/root/ngx_cache_purge-2.3 make make install ``` ##### 配置nginx ngx_cache_purge模块的作用:用于清除指定url的缓存 下载地址:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz Nginx.conf ``` http { include mime.types; default_type application/octet-stream; # log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; # access_log logs/access.log main; # access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # 设置缓存 server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; # 设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限 # levels=1:2 表示缓存文件有两级目录 1表示第一级目录名为1位数,2表示第二级目录名为2位数 proxy_cache_path /var/data/cache/nginx/proxy_cache levels=1:2 # keys_zone 缓存区域名字,分配200m空间,最大缓存1g,有效期30天 keys_zone=http-cache:200m max_size=1g inactive=30d; proxy_temp_path /var/data/cache/nginx/proxy_cache/tmp; # 设置 group1 的服务器 upstream fdfs_group1 { server 192.168.150.181:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.150.18:80 weight=1 max_fails=2 fail_timeout=30s; } # 设置 group2 的服务器 upstream fdfs_group2 { server 192.168.150.191:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.150.192:80 weight=1 max_fails=2 fail_timeout=30s; } # gzip on; listen 80; # charset koi8-r; # access_log logs/host.access.log main; # access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main; server_name localhost; location /group1/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group1; expires 30d; } location /group2/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group2; expires 30d; } # 设置清除缓存的访问权限 location ~/purge(/.*) { allow 127.0.0.1; allow 192.168.2.0/24; deny all; proxy_cache_purge http-cache $1$is_args$args; } ``` ##### purge命令清除静态缓存 http://域名+purge+静态资源相对路径 来清除静态资源缓存 ## 高可用 ### keepalived安装 [keepalived + nginx 实现高可用](https://www.princelei.club/archives/193.html) [1]: https://www.princelei.club/usr/uploads/2019/12/244676809.png Last modification:June 11th, 2020 at 06:12 pm © 允许规范转载