概述

seaweedfs是一个非常优秀的由 golang 开发的分布式存储开源项目。它是用来存储文件的系统,并且与使用的语言无关,使得文件储存在云端变得非常方便。

对应的流程图如下:

服务器分层存储架构_服务器分层存储架构

同步过程

流程图如下

服务器分层存储架构_运维_02

下载过程

流程图如下

服务器分层存储架构_服务器_03

在逻辑上Seaweedfs的几个概念:

Node 系统抽象的节点,抽象为DataCenter、Rack、DataNode
DataCenter 数据中心,对应现实中的不同机房
Rack 机架,对应现实中的机柜
Datanode 存储节点,用于管理、存储逻辑卷
Volume 逻辑卷,存储的逻辑结构,逻辑卷下存储Needle
Needle 逻辑卷中的Object,对应存储的文件
Collection 文件集,可以分布在多个逻辑卷上

所有包含模块

模块 模块说明
Weed master 开启一个master服务器
Weed volume 开启一个volume 服务器
Weed filer 开启一个指向一个或多个master服务器的file服务器
Weed upload 上传一个或多个文件
Weed server 启动一个服务器,包括一个volume服务器和自动选举一个master服务器
模块的参数说明及使用说明
Weed master
参数 类型 说明
-cpuprofile String Cpu profile输出文件
-defaultReplication String 如果没有指定默认备份类型。默认”000”
-garbageThreshold String 清空和回收空间的阈值(默认”0.3”)
-ip String Master 服务器ip地址(默认”localhost”)
-ip.bind String 需要绑定的ip地址(默认”0.0.0.0”)
-maxCpu Int 最大cpu数量。0表示所有可用的cpu
-mdir String 存储元数据的数据目录(默认”/tmp”)
-memprofile String 内存配置文件输出文件
-peers String 逗号分隔所有主节点ip:端口列表,示例127.0.0.1:9093,127.0.0.1: 9094
-port Int http监听端口(默认9333)
-pulseSeconds Int 心跳检测的时间间隔单位为秒(默认5)
-secure.secret String 加密json web token方法
-volumePreallocate 无 为volumes预先分配磁盘空间
-volumeSizeLimitMB Uint Master停止指向过量的volumes写的限定(默认30000)
-whiteList string 逗号分隔具有写权限的Ip地址。如果是空的,没有限制。即白名单

说明
defaultReplication
000 不备份, 只有一份数据
001 在相同的rackj里备份一份数据
010 在相同数据中心内不同的rack间备份一份数据
100 在不同的数据中心备份一份数据
200 在两个不同的数据中心各复制2次
110 在不同的rack备份一份数据, 在不同的数据中心备份一次
如果数据备份类型是 xyz形式
各自的意义
x 在别的数据中心备份的份数
y 不相同数据中心不同的racks备份的份数
z 在别的服务器相同的rack的备份份数

Weed volume
参数 类型 说明
-cpuprofile string Cpu profile输出文件
-dataCenter string 当前volume服务的数据中心名称
-dir string 存储数据文件的目录dir[,dir]…(默认”/tmp”)
-idleTimeout Int 连接空闲时间秒数(默认30)
-images.fix.orientation (true/false) 上传时调整jpg方向
-index string 选择内存~性能平衡模式[memory|leveldb|boltdb|btree]。(默认”memory”)
-ip string Ip地址或服务器名称
-ip.bind string 需要绑定的ip地址(默认”0.0.0.0”)
-max string Volumes的最大值,count[,count]…(默认”7”)
-maxCpu Int 最大cpu数量。0表示所有可用的cpu
-memprofile string 内存配置文件输出文件
-mserver string 用逗号分隔的master服务器列表(默认”localhost:9333”)
-port Int http监听端口号(默认8080)
-port.public Int 端口对外开放
-publicUrl string 公开访问地址
-pulseSeconds Int 心跳之间的秒数,必须小于或等于master 服务器设置(默认5)
-rack string 当前volume服务器的rack 名称
-read.redirect (true/false) 重新定向转移或非本地 volumes
-whiteList string 逗号分隔具有写权限的Ip地址。如果是空的,没有限制。

