分布式文件系统

 

一、基础知识

什么是分布式文件系统

分布式文件系统 distributed file system 是指文件系统管理的物理存储资源不一定直接链接在本地节点上,而是通过计算机网络与节点相连

分布式文件系统的设计基于客户机/服务器模式

一个典型的网络可能包括多个供多用户访问的服务器

对等特性允许一些系统扮演客户机和服务器的双重角色

分布式文件系统的特点

分布式文件系统可以有效解决数据的存储和管理难题

-将固定于某个地点的某个文件系统,扩展到任意多个地点/多个文件系统

-众多的节点组成一个文件系统网络

-每个节点可以分布在不同的地点,通过网络进行节点间的通信和数据传输

-人们在使用分布式文件系统时,无需关心数据是存储在哪个节点上、或者是从哪个节点获取的,只需要像使用本地文件系统一样管理和存储文件系统中的数据

衡量分布式文件系统的劣势

数据的存储方式 --无论采取何种存储方式,目的都是为了保证数据的存储安全和方便获取

数据的读取速率 --包括响应用户读取数据文件的请求、定位数据文件所在的节点、读取实际硬盘中数据文件的时间、不同节点间的数据传输时间以及一部分处理器的处理时间等

数据的安全机制--采取冗余、备份、镜像等方式

常用分布式文件系统

Lustre --一个大规模的、安全可靠的,具备高可用性的集群文件系统,它是有SUN公司开发和维护

Hadoop --不仅仅是一个用于存储的分布式文件系统,而是设计用来在由通用计算设备组成的大型集群上执行分布式应用的框架

OpenAFS --一套开放源代码的分布式文件系统,允许系统之间通过局域网和广域网来分享档案和资源

googleFs --一个可扩展分布式文件系统,用于大型的,分布式的,对大量数据进行访问的应用

基础架构

服务器角色

与单机的文件系统不同,分布式文件系统不是将这些数据放在一块磁盘上,由上层操作系统来管理

数据存放在一个服务器集群上,由集群中的服务器,各尽其责,通力合作,提供整个文件系统的服务

重要服务器包括 --主控服务器  Master管理各个数据服务器收集他们的信息,了解所有数据服务器的生存现状,然后给他们分配任务 ;主控服务器上放着所有的文件目录信息,要找一个文件,必须先访问它

  --数据服务器 存放数据的服务器;设计为冗余名模式;主要的工作模式就是定期向主控服务器汇报其状况,然后等待并处理命令,更快更安全的存放好数据

数据分布   以块的方式存储 -把文件数据切成数据块 -将数据块存储在数据服务器上 ;以独立文件的方式存储 -每台数据服务器存储独立的文件 -多台数据服务器存储相同的文件,实现冗余及负载均衡

fastDFS介绍

fastDFS是一款开源分布式文件系统,它用纯C语言实现,支持linuxfreeBSDAIXunix系统;作者为淘宝网的余庆,20087月发布;功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等;解决的大容量存储和负载均衡的问题;特别适合以文件为载体的在线服务,如相册网站、视频网站等

fastDFS用户列表:支付宝、京东商城、赶集网、迅雷、58同城、51CTOUC

fastDFS基本概念

fastDFS服务端有两个角色:跟踪器tracker和存储节点storage

跟踪器:主要做调度工作,在访问上起负载均衡的作用  

存储节点:完成文件管理的所有功能,即存储、同步和提供存取接口,同时对文件的元数据进行管理

fastDFS工作流程

fastDFS系统结构

跟踪器和存储节点都可以由一台多台服务器构成

跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务

跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少

fastDFS采用了分组存储方式

集群由一个或多个组构成,集群存储总容量为集群中所有的存储容量之和

一个组由一台或多台存储服务器组成,同组内的多台storage server之间是互备关系,同组存储服务器上的文件是完全一致的

文件上传、下载、删除等操作可以在组内任意一台storage server上进行

一个组的存储容量为该组内存储服务器容量最小的那个

采用分组存储方式的好处是灵活、可控性较强。比如上传文件时,可以由客户端直接指定上传到的组

一个分组的存储服务器访问压力较大时,可以在该组增加存储服务器来扩充服务能力(纵向扩容)

当系统容量不足时,可以增加组来扩充存储容量(横向扩容)

不同组的storage server 之间不会相互通信,同组内的storage server之间会相互连接进行文件同步

storage server采用binlog文件记录文件上传、删除等更新操作

binlog中只记录文件名,不记录文件内容

文件同步只在同组内的storage server 之间进行,采用push方式,即源头服务器同步给目标服务器

