2021-06-02-IPFS私有网络搭建


title: IPFS私有网络搭建
date: 2021-06-02 15:42:42
categories:

  • 数据库
    tags:
  • IPFS

IPFS私有网络集群搭建

前期准备

对于联盟链的业务中搭建一个私有网络的 IPFS 集群还是很有必要的,私有网络集群允许 IPFS 节点只连接到拥有共享密钥的其他对等节点,网络中的节点不响应来自网络外节点的通信。 IPFS-Cluster 是一个独立的应用程序和一个 CLI 客户端,它跨一组 IPFS 守护进程分配、复制和跟踪 pin。它使用基于 Raft 一致性算法来协调存储,将数据集分布到参与节点上。对于我们要将一个 peer 上的存储同步备份到所有集群上其他的 peers 时,或者对集群的节点管理,这时 IPFS-Cluster 就会起到一个很好的作用。

本人使用三台虚拟机 主机列表

节点名称IP
管理节点peer0Ubuntu1.010.211.55.7
peer1Ubuntu2.010.211.55.9
peer2Ubuntu3.010.211.55.10

IPFS 和 IPFS-Cluster 默认的端口: IPFS

  • 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/guides/install/ 这里为了 ipfs 版本选择和升级,所以使用ipfs-update方式安装,Go 是必须的。

Golang是Google开发的一种静态强类型强类型、编译型、并发型,并具有垃圾回收功能的编程语言。如果已经安装请跳过。

使用以下命令安装Golang。

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go

创建GOPATH以及GOROOT路径。

cd ~
mkdir go 

配置Go环境变量,使用Vim打开环境变量配置文件。

安装vim 如果有vim请跳过

sudo apt install vim
创建/usr/local/go文件夹
cd /usr/local
sudo mkdir go
cd ~
sudo vim /etc/profile

在文件最后输入一下内容:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOBIN

Esc 键退回一般模式,然后输入 :wq 命令并回车保存退出文件。再使用以下命令使变量立即生效。

source /etc/profile

由于Golang的官方代理源速度慢有时候会出现包不能下载的情况,我们使用以下命令把代理源设置为国内的代理源。

go env -w GOPROXY=https://goproxy.cn,direct

使用以下命令查看Golang版本信息。

go version
go1.16.4 linux/amd64

使用以下命令查看Golang环境变量配置。

go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build410299436=/tmp/go-build -gno-record-gcc-switches"

安装IPFS

在各个节点中安装ipfs-update:

GO111MODULE=on go get -u github.com/ipfs/ipfs-update

报错:

compile: version “go1.16.3” does not match go tool version “go1.16.4”

将goROOT/bin文件夹下的 go.exe和gofmt.exe文件复制到 usr目录下的bin文件夹下,替换掉原有的go.exe和gofmt.exe即可

报错:

crypto/md5: package crypto/md5 is not in GOROOT (/usr/local/go/src/crypto/md5)

进入这个网址https : //dist.ipfs.io/#ipfs-update手动下载

然后进入下载的文件夹 运行

sudo ./install.sh

也比较简单,由于 ipfs.io 官网被 dns 污染的原因,安装以后需要配置一下各个节点的/etc/hosts

sudo vi /etc/hosts 

添加:
209.94.78.78    ipfs.io
209.94.90.1     ipfs.io

通过 ipfs-update versions可以 列出所有可以使用和可以下载的ipfs版本.我们这里直接安装最新的版本:

sudo ipfs-update install latest
结果显示
fetching go-ipfs version v0.8.0
binary downloaded, verifying...
success! tests all passed.
checking if we should install in GOBIN: /home/tianzhiwei/go/bin
installing new binary to /home/tianzhiwei/go/bin/ipfs
checking if repo migration is needed...
Installation complete!

这样ipfs就安装成功了,接下来我们需要为每台节点的 IPFS 初始化一下:

ipfs init

创建共享的 key

