IPFS: A protocol and network designed to create a content-addressable, peer-to-peer method of storing and sharing hypermedia in a distributed file system. Read more
IPFS-Cluster: 一個 IPFS集群是一個獨立的程序和一個 CLI 客戶端。通過一組IPFS守護進程負責分配、復制以及跟蹤 pins 。 IPFS-Cluster uses a leader-based consensus algorithm Raft to coordinate storage of a pinset, distributing the set of data across the participating nodes.
A cluster peer application: ipfs-cluster-service, to be run along with go-ipfs.
A client CLI application: ipfs-cluster-ctl, which allows easily interacting with the peer's HTTP API.
An additional "follower" peer application: ipfs-cluster-follow, focused on simplifying the process of configuring and running follower peers.
IPFS 4001 – Communication with other nodes 5001 – API server 8080 – Gateway server
IPFS-CLUSTER 9094 – HTTP API endpoint 9095 – IPFS proxy endpoint 9096 – Cluster swarm, used for communication between cluster nodes
We will use recently created three virtual machines (in my case I used DigitalOcean) with installed Linux Ubuntu Distributive version 16.04 and command line as the main tool for installing necessary packages and settings. Depending on your cloud provider (AWS, Azure, Google, etc.), you may need to look at some additional settings, like firewall or security group configuration, to let your peers see each other.
Let’s suppose that we have three VMs with the following IP addresses: Node0: 192.168.10.1 Node1: 192.168.10.2 Node2: 192.168.10.3
Let’s start with the zero node (Node0) which will be our bootstrap node.
Step 1: 安裝 Go
First of all, let’s install Go as we will need it during our deployment process. Update Linux packages and dependencies:
We will install the latest version of the go-IPFS. At the moment of writing this article, it was v0.4.18 for Linux. You can check for the latest version here https://dist.IPFS.io/#go-IPFS
Download IPFS, unzip tar file, move unzipped folder under bin and initialise IPFS node:
Once you have Go and IPFS installed on all of your nodes, run the following command to install the swarm key generation utility. Swarm key allows us to create a private network and tell network peers to communicate only with those peers who share this secret key.
This command should be run only on your Node0. We generate swarm.key on the bootstrap node and then just copy it to the rest of the nodes.
1go get -u github.com/Kubuxu/go-IPFS-swarm-key-gen/IPFS-swarm-key-gen
?
?
Now run this utility on your first node to generate swarm.key under .IPFS folder:
1``IPFS-swarm-key-gen & > ~/.IPFS/swarm.key
?
?
Copy the file generated swarm.key to the IPFS directory of each node participating in the private network. First of all, you need to remove the default entries of bootstrap nodes from all the nodes you have created.
Step 4: 自引導IPFS 節點
1``IPFSbootstrap rm –all
?
?
Add the hash address of your bootstrap to each of the nodes including the bootstrap.
The IP part (192.168.10.1) will be changed to your Node0 machine IP. The last part is the peer ID which is generated when you initialise your peer IPFS init). You can see it above where it shows “peer identity:
1QmQVvZEmvjhYgsyEC7NvMn8EWf131EcgTXFFJQYGSz4Y83
?
?
or if you run *IPFS id* command in the console. So, you need to change IP and peer ID accordingly to you Node0. Do this for all of your nodes.
We also need to set the environment variable “LIBP2P_FORCE_PNET” to force our network to Private mode:
1export LIBP2P_FORCE_PNET=1
?
?
Configuring IP for communication
Inside the .IPFS folder, there is a “config” file. It contains a lot of settings including the network details on which our IPFS nodes will work on. Open this config file and find “Addresses”. It will look like this:
1"Addresses"``: {
?
?
2"API"``: ``"/ip4/192.168.10.1/tcp/5001"``,
?
?
3"Announce"``: [],
?
?
4"Gateway"``: ``"/ip4/192.168.10.1/tcp/8080"``,
?
?
5"NoAnnounce"``: [],
?
?
6"Swarm"``: [
?
?
7"/ip4/0.0.0.0/tcp/4001"``,
?
?
8"/ip6/::/tcp/4001"
?
?
9]
?
?
10},
?
?
The IP mentioned in the API is the one on which IPFS will bind on for communication. By default, it’s localhost (127.0.0.1), so to enable our nodes to “see” each other we need to set this parameter accordingly to each node’s IP. Gateway parameter is for access from the browser.
Step 5: 節點啟動與測試
We are done with all the configurations, and now it is time to start all the nodes to see if everything went well and if they are closed to the private network. Run IPFS daemon on all of your nodes.
1``IPFSdaemon
?
?
Now let’s add the file from one of the nodes and try to access it from another.
1mkdir test-files
?
?
2echo helloIPFS& > file.txt
?
?
3``IPFSadd file.txt
?
?
Take the printed hash and try to the cat file from another node.
You should see the contents of the added file from the first node. To check and be sure that we have a private network we can try to access our file by its CID from the public IPFS gateway. You can choose one of the public gateways from this list: https://IPFS.github.io/public-gateway-checker.
If you did everything right, then the file won’t be accessible. Also, you can run the *IPFS swarm peers* command, and it will display a list of the peers in the network it’s connected to. In our example, each peer sees two others.
Step 6: 后臺服務方式啟動 IPFS 守護進程
For IPFS demon to be continually running, even after we have exited from our console session, we will create systemd service. Before we do so, stop/kill your IPFS daemon. Create a file for a new service.
Reboot your system and check that IPFS daemon is active and running, and then you can again try to add the file from one node and access it from another.
We have completed part of creating a private IPFS network and running its demons as a service. At this phase, you should have three IPFS nodes organized in one private network. Now let’s create our IPFS-CLUSTER for data replication.
部署 IPFS-Cluster
After we create a private IPFS network, we can start deploying IPFS-Cluster on top of IPFS for automated data replication and better management of our data.
There are two ways how to organize IPFS cluster, the first one is to set a fixed peerset (so you will not be able to increase your cluster with more peers after the creation) and the other one – to bootstrap nodes (you can add new peers after cluster was created).
IPFS-Cluster includes two components:
IPFS-cluster-service mostly to initialize cluster peer and run its daemon
IPFS-cluster-ctl for managing nodes and data among the cluster
Step 1: 安裝 IPFS-Cluster
There are many ways how to install IPFS-Cluster. In this manual, we are using the installing from source method. You can see all the provided methods here.
Run next commands in your console terminal to install IPFS-cluster components:
Now we need to generate CLUSTER_SECRET and set it as an environment variable for all peers participating in the cluster. Sharing the same CLUSTER_SECRET allow peers to understand that they are part of one IPFS-Cluster. We will generate this key on the zero node and then copy it to all other nodes. On your first node run the following commands:
In order for CLUSTER_SECRET to not disappear after you exit the console session, you must add it as a constant environment variable to the .bashrc file. Copy the printed key after echo command and add it to the end of .bashrc file on all of your nodes.
And don’t forget to update your .bashrc file with command:
1source ~/.bashrc
?
?
Step 3: cluster初始化和啟動
After we have installed IPFS-Cluster service and set a CLUSTER_SECRET environment variable, we are ready to initialize and start first cluster peer (Node0).
Note: make sure that your IPFS daemon is running before you start the IPFS-cluster-service daemon. To initialize cluster peer, we need to run the command:
1-cluster-service init
?
?
To start cluster peer, run:
1-cluster-service daemon
?
?
You should see the output in the console:
1INFO cluster:IPFSCluster is ready cluster.go:461
?
?
2``IPFS-cluster-service daemon
?
?
You should see the output in the console:
1INFO cluster:IPFSCluster is ready cluster.go:461
?
?
Now open a new console window and connect to your second VM(node1). Note: make sure that your IPFS daemon is running before you start the IPFS-cluster-service daemon.
You need to install IPFS-Cluster components and set a CLUSTER_SECRET environment variable (copy from node0) as we did it for our first node. Run the following commands to initialise IPFS-Cluster and bootstrap it to node0:
The IP part (192.168.10.1) will be changed to your Node0 machine IP. The last part is the cluster peer ID which is generated when you initialise your cluster peer(IPFS-cluster-service init). Bear in mind that it should be IPFS-Cluster peer ID, not an IPFS peer ID.
You can run *IPFS-cluster-service* *id* command in the console to get this. You need to change IP and cluster peer ID according to your Node0. Do this for all of your nodes. To check that we have two peers in our cluster, run command:
1``IPFS-cluster-ctl peers ls
?
?
And you should see the list of cluster peers:
1node1 & >IPFS-cluster-ctl peers ls
?
?
2QmYFYwnFUkjFhJcSJJGN72wwedZnpQQ4aNpAtPZt8g5fCd | Sees 1 other peers
Repeat this step for the third node and all others nodes you want to join to the cluster.
Step 4: 以服務方式啟動 IPFS-Cluster 守護進程
For the IPFS-Cluster daemon to be continually running, even after we close console session, we will create systemd service for it. Run the following command to create a file for IPFS-Cluster system service:
Reboot your machine and check that both IPFS and IPFS-Cluster services are running.
Step 5: 測試 IPFS-Cluster 與數據復制
To test data replication, create the file and add it to the cluster:
1``IPFS-cluster-ctl add myfile.txt
?
?
Take CID of the recently added file and check its status:
1``IPFS-cluster-ctl status CID
?
?
You should see that this file has been PINNED among all cluster nodes.
總結
Are you wondering how you can apply this IPFS tutorial to support your real-life needs? This article describes how we started with an internal PoC and ended up with a real prototype allowing us to share files on the blockchain with IPFS securely.
If you have any questions regarding IPFS networks and their potential use for data replication and secure data sharing, don’t hesitate to get in touch!