FastDFS集群搭建

房东的猫 提交于 2020-12-10 07:42:14

fastdfs下载官方镜像
https://github.com/happyfish100
下载包libfastcommon和fastdfs 两个 fastdfs-master.zip libfastcommon-master.zip fastdfs-nginx-module-master.zip

http://bbs.chinaunix.net/forum-240-1.html
下载缓存包 ngx_cache_purge-2.3.tar.gz

http://nginx.org/en/download.html
下载nginx包 nginx-1.16.1

##新libfastcommon和fastdfs包
https://github.com/happyfish100

所有机器操作:

mkdir /home/centos/fastdfs  
cd  /home/centos/fastdfs  
sudo yum -y install make cmake gcc gcc-c++  unzip perl

FastDFS 的安装(所有跟踪服务器和存储服务器均执行如下操作)
安装FastDFS依赖包libfastcommon

cd  /home/centos/fastdfs 
unzip  libfastcommon-master.zip
cd  libfastcommon-master
su root
./make.sh
./make.sh install

安装FastDFS

cd  /home/centos/fastdfs
unzip  fastdfs-master.zip
cd  fastdfs-master
su root
./make.sh
./make.sh install

可略过:
查看服务脚本
/etc/init.d/fdfsstoraged
/etc/init.d/fdfstrackerd


查看配置文件(示例配置文件)
/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample


查看命令行工具(/usr/bin目录下)
/usr/bin/fdfsappendertest
/usr/bin/fdfsappendertest1
/usr/bin/fdfsappendfile
/usr/bin/fdfscrc32
/usr/bin/fdfsdeletefile
/usr/bin/fdfsdownloadfile
/usr/bin/fdfsfileinfo
/usr/bin/fdfsmonitor
/usr/bin/fdfsstoraged
/usr/bin/fdfstest
/usr/bin/fdfstest1
/usr/bin/fdfstrackerd
/usr/bin/fdfsuploadappender
/usr/bin/fdfsuploadfile













配置tracker两台服务器(192.168.74.94、192.168.53.7)

cd /etc/fdfs/ 
cp tracker.conf.sample tracker.conf

修改tracker配置文件

vi /etc/fdfs/tracker.conf
修改的内容如下:
disabled=false              # 启用配置文件
port=22122                  # tracker服务器端口(默认22122)
base_path=/home/centos/fastdfs/date # 存储日志和数据的根目录
store_lookup=0              # 轮询方式上传
测试完了要把store_lookup=0  改为2

创建基础数据目录

mkdir  -p /home/centos/fastdfs/date

启动tracker服务器

/etc/init.d/fdfs_trackerd start
或者
systemctl start fdfs_trackerd
或者
fdfs_trackerd /etc/fdfs/tracker.conf start

检查FastDFS Tracker Server是否启动成功:

ps aux | grep fdfs_trackerd

tracker server的关闭命令

/etc/init.d/fdfs_trackerd stop
或者
systemctl stop fdfs_trackerd
或者
fdfs_trackerd /etc/fdfs/tracker.conf stop

设置tracker server开机启动
chkconfig fdfs_trackerd on

配置storage四台服务器(group1 192.168.75.140、192.168.47.41、 group2 192.168.10.204、192.168.100.115)

cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

修改storage配置文件

vi /etc/fdfs/storage.conf
# 修改的内容如下:
disabled=false                                                   # 启用配置文件
port=23000                                                        # storage的端口号,同一个组的 storage 端口号必须相同
base_path=/home/centos/fastdfs/date              # 存储日志和数据的根目录
store_path0=/home/centos/fastdfs/date            # 第一个存储目录
store_path_count=1                                         #存储路径个数,需要和store_path个数匹配
tracker_server=192.168.74.94:22122               # tracker服务器的IP地址和端口
tracker_server=192.168.53.7:22122                # 多个tracker直接添加多条配置
配置group_name
不同分组配置不同group_name,两台gouup1参数改为group1, 两台gouup2参数改为group2,单台group1和group2思路一样就去掉每个group服务器中的一台,两台是为了做热备,一个group刮掉后无影响
gouup1两台服务器中:
group_name=group1
gouup2两台服务器中:
group_name=group2
mkdir -p  /home/centos/fastdfs/date 

启动storage服务器

/etc/init.d/fdfs_storaged start
或者
systemctl start fdfs_storaged
或者
fdfs_storaged /etc/fdfs/tracker.conf start

初次启动,会在/data/fastdfs/storage目录下生成logs、data两个目录。

检查FastDFS Tracker Server是否启动成功:

ps aux | grep fdfs_storaged

所有 Storage 节点都启动之后,可以在任一 Storage 节点上使用如下命令查看集群信息:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
storage server的关闭命令
/etc/init.d/fdfs_storaged stop
或者
systemctl stop fdfs_storaged
或者
fdfs_storaged /etc/fdfs/tracker.conf stop
设置storage server开机启动
chkconfig fdfs_storaged on

