對於聯盟鏈的業務中搭建一個私有網絡的 IPFS 集羣仍是頗有必要的,私有網絡集羣容許 IPFS 節點只鏈接到擁有共享密鑰的其餘對等節點,網絡中的節點不響應來自網絡外節點的通訊。
IPFS-Cluster 是一個獨立的應用程序和一個 CLI 客戶端,它跨一組 IPFS 守護進程分配、複製和跟蹤 pin。它使用基於 Raft 一致性算法來協調存儲,將數據集分佈到參與節點上。對於咱們要將一個 peer 上的存儲同步備份到全部集羣上其餘的 peers 時,或者對集羣的節點管理,這時 IPFS-Cluster 就會起到一個很好的做用。
下面簡單描述一下 IPFS 私有網絡以及 IPFS-Cluster 集羣的搭建配置過程(Ubuntu16.04)。git
IPFS 和 IPFS-Cluster 默認的端口:
IPFS:github
IPFS-CLUSTER:算法
IPFS 官方提供的安裝方式有安裝包方式,ipfs-update 方式,源碼編譯安裝方式,具體能夠查看 https://docs.ipfs.io/guides/g...
這裏爲了 ipfs 版本選擇和升級,因此使用ipfs-update
方式安裝,Go 是必須的,對於 Go 的安裝這裏再也不贅述。shell
在各個節點中安裝ipfs-update
:bootstrap
go get -u github.com/ipfs/ipfs-update
也比較簡單,因爲 ipfs.io 官網被 dns 污染的緣由,安裝之後須要配置一下各個節點的/etc/hosts
:bash
209.94.78.78 ipfs.io 209.94.90.1 ipfs.io
經過 ipfs-update versions
能夠 列出全部可使用和能夠下載的ipfs
版本.咱們這裏直接安裝最新的版本:網絡
$ ipfs-update install latest fetching go-ipfs version v0.4.22 binary downloaded, verifying... success! stashing old binary installing new binary to /home/hector/go/bin/ipfs checking if repo migration is needed... Installation complete!
這樣ipfs
就安裝成功了,接下來咱們須要爲每臺節點的 IPFS 初始化一下:dom
ipfs init
swarm.key
密鑰容許咱們建立一個私有網絡,並告訴網絡節點只和擁有相同祕鑰的節點通訊,在一個節點上執行下面命令:tcp
go get -u github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen ipfs-swarm-key-gen > ~/.ipfs/swarm.key
經過scp
或者上傳的方式將生成的swarm.key
拷貝到每一臺節點的~/.ipfs/swarm.key
。ide
爲了避免鏈接全球的 IPFS 網絡,你須要將默認的 bootstrap 的節點信息刪除。
ipfs bootstrap rm --all
在每臺節點中添加第一個節點的 bootstrap:
ipfs bootstrap add /ip4/192.168.11.11/tcp/4001/ipfs/QmSyQFFm5KdB9zoTNQJhPnMs4LYsVmEpY427QYxH2CaFqL
QmSyQFFm5KdB9zoTNQJhPnMs4LYsVmEpY427QYxH2CaFqL
爲ipfs init
時生成的節點 ID,也能夠經過ipfs id
查看當前節點的 ID。
咱們還須要設置環境變量LIBP2P FORCE PNET
來強制咱們的網絡進入私有模式
export LIBP2P_FORCE_PNET=1
每臺的 IPFS 啓動都加入系統的守護進程啓動,添加 /etc/systemd/system/ipfs.service
[Unit] Description=IPFS Daemon After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=simple ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub User=root [Install] WantedBy=multi-user.target
如今能夠經過下面的命令來啓動 IPFS 的後臺守護進程了:
systemctl daemon-reload systemctl enable ipfs systemctl start ipfs systemctl status ipfs
IPFS-Cluster 包含兩個組件:
-
咱們將ipfs-cluster
克隆到 GOPATH 下,而後 make 編譯安裝(系統已安裝 make):
git clone https://github.com/ipfs/ipfs-cluster.git $GOPATH/src ipfs-cluster make install
查看一下是否安裝成功:
ipfs-cluster-service --version ipfs-cluster-ctl --version
相似於 IPFS 的祕鑰,咱們管理節點中生成一個隨機密鑰:
od -vN 32 -An -tx1 /dev/urandom | tr -d ' \n'
將生成的隨機你字符串加入到環境變量中,好比:b55262c36de6f97bd50b5233f75866445ec51db74613bad78e906c4dc9ba1d30
分別修改每個節點的的~/.bashrc
添加到環境變量中:
export CLUSTER_SECRET=b55262c36de6f97bd50b5233f75866445ec51db74613bad78e906c4dc9ba1d30
保存後別忘了 source ~/.bashrc
每一臺節點執行初始化命令:
ipfs-cluster-service init
在管理節點啓動進程
ipfs-cluster-service daemon
其餘節點啓動--bootstrap
添加主節點:
ipfs-cluster-service daemon --bootstrap /ip4/192.168.11.11/tcp/9096/ipfs/12D3KooWEGrD9d3n6UJNzAJDyhfTUZNQmQz4k56Hb6TrYEyxyW2F
這裏注意下,12D3KooWEGrD9d3n6UJNzAJDyhfTUZNQmQz4k56Hb6TrYEyxyW2F
是 IPFS-Cluster 節點 ID,不是 IPFS 節點 ID,能夠經過ipfs-cluster-service id
查看。
能夠經過命令查看集羣節點狀態:
ipfs-cluster-ctl peers ls
添/etc/systemd/system/ipfs-cluster.service
:
[Unit] Description=IPFS-Cluster Daemon Requires=ipfs After=syslog.target network.target remote-fs.target nss-lookup.target ipfs [Service] Type=simple ExecStart=/root/go/bin/ipfs-cluster-service daemon User=root [Install] WantedBy=multi-user.target
如今能夠經過下面的命令來啓動 ipfs-cluster 的後臺守護進程了:
systemctl daemon-reload systemctl enable ipfs-cluster systemctl start ipfs-cluster systemctl status ipfs-cluster
在其中一臺節點中添加一個文件:
ipfs-cluster-ctl add test.txt
經過添加的文件 CID 來查看文件狀態,能夠看到文件以及在全部節點中PINNED
ipfs-cluster-ctl status CID
轉載請註明: 轉載自Ryan 是菜鳥 | LNMP 技術棧筆記
若是以爲本篇文章對您十分有益,何不 打賞一下
本文連接地址: IPFS 私有網絡集羣搭建
<img alt="知識共享許可協議" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/80x15.png" />
本做品採用知識共享署名-非商業性使用 4.0 國際許可協議進行許可