分布式文件系统之fastDFS部署

fastDFS下载地址:
https://github.com/happyfish100/fastdfs
部署参考文档:
http://joelhy.github.io/2015/01/27/FastDFS-v5-06-deploy/

我们接下来来部署fastDFS

部署结构:

192.168.42.150 node1 [Tracker]
192.168.42.151 node2 [group1 Storage]
192.168.42.152 node3 [group1 Storage]
192.168.42.153 node4 [group2 Storage]
192.168.42.154 node5 [group2 Storage]

FastDFS 5.x 取消了对 libevent 的依赖,添加了对 libfastcommon 的依赖。 本部署说明用到的软件版本

libfastcommon
git clone https://github.com/happyfish100/libfastcommon.git

FastDFS
git clone https://github.com/happyfish100/fastdfs.git

fastdfs-nginx-module
git clone https://github.com/happyfish100/fastdfs-nginx-module.git

准备工作(每个节点)

(1) 安装依赖的软件包

yum install git gcc perl -y

(2) 创建资源目录

mkdir /usr/local/src/FastDFS
cd /usr/local/src/FastDFS
git clone https://github.com/happyfish100/libfastcommon.git
git clone https://github.com/happyfish100/fastdfs.git
git clone https://github.com/happyfish100/fastdfs-nginx-module.git

[root@node5 FastDFS]# ls
fastdfs  fastdfs-nginx-module  libfastcommon

安装FastDFS,具体步骤:

(1)安装libfastcommon

cd libfastcommon && ./make.sh && ./make.sh install

(2)安装FastDFS

cd fastdfs && ./make.sh && ./make.sh install

(3)安装完成后,配置文件在目录/etc/fdfs下:

[root@node1 FastDFS]# ls /etc/fdfs/
client.conf.sample  storage.conf.sample  storage_ids.conf.sample  tracker.conf.sample

(4)所有可执行文件在目录/usr/bin下,以fdfs_开头:

[root@node1 FastDFS]# ls /usr/bin/fdfs_*
/usr/bin/fdfs_appender_test   /usr/bin/fdfs_download_file  /usr/bin/fdfs_test1
/usr/bin/fdfs_appender_test1  /usr/bin/fdfs_file_info      /usr/bin/fdfs_trackerd
/usr/bin/fdfs_append_file     /usr/bin/fdfs_monitor        /usr/bin/fdfs_upload_appender
/usr/bin/fdfs_crc32           /usr/bin/fdfs_storaged       /usr/bin/fdfs_upload_file
/usr/bin/fdfs_delete_file     /usr/bin/fdfs_test

(5)默认配置文件有些是没有复制到/etc/fdfs下的,我们需要把没有复制的也复制下

[root@node1 fastdfs-5.10]# ls conf/
anti-steal.jpg  client.conf  http.conf  mime.types  storage.conf  storage_ids.conf  tracker.conf

cd conf && cp anti-steal.jpg http.conf  mime.types  /etc/fdfs/

(6)将FastDFS,推送到其他节点也装上

scp -rp /usr/local/src/FastDFS root@192.168.42.151:/usr/local/src/
scp -rp /usr/local/src/FastDFS root@192.168.42.152:/usr/local/src/
scp -rp /usr/local/src/FastDFS root@192.168.42.153:/usr/local/src/
scp -rp /usr/local/src/FastDFS root@192.168.42.154:/usr/local/src/

(7)其他节点安装

yum install  gcc perl -y
cd /usr/local/src/FastDFS/libfastcommon && ./make.sh install
cd /usr/local/src/FastDFS/fastdfs && ./make.sh install
cd conf && cp anti-steal.jpg http.conf  mime.types  /etc/fdfs/
echo "

(8)检查是否安装成功

ls /usr/bin/fdfs_* && ls /etc/fdfs/

(9)修改配置文件

mkdir -p /fastdfs
cd /etc/fdfs
cp tracker.conf.sample tracker.conf
vim tracker.conf

base_path=/fastdfs
http.server_port=80

(10)设置开机自启动

[root@node1 fdfs]# chkconfig --add fdfs_trackerd
[root@node1 fdfs]# service fdfs_trackerd start
[root@node1 fdfs]# ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      1024                                           *:22122                                                      *:*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*   

vim http.conf
http.anti_steal.token_check_fail=/etc/fdfs/anti-steal.jpg

