Setup Hyperledger Fabric in multiple physical machines

Photo by Christian Fregnan on Unsplash

This tutorial will show how to setup an Hyperledger Fabric starting from the Basic-Network example that is shared in the Fabric-Samples in the Hyperledger Fabric official page.

I will be using two droplets of Digital Ocean, those droples counts with Ubuntu 16.04 and Docker 17.12.

You have to be sure that your physical machines can comunicate with each other, if not, this solution will not work.

So we will:

  • Setup a multi-host Hyperledger Fabric (two physical machines)
  • Install a Business Network to our Hyperledger Fabric (v 1.2.0)

Architecture

Basically we will create a Hyperledger Fabric with one orderer organization and one peer organization. 
So we basically will have:

Prerequisites

  • Install Docker (If you are not going to use Digital Ocean’s droplets)
  • Install Composer 
    npm install -g composer-cli@0.20
  • Install Composer Rest Server
    npm install -g composer-rest-server@0.20
  • Install Generator Hyperledger Composer 
    npm install -g generator-hyperledger-composer@0.20
  • Install Composer Playground
    npm install -g composer-playground@0.20
  • Download Fabric-Samples
    curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0 1.2.0 0.4.10

Let’s Start

We will start en our first server or machine, so I will asume that you downloaded all the prerequisites in that machine

We will open the basic-network folder inside the fabric-samples folder, and we will see this folder structure.

If we open the configtx.yaml file, it will have the orderer organization and peer organization configuration (I removed the comments in the file in order to see a cleaner code).

Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
Application: &ApplicationDefaults
Organizations:
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Profiles:
OneOrgOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
OneOrgChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1

We don’t need to change anything in this file

Now we open the crypto-config.yaml file, this also has configuration regarding the orderer organization and peer organization configuration.

The default configuration has configured one Peer. Since we need to create two peers, we will need to change some values in our file. So, when we create our certificates, those will be created correctly.

OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com
Template:
Count: 1
#Change for 2
Users:
Count: 1
#Change for 0

We need to change the number of peers that we want to use, in our case, we will use ‘2’ peers, and we will set the user count to ‘0’, you can see the comments to know where you have to make your changes.

Before we run the generate.sh file, we have to add to the PATH enviroment variable, the location of our fabric-tools, these tools are located in the bin folder inside of the fabric-samples folder.

$ export PATH=<path to download location>/bin:$PATH

Now that we have our configuration files OK and we added the fabric-tools to our PATH enviroment variable, we will need to run the script generate.sh file.

$ ./generate.sh

This will generate out crypto material (certificates, private keys, public keys), inside the crypto-config folder (We will copy this folder to our second server in the future).

Configure Docker Services

We will open now the docker-compose.yaml file, this file now has the following services:

  • ca.example.com
  • orderer.example.com
  • peer0.org1.example.com
  • couchdb
  • cli

In this file we will have to add/modify a couple of things.

We have to update our certificate for the ca.example.com for that we have to find this service in the file and update the key FABRIC_CA_SERVER_CA_KEYFILE. (You will find it in the code snippet as <NewKeyFile>), the file is located in:
 crypto-config/peerOrganizations/org1.example.com/ca/ 
You have to copy the filename of the file that ends with _sk and replace it for the <NewKeyFile> tag

ca.example.com:
image: hyperledger/fabric-ca
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca.example.com
- FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/<New KeyFile>
ports:
- "7054:7054"
command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
volumes:
- ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
container_name: ca.example.com
networks:
- basic

Also in the ca.example.com service in the command section, we add other parameter a -d, so the section will be something like.

command: sh -c 'fabric-ca-server start -b admin:adminpw -d'

Now you have to add an extra_hosts region after the volumes region in all the services but the cli and the couchdb services. In this section we will add the domain of our peer that will be located in the second machine (<Second machine IP address>)

volumes:
- ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
extra_hosts:
- "peer1.org1.example.com:<Second machine IP address>"

container_name: ca.example.com
networks:
- basic

After that we will have to create a new file named docker-compose-peer2.yaml, this file will have the configuration of your services for your second physical machine.

version: '2'
networks:
basic:
services:
peer1.org1.example.com:
container_name: peer1.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer1.org1.example.com
- CORE_LOGGING_PEER=debug
- CORE_CHAINCODE_LOGGING_LEVEL=DEBUG
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb1:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: peer node start
ports:
- 8051:7051
- 8053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
extra_hosts:
- "orderer.example.com:<First machine IP address>"
- "peer0.org1.example.com:<First machine IP address>"
- "ca.example.com:<First machine IP address>"
depends_on:
- couchdb1
networks:
- basic
  couchdb1:
container_name: couchdb1
image: hyperledger/fabric-couchdb
environment:
- COUCHDB_USER=
- COUCHDB_PASSWORD=
ports:
- 6984:5984
networks:
- basic

As you can see this just have two services peer1.org1.example.com and couchdb1, also you will need to setup you IP address of the first physical machine.

You can find it in the code snippet as <First machine IP address>.

Start Hyperledger Fabric

Now we will start the Hyperledger Fabric in our first physical machine. We will run the start.sh file.

$ ./start.sh

We will see an output with some information(You must not see errors).
And to validate that our Docker containers are up, we will run

$ docker ps

We must see 4 docker containers, if you don’t is because something went wrong.

So with that we have our Hyperledger Fabric running in our first server.

Now we go to the second server, we will copy our crypto-config folder created on our first server.
Also we will take the docker-composer-peer2.yml created some steps before, the env file and create a new file named start-peer2.sh.
The folder structure of out second server must be something like that.

The start-peer2.sh will start our second peer and join it to the Hyperledger Fabric started in our first server.
We will open the file and add this code.

set -ev
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
docker-compose -f docker-compose-peer2.yml down
docker-compose -f docker-compose-peer2.yml up -d peer1.org1.example.com couchdb1
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}
# Create the channel
docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer1.org1.example.com peer channel fetch config -o orderer.example.com:7050 -c mychannel
docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer1.org1.example.com peer channel join -b mychannel_config.block

Save and now we will run the start-peer2.sh.

$ ./start-peer2.sh

If everything its Ok we will see some logs, telling us that out peer is joining the network.

To validate that everything went fine lets run docker ps

$ docker ps

We have to see two containers, if not, something went wrong.
Now we will copy the container ID of our peer and run

$ docker logs <Peer Container ID>

If you see an error and some retry message is because something went wrong, maybe your machines can’t comunicate, or maybe there are some configuration errors.

If you don’t you have your Hyperledger Fabric up and running in two different physical machines! :)

We will see the installation of a Business Network in the running Hyperledger Fabric in the next tutorial!


Looking to develop your blockchain project? Drop us a line at hello@1950Labs.com