Hyperledger Fabric blockchain networks are notoriously cumbersome to build. This tutorial will show you how to deploy a Hyperledger Fabric blockchain cluster network with multiple sort and peer nodes distributed over four hosts, with the source code and configuration files available for download.

Recommended tutorials:

  • Hyperledger Fabric Java development in detail
  • Hyperledger Fabric Node.js development in detail

1. Service structure

The Hyperledger Fabric network topology is as follows:

  • One organization: org1.example.com
  • Three peer nodes: peer0.example.com, peer1.example.com, and peer2.example.com
  • One CA node: ca.example.com
  • Three sorting nodes: order0.example.com, order1.example.com, order2.example.com
  • Three ZooKeeper nodes: ZooKeeper0, ZooKeeper1, and zooKeeper2
  • Four kafka nodes: kafka0, kafka1, kafka2, kafka3

2. Deploy the service

2.1 Deploying CA, Orderer, and Kafka

As shown in the figure above, the CA node, the sort node, the kafka node and the Zookeeper node are first deployed on server1. The docker-compose file is docker-comement-kafka.yml:

# deploy ca, zookeerper, kafka and orderers on server0
docker-compose  -f deployment/docker-compose-kafka.yml up -d
Copy the code

In the Docker-compose file, we define the extra_hosts property for the CA and Orderer services, which contains information for all peer nodes. Since the CA and ORDERer may need to communicate with their peers, they need to know about their peers. Peers are deployed on different hosts, so we can define the hosts for peer0, peer1, and peer2 in the EXTRA_hosts field.

extra_hosts:
    - "Peer0.org1.example.com: 172.31.26.5"
    - "Peer1.org1.example.com: 172.31.20.177"
    - "Peer2.org1.example.com: 172.31.27.143"
Copy the code

2.2 Deploying Peer0 and CLI0

We deploy peer0 and cli0 on Server2, where CLI0 will access Peer0. Docker-compose: docker-compose: peer0.yml: docker-compose: peer0.yml: docker-compose: peer0.yml: docker-compose: peer0.yml: docker-compose: peer0.yml: docker-compose: peer0.yml

# deploy peer0
docker-compose -f deployment/docker-compose-peer0.yml up -d

# deploy cli0
docker-compose -f deployment/docker-compose-cli0.yml up -d
Copy the code

In docker-comement-peer0.yml, we define the following extra_hosts field, which contains the host information for all sort nodes, peer1 and peer2. The main reason for adding information about other peers is that peers use the Gossip protocol to broadcast blocks to other peers.

extra_hosts:
    - "Orderer0.example.com: 172.31.25.198"
    - "Orderer1.example.com: 172.31.25.198"
    - "Orderer2.example.com: 172.31.25.198"
    - "Ca.example.com: 172.31.25.198"
    - "Peer1.org1.example.com: 172.31.20.177"
    - "Peer2.org1.example.com: 172.31.27.143"
Copy the code

Docker-comement-cli0.yml contains the host information of the sorting node, because cli commands need to communicate with the sorting node when executing transactions:

extra_hosts:
  - "Orderer0.example.com: 172.31.25.198"
  - "Orderer1.example.com: 172.31.25.198"
  - "Orderer2.example.com: 172.31.25.198"
Copy the code

2.3 Deploying Peer1 and CLI1

Next we deploy peer1 and cli1 on server3:

# deploy peer1
docker-compose -f deployment/docker-compose-peer1.yml up -d

# deploy cli1
docker-compose -f deployment/docker-compose-cli1.yml up -d
Copy the code

The extra_hosts in docker-comement-peer1.yml contains information for sorting node hosts and other peer node hosts (peer0 and Peer2). The extra_hosts field in docker-comement-cli. yml defines the host information for the sort node.

2.4 Deploying Peer2 and CLI2

Finally, we deploy peer2 and cli2 on server4:

# deploy peer2
docker-compose -f deployment/docker-compose-peer2.yml up -d

# deploy cli2
docker-compose -f deployment/docker-compose-cli2.yml up -d
Copy the code

