IPFS私有網絡集羣搭建

對於聯盟鏈的業務中搭建一個私有網絡的 IPFS 集羣仍是頗有必要的,私有網絡集羣容許 IPFS 節點只鏈接到擁有共享密鑰的其餘對等節點,網絡中的節點不響應來自網絡外節點的通訊。
IPFS-Cluster 是一個獨立的應用程序和一個 CLI 客戶端,它跨一組 IPFS 守護進程分配、複製和跟蹤 pin。它使用基於 Raft 一致性算法來協調存儲,將數據集分佈到參與節點上。對於咱們要將一個 peer 上的存儲同步備份到全部集羣上其餘的 peers 時,或者對集羣的節點管理,這時 IPFS-Cluster 就會起到一個很好的做用。
下面簡單描述一下 IPFS 私有網絡以及 IPFS-Cluster 集羣的搭建配置過程(Ubuntu16.04)。git

1578557801228

IPFS 和 IPFS-Cluster 默認的端口:
IPFSgithub

  • 4001 – 與其餘節點同窗端口
  • 5001 – API server
  • 8080 – Gateway server

IPFS-CLUSTER算法

  • 9094 – HTTP API endpoint
  • 9095 – IPFS proxy endpoint
  • 9096 – Cluster swarm 集羣幾點通訊端口

Golang 安裝

IPFS 官方提供的安裝方式有安裝包方式,ipfs-update 方式,源碼編譯安裝方式,具體能夠查看 https://docs.ipfs.io/guides/g...
這裏爲了 ipfs 版本選擇和升級,因此使用ipfs-update方式安裝,Go 是必須的,對於 Go 的安裝這裏再也不贅述。shell

安裝 ipfs-update

在各個節點中安裝ipfs-update:bootstrap

go get -u github.com/ipfs/ipfs-update

也比較簡單,因爲 ipfs.io 官網被 dns 污染的緣由,安裝之後須要配置一下各個節點的/etc/hostsbash

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

建立共享的 key

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.keyide

移除默認的 bootstrap 節點

爲了避免鏈接全球的 IPFS 網絡,你須要將默認的 bootstrap 的節點信息刪除。

ipfs bootstrap rm --all

私有網絡節點配置

在每臺節點中添加第一個節點的 bootstrap:

ipfs bootstrap add /ip4/192.168.11.11/tcp/4001/ipfs/QmSyQFFm5KdB9zoTNQJhPnMs4LYsVmEpY427QYxH2CaFqL

QmSyQFFm5KdB9zoTNQJhPnMs4LYsVmEpY427QYxH2CaFqLipfs init 時生成的節點 ID,也能夠經過ipfs id 查看當前節點的 ID。
咱們還須要設置環境變量LIBP2P FORCE PNET來強制咱們的網絡進入私有模式

export LIBP2P_FORCE_PNET=1

將 IPFS 進程加入到系統進程中啓動

每臺的 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 包含兩個組件:

  • ipfs-cluster-service 用於初始化集羣 peer 並運行它的守護進程
  • ipfs-cluster-ctl 管理集羣的節點和數據

-

咱們將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

將 IPFS-Cluster 節點加入到系統進程中啓動

/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 國際許可協議進行許可

相關文章
相關標籤/搜索