fastDFS上传文件过程

client询问tracker上传到的storage,不需要附加参数;tracker返回一台可用的storageclient直接和storage通讯完成文件上传

fastDFS下载文件过程

client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);tracker返回一台可用的storageclient直接和storage通讯完成文件下载

FastDFS官网 --http://bbs.chinaunix.net/forum-240-1.html

国际网址 --https://sourceforge.net/projects/fastdfs/       

https://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz

服务器端脚本语言: python php java asp

客户端脚本语言:   javascript

meta data 元数据

二、FastDFS配置

1、准备四台主机,vh01.tedu.cn 192.168.4.1/24用于tracker

vh02vh03用于group1storagevh04用于group2storage

2、将vh02 vh03 vh04增加额外硬盘

3、初始化:ip地址 yum 防火墙 selinux

FastDFS  tracker配置

1、安装依赖包

[root@vh01 ~]# cd fdfs_soft/

[root@vh01 fdfs_soft]# yum install -y libevent-devel-2.0.21-4.el7.x86_64.rpm gcc libevent make

2、编译安装

[root@vh01 fdfs_soft]# tar xzf FastDFS_v4.06.tar.gz

[root@vh01 fdfs_soft]# cd FastDFS/

[root@vh01 FastDFS]# ./make.sh

[root@vh01 FastDFS]# ./make.sh install

3、配置

[root@vh01 FastDFS]# cp init.d/fdfs_trackerd /etc/init.d/

[root@vh01 FastDFS]# mkdir -pv /data/fastdfs 创建工作目录

[root@vh01 FastDFS]# vim /etc/fdfs/tracker.conf

base_path=/data/fastdfs     v  c

max_connections=4096

store_lookup=0

store_group=group1

use_storage_id = true

storage_ids_filename = storage_ids.conf

[root@vh01 FastDFS]# cp conf/storage_ids.conf   /etc/fdfs/

[root@vh01 FastDFS]# vim /etc/fdfs/storage_ids.conf

100001   group1  192.168.4.2
100002   group1  192.168.4.3

100003   group2  192.168.4.4

4、起动服务并验证

[root@vh01 FastDFS]# ln -s  /etc/init.d/fdfs_trackerd  /usr/bin/

[root@vh01 FastDFS]# systemctl restart fdfs_trackerd.service

[root@vh01 FastDFS]# chkconfig fdfs_trackerd on

[root@vh01 FastDFS]# cat /data/fastdfs/logs/trackerd.log

[root@vh01 FastDFS]# systemctl status fdfs_trackerd

[root@vh01 FastDFS]# netstat -tlnp | grep :22122

FastDFS  storage配置

1、准备存储,并永久将其挂载到/data/fastdfs目录下

[root@vh02 ~]# parted  /dev/vdb

(parted) mklabel  gpt

(parted) mkpart primary 1M  -1

[root@vh02 ~]# mkfs.ext4 /dev/vdb1

[root@vh02 ~]# blkid  /dev/vdb1

[root@vh02 ~]# echo UUID="89fe6d06-d4a3-405d-bbc6-6d75480b466e"  >> /etc/fstab

[root@vh02 ~]# vim  /etc/fstab

[root@vh02 ~]# mkdir -pv  /data/fastdfs

[root@vh02 ~]# mount -a

2、编译安装storage,与tracker完全相同

3、配置

[root@vh02 FastDFS]# cp init.d/fdfs_storaged     /etc/init.d/

[root@vh02 FastDFS]# vim /etc/fdfs/storage.conf

group_name=group1

base_path=/data/fastdfs

max_connections=4096

store_path_count=1

store_path0=/data/fastdfs

tracker_server=192.168.4.1:22122

4、起动并验证

[root@vh02 FastDFS]# /etc/init.d/fdfs_storaged  start

[root@vh02 FastDFS]# chkconfig fdfs_storaged on

[root@vh02 FastDFS]# netstat -tlnp | grep :23000

[root@vh02 FastDFS]# systemctl status fdfs_storaged

[root@vh02 FastDFS]# ls /data/fastdfs/data/00/

[root@vh02 FastDFS]# cat /data/fastdfs/logs/storaged.log

successfully connect to tracker server 192.168.4.1:22122, as a tracker client, my ip is 192.168.4.2

[root@vh02 FastDFS]# cat /data/fastdfs/data/.data_init_flag

[root@vh02 FastDFS]# date -d '@1502088654'    //时间戳

配置物理主机为客户端

1、基本配置

[root@room9pc16 ~]# mkdir /tmp/fdfs