Weed filer
参数 类型 说明
collection String 所有数据将存储在这个集合中
dataCenter String 首选在此数据中心写入volumes
defaultReplicaPlacement String 如果没有指定默认复制类型(默认“000”)
dirListLimit Int 限制子目录列表大小
disableDirListing 无 关闭目录清单
ip String Filter服务器http监听ip地址
master String 用逗号分隔的master服务器(默认“localhost:9333”)
maxMB Int 分割文件大于限制(默认32)
port Int Filer服务器htp监听端口(默认8888)
port.grpc Int Filer grpc服务器监听端口,默认为 http的端口+10000
port.public Int 对外开放的端口
redirectOnRead 无 在文件GET请求期间是代理还是重定向到volumes服务器
secure.secret String 加密Json Web令牌(JWT)的密钥

Weed upload
参数 类型 说明
collection string 可选的集合名称
dataCenter String 可选的数据中心名称
debug 无 显示debug信息
dir String 如果指定,则递归地上传整个文件夹。
include String 需要上传的文件,跟-dir配合使用,例如*.pdf,*.html,ab?d.txt等
master String Seaweedfs master服务器地址(默认”localhost:9333”)
maxMB Int 如果文件超过指定大小则进行分割
replication String 备份类型
secure.secret String 加密Json Web令牌(JWT)的密钥
ttl string 存活时间 1m,1h,1d,1M,1y

以上是maset volume upload filer 的所有参数说明。是主要的功能。

使用接口
申请一个fid
curl http://localhost:9333/dir/assign
上传文件
curl -F file=@/home/chris/myphoto.jpg http://127.0.0.1:8080/3,01637037d6
删除文件
curl -X DELETE http://127.0.0.1:8080/3,01637037d6
申请带存活时间的fid
curl http://localhost:9333/dir/assign?ttl=3m

安装git mercurial

yum install -y mercurial git

安装seaweedfs

  1. 下载

cd /soft/download

打开网站 :

https://github.com/chrislusf/seaweedfs/releases/

选择对应的版本下载:

wget https://github.com/chrislusf/seaweedfs/releases/download/1.79/linux_amd64.tar.gz
  1. 解压
tar -zxvf linux_amd64.tar.gz
  1. ./weed -h 查看帮助

服务器分层存储架构_运维_04

  1. 创建运行需要的目录
/usr/software/seaweedfs/master/data
/usr/software/seaweedfs/master/logs
/usr/software/seaweedfs/volume/vol
/usr/software/seaweedfs/volume/logs

方案一、设置一台master服务器和一台volume服务器

  1. 配置运行master
nohup /usr/software/seaweedfs/weed master -mdir=/usr/software/seaweedfs/master/data -port=9333 -ip="172.21.0.7" -volumeSizeLimitMB=30 &>> /usr/software/seaweedfs/master/logs/master.log &
  1. 配置运行volume
nohup /usr/software/seaweedfs/weed volume -dir=/usr/software/seaweedfs/volume/vol -max=3 -mserver="172.21.0.7:9333" -port=8080 -ip="172.21.0.7"  &>>/usr/software/seaweedfs/volume/logs/vol.log &
  1. 上传文件测试
    文件上传首先需要请求master, 去分配一个逻辑卷和fid
curl http://172.21.0.7:9333/dir/assign

返回结果

{"fid":"7,02d41f9f6b","url":"172.21.0.7:8080","publicUrl":"172.21.0.7:8080","count":1}

使用返回的url和fid上传文件

curl -F file=@/usr/software/seaweedfs/ww1.JPG http://172.21.0.7:8080/7,02d41f9f6b
{"name":"ww1.JPG","size":93533,"eTag":"1ae02c89"}

可以这么理解, 上传文件, 首先请求master分配volume和fid, 然后将文件上传到某个卷下

wget http://172.21.0.7:8080/7,02d41f9f6b

可以将该图片文件下载下来,

也可以在浏览器直接访问

http://172.21.0.7:8080/7,02d41f9f6b

或者

http://172.21.0.7:8080/7/02d41f9f6b

