手把手教你搭建FastDFS集群(下)

  因为博客图片量大,篇幅太长,所以须要分上,中,下三篇博客来写,上篇和中篇咱们已经一块儿学习完了,这篇博客咱们一块儿学习下剩余部分。php

一,配置反向代理html

       咱们须要在两个跟踪器上安装的nginx(也就是192.168.156.5和192.168.156.6)以提供反向代理服务,目的是使用统一的一个IP地址对外提供服务。为了不一些没必要要的错误,咱们先把其它四台虚拟机的窗口关掉。nginx

       1.解压ngx_cache_purge-2.3.tar.gz,解压命令:tar -zxvf ngx_cache_purge-2.3.tar.gz -C / usr / local / fast /,以下图所示(另外一台设备就不粘贴图片了)。缓存

         解压完以后咱们在在/ usr /本地/快/目录下能够看到多了一个ngx_cache_purge-2.3文件夹。以下图所示。服务器

      2.下载须要的依赖库,在两台设备上依次执行下面四条命令。cookie

 

[html] 查看纯文本 session

  1. yum安装pcre  
  2. yum安装pcre-devel  
  3. yum安装zlib  
  4. yum安装zlib-devel  

     3.为两台设备都安装nginx,咱们在XShell的下方的输入框中输入命令:cd / usr / local / software /并敲回车,两个窗口都会进入/ usr / local / software目录下,而后在下面的输入框再输入 “LL” 来查看在/ usr /本地/软件目录下的文件,以下图所示(只有输入框左边的图标是多窗口的状况下才能一次做用全部窗口,若是当前是单窗口图标,就以下图那样选择所有XSHELL)。app

 

      接着,咱们在下面的输入框中输入:tar -zxvf nginx-1.6.2.tar.gz -C / usr / local /并按回车,会在两个窗口同时执行解压操做以下图所示。
 负载均衡

      接下来咱们在下面的输入框中输入:cd / usr / local并按回车,两台设备都进入/usr/local/nginx-1.6.2目录下。以下图所示。tcp

        接着,在下面的输入框中加入模块命令:./configure --add-module = / usr / local / fast / ngx_cache_purge-2.3,回车就会在两台设备上都执行添加缓存模块并会检查环境。

         接着在下面的输入框中输入命令:make && make install,回车就会在两台设备上都执行编译安装以下图所示。

       下面咱们须要修改下两台设备在/ usr /本地/ nginx的/ conf目录/目录下的nginx.conf文件,你们能够直接把下面代码替换这个目录下的该文件,也能够直接到:HTTP://下载。 csdn .NET / detail / u012453843 / 9803673这个地址下载nginx.conf文件来替换。不过因为咱们搭建环境的虚拟机IP可能不同,所以,咱们须要根据实际状况修改下IP等信息(注意192.168.156.5和192.168.156.6这两台设备的在/ usr /本地/ nginx的/ CONF /目录下的nginx.conf都要修改

 

