Step by step guide to setup Hyperledger v2.1 Fabcar sample on Ubuntu 18.04

Sujith R Pillai
4 min readMay 4, 2020

--

In this code sample, we will setup a single node Hyperledger Fabcar sample application. All you will need as a pre-requisite to follow this guide is a fresh installation of Ubuntu 18.04 system with good internet connectivity.

This code sample is having the necessary Operations metrics enabled for Prometheus and Grafana dashboard.

Read this blog for configuring Prometheus from scratch.

I will share another blog to explain how to configure them.

(Though, I have the same set of information published in my GitHub as well here)

A screenshot of successful completion

For more information on Hyperledger v2.1 refer to the link https://hyperledger-fabric.readthedocs.io/en/release-2.1/whatis.html

A video with all the steps is shown below for your quick reference,

A video showing the whole process of installation

Pre-requisite 1: SSH, Curl, Docker & Docker-compose

Follow the below steps to configure SSH, Curl, Docker and Docker-compose on the system,

apt-get install openssh-server curl -y
apt-get remove docker docker-engine docker.io containerd runc
apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io -y
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Pre-requisite 2: Go version 1.14.x

The Blockchain platform is very choosy about the Go version that you use. So install the specific version recommended here,

wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
tar -xvzf go1.14.2.linux-amd64.tar.gz

Pre-requisite 3: NodeJS version 10.15.3 or higher

Install NodeJS as shown below,

wget https://nodejs.org/dist/latest-v10.x/node-v10.20.1-linux-x64.tar.gz
tar -xvzf node-v10.20.1-linux-x64.tar.gz
cp -av node-v10.20.1-linux-x64 /usr/local/

Pre-requisite 4: Python v2.7

Even if you have a higher version already installed, you need to make sure that the python 2.7 is configured and available in the path,

apt-get install python

Pre-requisite 4: Download images & set paths & variables

Download the Docker images and the Hyperledger fabric binaries. Refer this link for more information :- https://hyperledger-fabric.readthedocs.io/en/release-2.1/install.html

A script is available for quick and easy setup,

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

Set necessary environment variables,

export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/node-v10.20.1-linux-x64/bin:$GOPATH/bin:$HOME/fabric-samples/bin
export FABRIC_CFG_PATH=/root/fabric-samples/config
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/root/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/root/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

Make sure that these environment variables are set for the subsequent login sessions,

echo "export GOPATH=$HOME/go" >>/root/.bashrc
echo "export PATH=$PATH:/usr/local/node-v10.20.1-linux-x64/bin:$GOPATH/bin:$HOME/fabric-samples/bin" >> /root/.bashrc
echo "export FABRIC_CFG_PATH=/root/fabric-samples/config" >>/root/.bashrc
echo "export CORE_PEER_TLS_ENABLED=true" >>/root/.bashrc
echo "export CORE_PEER_LOCALMSPID=\"Org1MSP\"" >>/root/.bashrc
echo "export CORE_PEER_TLS_ROOTCERT_FILE=/root/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" >>/root/.bashrc
echo "export CORE_PEER_MSPCONFIGPATH=/root/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" >>/root/.bashrc
echo "export CORE_PEER_ADDRESS=localhost:7051" >>/root/.bashrc

Enable operator metrics

By default the hyperledger images has the operations metrics disabled. The steps in this section are custom steps that you wont find in the hyperledger install documentation(in the link above). What we do here is to enable it in the image and expose the service in the docker-compose configuration.

In this example, we are not enabling HTTPS(TLS). However, it is necessary for a real application.

docker run --rm --entrypoint cat hyperledger/fabric-peer:latest /etc/hyperledger/fabric/core.yaml > core.yaml
sed 's/provider: disabled/provider: prometheus/g' core.yaml > core.yaml.tmp
sed 's/listenAddress: 127.0.0.1:9443/listenAddress: 0.0.0.0:9443/g' core.yaml.tmp > core.yaml
cat <<EOF >Dockerfile
FROM hyperledger/fabric-peer:latest
ADD ./core.yaml /etc/hyperledger/fabric/core.yaml
EOF
docker build . -t hyperledger/fabric-peer:latest
docker run --rm --entrypoint cat hyperledger/fabric-orderer:latest /etc/hyperledger/fabric/orderer.yaml > orderer.yaml
sed 's/Provider: disabled/provider: prometheus/g' orderer.yaml > orderer.yaml.tmp
sed 's/ListenAddress: 127.0.0.1:8443/listenAddress: 0.0.0.0:9443/g' orderer.yaml.tmp > orderer.yaml
cat <<EOF >Dockerfile
FROM hyperledger/fabric-orderer:latest
ADD ./orderer.yaml /etc/hyperledger/fabric/orderer.yaml
EOF
docker build . -t hyperledger/fabric-orderer:latest

To expose the metrics port, the docker-compose file used by the network.sh script need to be edited. This file can be located here:- ~/fabric-samples/test-network/docker/docker-compose-test-net.yaml

In this file add extra lines to expose the port for each of the services, For example locate the section services: --> orderer.example.com: --> ports: and add an extra line. After adding it may look like this (The local port number can be of your choice)

ports:
- 7050:7050
- 9444:9443

Similarly, add entries for the other services too (Both the peer nodes) as shown respectively, Service section : services: --> peer0.org1.example.com: ports:

ports:
- 7051:7051
- 9445:9443

Service section : services: --> peer0.org2.example.com: ports:

ports:
- 9051:9051
- 9446:9443

With this the metrics will be available at the following address on the server,

  • Orderer Metrics : http://<IP_Address>:9444/metrics
  • Peer Org1 Metrics : http://<IP_Address>:9445/metrics
  • Peer Org2 Metrics : http://<IP_Address>:9446/metrics

Bring the Blockchain network up

From here on, its the standard procedure as explained here : https://hyperledger-fabric.readthedocs.io/en/release-2.1/test_network.html

cd fabric-samples/test-network
./network.sh up
./network.sh createChannel
./network.sh deployCC

Interact with Blockchain

Query blockchain to get the details,

peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

To update a record in the blockchain run a similar command as like the following,

cd /root/fabric-samples/test-network
peer chaincode invoke \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--tls true \
--cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
-C mychannel \
-n fabcar \
--peerAddresses localhost:7051 \
--tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
--peerAddresses localhost:9051 \
--tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
-c '{"function":"changeCarOwner","Args":["CAR9","Dave"]}'

Happy Coding…

--

--