FastDFS+Nginx+keepalived实现高可用分布式文件服务器

1.集群规划

192.168.1.247             storage 服务器1 第一组

192.168.1.248             storage 服务器2 第一组

192.168.1.249             storage 服务器3第二组

192.168.1.250             storage 服务器4第二组

192.168.1.251             tracker 服务器1

192.168.1.252             tracker 服务器2


安装包:

libfastcommon-master.zip
fastdfs-nginx-module_v1.16.tar.gz 

nginx-1.6.2.tar.gz
FastDFS_v5.05.tar.gz              

ngx_cache_purge-2.3.tar.gz
keepalived-1.2.18.tar.gz


一、FastDFS 的安装

编译和安装所需的依赖包:   yum install make cmake gcc gcc-c++

创建安装目录: mkdir /usr/local/fast

解压 libfastcommon-master.zip到安装目录:  unzip libfastcommon-master.zip -d /usr/local/fast

进入安装目录:cd/usr/local/fast/ libfastcommon-master

编译安装: ./make.sh

                  ./make.sh install

因为 FastDFS 主程序设置的 lib 目录是/usr/local/lib,所以需要创建软链接.
 ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
 ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
 ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
 ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

解压FastDFS_v5.05.tar.gz 到安装目录: tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast/

进入解压目录: cd /usr/local/fast/FastDFS

编译、安装(编译前要确保已经成功安装了 libfastcommon)
 ./make.sh
 ./make.sh install

将错误的路径修改

 vi /etc/init.d/fdfs_trackerd
使用查找替换命令进统一修改:%s+/usr/local/bin+/usr/bin
 vi /etc/init.d/fdfs_storaged
使用查找替换命令进统一修改:%s+/usr/local/bin+/usr/bin

-----------------------------------------------------------------------------以上操作在所有节点倒要-------------------------------------------------------------

配置tracker (只在192.168.1.252,192.168.1.152操作)

进入配置文件目录 :cd /etc/fdfs/ 

修改配置文件:

    cp tracker.conf.sample tracker.conf 

    vi /etc/fdfs/tracker.conf 

修改的内容如下: 
base_path=/fastdfs/tracker  #tracker 的数据文件和日志目录 

创建基础数据目录(参考基础目录 base_path 配置): 

mkdir -p /fastdfs/tracker 

修改防火墙开放端口22122:

vi /etc/sysconfig/iptables  

增加    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT 

重启防火墙:service iptables restart

启动tracker: /etc/init.d/fdfs_trackerd start 

查看 tracker 的启动日志,看是否有错误 
tail -n100 -f  /fastdfs/tracker/logs/trackerd.log 

关闭tracker:  /etc/init.d/fdfs_trackerd stop 

三、配置storage(只在192.168.1.247、192.168.1.248、192.168.1.248、192.168.1.250操作)

修改storage配置文件: 
cd /etc/fdfs/ 
 cp storage.conf.sample storage.conf 
vi /etc/fdfs/storage.conf

修改内容:

disabled=false      #启用配置文件 
group_name=group1        #组名(第一组为 group1,第二组为 group2) 
port=23000      #storage 的端口号一组的必须一样
base_path=/fastdfs/storage   #设置 storage 的日志目录 
store_path0=/fastdfs/storage   #存储路径 
store_path_count=1     #存储路径个数,需要和 store_path 个数匹配 
tracker_server=192.168.1.251:22122 #tracker 服务器的 IP 地址和端口 
tracker_server=192.168.1.252:22122 #多个 tracker 直接添加多条配置 
http.server_port=8888    #设置 http 端口号 



 创建基础数据目录(参考基础目录 base_path 配置): 

 mkdir -p /fastdfs/storage 

修改防火墙开放端口23000:

vi /etc/sysconfig/iptables  

增加    -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000-j ACCEPT 

 重启防火墙:service iptables restart 

启动storaged    /etc/init.d/fdfs_storaged start 

关闭storaged    /etc/init.d/fdfs_storaged stop 

四、测试文件上传(在一个tracker操作上即可,在192.168.1.251,192.168.1.252其中之一)

  cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf 
 # vi /etc/fdfs/client.conf 
 base_path=/fastdfs/tracker 
tracker_server=192.168.1.251:22122 
tracker_server=192.168.1.252:22122 
 
2、执行如下文件上传命令: 
启动tracker 和storage后
上传
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf  /home/1.txt
 返回 ID 号:
group2/M00/00/00/wKgB-Vkb2yuAEk80AAAABpDVNbM781.txt 

 (能返回以上文件 ID,说明文件上传成功) 

 

 五、在各存储节点(192.168.1.247、192.168.1.248、192.168.1.249、192.168.1.250)上安装 Nginx 

