CentOS 搭建 FastDFS-5.0.5集群

http://www.open-open.com/lib/view/open1435468300700.html 

   第一步,确定目标:

      Tracker  192.168.224.20:22122  CentOS

      Group1-Storage11   192.168.224.25:23000  CentOS

      Group1-Storage12   192.168.224.26:23000  CentOS

      虽然上述集群结构中tracker是单一节点,如果在生产环境中,这是不正确的,但现在我们主要是想通过这次的搭建熟悉基本的搭建流程,所以此处就暂时忽略吧。

 

      第二步,确定安装版本:

   分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 

      

      第四步,文件夹初始化: 

      按照我们第一步确认的目标,我们可以现在自由选择一个路径,创建一些文件夹目录,这些目录后边会在配置tracker和storage的过程中被用到。我在用虚拟机创建的时候,是在/opt这个目录下面创建相关的文件夹,详细如下,仅供参考:

      1. 配置tracker所需的base_path: /opt/fastdfs_tracker。

      2. 配置storage所需的日志目录: /opt/fastdfs_storage_info。 备注: 这个目录是用来存储storage之间同步文件等日志的

      3. 配置storage所需的存储文件目录: /opt/fastdfs_storage_data。备注: 这个目录是用来存储文件的

      其实,我们在使用过程中并不是每个物理机上都会同时部署tracker和storage,那么根据自己的实际情况,如果只部署storage,则就创建storage的那两个文件夹;反之则只创建tracker的那个文件夹。后边在解释配置文件如何配置的过程中我将会按照上边的目录路径做配置,大家可以比对着看每个路径对应哪个配置。

 

      第五步,安装libfastcommon-1.0.7.zip:

          解压后,安装  ./make.sh 和 ./make.sh install 这两个命令,完成libfastcommon的安装。

            分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 

       5.  注意,上述安装的路径在/usr/lib64/,但是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-5.05.tar.gz:

      前 边几步如果安装的都正确的话,这一步就比较简单了,解压缩fastdfs-5.05.tar.gz,然后依次执行 ./make.sh 和 ./make.sh install 这两个命令,过程中没有报错,并且打开 /etc/fdfs 这个目录发现有配置文件存在,就说明安装成功了。

       分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群     


      第七步,配置tracker:

      前 边的这六步无论是配置tracker还是配置storage都是必须的,而tracker和storage的区别主要是在安装完fastdfs之后的配置 过程中。我们在第一步确认目标时,计划在192.168.224.20这台虚拟机上配置tracker,所以我们就在这台机器上演示。

      1. 进入/etc/fdfs文件夹,执行命令: cp  tracker.conf.sample  tracker.conf。

      2. 编辑tracker.conf,执行命令: vi  tracker.conf ,将以下几个选项进行编辑:

          a. disabled=false            #启用配置文件

          b. port=22122                #设置tracker的端口号,一般采用22122这个默认端口

          c. base_path=/opt/fastdfs_tracker   #设置tracker的数据文件和日志目录(预先创建)

          d. http.server_port=8080     #设置http端口号   注意,这个配置在fastdfs5.05这个版本中已经不用配置,不用管这个!

          分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群

      3. 启动tracker,执行如下命令: /usr/local/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

          注意,一般fdfs_trackerd等命令在/usr/local/bin中没有,而是在/usr/bin路径下,所以命令修改如下: /usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

      4. 启动完毕后,可以通过以下两个方法查看tracker是否启动成功:

          a. netstat -unltp|grep fdfs,查看22122端口监听情况 

          b. 通过以下命令查看tracker的启动日志,看是否有错误: tail -100f  /opt/fastdfs_tracker/logs/trackerd.log

      5. 如果启动没有问题,可以通过以下步骤,将tracker的启动添加到服务器的开机启动中:

          a. 打开文件 vi /etc/rc.d/rc.local

          b. 将如下命令添加到该文件中 /usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

 

 

      第八步,配置storage:

      其实配置storage和配置tracker类似,只不过配置文件和配置内容不一样。我们以配置192.168.224.29配置storage为例。

      1. 进入/etc/fdfs文件夹,执行命令: cp  storage.conf.sample  storage.conf。

      2. 编辑storage.conf,执行命令: vi  storage.conf ,将以下几个选项进行编辑:

           a. disabled=false            #启用配置文件

           b. group_name=group2    #组名,根据实际情况修改

           c. port=23001     #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致

           d. base_path=/opt/fastdfs_storage_info    #设置storage的日志目录(需预先创建)

           e. store_path_count=1   #存储路径个数,需要和store_path个数匹配

           f.  store_path0=/opt/fastdfs_storage_data   #存储路径

           g. tracker_server=192.168.224.20:22122   #tracker服务器的IP地址和端口号

           h. http.server_port=8080     #设置http端口号   注意,这个配置在fastdfs5.05这个版本中已经不用配置,不用管这个!

           分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群          

           分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群

           分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群

      3. 启动storage,执行如下命令: /usr/local/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

          注意,一般fdfs_storage等命令在/usr/local/bin中没有,而是在/usr/bin路径下,所以命令修改如下: /usr/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

      4. 启动完毕后,可以通过以下两个方法查看storage是否启动成功:

          a. netstat -unltp|grep fdfs,查看23001端口监听情况 

          b. 通过以下命令查看storage的启动日志,看是否有错误: tail -100f  /opt/fastdfs_storage_info/logs/storage.log

      5. 启动成功后,可以通过fdfs_monitor查看集群的情况,即storage是否已经注册到tracker服务器中

          /usr/bin/fdfs_monitor /etc/fdfs/storage.conf

          查看192.168.224.29:23001 是ACTIVE状态即可

      6. 如果启动没有问题,可以通过以下步骤,将storage的启动添加到服务器的开机启动中:

          a. 打开文件 vi /etc/rc.d/rc.local

          b. 将如下命令添加到该文件中 /usr/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

      

