seaweedfs檔案儲存伺服器搭建
官方網站: ofollow,noindex">https://github.com/chrislusf/seaweedfs/wiki/Getting-Started
概述
seaweedfs是一個非常優秀的由 golang 開發的分散式儲存開源專案。它是用來儲存檔案的系統,並且與使用的語言無關,使得檔案儲存在雲端變得非常方便。
在邏輯上Seaweedfs的幾個概念:
- Node 系統抽象的節點,抽象為DataCenter、Rack、DataNode
- DataCenter 資料中心,對應現實中的不同機房
- Rack 機架,對應現實中的機櫃
- Datanode 儲存節點,用於管理、儲存邏輯卷
- Volume 邏輯卷,儲存的邏輯結構,邏輯卷下儲存Needle
- Needle 邏輯卷中的Object,對應儲存的檔案
- Collection 檔案集,可以分佈在多個邏輯捲上
一. 安裝go環境
- 安裝規劃
master: 172.16.20.71
volume:
172.16.20.71*3
172.16.20.72*2 - 檢視系統位數
getconf LONG_BIT - 下載原始碼包
https://golangtc.com/download - 選擇對應的版本下載
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/ 選擇對應的版本
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. 建立執行需要的目錄
/root/sea/data
/root/sea/vol/vol[1-3]
/root/sea/logs
5. 配置執行master
具體引數檢視幫助 /usr/local/weed master -h
API" target="_blank" rel="nofollow,noindex">https://github.com/chrislusf/seaweedfs/wiki/Master-Server-APInohup /usr/local/weed master -mdir=/root/sea/data -port=9333 -defaultReplication="001" -ip="172.16.20.71" &>> /root/sea/logs/master.log &
6. 配置執行volume
具體引數檢視幫助
/usr/local/weed volume -h
官方文件
https://github.com/chrislusf/seaweedfs/wiki/Volume-Server-API
配置volume邏輯卷時, 可以指定資料中心datacenter以及機架rack, 複製模式和資料中心和機架有關, 具體見文件
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
-
返回結果
{"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
因為我已經上傳了很多檔案, 所以這個目錄很大, 仔細觀察, 每一組都是有dat和idx組成, dat是資料部分,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
- 其他用法見官網
- 其他客戶端檢視
https://github.com/chrislusf/seaweedfs/wiki/Client-Libraries
五. 配置執行Filer並掛載到本地目錄
Filer允許以另一種方式上傳檔案
https://github.com/chrislusf/seaweedfs/wiki/Filer-Server-API1. 安裝啟動
- 生成配置檔案
mkdir -p /etc/seaweedfs cd /etc/seaweedfs touch filer.toml mkdir -p /root/sea/filer_path/level
將 /usr/local/weed scaffold filer -output=""
打印出的內容寫入到 filer.toml中, 並且修改其中的配置
dir = "/root/sea/filer_path/level"
可以使用檔案, SQL/">MySQL, redis等儲存 filer metadata, 只需要啟用或者停用對應的配置
-
啟動
/usr/local/weed filer -master=172.16.20.71:9333 -ip=172.16.20.71 -defaultReplicaPlacement='001'&
-
上傳檔案
curl -F "[email protected]" "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為主