30 minutes to Setup Production Ready Hyperledger Fabric cluster with Docker Swarm

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.

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

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. Chinese version of these articles will be available via Jasmine Yang soon. Eugene Yong tested every line of code in this article. 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: Set up the overlay network using docker swarm (our example IP address 167.71.121.213)

(Node 1)

docker swarm init --advertise-addr 167.71.121.213

You will be given a command that will look something like the following. Run this on Node 2 and Node 3.

docker swarm join --token SWMTKN-1-58xwguh8oa3jj6rcbcm4cyxg9lmitxyv1fs1sn1d5xy51e9arv-1hts5vhxebpjf6fz3kjskpbub 167.71.121.213:2377

(Node 1) Now you can create the overlay network.

docker network create --attachable --driver overlay fabric

You will be able to see the swarm network fabric if you call docker network ls

(Node 2 & 3) Create a busybox to have access to the overlay network

docker run -itd --name mybusybox --network fabric busybox

Step 6 (Node 1): Download files required to set up the fabric.

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

Step 7 (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 8 (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 9 (Node 2 and 3):Unzip the folder in the node.

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

Step 10: 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 11 (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 12 (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 13: Join all the peers to the channel

docker exec cli peer channel join -b mychannel.block

Step 14: Install the chain code 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 15: 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!

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. Chinese version of these articles will be available via Jasmine Yang soon. Eugene Yong tested every line of code in this article. 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.

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

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