解压 fastdfs-nginx-module_v1.16.tar.gz 到/usr/local/local/fast,解压 
# cd /usr/local/fast/
# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz 
 
修改 fastdfs-nginx-module 的 config 配置文件 
 # vi /usr/local/src/fastdfs-nginx-module/src/config 修改第四行
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/" 
修改为:CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/" 
 (注意:这个路径修改是很重要的,不然在 nginx 编译的时候会报错的) 


安装nginx:

tar -zxvf  nginx-1.6.2.tar.gz  -C /usr/local/

安装编译 Nginx 所需的依赖包 
yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel 
openssl openssl-devel 

进入解压目录 cd /usr/local/nginx-1.6.2 

编译安装:
 ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src 
 make && make install 

复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录,并修改 
cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ 
 vi /etc/fdfs/mod_fastdfs.conf 
(1)第一组 Storage 的 mod_fastdfs.conf 配置如下: 

connect_timeout=20 
base_path=/tmp 
tracker_server=192.168.1.251:22122 
tracker_server=192.168.1.252:22122 
 storage_server_port=23000 
group_name=group1    #第一组为group1 第二组为group2
url_have_group_name = true 
store_path0=/fastdfs/storage 
group_count = 2 
[group1] 
group_name=group1 
storage_server_port=23000 
store_path_count=1 
store_path0=/fastdfs/storage 
[group2] 
group_name=group2 
storage_server_port=23000 
store_path_count=1 
store_path0=/fastdfs/storage 


 
复制 FastDFS 的部分配置文件到/etc/fdfs 目录 
 cd /usr/local/fast/FastDFS/conf 
cp http.conf mime.types /etc/fdfs/ 

在/fastdfs/storage 文件存储目录下创建软连接,将其链接到实际存放数据的目录 
 ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00 


配置nginx, 配置样例: 

# vi /usr/local/nginx/conf/nginx.conf 

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([0-9])/M00 { 
            #alias /fastdfs/storage/data; 
            ngx_fastdfs_module; 
        } 
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   html; 
        } 
    } 
} 


修改防火墙开放端口8888:

vi /etc/sysconfig/iptables  

增加    -A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT 

 重启防火墙:service iptables restart 

启动nginx : /usr/local/nginx/sbin/nginx 

此时可以用浏览器测试:

http://192.168.1.248/group2/M00/00/00/wKgB-Vkb2yuAEk80AAAABpDVNbM781.txt 


六 、在tracker上配置nginx (在节点 92.168.1.251、192.168.1.252)

安装nginx 这里就不在说了  ,上面已经介绍

修改nginx配置文件

vi /usr/local/nginx/conf/nginx.conf 

#user  nobody;
worker_processes  1;
error_log  logs/error.log  info;

events {
    worker_connections  1024;
    use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
  
    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;
    proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2
    keys_zone=http-cache:200m max_size=1g inactive=30d;
    proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;
    #设置 group1 的服务器 
    upstream fdfs_group1 {
         server 192.168.1.247:8888 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.1.248:8888 weight=1 max_fails=2 fail_timeout=30s;
   }
   #设置 group2 的服务器 
    upstream fdfs_group2 {
         server 192.168.1.249:8888 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.1.250:8888 weight=1 max_fails=2 fail_timeout=30s;
    }
    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;
         #设置 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.1.0/24;
            deny all;
            proxy_cache_purge http-cache $1$is_args$args;
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }

}
按以上 nginx 配置文件的要求,创建对应的缓存目录: 
mkdir -p /fastdfs/cache/nginx/proxy_cache 

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


修改防火墙开放端口8000:

vi /etc/sysconfig/iptables  

增加    -A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT 

 重启防火墙:service iptables restart 

启动nginx : /usr/local/nginx/sbin/nginx 

再次测试

http://192.168.1.252:8000/group2/M00/00/00/wKgB-Vkb2yuAEk80AAAABpDVNbM781.txt
http://192.168.1.251:8000/group2/M00/00/00/wKgB-Vkb2yuAEk80AAAABpDVNbM781.txt 


七、keepalive

 安装参见:http://blog.csdn.net/sunqingzhong44/article/details/71520913

nginx的配置如下:


 upstream fastdfs_tracker { 
         server 192.168.1.251:8000 weight=1 max_fails=2 fail_timeout=30s; 
         server 192.168.1.252:8000 weight=1 max_fails=2 fail_timeout=30s; 
    } 
 
 

service keepalived start #启动keepalived

测试:

http://192.168.1.124:8000/group2/M00/00/00/wKgB-Vkb2yuAEk80AAAABpDVNbM781.txt 

配置完成 ,配置十分繁琐


你可能感兴趣的:(fdfs)