[root@vh01 ~]# scp /etc/fdfs/client.conf 192.168.4.254:/tmp/fdfs/

[root@vh01 ~]# cd /usr/local/bin/

[root@vh01 bin]# scp fdfs_test fdfs_upload_file fdfs_download_file fdfs_delete_file       192.168.4.254:/tmp/fdfs/

[root@room9pc16 cluster]# cd /tmp/fdfs

[root@room9pc16 fdfs]# vim client.conf

base_path=/tmp/fdfs

tracker_server=192.168.4.1:22122

2、使用测试命令

[root@room9pc16 fdfs]# ./fdfs_test client.conf upload tedu.jpg  上传

group_name=group1, remote_filename=M00/00/00/wKgEAllCOtaANcYYAACwEV-ILDc398.jpg

[root@vh02 sync]# ls /data/fastdfs/data/00/00/

[root@room9pc16 fdfs]# ./fdfs_test client.conf download

group1  M00/00/00/wKgEAllCOtaANcYYAACwEV-ILDc398.jpg  tarena.jpg  下载,同时改名

[root@room9pc16 fdfs]# eog    tarena.jpg

[root@room9pc16 fdfs]# ./fdfs_test client.conf delete

group1  M00/00/00/wKgEAllCOtaANcYYAACwEV-ILDc398.jpg   删除

3、常规命令

[root@room9pc16 fdfs]# ./fdfs_upload_file   client.conf   tedu.jpg

group1/M00/00/00/wKgEAllCP5WAEapvAACwEV-ILDc774.jpg

[root@room9pc16 fdfs]# ./fdfs_download_file  client.conf

    group1/M00/00/00/wKgEAllCP5WAEapvAACwEV-ILDc774.jpg ttt.jpg

[root@room9pc16 fdfs]# ./fdfs_delete_file client.conf

    group1/M00/00/00/wKgEAllCP5WAEapvAACwEV-ILDc774.jpg

配置web访问

1、在vh02上配置web访问

[root@vh02 ~]# yum install -y  pcre-devel   zlib-devel   openssl-devel

[root@vh02 ~]# cd fdfs_soft/

[root@vh02 fdfs_soft]# tar xzf fastdfs-nginx-module_v1.16.tar.gz

[root@vh02 fdfs_soft]# tar xzf nginx-1.7.10.tar.gz

[root@vh02 fdfs_soft]# useradd -s /sbin/nologin nginx

[root@vh02 fdfs_soft]# cd nginx-1.7.10/

[root@vh02 nginx-1.7.10]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=../fastdfs-nginx-module/src/

[root@vh02 nginx-1.7.10]# make && make install

2、配置

[root@vh02 nginx-1.7.10]# cd ../fastdfs-nginx-module/

[root@vh02 fastdfs-nginx-module]# cp src/mod_fastdfs.conf /etc/fdfs/

[root@vh02 fastdfs-nginx-module]# vim /etc/fdfs/mod_fastdfs.conf

tracker_server=192.168.4.1:22122

group_name=group1

url_have_group_name = true

store_path_count=1

store_path0=/data/fastdfs

[root@vh02 ~]# vim /usr/local/nginx/conf/nginx.conf

        location / {

            # root   html;

            # index  index.html index.htm;

            ngx_fastdfs_module;

        }

3、起动并访问

[root@vh02 ~]# /usr/local/nginx/sbin/nginx

[root@room9pc16 fdfs]# ./fdfs_upload_file client.conf tedu.jpg

group1/M00/00/00/wKgEAllCSY6ADkJqAACwEV-ILDc000.jpg

[root@room9pc16 fdfs]# firefox  

http://192.168.4.2/group1/M00/00/00/wKgEAllCSY6ADkJqAACwEV-ILDc000.jpg

vh01上配置web访问

vh02上配置web访问完全一样

[root@room9pc16 fdfs]# firefox

http://192.168.4.1/group1/M00/00/00/wKgEAllCSY6ADkJqAACwEV-ILDc000.jpg

多服务器、多组配置

1vh01上修改storage_ids.conf

2vh03vh04的配置与vh02基本上完全一样

[root@vh04 fdfs_soft]# ls /data/fastdfs/data/00/00/

[root@room9pc16 fdfs]#firefox

http://192.168.4.4/group2/M00/00/00/wKgEBFmIOIyACaVzAACwEV-ILDc757.jpg

[root@room9pc16 fdfs]#./fdfs_download_file   client.conf  

group2/M00/00/00/wKgEBFmIOIyACaVzAACwEV-ILDc757.jpg  chen.jpg