也可以直接上传文件

curl -F file=@/usr/software/seaweedfs/phx.mp4 http://172.21.0.7:9333/submit
{"eTag":"9cc46572","fid":"7,0572e19181","fileName":"phx.mp4","fileUrl":"172.21.0.7:8080/7,0572e19181","size":941529}
  1. 开启filer
nohup /usr/software/seaweedfs/weed filer -ip=XXX X -port=8882 -master=XXXX:9333 -collection=test &>>/usr/software/seaweedfs/filer/logs/filer.log &
设置多台master服务器和多台volume服务器

SeaweedFS 架构描述
在 SeaweedFS 在运行中分为两种角色:Master Server 和普通 Volume Server。

Master Server:用于管理普通 Volume Server, master会存储集群中 Volume Server 的状态,并且会负责接收 Client 的请求,并将具体的 URL 地址返回给客户端,master 之间通过 Raft 协议来保证一致性,在主节点崩溃的情况下会自动在从节点中选举中新的 master。
Volume Server:管理具体的存储目录,其工作包括写入、查询、删除数据,并会进行数据的压缩工作。
架构图如下:

服务器分层存储架构_数据中心_05


搭建框架图

在 的单机中搭建 SeaweedFS,框架图如下:

服务器分层存储架构_服务器分层存储架构_06

存储系统由一个 master 进程和三个 volume 组成。

我们启动一个master,并设置一些参数,参数有很多都是默认的,可以不设置
# mdir 存储元数据的数据目录
# port 监听端口
# peers 主节点ip:端口
# defaultReplication 备份策略
# ip 服务器ip
# garbageThreshold 清空和回收空间的阈值
# maxCpu 最大cpu数量,0是所有
# pulseSeconds 心跳检测的时间间隔单位为秒
# ip.bind 绑定ip
# volumeSizeLimitMB volumes超载量,最大30G,即一个卷可以存多少数据,当然一个卷不代表一个磁盘,

/root/weed master -mdir=/data/seaweedfs -port=9333 -peers=172.16.100.107:9333,172.16.100.111:9333,172.16.100.106:9333 
-defaultReplication="000" -ip="172.16.100.107" -garbageThreshold=0.3 -maxCpu=0 -pulseSeconds=5 -ip.bind=0.0.0.0 -volumeSizeLimitMB=30000 >>/root/master.out &
./weed -logdir=master/log master -mdir=master/data -port=9333 -peers=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333  -ip=172.18.91.2 -port=9333 -defaultReplication="010" -volumeSizeLimitMB=30000 -defaultReplication=011 &
./weed -logdir=master/log master -mdir=master/data -port=9333 -peers=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333  -ip=172.18.91.3 -port=9333 -defaultReplication="010" -volumeSizeLimitMB=30000 -defaultReplication=011 &
./weed -logdir=master/log master -mdir=master/data -port=9333 -peers=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333  -ip=172.18.91.4 -port=9333 -defaultReplication="010" -volumeSizeLimitMB=30000 -defaultReplication=011 &
  1. 配置运行volume
# dir 存储数据文件的目录,刚才的挂载点
# mserver master服务器列表
# port 监听端口
# ip 服务器ip
# max 本机volumes的最大值,在master上我们定义的每个卷为30G,可是我们的磁盘不可能就这么点,而max的作用就是表示这个磁盘上可以分多少个卷,默认是7,也就是30G * 7 = 210G,很明显磁盘被浪费了很多空间,因此我们需要指定一个大max值,保证volumeSizeLimitMB * max >= 磁盘容量,当然你可以直接100,简单粗暴
# dataCenter 机房
# rack 机架
# idleTimeout 连接空闲时间秒数
# images.fix.orientation 上传时调整jpg方向
# ip.bind 监听ip
# maxCpu 最大cpu数量
# read.redirect 重新定向转移非本地volumes


