grepruby / hyperlegder-fabric-on-kubernetes

3 stars 7 forks source link

Hyperledger Fabric on kubernetes cluster

This script and helm charts would set up the necessary components of Hyperledger fabric. By default, it creates the setup for 2 Organization and 2 peer pods per organization. You can change this configuration by modifying crpyto-config.yaml file.

Prerequisites

Before the setup, you should have following prerequisites ready

Cluster Architechture

Script would by default have the following pods instantiated:

  1. Orderer service Pod
  2. Org 1 CLI pod
  3. Org 1 Peer Pods
  4. Org 2 CLI pod
  5. Org 2 Peer Pods
  6. NFS service Pod (If specified)
  7. Org 1 Extra App Pods (If specified)
  8. Org 2 Extra App Pods (If specified)

hyperledger-fabric-kubernetes-architechture

Setup

Step 1: Setup crpyto-config.yaml

Here we utilize the crypto-config.yaml file to setup our cluster requirement. This is the same file which is used by cryptogen tool to create peers’ and orderers’ certificates. We can modify this file to specify how many organizations we need, and how many peers are required in each organization. We may also specify our own unique application running for each organization by passing it in ExtraPods field.

By default it will have a 2 Organization setup, with 2 Peer pods per organization and a single channel between them.

Step 2: Modify the configtx.yaml file

Next step, would be to modify the configtx.yaml file in the same fashion

Step 3: Bring the fiber components up

Use the command make fiber-up to setup the fabric components in our cluster. This command will invoke the init_orderers.py and init_peers.py scripts that would generate the pods according to the modified files. The script does the following tasks in chronological order:

Step 4(Optional): Setup Extra App Pods

You need to perform this step only if you want a separate app to run per organization. This app can be used to communicate with Fabric component using nodeSDK of hyperledger fabric.

Step 4.A: Updating crypto-config.yaml and adding helm chart for your app

ExtraPods:
     - Name: buyerone-node-app
       Chart: ./buyerone
       Values:
         - name: "replicaCount"
           value: "1"
         - name: "name"
           value: buyerone-node-app
         - name: "NODE_ENV"
           value: "production"

Step 4.B: Setting up NFS storage

NOTE: If your extra app doesn’t need nodeSDK or network-config.yaml, you can skip Step 4.B

cloud compute disks create --size=10GB --zone=us-east1-b nfs-disk

You can also go in gcloud console and create it using UI dashboard.

Step 4.C: Setting up Extra App Pods

Bring down the cluster

If we want to bring down the cluster we setup, we can simply run make down Alternatively, if you wish to remove or recreate only a portion of our cluster you can use following commands:

make peer-down : to tear down only the peer pods and other organizational pods make orderer-down : to tear down only orderer pods and namespace.

For more details about these commands, check the Makefile in the repository.

Testing the chaincode

The script automatically creates a default channel between two organization and joins all the peer pods in the channel. After that, you can install a chaincode in two ways:

Here is how you can do it using CLI pods:

Install the chaincode

kubectl exec -it <CLI_POD_ID> --namespace=peers -- /bin/bash

In the terminal, install chaincode on both the org peer pods. The commands to install it in one peer is given below:

# Update the environment variable for Peer0
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/buyerone-example-com/users/Admin@buyerone-example-com/msp
export CORE_PEER_LOCALMSPID="BuyerOneMSP"
export CORE_PEER_ADDRESS=peer0-buyerone-example-com:7051
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/buyerone-example-com/peers/peer0-buyerone-example-com/tls/ca.crt

# Install the chaincode for Peer0
peer chaincode install -n mycc2 -v 2.0 -l node -p /opt/gopath/src/github.com/chaincode/default_chaincode/node/

Instantiate the chaincode

– Enter the bash of one of the Org CLI pod and instantiate chaincode by following command

# Instantiate the chaincode
peer chaincode instantiate -o orderer-example-com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example-com/tlsca.example-com-cert.pem -C buyer1seller1channel1 -n mycc2 -l node -v 2.0 -c '{"Args":["init","key", "test value"]}' -P "AND ('BuyerOneMSP.peer','SellerOneMSP.peer')"

Query the chaincode

# Query the chaincode
peer chaincode query -C buyer1seller1channel1 -n mycc2 -c '{"Args":["query","key"]}'

If all goes well, you should see the passed value of the key