[root@room8pc205 fdfs]# ./fdfs_upload_file  client.conf tedu.jpg 192.168.4.4:23000     //指定主机对应组

group2/M00/00/00/wKgEBFmIV4OAPpTNAACwEV-ILDc093.jpg

Top 

NSD CLUSTER DAY05

配置tracker 

配置storage 

文件测试及web访问 

1 配置tracker

1.1 问题

FastDFS是一个分布式文件系统,主要的服务器角色有Tracker和Storage。本例安装一台Tracker,实现以下功能:

接受客户端的访问

检索存储节点,为客户端提供一台可用的storage

1.2 方案

准备一台虚拟机,安装FastDFS Tracker。

主机的主机名及对应的IP地址如表-1所示。

表-1 主机名称及对应IP地址表

1.3 步骤

实现此案例需要按照如下步骤进行。

1)配置yum源,并安装所需的软件包。

[root@fdfs_tracker1 ~]# yum install -y gcc libevent make

[root@fdfs_tracker1 ~]# yum install -y libevent-devel-*.rpm

2)解压缩FastDFS安装包,查看安装说明文档进行安装。

[root@fdfs_tracker1 fdfs]# tar xzf FastDFS_v4.06.tar.gz

[root@fdfs_tracker1 fdfs]# cd FastDFS

[root@fdfs_tracker1 FastDFS]# vim INSTALL

[root@fdfs_tracker1 fdfs]# ./make.sh

[root@fdfs_tracker1 fdfs]# ./make.sh install

[root@fdfs_tracker1 FastDFS]# vim INSTALL

[root@fdfs_tracker1 FastDFS]# cp init.d/fdfs_trackerd /etc/init.d/

3)创建存储目录(存储日志等)。

[root@fdfs_tracker1 FastDFS]# mkdir -pv /data/fastdfs

4)修改配置文件。

[root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/tracker.conf

base_path=/data/fastdfs/     # 设置基础目录

max_connections=4096         # 设置最大连接数

store_group=group1           # 设置存储组名

use_storage_id = true        # 使用stroage_id代替ip地址

5)创建storage_id文件。

[root@fdfs_tracker1 FastDFS]# cp conf/storage_ids.conf /etc/fdfs/

6)编辑storage id文件,加入存储组声明。

[root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/storage_ids.conf

100001   group1  192.168.113.60

7)修改客户端配置文件。

[root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/client.conf

base_path=/data/fastdfs

tracker_server=192.168.113.50:22122

8)控制tracker启动。

[root@fdfs_tracker1 FastDFS]# /etc/init.d/fdfs_trackerd

用法:/etc/init.d/fdfs_trackerd {start|stop|status|restart|condrestart}

9)查看tracker运行状态。

[root@fdfs_tracker1 FastDFS]# /etc/init.d/fdfs_trackerd status

fdfs_trackerd (pid 28928) 正在运行...

[root@fdfs_tracker1 FastDFS]# netstat -tlnp | grep tracker

tcp  0    0   0.0.0.0:22122      0.0.0.0:*    LISTEN      28928/fdfs_trackerd

2 配置storage

2.1 问题

安装FastDFS Storage存储节点,存储节点被划分到相应的组里。同一个组中的服务器保存完全相同的内容, 不同分组存不同的内容。存储节点具有以下功能:

接受Tracker的调度

响应客户端请求

将客户端发来的文件存储至相应存储位置

读取请求的文件,发送给客户端

2.2 方案

准备一台虚拟机,安装FastDFS Tracker。

主机的主机名及对应的IP地址如表-2所示。

表-2 主机名称及对应IP地址表

2.3 步骤

实现此案例需要按照如下步骤进行。

1)配置yum源,并安装所需的软件包。

[root@fdfs_storage1 ~]# yum install -y gcc libevent make

[root@fdfs_storage1 ~]# yum install -y libevent-devel-*.rpm

2)解压缩、查看安装指南进行安装

[root@fdfs_storage1 fdfs]# tar xzf FastDFS_v4.06.tar.gz

[root@fdfs_storage1 fdfs]# cd FastDFS

[root@fdfs_storage1 FastDFS]# vim INSTALL

[root@fdfs_storage1 fdfs]# ./make.sh

[root@fdfs_storage1 fdfs]# ./make.sh install

[root@fdfs_storage1 FastDFS]# vim INSTALL

[root@fdfs_storage1 FastDFS]# cp init.d/fdfs_storaged /etc/init.d/

3)创建数据存储目录(存储日志、数据文件等)

[root@fdfs_storage1 FastDFS]# mkdir -pv /data/fastdfs

