分布式文件系統之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.www58058.com/78105

(0)
sraybansrayban
上一篇 2017-06-16 19:21
下一篇 2017-06-16 20:40

相關推薦

  • N25-第九周作業

    第九周 1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash # sum_login=0 sum_nologin=0 for i in $(awk -F: ‘{print $7}’ /etc/passwd);do if…

    Linux干貨 2017-03-10
  • diy自己的linux

    準備一臺liunx的機器,我用的是liunx虛擬機,Centos6.8的鏡像。 單獨加載一塊硬盤,我加載了一塊20G的硬盤,然后用fdisk /dev/sdb進行分區,sdb1用來作為將來系統的/boot分區,所有100M就可以了,sdb2用來作為將來系統的/分區,我給了10G。然后將兩個分區格式化為ext4系統。 mkdir /mnt/boot mkdir…

    Linux干貨 2016-09-13
  • 刪除boot,同時刪除/etc/fstab,如何恢復

    刪除boot,同時刪除/etc/fstab,如何恢復 /etc/fstab文件丟失的時候,得光盤啟動進入linux rescue下。 掛載好cdrom后,按ctrl+alt+del,然后按f2,進入bios,調整為cdrom啟動,然后reboot,進入linux?rescue急救模式/etc/fstab配置文件決定了linux系統在啟動后如何加載各個分區,如…

    2017-09-23
  • 實現CA和證書申請,

    接下來講的是在centos7.3和centos6.8中實現CA和證書申請,centos7.3作為主機,centos6.8作為客戶端 首先你授權客戶端CA證書,必須本身主機也具有CA,自己證明自己,先CA自簽證書,然后在7.3創建私鑰 為了方便以后的操作CD進入 cd /etc/pki/CA   生成自簽名證書 -new:  生成…

    2017-04-11
  • 文件的查找命令 find 和 locate

    文件的查找命令 find 和 locate find 功能:實時查找工具,通過遍歷指定路徑完成文件查找 工作特點: ?  查找速度略慢?  精確查找?  實時查找?  可能只搜索用戶具備讀取和執行權限的目錄 語法:   find [OPTION]… [查找路徑] [查找條件] [處理動作] 查找路徑:指定具…

    Linux干貨 2016-08-16
欧美性久久久久