/root/weed volume -dir=/data/seaweedfs/volume1 -mserver=172.16.100.107:9333,172.16.100.111:9333,172.16.100.106:9333 -ip="172.16.100.136" -max=100 -dataCenter=zw -rack=openstack-bj-zw-bgp5 -idleTimeout=30 -images.fix.orientation=true -ip.bind=0.0.0.0 -maxCpu=0 -port=8081 -read.redirect=true >>/root/volume1.out &
/root/weed volume -dir=/data/seaweedfs/volume2 -mserver=172.16.100.107:9333,172.16.100.111:9333,172.16.100.106:9333 -ip="172.16.100.136" -max=100 -dataCenter=zw -rack=openstack-bj-zw-bgp5 -idleTimeout=30 -images.fix.orientation=true -ip.bind=0.0.0.0 -maxCpu=0 -port=8082 -read.redirect=true >>/root/volume2.out &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.2 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack2-4 -images.fix.orientation=true -read.redirect=true &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.3 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack2-4 -images.fix.orientation=true -read.redirect=true &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.4 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack2-4 -images.fix.orientation=true -read.redirect=true &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.5 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack5-7 -images.fix.orientation=true -read.redirect=true &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.6 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack5-7 -images.fix.orientation=true -read.redirect=true &
./weed -logdir=volume/log volume -dir=volume/data  -mserver=172.18.91.2:9333,172.18.91.3:9333,172.18.91.4:9333 -ip=172.18.91.7 -port=8080 -publicUrl=124.126.120.19:8080 -max=20 -dataCenter=dc -rack=rack5-7 -images.fix.orientation=true -read.redirect=true &

另一种配置分布式环境

启动一个测试集群:2 filer(8801-8802) + 3 master(9331-9333) + 3 volume(8081-8083)

下载seaweedfs:

https://github.com/chrislusf/seaweedfs/releases/download/1.44/linux_amd64.tar.gz

先创建所需要目录

启动master

xuliang@xuliang-PC:/data/seaweedfs$ cat start_master.sh 
./weed -logdir=log/master1 master -mdir=master1 -peers=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=9331 -defaultReplication=001 &
./weed -logdir=log/master2 master -mdir=master2 -peers=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=9332 -defaultReplication=001 &
./weed -logdir=log/master3 master -mdir=master3 -peers=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=9333 -defaultReplication=001 &
xuliang@xuliang-PC:/data/seaweedfs$

启动volume

xuliang@xuliang-PC:/data/seaweedfs$ cat start_volume.sh 
./weed -logdir=log/volume1 volume -dir=volume/data1 -max=300 -mserver=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=8081 &
./weed -logdir=log/volume2 volume -dir=volume/data2 -max=300 -mserver=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=8082 &
./weed -logdir=log/volume3 volume -dir=volume/data3 -max=300 -mserver=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -port=8083 &
xuliang@xuliang-PC:/data/seaweedfs$

使用weed scaffold -config filer -output="." 在当前目录生成配置文件

查看帮助信息知道

The configuration file “filer.toml” is read from “.”, “$HOME/.seaweedfs/”, or “/etc/seaweedfs/”, in that order.

配置文件要放在当前目录或"$HOME/.seaweedfs/", or "/etc/seaweedfs/

修改存储元数据信息默认[leveldb2]

[leveldb2]
£ local on disk, mostly for simple single-machine setup, fairly scalable
£ faster than previous leveldb, recommended.
enabled = true
dir = “.”

修改为redis

[redis]
enabled = true
address  = "localhost:6379"
password = ""
database = 0

启动命令:

xuliang@xuliang-PC:/data/seaweedfs$ cat start_filer.sh 
./weed -logdir=./log/filer1 filer -port=8801 -master=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -defaultReplicaPlacement=001 &
./weed -logdir=./log/filer2 filer -port=8802 -master=192.168.11.103:9331,192.168.11.103:9332,192.168.11.103:9333 -ip=192.168.11.103 -defaultReplicaPlacement=001 &
xuliang@xuliang-PC:/data/seaweedfs$

挂载:

xuliang@xuliang-PC:/data/seaweedfs$ cat start_mount.sh
./weed mount -filer=192.168.11.103:8801 -dir=/mnt -filer.path=/

xuliang@xuliang-PC:/data/seaweedfs$