service fdfs_trackerd restart

配置Storage存储节点(node2,node3,node4,node5)

(1)修改配置文件

mkdir -p /data/fastdfs
cd /etc/fdfs
cp storage.conf.sample  storage.conf
vim storage.conf
base_path=/data/fastdfs
store_path0=/data/fastdfs
tracker_server=192.168.42.150:22122
http.server_port=80

(2)设置开机自启动

[root@node1 fdfs]# chkconfig --add fdfs_storaged
[root@node1 fdfs]# service fdfs_storaged start
#需要等一会儿才能看到
[root@node2 fdfs]# ss -tnl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      1024                  *:23000                             *:*                  
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                  :::22                               :::*                  
LISTEN     0      100                 ::1:25                               :::*

(3)将配置推送到,node3,node4,node5上

scp  storage.conf root@192.168.42.152:/etc/fdfs
scp  storage.conf root@192.168.42.153:/etc/fdfs
scp  storage.conf root@192.168.42.154:/etc/fdfs

(4)其他节点需要创建目录

mkdir -p /data/fastdfs

(5)node4,node5节点我们规定的是group2,因此我们需要在node4,node5节点更改

vim /etc/fdfs/storage.conf
group_name=group2

(6)在node3,node4,node5上操作

[root@node1 fdfs]# chkconfig --add fdfs_storaged
[root@node1 fdfs]# service fdfs_storaged start

在每个节点上查看保证都能监听23000

[root@node5 conf]# ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      1024                                           *:23000                                                      *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      100                                          ::1:25

(7)确认storage是否启动成功

cat /data/fastdfs/logs/storaged.log

client客户端配置

(1)在node1上配置

cd /etc/fdfs
mkdir -p /data/fastdfs/client
cp client.conf.sample client.conf

vim client.conf
base_path=/data/fastdfs/client
tracker_server=192.168.42.150:22122

FastDFS测试

(1)上传测试:

[root@node1 tmp]# fdfs_upload_file /etc/fdfs/client.conf /tmp/1.jpg
group1/M00/00/00/wKgql1lDVeeAeW1cAAIRjk6H6X0328.jpg

[root@node1 tmp]# fdfs_upload_file /etc/fdfs/client.conf /tmp/83e.gif
group1/M00/00/00/wKgqmFlDVfqAaUVGAAefBex1G80469.gif

[root@node1 tmp]# fdfs_upload_file /etc/fdfs/client.conf /tmp/zhuoku194.jpg
group1/M00/00/00/wKgql1lDVgmAQPoSAAW2iO4L3ms043.jpg

(2)查看文件信息:

fdfs_file_info /etc/fdfs/client.conf  group1/M00/00/00/wKgql1lDVeeAeW1cAAIRjk6H6X0328.jpg
[root@node1 tmp]# fdfs_file_info /etc/fdfs/client.conf  group1/M00/00/00/wKgql1lDVeeAeW1cAAIRjk6H6X0328.jpg
source storage id: 0
source ip address: 192.168.42.151
file create timestamp: 2017-06-16 11:52:07
file size: 135566
file crc32: 1317529981 (0x4E87E97D)

(3)下载测试:

cd /tmp && mkdir test && cd test
fdfs_download_file /etc/fdfs/client.conf  group1/M00/00/00/wKgql1lDVeeAeW1cAAIRjk6H6X0328.jpg
[root@node1 test]# ls
wKgql1lDVeeAeW1cAAIRjk6H6X0328.jpg

安装并配置 nginx

(1).因为FastDFS 5.0以后,默认不再支持http,因此我们需要在每个节点安装nginx,蛋疼

nginx 的fastdfs模块,我们之前已经下载好了

cd /usr/local/src/FastDFS
wget http://192.168.42.26/nginx-1.11.8.tar.gz
tar xvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
yum groupinstall "Development Tools" "Server Platform Developments" 
yum install pcre-devel openssl-devel  zlib-devel -y
groupadd -r nginx
useradd -r -g nginx nginx

./configure --with-pcre  --user=nginx --group=nginx  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-http_realip_module --add-module=../fastdfs-nginx-module/src

make && make install

复制mod_fastdfs.conf到/etc/fdfs/

cd /usr/local/src/FastDFS/fastdfs-nginx-module/src
cp mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs
cp storage_ids.conf.sample storage_ids.conf

