官方網站: 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
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
具體參數查看幫助
/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-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=""
打印出的內容寫入到 filer.toml中, 並且修改其中的配置
dir = "/root/sea/filer_path/level"
可以使用文件, MySQL, redis等保存 filer metadata, 只需要啟用或者停用對應的配置
-
啟動
/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為主