[html] 查看纯文本 

  1. #user nobody  
  2. worker_processes 1;  
  3.   
  4. #error_log logs / error.log;  
  5. #error_log logs / error.log notice;  
  6. #error_log logs / error.log info;  
  7.   
  8. #pid logs / nginx.pid;  
  9.   
  10.   
  11. 事件{  
  12.     worker_connections 1024;  
  13.     使用epoll  
  14. }  
  15.   
  16.   
  17. http {  
  18.     包括mime.types;  
  19.     default_type application / octet-stream;  
  20.   
  21.     #log_format main'$ remote_addr - $ remote_user [$ time_local]“$ request”'  
  22.     #'$ status $ body_bytes_sent“$ http_referer”'  
  23.     #'“$ http_user_agent”“$ http_x_forwarded_for”';  
  24.   
  25.     #access_log logs / access.log main;  
  26.   
  27.     发送文件;  
  28.     tcp_nopush on;  
  29.     #tcp_nopush on  
  30.   
  31.     #keepalive_timeout 0;  
  32.     keepalive_timeout 65;  
  33.   
  34.     #gzip on;  
  35.     #设置缓存  
  36.     server_names_hash_bucket_size 128;  
  37.     client_header_buffer_size 32k;  
  38.     large_client_header_buffers 4 32k;  
  39.     client_max_body_size 300m;  
  40.   
  41.     proxy_redirect关闭  
  42.     proxy_set_header主机$ http_host;  
  43.     proxy_set_header X-Real-IP $ remote_addr;  
  44.     proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  45.     proxy_connect_timeout 90;  
  46.     proxy_send_timeout 90;  
  47.     proxy_read_timeout 90;  
  48.     proxy_buffer_size 16k;  
  49.     proxy_buffers 4 64k;  
  50.     proxy_busy_buffers_size 128k;  
  51.     proxy_temp_file_write_size 128k;  
  52.     #设置缓存存储路径,存储方式,分别内存大小,磁盘最大空间,缓存期限  
  53.     proxy_cache_path / fastdfs / cache / nginx / proxy_cache  levels = 1 :2  
  54.     keys_zone = http -cache:200m  max_size = 1g inactive = 30d ;   
  55.     proxy_temp_path / fastdfs / cache / nginx / proxy_cache / tmp;  
  56.     #1组的服务设置  
  57.     上游fdfs_group1 {  
  58.          服务器192.168.156.7:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  59.          服务器192.168.156.8:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  60.     }  
  61.     #第2组的服务设置  
  62.     上游fdfs_group2 {  
  63.          服务器192.168.156.9:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  64.          服务器192.168.156.10:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  65.     }  
  66.   
  67.     服务器{  
  68.         听8000;  
  69.         server_name localhost;  
  70.   
  71.         #charset koi8-r  
  72.   
  73.         #access_log logs / host.access.log main;  
  74.         #1组的负载均衡配置  
  75.         位置/ group1 / M00 {  
  76.             proxy_next_upstream http_502 http_504错误超时invalid_header;  
  77.             proxy_cache http-cache;  
  78.             proxy_cache_valid 200 304 12h;  
  79.             proxy_cache_key $ uri $ is_args $ args;  
  80.             #对应组1的服务设置  
  81.             proxy_pass http:// fdfs_group1;  
  82.             到期30d;  
  83.         }  
  84.   
  85.         位置/ group2 / M00 {  
  86.             proxy_next_upstream http_502 http_504错误超时invalid_header;  
  87.             proxy_cache http-cache;  
  88.             proxy_cache_valid 200 304 12h;  
  89.             proxy_cache_key $ uri $ is_args $ args;  
  90.             #对应组2的服务设置  
  91.             proxy_pass http:// fdfs_group2;  
  92.             到期30d;  
  93.          }  
  94.   
  95.         位置〜/ purge(/.*){  
  96.             容许127.0.0.1;  
  97.             容许192.168.156.0/24;  
  98.             拒绝全部  
  99.             proxy_cache_purge http-cache $ 1 $ is_args $ args;  
  100.         }  
  101.   
  102.         位置 / {  
  103.             根html;  
  104.             index index.html index.htm;  
  105.         }  
  106.   
  107.         #error_page 404 /404.html;  
  108.   
  109.         #将服务器错误页面重定向到静态页面/50x.html  
  110.         #  
  111.         error_page 500 502 503 504 /50x.html;  
  112.         location  = /50x.html {  
  113.             根html;  
  114.         }  
  115.   
  116.         #代码PHP脚本到Apache在127.0.0.1:80上监听  
  117.         #  
  118.         #location〜\ .php $ {  
  119.         #proxy_pass http://127.0.0.1;  
  120.         #}  
  121.   
  122.         #将PHP脚本传递给FastCGI服务器,侦听127.0.0.1:9000  
  123.         #  
  124.         #location〜\ .php $ {  
  125.         #root html;  
  126.         #fastcgi_pass 127.0.0.1:9000;  
  127.         #fastcgi_index index.php;  
  128.         #fastcgi_param SCRIPT_FILENAME / scripts $ fastcgi_script_name;  
  129.         #include fastcgi_params;  
  130.         #}  
  131.   
  132.         #deny访问.htaccess文件,若是是Apache的文档根  
  133.         #赞成nginx的  
  134.         #  
  135.         #location〜/\.ht {  
  136.         #deny all;  
  137.         #}  
  138.     }  
  139.   
  140.   
  141.     #另外一个使用IP-,基于名称和端口配置的虚拟主机  
  142.     #  
  143.     #server {  
  144.     #听8000;  
  145.     #听somename:8080;  
  146.     #server_name somename alias another.alias;  
  147.   
  148.     # 位置 / {  
  149.     #root html;  
  150.     #index index.html index.htm;  
  151.     #}  
  152.     #}  
  153.   
  154.   
  155.     #HTTPS服务器  
  156.     #  
  157.     #server {  
  158.     #listen 443 ssl;  
  159.     #server_name localhost;  
  160.   
  161.     #ssl_certificate cert.pem;  
  162.     #ssl_certificate_key cert.key;  
  163.   
  164.     #ssl_session_cache shared:SSL:1m;  
  165.     #ssl_session_timeout 5m;  
  166.   
  167.     #ssl_ciphers HIGH:!aNULL:!MD5;  
  168.     #ssl_prefer_server_ciphers on;  
  169.   
  170.     # 位置 / {  
  171.     #root html;  
  172.     #index index.html index.htm;  
  173.     #}  
  174.     #}  
  175.   
  176. }                               

      修改完nginx.conf文件以后,咱们下面须要建立/ fastdfs /缓存/ nginx的/ proxy_cache和/ fastdfs /缓存/ nginx的/ proxy_cache的/ tmp目录,这是由于咱们在nginx.conf文件中配置缓存路径时指定了该目录,可是这两个目录目前还不存在,所以咱们须要在192.168.156.5和192.168.156.6这两台设备上都建立下这两个目录,因为涉及到多级,所以须要递归建立目录,使用命令:mkdir -p / fastdfs / cache / nginx / proxy_cache和mkdir -p / fastdfs / cache / nginx / proxy_cache / tmp,以下图所示。

 

       因为咱们配置了两个跟踪器的访问端口是8000,而咱们的防火墙是不容许访问该端口的,所以咱们须要修改下防火墙,使其容许访问8000端口,这个操做我在上篇和中篇都介绍过了,这里就不啰嗦了。

       下面咱们便来启动这两台设备上的nginx。启动所用的命令是/ usr / local / nginx / sbin / nginx。启动完后以后,可使用ps -ef | grep nginx命令来查看nginx是否正常启动,若是看到根4027 1 0 08:18?00:00:00 nginx:master进程/ usr / local / nginx / sbin / nginx这条信息,说明正常启动了。

 