swarm.key 密钥允许我们创建一个私有网络,并告诉网络节点只和拥有相同秘钥的节点通信,在一个节点上执行下面命令:

go get -u github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen


ipfs-swarm-key-gen > ~/.ipfs/swarm.key

报错:

github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen imports
crypto/rand: package crypto/rand is not in GOROOT (/usr/local/go/src/crypto/rand)
github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen imports
encoding/hex: package encoding/hex is not in GOROOT (/usr/local/go/src/encoding/hex)
github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen imports
fmt: package fmt is not in GOROOT (/usr/local/go/src/fmt)
github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen imports
log: package log is not in GOROOT (/usr/local/go/src/log)

这种问题 可能新系统没跑go程序,里面缺少包,找个旧系统生成swarm.key复制到新系统中

通过scp或者上传的方式将生成的swarm.key拷贝到每一台节点的~/.ipfs/swarm.key

打开查看所有文件找 有可能会被隐藏

移除默认的 bootstrap 节点

为了不连接全球的 IPFS 网络,你需要将默认的 bootstrap 的节点信息删除。

ipfs bootstrap rm --all

私有网络节点配置

在每台节点中添加管理节点的 bootstrap:

ipfs bootstrap add /ip4/10.211.55.7/tcp/4001/ipfs/12D3KooWDivb3qWFLE99jJqqYmWNvWKZbY9mQAWeGGJ6czgkFMFa

ipfs init 时生成的节点 ID,也可以通过ipfs id 查看当前节点的 ID。10.211.55.7为第一个节点的iP地址

我们还需要设置环境变量LIBP2P FORCE PNET来强制我们的网络进入私有模式

export LIBP2P_FORCE_PNET=1

这里将分开讲述

非集群搭建(较简单)

私有网络节点配置

在每台节点中添加另外节点的 bootstrap: (我只用两个节点试了一下 可行 三个节点没试)

ipfs bootstrap add /ip4/10.211.55.7/tcp/4001/ipfs/12D3KooWDivb3qWFLE99jJqqYmWNvWKZbY9mQAWeGGJ6czgkFMFa

添加方法是一样的 这里是相互添加

export LIBP2P_FORCE_PNET=1

后续补充的内容:注意,我在三个节点上试了,可行,在另外两台虚拟机上添加主节点bootstrap就行,无需相互添加

启动服务

先查看虚拟机中添加到节点情况

ipfs swarm peers
显示:
/ip4/10.211.55.7/tcp/4001/p2p/12D3KooWEtfPUEWnpuCyMP6VZJeacobzsKZN9N25SnceTD2mFTdh

启动服务

ipfs daemon       所有节点都启动
tianzhiwei@ubuntu:~$ ipfs daemon
Initializing daemon...
go-ipfs version: 0.8.0
Repo version: 11
System version: amd64/linux
Golang version: go1.15.8
Swarm is limited to private network of peers with the swarm key
Swarm key fingerprint: 49abef989ff17aab09ed85dc7c1e78e2
Swarm listening on /ip4/10.211.55.9/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/fdb2:2c26:f4e4:0:10c8:932d:a22c:347d/tcp/4001
Swarm listening on /ip6/fdb2:2c26:f4e4:0:dcb1:8b4e:6ee1:608d/tcp/4001
Swarm listening on /p2p-circuit
Swarm announcing /ip4/10.211.55.9/tcp/4001
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
WebUI: http://127.0.0.1:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready
ipfs swarm peers

另开一个命令行

服务测试

先选择好需要上传的文件,此处可以新建文件来用于测试。

echo helloworld > hello .txt
cat hello.txt

在第一台虚拟机上添加文件

ipfs add hello.txt
tianzhiwei@ubuntu:~/go/src$ ipfs add hello.txt
added QmUU2HcUBVSXkfWPUc3WUSeCMrWWeEJTuAgR9uyWBhh9Nf hello.txt
 11 B / 11 B [=========================================================] 100.00%