================================== 长长的分割 线===========================================   

      以上八步其实已经完成了fastdfs的配置,如果此时你用java等api编辑客户端,实际上就可以完成文件的上传、同步和下载。但是为什么网上还会有 很多人说需要nginx呢???其实主要原因时因为,我们可以通过配置nginx为下载提供基于http协议的下载等功能。

      其实,storage中安装nginx,主要是为了为提供http的访问服务,同时解决group中storage服务器的同步延迟问题。而tracker中安装nginx,主要是为了提供http访问的反向代理、负载均衡以及缓存服务 

 

      第九步,安装nginx的准备:

      不管是在tracker中还是storage中安装nginx,前提都需要安装一些基础软件。一些大公司的服务器默认都会初始化这些软件,但是你在配置的时候可能还是最好自己利用命令确认一下。

      1. yum install -y gcc  这个前边在安装libfastcommon之前已经安装了

      2. yum install -y gcc-c++ 这个前边在安装libfastcommon之前已经安装了

      3. yum install -y pcre pcre-devel

      4. yum install -y zlib zlib-devel

      5. yum install -y openssl openssl-devel

      

      第十步,在storage中安装nginx:

      1. 创建nginx默认的安装文件夹: mkdir /usr/local/nginx

      2. 提前将 nginx1.7.8.tar.gz 和 fastdfs-nginx-module_v1.16.tar.gz 解压缩,然后进入nginx1.7.8的文件夹目录,执行如下命令:

          ./configure  --prefix=/usr/local/nginx  --add-module=/myself_settings/fastdfs-nginx-module/fastdfs-nginx-module/src

      3. 执行完上述命令,如果没有报错的话,我们继续执行 make 命令,此时编译有可能会报错,那是因为我们忘了做一项重要的工作,我们可以参考下面这篇文章http://bbs.chinaunix.net/thread-4163021-1-1.html中的解答,其实汇总就是下面两个意思:

          a. 我们在解压缩fastdfs-nginx-module_v1.16.tar.gz之后,需要进入fastdfs-nginx-module/src目 录,编辑config文件,找到包含CORE_INCS这个一行,将路径中local全部去掉,变为CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

          b. 建立软连接,这个软连接我们在第五步安装libfastcommon时的最后一个操作就已经建立了,所以此处不用再建立了。

      4. 再依次执行 make 和 make install 这两个命令,没有报错误就是安装成功了。

      5. 执行命令 cd /usr/local/nginx/conf,编辑 nginx.conf 这个文件,编辑如下:

          listen       8080;

          在server段中添加: 

          location ~/group[1-3]/M00{

                 root/fdfs/storage/data;

                 ngx_fastdfs_module;

          }

          注意,如果配置的storage是在group2组,则下面的location应该是 ~/group2/M00

          分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群

      6. 执行命令 cd /myself_settings/fastdfs5.0.5/fastdfs-5.05/conf,即进入fastdfs5.0.5的安装文件夹的conf目录下,将目录下面的http.conf和mime.types拷贝到/etc/fdfs/下,如果不执行这一步,后边在启动nginx时会报错。

      7. 执行命令 cd /myself_settings/fastdfs_nginx_module/fastdfs-nginx-module/src,即进入 fastdfs-nginx-module_v1.16的安装文件夹的src目录下,将目录下面的mod_fastdfs.conf这个文件拷贝到 /etc/fdfs 目录下。

      8. 打开 /etc/fdfs 这个目录,编辑 mod_fastdfs.conf 这个文件,如下:

          注意以下配置是group2的第二个storage 192.168.224.29的配置,按照第一步确定的集群目标来配置的,供大家参考。

          a. base_path=/opt/fastdfs_storage_info   #保存日志目录

          b. tracker_server=192.168.224.20:22122   #tracker服务器的IP地址以及端口号

          c. storage_server_port=23001   #storage服务器的端口号

          d. group_name=group2  #当前服务器的group名

          c. url_have_group_name= true        #文件url中是否有group名

          d. store_path_count=1          #存储路径个数,需要和store_path个数匹配

          e. store_path0=/opt/fastdfs_storage_data         #存储路径

          f.  http.need_find_content_type=true     #从文件扩展名查找文件类型(nginx时为true)  注意:这个配置网上一般都会列出,但是在fastdfs5.05的版本中是没有的

          h. group_count= 2       #设置组的个数

          i.  在文件的末尾,按照第一步确定的集群目标,追加如下图的配置:

          分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 

      9. 建立软连接 ln  -s  /opt/fastdfs_storage_data/data  /opt/fastdfs_storage_data/data/M00

      10. 执行命令启动nginx: /usr/local/nginx/sbin/nginx ,如果/usr/local/nginx/logs/error.log中没有报错,同时访问192.168.224.29:8080这个url能看到 nginx的欢迎页面。

 

      第十一步,在tracker中安装nginx:

      注意,tracker中nginx安装时比storage中的nginx安装时多安装一个nginx的缓存模块,同时在配置的时候有很大的不同。

      1. 创建nginx默认的安装文件夹: mkdir /usr/local/nginx

      2. 提前将 nginx1.7.8.tar.gz、fastdfs-nginx-module_v1.16.tar.gz、ngx_cache_purge-2.1.tar.gz解压缩,然后进入nginx1.7.8的文件夹目录,执行如下命令:

          ./configure  --prefix=/usr/local/nginx  --add-module=/myself_settings/fastdfs-nginx-module/fastdfs-nginx-module/src  --add-module=/myself_settings/ngx_cache_purge/ngx_cache_purge-2.1

      3. 执行完上述命令,如果没有报错的话,我们继续执行 make 命令,此时编译有可能会报错,那是因为我们忘了做一项重要的工作,我们可以参考下面这篇文章http://bbs.chinaunix.net/thread-4163021-1-1.html中的解答,其实汇总就是下面两个意思:

          a. 我们在解压缩fastdfs-nginx-module_v1.16.tar.gz之后,需要进入fastdfs-nginx-module/src目 录,编辑config文件,找到包含CORE_INCS这个一行,将路径中local全部去掉,变为CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

          b. 建立软连接,这个软连接我们在第五步安装libfastcommon时的最后一个操作就已经建立了,所以此处不用再建立了。

      4. 执行完3个步骤后,我们在重复执行2中的命令,然后再依次执行 make 和 make install 这两个命令,没有报错误就是安装成功了。

      5. 执行命令 cd /usr/local/nginx/conf,编辑 nginx.conf 这个文件,编辑如下:

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
     worker_connections  1024;
}
 
 
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 /opt/cache/nginx/proxy_cache  levels=1:2
     keys_zone=http-cache:500m max_size=10g inactive=30d;
     proxy_temp_path /opt/cache/nginx/proxy_cache/tmp ;
 
     upstream fdfs_group1 {
          server 192.168.224.25:8080 weight=1 max_fails=2 fail_timeout=30s;
          server 192.168.224.26:8080 weight=1 max_fails=2 fail_timeout=30s;
     }
     
     upstream fdfs_group2 {
          server 192.168.224.28:8080 weight=1 max_fails=2 fail_timeout=30s;
          server 192.168.224.29:8080 weight=1 max_fails=2 fail_timeout=30s;
     }
 
     server {
         listen       8080;
         server_name  localhost;
 
         #charset koi8-r;
 
         #access_log  logs/host.access.log  main;
 
         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.224.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;
         }
 
         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
         #
         #location ~ \.php$ {
         #    proxy_pass   http://127.0.0.1;
         #}
 
         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
         #
         #location ~ \.php$ {
         #    root           html;
         #    fastcgi_pass   127.0.0.1:9000;
         #    fastcgi_index  index.php;
         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
         #    include        fastcgi_params;
         #}
 
         # deny access to .htaccess files, if Apache's document root
         # concurs with nginx's one
         #
         #location ~ /\.ht {
         #    deny  all;
         #}
     }
 
     # another virtual host using mix of IP-, name-, and port-based configuration
     #
     #server {
     #    listen       8000;
     #    listen       somename:8080;
     #    server_name  somename  alias  another.alias;
 
     #    location / {
     #        root   html;
     #        index  index.html index.htm;
     #    }
     #}
 
 
     # HTTPS server
     #
     #server {
     #    listen       443 ssl;
     #    server_name  localhost;
 
     #    ssl_certificate      cert.pem;
     #    ssl_certificate_key  cert.key;
 
     #    ssl_session_cache    shared:SSL:1m;
     #    ssl_session_timeout  5m;
 
     #    ssl_ciphers  HIGH:!aNULL:!MD5;
     #    ssl_prefer_server_ciphers  on;
 
     #    location / {
     #        root   html;
     #        index  index.html index.htm;
     #    }
     #}
 
}
pasting

 

    6. 执行命令启动nginx: /usr/local/nginx/sbin/nginx ,如果/usr/local/nginx/logs/error.log中没有报错,同时访问192.168.224.20:8080这个url能看到 nginx的欢迎页面。

 

      第十二步,测试上传:

      1. 打开 /etc/fdfs 文件夹,编辑 client.conf 文件,编辑内容如下:

          a. base_path=/opt/fastdfs_tracker      #存放路径

          b. tracker_server=192.168.224.20:22122          #tracker服务器IP地址和端口号

          c. http.tracker_server_port=8080              #tracker服务器的http端口号,注意,这个配置在fastdfs5.0.5中已经没有用了

      2. 模拟上传文件,执行如下命令: /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /opt/1.txt

          使用浏览器访问返回的url: http://192.168.224.20:8080/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

          也可以直接访问文件所在的storage: http://192.168.224.25:8080/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

          上述如果访问成功,会在 tracker 192.168.224.20 的 /opt/cache中产生缓存

          我们还可以通过在url中添加purge清除缓存,例如: http://192.168.224.20:8080/purge/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

 