编辑mod_fastdfs.conf

vim mod_fastdfs.conf

tracker_server=192.168.42.150:22122
#这里我们是group1的,node4,node5上需要改成group2
group_name=group1
store_path0=/data/fastdfs
base_path=/data/fastdfs/logs/
tracker_server=192.168.42.150:22122

创建软链接

cd /data/fastdfs/data
ln -s ../data/  M00

配置 nginx.conf,添加一个location

user  nginx;
worker_processes  auto;

location /M00 {
    root /data/fastdfs/data;
    ngx_fastdfs_module;
}

启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(2)node3,node4,node5上同样安装nginx

推送nginx-1.11.8
scp -rp nginx-1.11.8 root@192.168.42.152:/usr/local/src/FastDFS/
scp -rp nginx-1.11.8 root@192.168.42.153:/usr/local/src/FastDFS/
scp -rp nginx-1.11.8 root@192.168.42.154:/usr/local/src/FastDFS/

推送 mod_fastdfs.conf
cd /etc/fdfs
scp mod_fastdfs.conf root@192.168.42.152:/etc/fdfs/
scp mod_fastdfs.conf root@192.168.42.153:/etc/fdfs/
scp mod_fastdfs.conf root@192.168.42.154:/etc/fdfs/

推送nginx.conf
scp -rp /usr/local/nginx/conf/nginx.conf root@192.168.42.152:/usr/local/nginx/conf/
scp -rp /usr/local/nginx/conf/nginx.conf root@192.168.42.153:/usr/local/nginx/conf/
scp -rp /usr/local/nginx/conf/nginx.conf root@192.168.42.154:/usr/local/nginx/conf/

node4,node5,改下mod_fastdfs.conf
vim /etc/fdfs/mod_fastdfs.conf
group_name=group2

三个节点操作

cd /usr/local/src/FastDFS/nginx-1.11.8/ && make install

三个节点创建软链接

cd /data/fastdfs/data
ln -s ../data/  M00

启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

在浏览器测试(之前上传的图片在group1上)

http://192.168.42.152/M00/00/00/wKgql1lDjSGAej-6AAW2iO4L3ms072.jpg
http://192.168.42.153/
http://192.168.42.154/

(3)我们用tracker节点来做反代(node1),因此也要安装nginx,因为只是用反代的,不用安装fastdfs-nginx-module

cd /usr/local/src/FastDFS
wget http://192.168.42.26/nginx-1.11.8.tar.gz
tar xvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
yum install pcre-devel openssl-devel  zlib-devel -y
groupadd -r nginx
useradd -r -g nginx nginx

./configure --with-pcre  --user=nginx --group=nginx  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-http_realip_module 

make && make install

编辑nginx.conf

user  nginx;
worker_processes  auto;


在 http {} 中添加:
upstream fdfs1 {
    server   192.168.42.151:80;
    server   192.168.42.152:80;
    server   192.168.42.153:80;
    server   192.168.42.154:80;
}

在 server{} 中添加:
location /M00 {
    proxy_pass http://fdfs1;
}

FastDFS客户端

PHP扩展安装 client主机上安装的是php v5.4.33

cd ~/fdfs/FastDFS/php_client
/usr/local/php-5.4.33/bin/phpize
./configure  --with-php-config=/usr/local/php-5.4.33/bin/php-config
make && make install
mkdir /usr/local/php-5.4.33/etc/conf.d
cp fastdfs_client.ini /usr/local/php-5.4.33/etc/conf.d
PHP扩展使用
<?php
// fastdfs.php
class Fdfs {
    private $fdfs, $tracker, $storage;
    public function __construct() {
        $this->fdfs = new FastDFS();
        // get a connected tracker server
        $this->tracker = $this->fdfs->tracker_get_connection();
        if (!$this->tracker) {
            throw new Exception('cannot connect to tracker server:[' .  
                $this->fdfs->get_last_error_no() . '] ' . 
                $this->fdfs->get_last_error_info());
        }
        // get the storage server info and connect to it
        $this->storage = $this->fdfs->tracker_query_storage_store();
        $this->server = $this->fdfs->connect_server(
            $this->storage['ip_addr'], $this->storage['port']);
        if ($this->server === false) {
            throw new Exception('cannot connect to storage server' . 
                $this->storage['ip_addr'] . ':' . 
                $this->storage['port'] .  ' :[' .  
                $this->fdfs->get_last);
        }
        $this->storage['sock'] = $this->server['sock'];
    }