在第二台虚拟机上读取文件

ipfs cat 哈希值
ipfs cat QmUU2HcUBVSXkfWPUc3WUSeCMrWWeEJTuAgR9uyWBhh9Nf
helloworld

在第二台虚拟机上通过网址访问

wget http://127.0.0.1:8080/ipfs/QmUU2HcUBVSXkfWPUc3WUSeCMrWWeEJTuAgR9uyWBhh9Nf

--2021-06-17 20:11:31--  http://127.0.0.1:8080/ipfs/QmTEzo7FYzUCd5aq7bGKoMLfsbbsebpfARRZd4Znejb25R
正在连接 127.0.0.1:8080... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 4 [application/json]
正在保存至: “QmTEzo7FYzUCd5aq7bGKoMLfsbbsebpfARRZd4Znejb25R”

QmTEzo7FYzUCd5aq7bG 100%[===================>]       4  --.-KB/s    用时 0s    

2021-06-17 20:11:31 (213 KB/s) - 已保存 “QmTEzo7FYzUCd5aq7bGKoMLfsbbsebpfARRZd4Znejb25R” [4/4])

通过游览器访问

打开浏览器 输入
http://127.0.0.1:8080/ipfs/QmUU2HcUBVSXkfWPUc3WUSeCMrWWeEJTuAgR9uyWBhh9Nf

**注意 http://127.0.0.1: 这个127.0.0.1是本地访问 本来要输入IP地址的

具体我没试 可以参照下面这段话

特别说明

在外部游览器访问时,要记得修改config文件,不然是访问不了的。

vim /home/.ipfs/config

修改结果如下,主要是修改成你本机的ip即可。
也可以通过指令修改,下面介绍。

通过指令修改,相当于将配置文件中的ip地址直接修改成0.0.0.0

ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080 

集群搭建

将 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.service - IPFS Daemon
   Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: ena
   Active: active (running) since Fri 2021-06-18 10:38:24 CST; 18min ago
 Main PID: 671 (ipfs)
    Tasks: 9 (limit: 2317)
   CGroup: /system.slice/ipfs.service
           └─671 /usr/local/bin/ipfs daemon --enable-namesys-pubsub

6月 18 10:38:26 ubuntu ipfs[671]: Swarm listening on /ip6/fdb2:2c26:f4e4:0:750b:
6月 18 10:38:26 ubuntu ipfs[671]: Swarm listening on /ip6/fdb2:2c26:f4e4:0:98ac:
6月 18 10:38:26 ubuntu ipfs[671]: Swarm listening on /p2p-circuit
6月 18 10:38:26 ubuntu ipfs[671]: Swarm announcing /ip4/10.211.55.10/tcp/4001
6月 18 10:38:26 ubuntu ipfs[671]: Swarm announcing /ip4/127.0.0.1/tcp/4001
6月 18 10:38:26 ubuntu ipfs[671]: Swarm announcing /ip6/::1/tcp/4001
6月 18 10:38:26 ubuntu ipfs[671]: API server listening on /ip4/127.0.0.1/tcp/500
6月 18 10:38:26 ubuntu ipfs[671]: WebUI: http://127.0.0.1:5001/webui
6月 18 10:38:26 ubuntu ipfs[671]: Gateway (readonly) server listening on /ip4/12
6月 18 10:38:26 ubuntu ipfs[671]: Daemon is ready

报错:

Failed to enable unit: File /etc/systemd/system/syslog.service already exists and is a symlink to /lib/systemd/system/rsyslog.service.

进入/etc/systemd/system/syslog.service 删除syslog.service

报错

Job for ipfs.service failed because the control process exited with error code.
See “systemctl status ipfs.service” and “journalctl -xe” for details.

解决办法:打开ipfs.service文件 确认内容是否改变 变了就改回来

报错:

● ipfs.service - IPFS Daemon
Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2021-06-17 09:50:56 CST; 5s ago
Process: 32326 ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub (code=exited,
Main PID: 32326 (code=exited, status=1/FAILURE)

6月 17 09:50:56 ubuntu systemd[1]: Started IPFS Daemon.
6月 17 09:50:56 ubuntu ipfs[32326]: Initializing daemon…
6月 17 09:50:56 ubuntu ipfs[32326]: go-ipfs version: 0.8.0
6月 17 09:50:56 ubuntu ipfs[32326]: Repo version: 11
6月 17 09:50:56 ubuntu ipfs[32326]: System version: amd64/linux
6月 17 09:50:56 ubuntu ipfs[32326]: Golang version: go1.15.8
6月 17 09:50:56 ubuntu ipfs[32326]: Error: no IPFS repo found in /root/.ipfs.
6月 17 09:50:56 ubuntu ipfs[32326]: please run: ‘ipfs init’
6月 17 09:50:56 ubuntu systemd[1]: ipfs.service: Main process exited, code=exited, status=1/
6月 17 09:50:56 ubuntu systemd[1]: ipfs.service: Failed with result ‘exit-code’.

解决办法:

将/home/tianzhiwei/.ipfs目录复制到root/目录下

报错:

ipfs.service - IPFS Daemon
Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2021-06-17 21:44:29 CST; 4s ago
Process: 27908 ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub (code=exited, status=203/EXEC)
Main PID: 27908 (code=exited, status=203/EXEC)

6月 17 21:44:29 ubuntu systemd[1]: Started IPFS Daemon.
6月 17 21:44:29 ubuntu systemd[27908]: ipfs.service: Failed to execute command: No such file or directory
6月 17 21:44:29 ubuntu systemd[27908]: ipfs.service: Failed at step EXEC spawning /usr/local/bin/ipfs: No such file or directory
6月 17 21:44:29 ubuntu systemd[1]: ipfs.service: Main process exited, code=exited, status=203/EXEC
6月 17 21:44:29 ubuntu systemd[1]: ipfs.service: Failed with result ‘exit-code’.

解决办法:

搜索ipfs 和ipfs-update文件 放到/usr/local/bin目录下 我也不知道同样的虚拟机下载的文件乱跑

截屏2021-06-17 下午10.02.40

报错:

6月 17 22:03:39 ubuntu ipfs[31573]: 2021-06-17T22:03:39.410+0800 ERROR cmd/ipfs error from node construction: failed to listen on any addresses: [listen tcp4 0.0.0.0:4001: bind: address already in use listen tcp6 [::]:4001: bind: address already in use no transport for protocol no transport for protocol]
6月 17 22:03:39 ubuntu ipfs[31573]: Error: failed to listen on any addresses: [listen tcp4 0.0.0.0:4001: bind: address already in use listen tcp6 [::]:4001: bind: address already in use no transport for protocol no transport for protocol]
6月 17 22:03:39 ubuntu systemd[1]: ipfs.service: Main process exited, code=exited, status=1/FAILURE
6月 17 22:03:39 ubuntu systemd[1]: ipfs.service: Failed with result ‘exit-code’.

解决办法:

重启电脑

ipfs bootstrap add /ip4/10.211.55.7/tcp/4001/ipfs/12D3KooWDivb3qWFLE99jJqqYmWNvWKZbY9mQAWeGGJ6czgkFMFa

从这一步开始重新来一遍 里面添加的节点 不知道怎么变了

报错:

$ ipfs daemon

Initializing daemon…
go-ipfs version: 0.8.0
Repo version: 11
System version: amd64/linux
Golang version: go1.15.8
2021-06-17T15:24:50.585+0800 ERROR cmd/ipfs error from node construction: could not build arguments for function “github.com/ipfs/go-ipfs/core/node”.PeerWith.func1 (github.com/ipfs/go-ipfs@v0.8.0/core/node/peering.go:29): failed to build *peering.PeeringService: could not build arguments for function “github.com/ipfs/go-ipfs/core/node”.Peering (github.com/ipfs/go-ipfs@v0.8.0/core/node/peering.go:14): failed to build host.Host: could not build arguments for function “github.com/ipfs/go-ipfs/core/node/libp2p”.Host (github.com/ipfs/go-ipfs@v0.8.0/core/node/libp2p/host.go:40): could not build value group []config.Option[group=“libp2p”]: received non-nil error from function “github.com/ipfs/go-ipfs/core/node/libp2p”.PNet (github.com/ipfs/go-ipfs@v0.8.0/core/node/libp2p/pnet.go:21): failed to configure private network: EOF

解决办法:swarm.key没有生成好 你可能生成的文件是空的 百度各种方法 反正我搞了半天

报错:

● ipfs.service - IPFS Daemon
Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2021-06-18 10:59:40 CST; 16s ago
Process: 6385 ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub (code=exite
Main PID: 6385 (code=exited, status=1/FAILURE)

6月 18 10:59:40 ubuntu systemd[1]: Started IPFS Daemon.
6月 18 10:59:40 ubuntu ipfs[6385]: Initializing daemon…
6月 18 10:59:40 ubuntu ipfs[6385]: go-ipfs version: 0.8.0
6月 18 10:59:40 ubuntu ipfs[6385]: Repo version: 11
6月 18 10:59:40 ubuntu ipfs[6385]: System version: amd64/linux
6月 18 10:59:40 ubuntu ipfs[6385]: Golang version: go1.15.8
6月 18 10:59:40 ubuntu ipfs[6385]: Error: resource temporarily unavailable
6月 18 10:59:40 ubuntu systemd[1]: ipfs.service: Main process exited, code=exited, statu
6月 18 10:59:40 ubuntu systemd[1]: ipfs.service: Failed with result ‘exit-code’.

解决办法

ipfs奔溃了 可能是我昨晚没关电脑所致 好几次了 可以选择删掉这个虚拟机从头弄

删掉LOCK 和repo.lock这两个文件 具体百度 我忘记抄了

报错:

● ipfs.service - IPFS Daemon
Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2021-06-18 11:26:49 CST; 3s ago
Process: 11445 ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub (code=exited, status=1/FAILURE)
Main PID: 11445 (code=exited, status=1/FAILURE)

6月 18 11:26:49 ubuntu ipfs[11445]: Swarm listening on /ip6/::1/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm listening on /ip6/fdb2:2c26:f4e4:0:c42e:2211:d697:18e1/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm listening on /ip6/fdb2:2c26:f4e4:0:dcb1:8b4e:6ee1:608d/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm listening on /p2p-circuit
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm announcing /ip4/10.211.55.9/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm announcing /ip4/127.0.0.1/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Swarm announcing /ip6/::1/tcp/4001
6月 18 11:26:49 ubuntu ipfs[11445]: Error: serveHTTPApi: manet.Listen(/ip4/127.0.0.1/tcp/5001) failed: listen tcp4 127.0.0.1:5001: bind: address already in use
6月 18 11:26:49 ubuntu systemd[1]: ipfs.service: Main process exited, code=exited, status=1/FAILURE
6月 18 11:26:49 ubuntu systemd[1]: ipfs.service: Failed with result ‘exit-code’.

解决办法:

端口被占用了 可能是我试那个非集群的时候导致的

修改端口文件 config 5001我改成了6001

我他妈心态崩了 三台虚拟机所有的坑试完 下一步

IPFS-Cluster 安装

IPFS-Cluster 包含两个组件:

  • ipfs-cluster-service 用于初始化集群 peer 并运行它的守护进程

  • ipfs-cluster-ctl 管理集群的节点和数据

我们将ipfs-cluster克隆到 GOPATH 下,然后 make 编译安装(系统已安装 make):

cd $GOPATH/src
git clone https://github.com/ipfs/ipfs-cluster.git
cd ipfs-cluster
export GO111MODULE=on # optional, if checking out the repository in $GOPATH.
go install ./cmd/ipfs-cluster-service
go install ./cmd/ipfs-cluster-ctl
go install ./cmd/ipfs-cluster-follow

查看一下是否安装成功:

ipfs-cluster-service --version
ipfs-cluster-ctl --version

报错:

go:11:8: cannot find package “unsafe” in any of:
/usr/local/go/src/unsafe (from $GOROOT)
/home/tianzhiwei/go/src/unsafe (from $GOPATH)

解决办法:

鬼知道又是什么问题 换一种安装方法

到官网下载https://dist.ipfs.io/#ipfs-cluster-service

下载ipfs-cluster-service的二进制执行文件到$GOPATH/src文件夹下

命令行运行解压 tar zxvf ipfs-cluster-service_v0.13.3_linux-amd64.tar.gz

cd ipfs-cluster-service

将ipfs-cluster-service文件复制到usr/local/bin

设置集群密钥

类似于 IPFS 的秘钥,我们管理节点中生成一个随机密钥:

 od -vN 32 -An -tx1 /dev/urandom | tr -d ' \n'

将生成的随机你字符串加入到环境变量中,比如: b55262c36de6f97bd50b5233f75866445ec51db74613bad78e906c4dc9ba1d30 分别修改每一个节点的的~/.bashrc添加到环境变量中:

cd ~
vi .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

报错:

月 18 16:36:09 ubuntu systemd[1]: Started IPFS-Cluster Daemon.
6月 18 16:36:09 ubuntu systemd[54464]: ipfs-cluster.service: Failed to execute command: No such file or directory
6月 18 16:36:09 ubuntu systemd[54464]: ipfs-cluster.service: Failed at step EXEC spawning /root/go/bin/ipfs-cluster-service: No such fil>
6月 18 16:36:09 ubuntu systemd[1]: ipfs-cluster.service: Main process exited, code=exited, status=203/EXEC
6月 18 16:36:09 ubuntu systemd[1]: ipfs-cluster.service: Failed with result ‘exit-code’.

解决办法

跟上面一样 复制文件

报错:

ipfs-cluster.service - IPFS-Cluster Daemon
Loaded: loaded (/etc/systemd/system/ipfs-cluster.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2021-06-18 16:46:46 CST; 4s ago
Process: 56896 ExecStart=/usr/local/bin/ipfs-cluster-service daemon (code=exited, status=1/FAILURE)
Main PID: 56896 (code=exited, status=1/FAILURE)

6月 18 16:46:46 ubuntu systemd[1]: Started IPFS-Cluster Daemon.
6月 18 16:46:46 ubuntu ipfs-cluster-service[56896]: 2021-06-18T16:46:46.208+0800 INFO service ipfs-cluster-service/>
6月 18 16:46:46 ubuntu ipfs-cluster-service[56896]: error creating libp2p host: failed to listen on any addresses: [listen tcp4 0.0.0.0:>
6月 18 16:46:46 ubuntu systemd[1]: ipfs-cluster.service: Main process exited, code=exited, status=1/FAILURE
6月 18 16:46:46 ubuntu systemd[1]: ipfs-cluster.service: Failed with result ‘exit-code’.

解决办法:

解决不了 心态崩了 不知道为什么要设置守护程序 不守护感觉也行啊

测试一下集群数据复制

在其中一台节点中添加一个文件:

ipfs-cluster-ctl add test.txt

通过添加的文件 CID 来查看文件状态,可以看到文件以及在所有节点中PINNED

ipfs-cluster-ctl status CID

https://www.itread01.com/content/1533578312.html

后面有心的大佬可以根据这个改一下 试试不设置系统守护程序会怎样

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值