[2016-01-22 07:23:18] ERROR - file: ../client/client_func.c, line: 257, “/home/yuqing/fastdfs” can’t be accessed, error info: No such file or directory 
[root@mall_hc_244 nginx-1.4.2]#

修改配置文件:/etc/fdfs/client.conf: 
base_path=/home/storage/fastdfs 
tracker_server=192.168.121.244:22122

关闭防火墙: 
iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 8080 -j ACCEPT

       参考资料:

       http://blog.csdn.net/lynnlovemin/article/details/39398043    fastdfs集群的配置教程

       http://blog.csdn.net/poechant/article/details/6977407   fastdfs系列教程  

        http://m.blog.csdn.net/blog/hfty290/42030339     tracker-leader的选举 

 

附件如下:
 libfastcommon-1.0.7.zip (94.85 KB, 下载次数: 446) 
 FastDFS_v5.05.tar.gz (337.3 KB, 下载次数: 445) 
 nginx-1.7.8.tar.gz (803.83 KB, 下载次数: 548) 
 fastdfs-nginx-module_v1.16.tar.gz (17.1 KB, 下载次数: 371) 
 fastdfs_client_java_v1.10.tar.gz (17.08 KB, 下载次数: 312) 
 FastDFS_Java_Doc_v1.20.tar.gz (68.95 KB, 下载次数: 354) 

猜你喜欢

转载自blog.csdn.net/xll_csdn/article/details/78803689