[html] 查看纯文本 

  1. [root @ itcast05 conf]#/ usr / local / nginx / sbin / nginx  
  2. [root @ itcast05 conf]#ps -ef | grep nginx  
  3. 根4027 1 0 08:18?00:00:00 nginx:主进程/ usr / local / nginx / sbin / nginx  
  4. 没有人4028 4027 0 08:18?00:00:00 nginx:worker进程        
  5. 没人4029 4027 0 08:18?00:00:00 nginx:缓存管理器进程  
  6. 没有人4030 4027 0 08:18?00:00:00 nginx:缓存加载器进程  
  7. root 4032 1522 0 08:18 pts / 0 00:00:00 grep nginx  

        两台设备都启动完nginx的以后,咱们再在192.168.156.5上上传两次次图片,第一次返回的路径是在组1下,第二次返回的路径是在组2下。

 

 

[html] 查看纯文本 

  1. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg  
  2. 组1 / M00 / 00/00 / wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg  
  3. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg  
  4. 组2 / M00 / 00/00 / wKicCVjkOeaAVb0dAAHk-VzqZ6w123.jpg  
  5. [root @ itcast05 conf]#   

        因为咱们在192.168.156.5和192.168.156.6上配置了代理,代理端口是8000,因此咱们能够访问这两个IP的8000端口来访问咱们刚才上传的图片,以下图所示(咱们访问的http:// 192.168.156.5:8000/group1/M00/00/00/wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg也能访问到该图片)。这说明咱们配置的代理彻底没问题。

       咱们知道,nginx的对外提供服务有可能碰到服务挂掉的时候,这时候高可用就显得异常重要了,所以如今咱们搭建一个nginx的和KEEPALIVED结合实现的nginx的集群高可用的环境,你们能够参考的http: //blog.csdn .net / u012453843 / article / details / 69668663这篇博客进行学习。

       咱们如今要把KEEPALIVED实现的nginx的集群高可用应用到咱们的FastDFS集群当中,如今用于搭建nginx的集群高可用的设备是192.168.156.11和192.168.156.12,咱们只须要修改下这两台设备的nginx的。的conf文件,配置文件以下

 

[html] 查看纯文本 

  1. #user nobody  
  2. worker_processes 1;  
  3.   
  4. #error_log logs / error.log;  
  5. #error_log logs / error.log notice;  
  6. #error_log logs / error.log info;  
  7.   
  8. #pid logs / nginx.pid;  
  9.   
  10.   
  11. 事件{  
  12.     worker_connections 1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     包括mime.types;  
  18.     default_type application / octet-stream;  
  19.   
  20.     #log_format main'$ remote_addr - $ remote_user [$ time_local]“$ request”'  
  21.     #'$ status $ body_bytes_sent“$ http_referer”'  
  22.     #'“$ http_user_agent”“$ http_x_forwarded_for”';  
  23.   
  24.     #access_log logs / access.log main;  
  25.   
  26.     发送文件;  
  27.     #tcp_nopush on  
  28.   
  29.     #keepalive_timeout 0;  
  30.     keepalive_timeout 65;  
  31.   
  32.     #gzip on;  
  33.       
  34.     上游fastdfs_tracker {  
  35.        服务器192.168.156.5:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  36.        服务器192.168.156.6:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  37.     }  
  38.   
  39.     服务器{  
  40.         听80  
  41.         server_name localhost;  
  42.   
  43.         #charset koi8-r  
  44.   
  45.         #access_log logs / host.access.log main;  
  46.   
  47.         location / fastdfs {  
  48.            根html;  
  49.            index index.html index.htm;  
  50.            proxy_pass http:// fastdfs_tracker /;  
  51.            proxy_set_header主机$ http_host;  
  52.            proxy_set_header Cookie $ http_cookie;  
  53.            proxy_set_header X-Real-IP $ remote_addr;  
  54.            proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  55.            proxy_set_header X-Forwarded-Proto $ scheme;  
  56.            client_max_body_size 300m;  
  57.         }  
  58.   
  59.         #error_page 404 /404.html;  
  60.   
  61.         #将服务器错误页面重定向到静态页面/50x.html  
  62.         #  
  63.         error_page 500 502 503 504 /50x.html;  
  64.         location  = /50x.html {  
  65.             根html;  
  66.         }  
  67.     }  
  68. }  


 

 

      咱们对配置文件作了两处修改,一处是添加了负载均衡上游fastdfs_tracker,以下所示咱们是把192.168.156.5和192.168.156.6两台设备做为追踪器,如今咱们加了一层nginx来代理这两个跟踪器。

 

[html] 查看纯文本 

  1. 上游fastdfs_tracker {  
  2.    服务器192.168.156.5:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  3.    服务器192.168.156.6:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  4. }  

      第二处修改是,添加了一个位置,而且匹配规则是路径当中有fastdfs以下所示。

 

 

[html] 查看纯文本 

  1. location / fastdfs {  
  2.            根html;  
  3.            index index.html index.htm;  
  4.            proxy_pass http:// fastdfs_tracker /;  
  5.            proxy_set_header主机$ http_host;  
  6.            proxy_set_header Cookie $ http_cookie;  
  7.            proxy_set_header X-Real-IP $ remote_addr;  
  8.            proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  9.            proxy_set_header X-Forwarded-Proto $ scheme;  
  10.            client_max_body_size 300m;  
  11.         }  

       作好了修改以后,咱们只需从新启动192.168.156.11和192.168.156.12这两台设备的nginx的便可。

 

 

[html] 查看纯文本 

  1. [root @ nginx1 conf]#/ usr / local / nginx / sbin / nginx -s reload  
  2. [root @ nginx1 conf]#  

[html] 查看纯文本 

  1. [root @ nginx2 conf]#/ usr / local / nginx / sbin / nginx -s reload  
  2. [root @ nginx2 conf]#  

       这样咱们便配置好了虚拟IP,如今咱们从192.168.156.5再上传一张图片,以下所示。

 

 

[html] 查看纯文本 

  1. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg   
  2. 组1 / M00 / 00/00 / wKicB1jqnPqARiT6AAHk-VzqZ6w956.jpg  
  3. [root @ itcast05 conf]#   

       咱们如今就用虚拟IP192.168.156.110来访咱们刚才上传的图片,只是注意在地址栏中要记得输入fastdfs(这是咱们nginx.conf文件中位置/ fastdfs {}规则规定的)以下图所示,发现,咱们经过虚拟IP即可以访问咱们上传的图片了。这样的好处是,对用户来讲,只须要访问这个虚拟IP就能够了,不用关心FastDFS集群内部的转发机制。

 



       这样咱们的FastDFS集群便搭建完了,搭建完后的集群图以下图所示。这个集群当中192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10这四台设备以8888端口对外提供服务,咱们使用追踪器跟踪器管理这四台存储设备,两个跟踪器的nginx的对外提供的端口号是8000,也就是咱们可使用两台跟踪的任何一台设备的IP而且使用端口8000来访问存储在存储上的资源文件。其实咱们彻底能够在两台跟踪设备上搭建KEEPALIVED和nginx的相结合的高可用环境而且对外提供虚拟IP192.168.156.110和端口80来访问资源文件。只不过这里为了展现多层nginx的负载均衡因此才在192.168.156.11和192.168.156.12上专门搭建了KEEPALIVED和nginx的相结合的高可用环境,由这两台设备对外提供虚拟IP服务,因为端口使用了默认的80,所以咱们在使用虚拟IP192。 168.156.110访问图片的时候才不用输入端口号的。

       备注:启动集群步骤

        1.启动6台设备(192.168.156.5,192.168.156.6,192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10)的nginx的(其中192.168.156.11和192.168.156.12配置了KEEPALIVED开机自启动,顺带会启动nginx的,所以这两台设备不用启动nginx的)

        2.启动跟踪器(192.168.156.5和192.168.156.6,启动命令:/etc/init.d/fdfs_trackerd start)

        启动存储(192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10,启动命令:/etc/init.d/fdfs_storaged start)

        这样FastDFS集群便都启动完了。

相关文章
相关标签/搜索