文件上传测试(tracker服务器中操作)

sudo ln -s /home/centos/fastdfs/date  /home/centos/fastdfs/date/M00

1、修改Tracker服务器客户端配置文件

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vi /etc/fdfs/client.conf
# 修改以下配置,其它保持默认
base_path=/home/centos/fastdfs/date 
tracker_server=192.168.74.94:22122 # tracker服务器IP和端口
tracker_server=192.168.53.7:22122  #tracker服务器IP2和端口

2、执行文件上传命令

#/root/test.png 是需要上传文件路径 
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/test.png
返回文件ID号:group1/M00/00/00/wKgAfFzMX_2AB9V9AADa-k_GxyQ840.jpg
(能返回以上文件ID,说明文件已经上传成功)

在所有storage服务器(group1 192.168.75.140、192.168.47.41、 group2 192.168.10.204、192.168.100.115)安装Nginx
http://nginx.org/en/download.html**

cd  /home/centos/fastdfs
unzip fastdfs-nginx-module-master.zip
cd fastdfs-nginx-module-master/src
vi config
将/usr/local/路径改为/usr/,或者直接执行命令:%s+/usr/local/+/usr/+g批量替换

安装nginx

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/src/
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-http_realip_module --add-module=/home/centos/fastdfs/fastdfs-nginx-module-master/src
make && make install

复制 fastdfs-nginx-module 源码中的配置文件到 /etc/fdfs 目录,并修改

cp /home/centos/fastdfs/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置:
connect_timeout=5
base_path=/tmp
tracker_server=192.168.74.94:22122       # tracker服务器IP和端口
tracker_server=192.168.53.7:22122        # tracker服务器IP2和端口
group_name=group1                        # 当前服务器的group名
url_have_group_name=true                 # url中包含group名称
store_path0=/home/centos/fastdfs/date    # 存储路径
group_count=2                            # 设置组的个数
#在最后添加 
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/centos/fastdfs/date

[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/home/centos/fastdfs/date

复制fastdfs-master 的部分配置文件到 /etc/fdfs 目录

cd /home/centos/fastdfs/fastdfs-master/conf
cp http.conf mime.types /etc/fdfs/
sudo ln -s /home/centos/fastdfs/date/  /home/centos/fastdfs/date/M00

配置 Nginx,简洁版nginx配置样例

user root;
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 8888;
        server_name localhost;
        location ~/group[1-2]/M00 {
            ngx_fastdfs_module;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

说明:

A、8888 端口值是要与/etc/fdfs/storage.conf中的 http.server_port=8888 相对应,因为 http.server_port 默认为 8888,如果想改成 80,则要对应修改过来。
B、Storage 对应有多个 group 的情况下,访问路径带 group 名,如/group1/M00/00/00/xxx,对应的 Nginx 配置为:
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}



设置开机自启

vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl start nginx
systemctl enable nginx  开机自启
systemctl status nginx  查看状态  ss -nuplt | grep nginx

如果nginx启动失败则pkill 掉nginx 后重新启动。

在tracker节点(192.168.0.121、192.168.0.122)安装Nginx

安装nginx,加入ngx_cache_purge(加入缓存模块)

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /home/centos/fastdfs
tar -zxvf nginx-1.16.1.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz
cd  nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-http_realip_module --add-module=/home/centos/fastdfs/ngx_cache_purge-2.3
make && make install

配置nginx负载均衡和缓存

user root;
worker_processes 2;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
    worker_connections 65535;
    use epoll;
}
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;
    sendfile on;
    tcp_nopush on;
    #keepalive_timeout 0;
    keepalive_timeout 65;
    #gzip on;
    #设置缓存
    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;
    #设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限
    proxy_cache_path /home/centos/fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:200m max_size=4g inactive=30d;
    proxy_temp_path /home/centos/fastdfs/cache/nginx/proxy_cache/tmp;
    #设置 group1 的服务器
    upstream fdfs_group1 {
        server 192.168.75.140:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.47.41:8888 weight=1 max_fails=2 fail_timeout=30s;
    }
    #设置 group2 的服务器
    upstream fdfs_group2 {
        server 192.168.10.204:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.100.115:8888 weight=1 max_fails=2 fail_timeout=30s;
    }
    server {
        listen 8000;
        server_name localhost;
        #charset koi8-r;
        #access_log logs/host.access.log main;
        #设置 group 的负载均衡参数
        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.0.0/24;
            deny all;
            proxy_cache_purge http-cache $1$is_args$args;
        }
        #error_page 404 /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            root html;
        }
    }
}

按以上 nginx 配置文件的要求,创建对应的缓存目录:

mkdir -p /data/fastdfs/cache/nginx/proxy_cache
mkdir -p /data/fastdfs/cache/nginx/proxy_cache/tmp

启动Nginx

systemctl start nginx
或者
/usr/local/nginx/sbin/nginx
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!