    public function upload($localfile, $ext_name) {
        //$info = $this->fdfs->storage_upload_by_filename($localfile);
        $info = $this->fdfs->storage_upload_by_filename($localfile, $ext_name, 
            array(), null, $this->tracker, $this->storage);
        if (is_array($info)) {
            $group_name = $info['group_name'];
            $remote_filename = $info['filename'];
            $source_info = $this->fdfs->get_file_info($group_name, 
                $remote_filename);
            $source_ip = $source_info['source_ip_addr'];
            $file_size = $source_info['file_size'];
            return compact('group_name', 'remote_filename', 
                'source_ip', 'file_size');
        }
        return false;
    }
    public function download_to_buff($group_name, $remote_filename) {
        $content = $this->fdfs->storage_download_file_to_buff(
            $group_name, $remote_filename);
        return $content;
    }
    public function download_to_file($group_name, 
            $remote_filename, $dst_localfile) {
        return $this->fdfs->storage_download_file_to_file($group_name, 
            $remote_filename, $dst_localfile);
    }
    public function delete($group_name, $remote_filename) {
        return $this->fdfs->storage_delete_file($group_name, $remote_filename);
    }
    public function exists($group_name, $remote_filename) {
        return $this->fdfs->storage_file_exist($group_name, $remote_filename);
    }
    public function get_file_info($group_name, $remote_filename) {
        return $this->fdfs->get_file_info($group_name, $remote_filename);
    }
}

PHP客户端上传图片测试

# cat test.php
<?php
require_once('fastdfs.php');
$fdfs = new FDFS();
$localfile = './test.xml';
$fileinfo = $fdfs->upload($localfile);
if ($fileinfo) {
  // update file info in the database etc
}
var_dump($fileinfo);
# php test.php 
array(4) {
  ["group_name"]=>
  string(6) "group1"
  ["remote_filename"]=>
  string(44) "M00/00/00/wKhHf1S-qbuAISbeAAAE2uRlJkA789.xml"
  ["source_ip"]=>
  string(14) "192.168.71.127"
  ["file_size"]=>
  int(1242)
}

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78105

(0)
sraybansrayban
上一篇 2017-06-16
下一篇 2017-06-16

相关推荐

  • test

    欢迎使用马克飞象 作业帮 示例笔记本 马克飞象 帮助 Markdown 马克飞象是一款专为印象笔记(Evernote)打造的Markdown编辑器,通过精心的设计与技术实现,配合印象笔记强大的存储和同步功能,带来前所未有的书写体验。特点概述: 功能丰富 :支持高亮代码块、LaTeX 公式、流程图,本地图片以及…

    Linux干货 2017-03-20
  • 马哥教育网络班第21期+第三周课程作业

    1. 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 who | awk  '{print $1}'|uniq 2.列出最后登录到当前系统的用户的相关信息 last | head -1   3. 读取当前系统上被用户当做其默认shell最多的那个shell cat /etc/pa…

    Linux干货 2016-07-26
  • 关于生命中出现的那个男人

    绝对真男人     一个人的一生,会遇到很多很多的男人,哦,也会遇到很多很多的女人,但是我们今天就谈谈男人吧.在这芸芸众生中对于我们这些志向于在linux求发展谋生存的人来说,有个男人会是所有linux爱好者的良师益友.这个男人就是man,这个男人是绝对的男人,因为他是在linux这个系统中无所不知的那个男人. &nb…

    Linux干货 2016-10-27
  • 文本处理工具与正则表达式

    一、文本处理工具     1.文本查看命令           cat [OPTION]… [FILE]…         -n 显示行…

    Linux干货 2016-08-08
  • 文件查找locate和find

    locate 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 依赖于事先构建的索引 索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb) 索引构建过程需要遍历整个根文件系统,极消耗资源 工作特点: •查找速度快 •模糊查找 •非实时查找 •搜索的是文件的全路径,不仅仅是文件名 •…

    2017-08-11
  • N26-第四周-孙逸

    1、  复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 cp –r /etc/skel /home/tuser1 chmod –R 700 /home/tuser1 2、  编辑/etc/group文件,添加组hadoop。 group文件的内容格式: &…

    2017-03-10