30 minutes to Setup Dev/Test Hyperledger Fabric cluster on multiple hosts (not for deployment)

(20210124 This article is (over)due for an update to Fabric 2.3/2.2.x, until then, you can still play around with 1.4.4 as below.)

This is a guide to set up a Fabric Network of 1 orderer with Kafka, 1 Organisation with 3 peers, deployed across 3 nodes using extra_hosts configuration. This network is tested on Digital Ocean droplets with Ubuntu 18.04 with Hyperledger Fabric 1.4.4. Bare minimum of 2GB RAM for around 60% CPU (high) utilisation.

For Chinese verison of this article, clicke here. 点击这里即可查看本文的中文版。

Depending on the speed of the network connection, you can just about run all the steps below perfectly and install it in around 30 mins. Let’s start. (I recommend getting at least the $15 bucks a month Droplet from DigitalOcean, you will need the 3GB RAM to run it smoothly, use my link (Discount code for newbies, I get some free referral credits too) to sign up. Then run the following steps.

Check out my other articles on just running Hyperledger Fabric 1.4.4, Hyperledger Composer and Hyperledger Explorer. We aim to publish a full set of these guides on how to play around with Hyperledger technologies. I am a big fan of Brave browser, if you are into blockchain and crypto, should try it, make a little crypto from your regular web browsing.

The setup of the nodes are as followed:

(Unless specified, the steps will need to be run on all droplets.)

Step 1: Set up your new droplet
On all 3 droplets, run the following command to start it up.

sudo apt-get update && sudo apt-get upgrade

Step 2: Create a new user and switch into the user
For subsequent steps, we need to create a new user in order to set up the network.

sudo adduser frog
sudo usermod -aG sudo frog
su - frog

Step 3: Download dependencies for Hyperledger Fabric.

curl -O https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh
chmod u+x prereqs-ubuntu.sh
./prereqs-ubuntu.sh
wget https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz
tar -xzvf go1.11.2.linux-amd64.tar.gz
sudo mv go/ /usr/local
nano ~/.bashrc
#(add these 2 lines to end of file)
export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
exit
su - frog

Step 4: Fetch Fabric image and other tools required to generate channel artefacts and certificates.

curl -sSL http://bit.ly/2ysbOFE | bash -s

Step 5: (Node 1) Download files required to set up Fabric.

git clone -b extra_hosts https://github.com/eugeneyl/one-org-kafka.git

Step 6: (Node 1) Generate the channel artefacts and certificates required.

cd fabric-samples
export PATH=$PATH:$PWD/bin
cd one-org-kafka
nano .env

#Change the ip address of the nodes to the ip address of your droplets.

./generate.sh

Change the FABRIC_CA_SERVER_CA_KEYFILE and FABRIC_CA_SERVER_TLS_KEYFILE of the CA in node1.yaml to reflect the actual key that is generated. You can find the key in one-org-kafka/crypto-config/peerOrganizations/org1.example.com/ca

Step 7: (Node1) Zip the file and transfer the file to the other 2 nodes. You can use filezilla for this transfer.

cd ..
tar -czvf one-org-kafka.tar.gz one-org-kafka

Step 8: (Node 2 and 3) Unzip the folder in the node.

tar -xzvf one-org-kafka.tar.gz one-org-kafka

Step 9: Set up the docker containers for the different components.

cd one-org-kafka
docker-compose -f node1.yaml up -d
docker-compose -f node2.yaml up -d
docker-compose -f node3.yaml up -d

Step 10 (Node 1): Create the channel block and transfer it to the other nodes.

docker exec cli peer channel create -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pemdocker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block .

Step 11 (Node 2 and 3): Transfer channel block into the cli container.

docker cp mychannel.block cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/

Step 12: Join all the peers to the channel

docker exec cli peer channel join -b mychannel.block

Step 13: Install the chaincode on all peers, instantiate only on node 1

docker exec cli peer chaincode install -n orders -v 1.0 -p github.com/chaincode/orders/

(Only on node 1)

docker exec cli peer chaincode instantiate -o orderer0.example.com:7050 -C mychannel -n orders -v 1.0 -c '{"Args":[]}' --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Step 14: Try out the chain code on each peer

docker exec cli peer chaincode invoke -o orderer0.example.com:7050 -C mychannel -n orders -c '{"Args":["initLedger"]}' --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pemdocker exec cli peer chaincode query -C mychannel -n orders -c '{"Args":["queryAllOrders"]}'docker exec cli peer chaincode invoke -o orderer0.example.com:7050 -C mychannel -n orders -c '{"Args":["createOrder","ORDER14", "23459348", "5493058", "Pending"]}' --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pemdocker exec cli peer chaincode query -C mychannel -n orders -c '{"Args":["queryOrder", "ORDER2"]}'

You now have a working Fabric network that is set up across 3 nodes! You can follow the guide on https://medium.com/@eplt/5-minutes-to-install-hyperledger-explorer-with-fabric-on-ubuntu-18-04-digitalocean-9b100d0cfd7d to set up a Hyperledger Explorer to view the Fabric.

Tearing down of the network
In order for you to tear down the entire Hyperledger network, you can call the following command.

docker rm -f $(docker ps -aq) && docker rmi -f $(docker images | grep dev | awk '{print $3}') && docker volume prune

Reminder: Check out my other articles on just running Hyperledger Fabric 1.4.4, Hyperledger Composer and Hyperledger Explorer. We aim to publish a full set of these guides on how to play around with Hyperledger technologies. I am a big fan of Brave browser, if you are into blockchain and crypto, should try it, make a little crypto from your regular web browsing.

点击这里即可查看本文的中文版。

Special thanks to Jasmine Yang and Eugene Yong for helping with these articles.

Experienced technologist, focused on selective combinations of blockchain and AI. I am not a writer, I use Medium like an online notepad to share my thoughts.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store