seaweedfs文件存儲服務(wù)器搭建

官方網(wǎng)站: https://github.com/chrislusf/seaweedfs/wiki/Getting-Started

概述

seaweedfs是一個非常優(yōu)秀的由 golang 開發(fā)的分布式存儲開源項目。它是用來存儲文件的系統(tǒng),并且與使用的語言無關(guān),使得文件儲存在云端變得非常方便。

在邏輯上Seaweedfs的幾個概念:

  • Node 系統(tǒng)抽象的節(jié)點,抽象為DataCenter、Rack、DataNode
  • DataCenter 數(shù)據(jù)中心,對應(yīng)現(xiàn)實中的不同機房
  • Rack 機架,對應(yīng)現(xiàn)實中的機柜
  • Datanode 存儲節(jié)點,用于管理、存儲邏輯卷
  • Volume 邏輯卷,存儲的邏輯結(jié)構(gòu),邏輯卷下存儲Needle
  • Needle 邏輯卷中的Object,對應(yīng)存儲的文件
  • Collection 文件集,可以分布在多個邏輯卷上

一. 安裝go環(huán)境

  • 安裝規(guī)劃
    master: 172.16.20.71
    volume:
    172.16.20.71*3
    172.16.20.72*2
  • 查看系統(tǒng)位數(shù)
    getconf LONG_BIT
  • 下載源碼包
    https://golangtc.com/download
  • 選擇對應(yīng)的版本下載
cd /usr/local
# 下載
wget https://golangtc.com/static/go/1.9.2/go1.9.2.linux-amd64.tar.gz
# 將其傳到其他兩臺機器
# 解壓
tar -zxf go1.9.2.linux-amd64.tar.gz
# 配置
vim /etc/profile
#加入
export GOPATH=/opt/go
export GOROOT=/usr/local/go
export GOOS=linux
export GOBIN=$GOROOT/bin
export GOTOOLS=$GOROOT/pkg/tool/
export PATH=$PATH:$GOBIN:$GOTOOLS

# 使配置文件生效
source /etc/profile

# 查看
go version
  • 安裝git mercurial
    yum install -y mercurial git

二. 安裝seaweedfs

1. 下載

cd /usr/local
https://github.com/chrislusf/seaweedfs/releases/選擇對應(yīng)的版本
wget https://github.com/chrislusf/seaweedfs/releases/download/0.96/linux_amd64.tar.gz

2. 解壓

tar -zxf linux_amd64.tar.gz

3. ./weed -h 查看幫助

4. 創(chuàng)建運行需要的目錄

/root/sea/data
/root/sea/vol/vol[1-3]
/root/sea/logs

5. 配置運行master

具體參數(shù)查看幫助 /usr/local/weed master -h
https://github.com/chrislusf/seaweedfs/wiki/Master-Server-API

nohup /usr/local/weed master -mdir=/root/sea/data -port=9333 -defaultReplication="001" -ip="172.16.20.71" &>> /root/sea/logs/master.log &

6. 配置運行volume

具體參數(shù)查看幫助
/usr/local/weed volume -h
官方文檔
https://github.com/chrislusf/seaweedfs/wiki/Volume-Server-API
配置volume邏輯卷時, 可以指定數(shù)據(jù)中心datacenter以及機架rack, 復(fù)制模式和數(shù)據(jù)中心和機架有關(guān), 具體見文檔
https://github.com/chrislusf/seaweedfs/wiki/Replication

# 172.16.20.71
/usr/local/weed volume -dir=/root/sea/vol/vol1 -mserver="172.16.20.71:9333" -port=8081 -ip="172.16.20.71" &>>/root/sea/logs/vol1.log &
/usr/local/weed volume -dir=/root/sea/vol/vol2 -mserver="172.16.20.71:9333" -port=8082 -ip="172.16.20.71" &>>/root/sea/logs/vol1.log &
/usr/local/weed volume -dir=/root/sea/vol/vol3 -mserver="172.16.20.71:9333" -port=8083 -ip="172.16.20.71" &>>/root/sea/logs/vol1.log &

# 172.16.20.72
/usr/local/weed volume -dir=/root/sea/vol/vol1 -mserver="172.16.20.71:9333" -port=8081 -ip="172.16.20.72" &>>/root/sea/logs/vol1.log &
/usr/local/weed volume -dir=/root/sea/vol/vol2 -mserver="172.16.20.71:9333" -port=8082 -ip="172.16.20.72" &>>/root/sea/logs/vol1.log &
/usr/local/weed volume -dir=/root/sea/vol/vol3 -mserver="172.16.20.71:9333" -port=8083 -ip="172.16.20.72" &>>/root/sea/logs/vol1.log &