4)修改配置文件。

[root@fdfs_storage1 FastDFS]# vim /etc/fdfs/storage.conf

base_path=/data/fastdfs/      # 设置基础工作目录

max_connections=4096           # 设置最大连接数

store_path0=/data/fastdfs/    # 设置上传文件目录

tracker_server=192.168.113.50:22122    # 设置连接的Tracker

upload_priority=10              # 设置组内服务器优先级

5)控制storage启动

[root@fdfs_storage1 FastDFS]# /etc/init.d/fdfs_storaged

用法:/etc/init.d/fdfs_storaged {start|stop|status|restart|condrestart}

6)查看storage工作状态

[root@fdfs_storage1 FastDFS]# /etc/init.d/fdfs_storaged status

fdfs_storaged (pid 28916) 正在运行...

[root@fdfs_storage1 FastDFS]# netstat -tlnp | grep storage

tcp        0      0 0.0.0.0:23000               0.0.0.0:*                   LISTEN      28916/fdfs_storaged

3 文件测试及web访问

3.1 问题

为实现文件的上传、下载和删除等功能,FastDFS提供相应的命令行工具。测试这些工具的使用。

为了方便用户访问,可以在storage上配置web访问功能,使得文件通够通过web页面进行访问。

3.2 步骤

步骤一:通过fdfs_test

1)上传文件

测试程序会对一个文件上传两次。返回的文件id也是两个并上传文件附加属性。storage上生成4个文件

[root@fdfs_tracker ~]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload install.log

查看上传到storage的文件

[root@fdfs_storage1 FastDFS]# ls /data/fastdfs/data/00/00/

wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log    wKhxPFc3oD6ARewxAAC6OPZ7hp8271.log

wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log-m  wKhxPFc3oD6ARewxAAC6OPZ7hp8271.log-m

2)删除文件

[root@fdfs_tracker1 fastdfs]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf\

 delete group1 M00/00/00/wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log

步骤二:通过其他相关命令

1)通过fdfs_upload_file上传文件

[root@fdfs_tracker1 fastdfs]# /usr/local/bin/fdfs_upload_file

 /etc/fdfs/client.conf /etc/passwd

group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

2)通过fdfs_download_file下载文件

[root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_download_file /etc/fdfs/client.conf

 group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

3)通过fdfs_delete_file删除文件

[root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_delete_file /etc/fdfs/client.conf\

 group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

步骤三:配置web访问

1)在storage上安装nginx

[root@fdfs_storage1 ~]# yum install -y pcre pcre-devel zlib zlib-devel

[root@fdfs_storage1 fdfs]# tar xzf fastdfs-nginx-module_v1.16.tar.gz

[root@fdfs_storage1 fdfs]# tar xzf nginx-1.7.10.tar.gz

[root@fdfs_storage1 fdfs]# cd nginx-1.7.10

[root@fdfs_storage1 nginx-1.7.10]# useradd -s /sbin/nologin nginx

[root@fdfs_storage1 nginx-1.7.10]# ./configure --prefix=/usr/local/nginx

--user=nginx --group=nginx --add-module=../fastdfs-nginx-module/src/

[root@fdfs_storage1 nginx-1.7.10]# make && make install

2)为nginx修改fastdfs模块配置

[root@fdfs_storage1 fdfs]# cp fastdfs-nginx-module/src/mod_fastdfs.conf \

/etc/fdfs

[root@fdfs_storage1 fdfs]# vim /etc/fdfs/mod_fastdfs.conf

tracker_server=192.168.113.50:22122

group_name=group1

url_have_group_name = true

store_path0=/data/fastdfs/

3)修改ngin配置文件并启动

[root@fdfs_storage1 fdfs]# vim /usr/local/nginx/conf/nginx.conf

# location / {

#    root   html;

#    index  index.html index.htm;

# }                                      将这段说明注释掉,增加以下说明

 location / {

            ngx_fastdfs_module;

        }

[root@fdfs_storage1 fdfs]# /usr/local/nginx/sbin/nginx

4)测试web访问

[root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_upload_file

/etc/fdfs/client.conf tedu.jpg

group1/M00/00/00/wKhxPFc4f2yAVLH7AACwEV-ILDc194.jpg

[root@fdfs_tracker1 ~]# firefox

 http://192.168.113.60/group1/M00/00/00/wKhxPFc4f2yAVLH7AACwEV-ILDc194.jpg &

结果如图1所示

-1

猜你喜欢

转载自www.cnblogs.com/fuzhongfaya/p/8953275.html