fastdfs分布式服务器搭建(高可用集群)

  • 这里在虚拟机上搭建的,真正服务器搭建同理,废话少说,开始正文
  • 首先准备一台母机(192.168.150.10),此母机上不搭建东西,只是为了后续虚拟机的克隆,我的是centos7.x
  • 准备基本环境
    • 检查c/c++ 环境
      yum -y install gcc gcc-c++
    • 检查zip/unzip
      yum -y install zip unzip
    • 检查vim(因为fastdfs会用到vim,否则安装失败)
      yum -y install vim*
    • 检查nginx所需包
      yum -y install pcre* zlib* opssl*
    • 关闭防火墙
      systemctl stop firewalld.service
    • 禁止防火墙开机启动
      systemctl disable firewalld
    • 准备软件压缩包所在文件夹
      makdir -p /usr/local/apps
    • 下载软件(按我的版本来,因为fastdfs存在版本兼容问题)
      wget https://codeload.github.com/happyfish100/fastdfs/tar.gz/V5.05
      wget https://codeload.github.com/happyfish100/libfastcommon/tar.gz/V1.0.36
      wget http://219.239.26.11/files/1095000009284083/nginx.org/download/nginx-1.8.1.tar.gz
      wget https://codeload.github.com/happyfish100/fastdfs-nginx-module/tar.gz/V1.20
      wget https://codeload.github.com/FRiCKLE/ngx_cache_purge/tar.gz/2.3
    • 所有从github下载的东西,需要重新命名文件名,后缀为tar.gz
  • 以母机(192.168.150.10)为镜像克隆出第一台tracker服务器(192.168.150.20)
  • 修改ipconfig
    vim /etc/sysconfig/network-scripts/[静态网卡名]
  • 搭建第一台tracker
    • 解压
      tar -zxvf libfastcommon.tar.gz
    • 移动
      mv libfastcommon ../
    • 安装
      进入 libfastcommon目录,执行:./make.sh && ./make.sh install
    • 安装fastdfs
    • 解压
      tar ...
    • 移动到/usr/local目录下
    • 进入fastdfs-5.0.5目录
    • 执行:./make.sh && ./make.sh install
    • 创建tracker所在目录mkdir -p /opt/fastdfs_tracker
    • 修改配置文件
      cd /etc/fdfs
      cp tracker.conf.sample tracker.conf
      vim tracker.conf

      disabled=false #启用配置文件(默认启用)
      port=22122 #设置tracker的端口号,通常采用22122这个默认端口
      base_path=/opt/fastdfs_tracker #设置tracker的数据文件和日志目录
      http.server_port=8080 #设置http端口号,默认为8080
    • 创建软引用(因为tracker的脚本里面路径是/usr/local/bin然而,实际安装却是在/usr/bin)

      ln -s /usr/bin/fdfs_trackerd /usr/local/bin
      ln -s /usr/bin/stop.sh /usr/local/bin
      ln -s /usr/bin/restart.sh /usr/local/bin
    • 配置ngx_cache_purge
      • 解压ngx_cache_purge-2.3移动到/usr/local目录下
      • 解压nginx-1.8.1移动到/usr/local目录下
      • 进入nginx-1.8.1目录
      • 执行:./configure --add-module=/usr/local/ngx_cache_purge-2.3
      • 编译安装make && make install
      • 创建缓存目录
        mkdir -p /fastdfs/cache/nginx/proxy_cache
        mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp
      • 修改配置文件
        vim /usr/local/nginx/conf/nginx.conf

        user root;
        worker_processes 1;
        events {
        worker_connections 1024;
        use epoll;
        }
        http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        tcp_nopush 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.150.22:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.150.23:8888 weight=1 max_fails=2 fail_timeout=30s;
        }
        # group2的服务设置
        upstream fdfs_group2 {
        server 192.168.150.24:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.150.25: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;
        # group1的负载均衡配置
        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;
        # 对应group1的服务设置
        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;
        # 对应group2的服务设置
        proxy_pass http://fdfs_group2;
        expires 30d;
        }
        location ~/purge(/.*) {
        allow 127.0.0.1;
        allow 192.168.150.0/255;
        deny all;
        proxy_cache_purge http-cache $1$is_args$args;
        }
        location / {
        root html;
        index index.html index.htm;
        }
        # 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;
        }
        }
        }
      • 注意:listen端口监听要和tracker一样,
  • 关闭(192.168.150.20)虚拟机,以(192.168.150.20)为镜像克隆第二台tracker(192.168.150.21,修改ipconfig前面说过了)
  • 以母机克隆第一台storage(192.168.150.22)
  • 同上安装libfastcommon和fastdfs(不需要配置tracker)
  • 配置storage
    • 修改storage配置文件
      vim /etc/fdfs/storage.conf

      disabled=false #启用配置文件(默认启用)
      group_name=group1 #组名,根据实际情况修改
      port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致
      base_path=/opt/fastdfs_storage #设置storage数据文件和日志目录
      store_path_count=1 #存储路径个数,需要和store_path个数匹配
      store_path0=/opt/fastdfs_storage_data #实际文件存储路径
      tracker_server=192.168.150.20:22122 #tracker 服务器的 IP地址和端口号,如果是单机搭建,IP不要写127.0.0.1,否则启动不成功(此处的ip是我的CentOS虚拟机ip)
      http.server_port=8888 #设置 http 端口号
    • 创建storage存放目录:
      mkdir -p /opt/fastdfs_storage
      mkdir -p /opt/fastdfs_storage_data
    • 创建软引用
      ln -s /usr/bin/fdfs_storaged /usr/local/bin
    • 解压fastdfs-nginx-module移动到/usr/local
    • 进入fastdfs-nginx-modul
    • 修改配置
      vim src/config
      去掉CORE_INCS中的local,因为实际安装在/usr/bin目录
    • 安装nginx
      • 解压nginx-1.8.1移动到/usr/local
      • 进入nginx-1.8.1,
      • 执行:
        ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module-master/src
      • 安装:make && make install
      • cp -r /usr/local/fastdfs-5.05/conf/http.conf /etc/fdfs/
      • cp -r /usr/local/fastdfs-5.05/conf/mime.types /etc/fdfs/
      • cp -r /usr/local/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
      • vim /etc/fdfs/mod_fastdfs.conf

        connect_timeout=10
        tracker_server=192.168.150.20:22122
        tracker_server=192.168.150.21:22122
        storage_server_port=23000//默认就是2300,不用做修改
        url_have_group_name=true
        store_path0=/opt/fastdfs_storage_data
        group_name=group1
        group_count=2
        [group1]
        group_name=group1
        storage_server_port=23000
        store_path_count=1
        store_path0=/opt/fastdfs_storage_data
        [group2]
        group_name=group2
        storage_server_port=23000
        store_path_count=1
        store_path0=/opt/fastdfs_storage_data
      • 修改nginx配置

        listen 8888;
        location ~/group([0-9])/M00 {
        ngx_fastdfs_module;
        }
      • 注意:此nginx监听端口要和storage中配置的http端口一样
  • 以(192.168.150.22)为虚拟机克隆第二台storage(192.168.150.23),不要做任何修改
  • 以(192.168.150.22)为虚拟机克隆第三台storage(192.168.150.24),修改storage.confgroup_name=group2,修改mod_fastdfs.confgroup_name=group2
  • 以第三台storage(192.168.150.24)克隆第四台storage(192.168.150.25),不要做任何修改
  • 以nginx为代理配置统一出口
  • 以母机为镜像克隆一台nginx虚拟机(192.168.150.26)
  • 解压nginx-1.8.1移动到/usr/local
  • 进入nginx-1.8.1
  • 执行:
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  • vim /usr/local/nginx/conf/nginx.conf

    
    # 以下在http模块里面
    
    upstream fastdfs_tracker {
           server 192.168.150.20:8080 weight=1 max_fails=2 fail_timeout=30s;
           server 192.168.150.21:8080 weight=1 max_fails=2 fail_timeout=30s;
    }
    
    # 以下在server模块里
    
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
           root html;
           index index.html index.htm;
           proxy_pass http://fastdfs_tracker/;
           proxy_set_header Host $http_host;
           proxy_set_header Cookie $http_cookie;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           client_max_body_size 300m;
        }
  • 到此所有配置以及完成了,下面配置开机启动脚本
  • 在两台tracker服务器中执行

    • chmod +x /etc/rc.d/rc.local
    • cd /root
    • vim tracker.sh

      
      #!/bin/bash
      
      service network restart;# 重启network,因为配置双网卡,不知道为什么每次都需要重启
      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf;# 启动nginx
      service fdfs_trackerd start;# 启动tracker服务
      
    • chmod +x /root/tracker.sh

    • vim /etc/rc.d/rc.local
      最后一行添加/root/tracker.sh
  • 在4台storage做如下配置

    • chmod +x /etc/rc.d/rc.local
    • cd /root
    • vim storage.sh

      
      #!/bin/bash
      
      service network restart;# 重启network,因为配置双网卡,不知道为什么每次都需要重启
      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf;# 启动nginx
      service fdfs_storaged start;# 启动storage
      
    • chmod +x /root/tracker.sh

    • vim /etc/rc.d/rc.local
      最后一行添加/root/storage.sh
  • 在最后一台nginx反向代理服务器做如下配置
    • chmod +x /etc/rc.d/rc.local
    • vim /etc/rc.d/rc.local
      最后一行添加/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 至此整个服务分布式集群已经搭建好了,当然可以搭建ntracker,方法同理,mstorage方法也同理,注意:我们虽然有4个storage,但是只有2个group,这样等于主-备,同一个组的一个storage挂了,另外一台立马顶上,当然也可搭建hnginx反向代理,在加一层slb或者lvs等等,都可以
  • 下面说下启动顺序,因为storage是基于tracker的,所以要先启动两台tracker,再启动4台storage,至于最后一台nginx服务,什么时候启动都可以
  • 检查tracker,storage是否启动成功
    netstat -unltp|grep fdfs看是否有22122,23000端口
  • 测试:
    • 在第一台tracker上准备一张图片,比如test.jpg
    • 修改client.config配置(默认是client.config.sample,需要拷贝一份为client.config)
    • 修改base_pathtrackerbase_path,tracker_server修改为实际的tracker服务器ip(不要写127.0.0.1)
    • /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/timg.jpg
    • 如果有结果返回就说明上传成功,比如:group1/M00/00/00/wKiWh1tawuqAAjwEAADX0sGyaUA511.jpg
    • 则可以通过nginx反向代理服务器访问:
      http://192.168.150.26/group1/M00/00/00/wKiWh1tawuqAAjwEAADX0sGyaUA511.jpg
  • 如此就是整个fastdfs分布式服务器搭建(高可用集群)
  • 本屌已搭建好的一套,请点击这里,提取码:id6a
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值