Extra_hosts also needs to be specified in the docker-compose file.

3. Configure channels

Now that the service is deployed, it’s time to configure the channels.

3.1 Creating a Channel

Create a channel by accessing peer0 on server2:

# create channel from peer0 on server2
# it connects to orderer0
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" \
            -e "CORE_PEER_MSPCONFIGPATH=/var/hyperledger/users/[email protected]/msp" \
            peer0.org1.example.com peer channel create 
            -o orderer0.example.com:7050 -c mychannel -f /var/hyperledger/configs/channel.tx
Copy the code

Next we will add all three peers to the channel

3.2 Add Peer0 to the Channel

Now access peer0 on Server2 and execute the following command:

# join peer0 to channel
# execute this command from server1
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" \
            -e "CORE_PEER_MSPCONFIGPATH=/var/hyperledger/users/[email protected]/msp" \
            peer0.org1.example.com peer channel join -b mychannel.block
Copy the code

The command above generates mychannel.block in the Peer0 container.

3.3 Copy mychannel.block to peer1 and peer2

# copy mychannel.block from peer0 to host(server2)
docker cp peer0.org1.example.com:/mychannel.block .

# transfer mychannel.block to server3 and server4 via scpScp-r mychannel.block [email protected]: scp-r mychannel.block [email protected]:# copy mychannel.block to peer1 and peer2 
# peer1 is on server3
# peer2 is on server4
docker cp mychannel.block peer1.org1.example.com:/mychannel.block
docker cp mychannel.block peer2.org1.example.com:/mychannel.block

# remove mychannel.block from server2, server3 and server4
rm mychannel.block
Copy the code

3.4 Adding Peer1 to the Channel

Now you can add peer1 on server3 to the channel:

# join peer1 to channel
# execute this command from server3 machine
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/var/hyperledger/users/[email protected]/msp" \
            peer1.org1.example.com peer channel join -b mychannel.block
Copy the code

3.5 Add Peer2 to the Channel

Similarly, peer2 on server4 can be added to the channel:

# join peer2 to channel
# execute this command from server4 machine
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/var/hyperledger/users/[email protected]/msp" \
            peer2.org1.example.com peer channel join -b mychannel.block
Copy the code

4. Configure the chain code

4.1 Installing chain codes

The chaincode is in the chaincode directory. We used cli to install chain codes on each peer node.

# install on peer0 on server2
# cli container connects to peer0 
docker exec -it cli peer chaincode install -n mycc -p github.com/chaincode -v v0

# install on peer1 on server3
# cli container connects to peer1 
docker exec -it cli peer chaincode install -n mycc -p github.com/chaincode -v v0

# install on peer2 on server4
# cli container connects to peer2
docker exec -it cli peer chaincode install -n mycc -p github.com/chaincode -v v0
Copy the code

4.2 Instantiating chain codes

Now you can instantiate the chain code. Only one instantiation is required on the channel. Therefore, we use cli0 on Server2 for chain code instantiation:

# instantiate chaincode from peer0 on server2
# it connects to orderer0 
docker exec -it cli peer chaincode instantiate -o orderer0.example.com:7050 \
            -C mychannel -n mycc github.com/chaincode -v v0 -c '{"Args": ["a", "100"]}'
Copy the code

5. Transaction execution and inquiry

5.1 Executing a Transaction

We use cli0 on Server2 to call the chain code transaction:

# invoke transaction from peer0 on server2
# it connects to orderer0 on server1
docker exec -it cli peer chaincode invoke -o orderer0.example.com:7050 \
            -n mycc -c '{"Args":["set", "a", "20"]}' -C mychannel
Copy the code

5.2 Querying a Transaction

We use cli2 on Server4 to query transactions:

# query transaction from peer2 on server4
docker exec -it cli peer chaincode query \
            -n mycc -c '{"Args":["query","a"]}' -C mychannel
Copy the code

The source code and configuration files for this tutorial can be downloaded here.


Hyperledger Fabric Multi-machine Deployment Tutorial – Huizhi.com