7. 上傳文件測試

  • 文件上傳首先需要請求master, 去分配一個邏輯卷和fid
    curl http://172.16.20.71:9333/dir/assign

  • 返回結(jié)果
    {"fid":"3,57f4e1098c93","url":"172.16.20.71:8082","publicUrl":"172.16.20.71:8082","count":1}

  • 使用返回的url和fid上傳文件
    curl -F "file=/home/hufengjiu/ka.jpg" 172.16.20.71:8082/3,57f4e1098c93
    可以這么理解, 上傳文件, 首先請求master分配volume和fid, 然后將文件上傳到某個卷下

  • wget 172.16.20.71:8082/3,57f4e1098c93 可以將該圖片文件下載下來,

  • 也可以在瀏覽器直接訪問
    172.16.20.71:8082/3,57f4e1098c93

  • 也可以指定圖片大小
    172.16.20.71:8082/3,57f4e1098c93?width=100&height=20

  • 查看卷目錄


    image.png

    因為我已經(jīng)上傳了很多文件, 所以這個目錄很大, 仔細觀察, 每一組都是有dat和idx組成, dat是數(shù)據(jù)部分,idx是索引部分

四. python客戶端[Python-weed]的安裝和使用(https://github.com/darkdarkfruit/python-weed)

  • python版本: python2.7
  • pip安裝
pip install python-weed
# 需要的依賴庫
pip install requests
pip install conf
  • 使用
    將/root/hufengjiu/pic目錄下所有的圖片文件上傳, 代碼如下
# ** -- coding: utf-8 -- **
#!/usr/bin/env python

from weed.master import WeedMaster
from weed.volume import WeedVolume
import glob
import os.path as op

# 獲取pic目錄下所有圖片文件
globlist = glob.glob(r"/root/hufengjiu/pic/*/*.[png|jpg|jpeg]*")
print len(globlist)
master = WeedMaster(host='172.16.20.71')
urls = []
results = []
for i in globlist:
    #分配volume和fid
    assign = master.get_assign_key()
    host_port = assign['url'].split(':')
    url = assign['url'] + '/' + assign['fid']
    volume = WeedVolume(host=host_port[0], port=int(host_port[1]))
    #上傳圖片
    if op.isfile(i):
        result = volume.put_file(i, assign['fid'])
        urls.append(url)
        results.append(result)
# 將所有的圖片url保存起來
print urls

五. 配置運行Filer并掛載到本地目錄

Filer允許以另一種方式上傳文件
https://github.com/chrislusf/seaweedfs/wiki/Filer-Server-API

1. 安裝啟動

  • 生成配置文件
mkdir -p /etc/seaweedfs
cd /etc/seaweedfs
touch filer.toml
mkdir -p /root/sea/filer_path/level

/usr/local/weed scaffold filer -output=""打印出的內(nèi)容寫入到 filer.toml中, 并且修改其中的配置

dir = "/root/sea/filer_path/level"

可以使用文件, MySQL, redis等保存 filer metadata, 只需要啟用或者停用對應(yīng)的配置

  • 啟動
    /usr/local/weed filer -master=172.16.20.71:9333 -ip=172.16.20.71 -defaultReplicaPlacement='001'&

  • 上傳文件
    curl -F "filename=@ka.jpg" "http://172.16.20.71:8888/path/to/sources/"
    會返回{"name":"ka.jpg","size":8601,"fid":"6,57f5feb19f1c","url":"http://172.16.20.71:8082/6,57f5feb19f1c"}

  • 訪問
    使用http://172.16.20.71:8888/path/to/sources/ka.jpg或者http://172.16.20.71:8082/6,57f5feb19f1c都可以訪問

2. mount掛載

https://github.com/chrislusf/seaweedfs/wiki/Mount
可以將filer掛載到本地某個目錄進行管理

yum install -y fuse
/usr/local/weed mount -filer=172.16.20.71:8888 -dir=/root/sea/mount &

cd /root/sea/mount, 就可以查看具體上傳的文件

具體信息以官方wiki為主

參考博客:
https://blog.csdn.net/Anumbrella/article/details/78585937
https://blog.csdn.net/github_37459410/article/details/81141365
http://blog.chinaunix.net/uid-25057421-id-5676348.html
http://ju.outofmemory.cn/entry/109645

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容