Teach you how to build an IPFS private network

Teach you how to build an IPFS private network

Loading

Distributed storage is getting started, let’s learn step by step to build your own IPFS network.

Original title: “IPFS Builds and Uses Private Network”
Written: Six days

In the consortium chain scenario, IPFS is the preferred solution for decentralized storage. This article will introduce how to use go-ipfs to build a private network and use it simply.

My environment

Due to resource constraints, I use docker to build a two-node IPFS private network. If possible, you can install it directly on multiple machines or multiple virtual machines.

Start building

1. Generate swarm.key

swarm.key is a shared key. Only nodes with the same key can communicate with each other to form a private key network. swarm.key can be generated using tools , the installation command of the tool is:

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

After the installation is complete, generate swarm.key, the command is:

 ipfs-swarm-key-gen> /Users/sixdays/tmp/ipfs/swarm.key  

among them

  • /Users/sixdays/tmp/ipfs/ directory is my ipfs directory.
  • /Users/sixdays/tmp/ipfs/node1 is my ipfs node 1 directory.
  • /Users/sixdays/tmp/ipfs/node2 is my ipfs node 2 directory.

2. Start the node

Run node 1 and node 2

 // run node 1
docker run -d --name ipfs_node_1 -e IPFS_SWARM_KEY_FILE=/Users/sixdays/tmp/ipfs/swarm.key -v /Users/sixdays/tmp/ipfs/node1/staging:/export -v /Users/sixdays/tmp/ ipfs/node1/data:/data/ipfs -p 4001:4001 -p 4001:4001/udp -p 127.0.0.1:8080:8080 -p 127.0.0.1:5001:5001 ipfs/go-ipfs:latest

// run node 2
docker run -d --name ipfs_node_2 -e IPFS_SWARM_KEY_FILE=/Users/sixdays/tmp/ipfs/swarm.key -v /Users/sixdays/tmp/ipfs/node2/staging:/export -v /Users/sixdays/tmp/ ipfs/node2/data:/data/ipfs -p 4002:4001 -p 4002:4001/udp -p 127.0.0.1:8081:8080 -p 127.0.0.1:5002:5001 ipfs/go-ipfs:latest

Clear all default startup nodes bootstrap

 docker exec ipfs_node_1 ipfs bootstrap rm all  
    docker exec ipfs_node_2 ipfs bootstrap rm all  

View node id

 docker exec ipfs_node_1 ipfs id  
    docker exec ipfs_node_2 ipfs id  

Here, the Id of my node 1 is:
12D3KooWEVo8FqH8YUT1noXvca5hgSRWBRcDQomEcFY2zXwA7dbw
The address of node 1 is:
/ip4/172.17.0.3/tcp/4001/p2p/12D3KooWEVo8FqH8YUT1noXvca5hgSRWBRcDQomEcFY2zXwA7dbw

The Id of node 2 is
12D3KooWRcx6gpbsbvb6YiNsDpJgnmNHFCgCiUmVF42o4zn2W5Pj
The address of node 2 is
/ip4/172.17.0.4/tcp/4001/p2p/12D3KooWRcx6gpbsbvb6YiNsDpJgnmNHFCgCiUmVF42o4zn2W5Pj

The node address I use is the ip address assigned by docker.

Add node id

Add node 2 address in node 1

 docker exec ipfs_node_1 ipfs bootstrap add /ip4/172.17.0.4/tcp/4001/p2p/12D3KooWRcx6gpbsbvb6YiNsDpJgnmNHFCgCiUmVF42o4zn2W5Pj

Add node 1 address in node 2

 docker exec ipfs_node_1 ipfs bootstrap add /ip4/172.17.0.3/tcp/4001/p2p/12D3KooWEVo8FqH8YUT1noXvca5hgSRWBRcDQomEcFY2zXwA7dbw

So far, our two-node IPFS private network has been completed.

Use it

Use command

 docker exec ipfs_node_1 ipfs -h  

You can see the basic commands of IPFS.

Add file add

 $ docker exec ipfs_node_1 ipfs add /data/ipfs/swarm.key                                                                                                                    
    added QmRitSEMhFJtNhLYtwGRJvhDrTTT4gQLjuLepZjo9C8a2X swarm.key  

among them:

View file cat

 $ docker exec ipfs_node_2 ipfs cat QmRitSEMhFJtNhLYtwGRJvhDrTTT4gQLjuLepZjo9C8a2X   
    /key/swarm/psk/1.0.0/  
    /base16/  
    5b9941085678c502b44cc98e2614dd648cb801115dcb6acee8e83d9bf8cf454c  

You can see that we can view the content of the file uploaded by node1 in node2.

Download file get

 $ docker exec ipfs_node_2 ipfs get QmRitSEMhFJtNhLYtwGRJvhDrTTT4gQLjuLepZjo9C8a2X -o /data/ipfs/test.key  
    Saving file(s) to /data/ipfs/test.key  
     95 B / 95 B 100.00% 0s  

Where -o represents the output directory, /data/ipfs in the docker container corresponds to our host directory as /Users/sixdays/tmp/ipfs/node2/data

View file list ls

 $ docker exec ipfs_node_2 ipfs pin ls  
    QmQ5vhrL7uv6tuoN9KeVBwd4PwfQkXdVVmDLUZuTNxqgvm indirect  
    QmU5k7ter3RdjZXu3sHghsga1UQtrztnQxmTL22nPnsu3g indirect  
    QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y indirect  
    QmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y indirect  
    QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB indirect  
    QmQGiYLVAdSHJQKYFRTJZMG4BXBHqKperaZtyKGmCRLmsF indirect  
    QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc recursive  
    QmQy6xmJhrcC5QLboAcGFcAE1tC8CrwDVkrHdEYJkLscrQ indirect  
    QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn recursive  

Where recursive means folder, indirect means file

reference

Source link: mp.weixin.qq.com