Skip to content
 
 

Repository files navigation

Hyperledger Fabric for Trusted IoT using Node-RED on Kubernetes

Workshop: NYC Blockchain Center, November 6th 2019

Co-Presenter: Grant Steinfeld - IBM Devloper Advocate - Blockhain and JVM

Twitter: @gsteinfeld

Co-Presenter: John Walicki - STSM, CTO IoT Developer Advocacy

Twitter: @johnwalicki

Blockchain and IoT is omnipresent across multiple industries these days!

SmartAgriculture, Supply Chain and Home Weather stations are just a few examples where an immutable distributed ledger makes sense.

Tonight we will demonstrate how the TexasInstrument SenseTag sends data to IBM Cloud and stores it on Hyperledger Fabric instance on Kubernetes

TexasInstrument-SenseTag-Board

The Board

demo ...

Setup the SenseTag!

instructions to setup SenseTag

Let's Go!

navigate to this repo and clone it.

#cd to your dev dir
git clone https://github.com/Grant-Steinfeld/Hyperledger-Fabric-for-Trusted-IoT.git

cd Hyperledger-Fabric-for-Trusted-IoT/

Access IBM Cloud Kubernetes Cluster

As Hyperledger Fabric is a network consists of several components, we use microservice architecture on IBM Cloud Kubernetes Service.

Add a preprovisioned Kubernetes cluster to your IBM Cloud account

https://codernyctrustediot.mybluemix.net

Key: ikslab Region: Washington( US-East / us-east)

Get your own power Kubernetes cluster on your IBM account!

signup step 1

If all goes well

signup step 2

You are set!

The cluster will be available to you for 4 more days for experimentation and in the case you don't get to finish this workshop

Check installation prerequisites

Open a new Terminal window and execute following commands to be sure you have installed prerequisites.

    $> docker version
    Docker version 19.03.4, build 9013bf5

    $> ibmcloud --version
    ibmcloud version 0.20.0+5d69177-2019-10-31T09:49:54+00:00

Gain access to your Cluster

Navigate to your IBM Cloud Account and login

Switch to see your cluster

Choose your cluster from pulldown

Click on the clusters hyperlink. Confirm-cluster-linked

  • Click on the cluster name ( in my case it's codernyctrustediot12 )
  • Click on the Connect via CLI ( blue button )

Confirm-cluster-linked

  • Log in
ibmcloud login -a cloud.ibm.com -r us-east -g Default

login IBM Cloud

use you Email and Password you signed up with Select account 2

  • . Download the kubeconfig files for your cluster.
# for example
$> ibmcloud ks cluster config --cluster bn06p62w0egb69ffousge

# follow the instructions and export KUBECONFIG env var 
# for example something similar to
# export KUBECONFIG=/Users/grantsteinfeld/.bluemix/plugins/container-service/clusters/bn06p62w0egb69ffousge/kube-config-wdc04-codernyctrustediot102.yml

  • Execute the following command to verify that the kubectl commands run properly.
kubectl version --short

kubectl-ver-short

Now we are ready to install the cluster!! Make sure you are at the root directory you cloned above

pwd
#e.g. 
/Users/<username>/dev/Hyperledger-Fabric-for-Trusted-IoT

Deploy Hyperledger Fabric

Create PV and PVC

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A PersistentVolumeClaim (PVC) is a request for storage by a user. We will use this storage to store our configuration files and chaincode.

/click arrorw for more info

Learn More about Persistent Volumes

Managing storage is a distinct problem from managing compute instances. The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed. To do this, we introduce two new API resources: PersistentVolume and PersistentVolumeClaim***.

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.

A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted once read/write or many times read-only).

While PersistentVolumeClaims allow a user to consume abstract storage resources, it is common that users need PersistentVolumes with varying properties, such as performance, for different problems. Cluster administrators need to be able to offer a variety of PersistentVolumes that differ in more ways than just size and access modes, without exposing users to the details of how those volumes are implemented.

read and learn more on the kubernetes.io website

root dir structure of repo

  • If you have Free Cluster use the following command.
    kubectl create -f createPVandPVC.yaml

createPVandPVC

  • If you have Standard Cluster FIRST change the region and zone variables inside the createPVC.yaml according to your cluster location and use the following command.
    kubectl create -f createPVC.yaml

Note: You have to wait until the PVC will be bound to a storage.

Copy Artifacts

Once our PV and PVC are created, you are able to copy the local files to the storage on the cloud.

    cd jobs
    kubectl apply -f copyArtifactsJob.yaml
    pod=$(kubectl get pods --selector=job-name=copyartifacts --output=jsonpath={.items..metadata.name})
    kubectl cp ../artifacts $pod:/shared/
    kubectl get pods -w

Generating Hyperledger Fabric key materials and channel config related artifacts

Cryptogen is an utility for generating Hyperledger Fabric key material. It is provided as a means of preconfiguring a network for testing purposes. The configtxgen command allows users to create and inspect channel config related artifacts.

Note: In the following steps check your jobs Status and make sure it is Completed before moving on to the following step. This is essential to prevent any conflicts due to resources not yet provisioned. You can see your pod's status by executing "kubectl get pods"

  • The following command will generate MSPs (Membership Service Providers)
    kubectl apply -f generateCryptoConfig.yaml
  • The following command will generate 'genesis.block' which will be used to deploy Orderer.
    kubectl apply -f generateGenesisBlock.yaml
  • The following command will generate 'channel1.tx' which will be used to create channel.
    kubectl apply -f generateChanneltx.yaml
  • The following command will generate 'Org1MSPanchors.tx' and 'Org2MSPanchors.tx' which will be used to set the Anchor Peers in the network.
    kubectl apply -f generateAnchorPeerMSPs.yaml

Note: A peer node on a channel that all other peers can discover and communicate with. Each Member on a channel has an anchor peer (or multiple anchor peers to prevent single point of failure), allowing for peers belonging to different Members to discover all existing peers on a channel.

Network Deployment

You have completed prerequired steps for the network deployment. Now, you will deploy the Hyperledger Fabric components, Certificate Authority, Orderer, and Peers to your cluster.

Note: In the following step check that all of your deployment's Status is Running before moving on. Again, this is essential to prevent any conflicts due to resources not yet started. You can see your pod's status by executing "kubectl get pods"

    cd ../network-deployment
    sh deployAll.sh

Network Configuration

You should have your Hyperledger Fabric components are running. In the following steps, we will configure these components according to our use case.

Note: In the following steps, wait until your job's Status to become Completed, to prevent any conflicts. You can see your pod status by executing "kubectl get pods"

  • The following command will create a channel named 'channel1'
    cd ../jobs
    kubectl apply -f create_channel.yaml
  • The following command will join all the peers to the 'channel1'
    kubectl apply -f join_channel.yaml

/click arrorw for more info

v

Troubleshooting - pod error

sometimes it's necessary to remove everything and start again! ```bash

#remove ALL! Kubernetes artifacts for this cluster.

kubectl delete daemonsets,replicasets,services,deployments,pods,jobs,pv,pvc,jobs,configmap --all

``` 
  • The following command will install chaincode to peers (org1peer2, org2peer2). These peers will be your endorser peer.
    kubectl apply -f chaincode_install.yaml
  • The following command will instantiate the installed chaincode to the channel. Besides, sets the endorsement policy as requests 1 signature from each of the two organizations.
    kubectl apply -f chaincode_instantaite.yaml
  • The following command will update the channel and will set peers (org1peer1, org2peer1) as Anchor Peers.
    kubectl apply -f updateAnchorPeers.yaml

Deploy Hyperledger Fabric SDK for Node.js

Up until now, you have developed Hyperledger Fabric Network which might be a backend for an application. However, we need a middleware in order to connect the back-end and the front-end. For this purpose you will use Hyperledger Fabric Client SDK for Node.js which makes it possible to use APIs to interact with a Hyperledger Fabric blockchain.

Note: For the following steps, you must have 'DockerHub Account' in order to push and pull your container images. You can create an account from here.

  • The following commands will create a container image and push it to your container registry.
    cd ../API
    docker build . -t <your_account_name>/rest-api
    docker push <your_account_name>/rest-api

/click arrorw for more info

v

If you are using a private registry other than Docker Hub
  • If you are using a private registry, the Kubernetes Service needs permissions to pull your private container image from your registry. You can provide the Kubernetes Service with your docker secrets by running this command:
    kubectl create secret docker-registry regcred --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email>
  • The following commands will first pull the container image from your registry and create a deployment named "rest-api", then create a Kubernetes Service which exposes this deployment
    cd ..
    kubectl run rest-api --image=<your_account_name>/rest-api --port=3000
    kubectl apply -f rest-api-svc.yaml

Deploy Node-RED

Node-RED dashboard will be your front-end. You will be able to see incoming sensor data and the history of the ledger from this dashboard. Besides, all the HTTP requests will be execute via this tool.

Note: There is Node-RED service in the IBM Cloud Catalog. However, in this pattern you will use Node-RED inside a container.

Note: If you wish to use Node-RED service you can import the flow by using this

  • The following commands will first pull the container image from DockerHub and create a deployment named "nodered", then create a Kubernetes Service which exposes this deployment
    kubectl run nodered --image=yigitpolat/hyperledger-iot-nodered --port=1880
  • If you have Free Cluster use the following command to make nodered deployment accesible from the network.
    kubectl apply -f node-red-svc.yaml
  • If you have a Standard Cluster, IBM Cloud will provide you an Ingress Controller and Application Load Balancer which you can use to access your cluster from network. So that, you need to create ingress rules by following.

Note that, you must modify hosts and the secretName fields in the "create-ingress.yaml". To learn your Ingress Subdomain and Ingress Secret execute the following commands.

    ibmcloud ks cluster-get <your_cluster_name>

Now, you can create Node-RED service and Ingress rules.

    kubectl apply -f node-red-svc-clusterIP.yaml
    kubectl apply -f create-ingress.yaml

Understanding the Application

Congratulations! You have deployed your very first Hyperledger Fabric - IoT collabrative application. Now, it is time to understand how the manage the application.

Access to Node-RED

  • If you have Free Cluster follow the below instructions to access to dashboard.

First, execute the below commands to get your Kubernetes Worker Node's external IP.

    kubectl get pods -o wide
    kubectl get nodes

Open your favorite browser and navigate to "Your_external_IP":30002 which will end up with Node-RED service. For example, 52.116.26.52:30002

  • If you have Standart Cluster just navigate to host name of your cluster from the browser. For example, nodered.teknopark-hyperledger-iot.us-south.containers.appdomain.cloud

Registration

Double click on "Registration" tab, set Status to "Enabled", hit done. Deploy the application from right top corner. Execute the three HTTP Post request respectively.

  • First POST will enroll an admin named "admin" to the Certificate Authority of the Organization 1.
  • Second POST will enroll a register and enroll user named "user1" to the Certificate Authority of the Organization 2.
  • Third POST Will register a new sensor which will be used to collect the data from.

You will end up with a screen as below. You can see the returning results on the right-hand side.

Dashboard

It is time to create an User Interface to make the application to look fancy. Double click on "Dashboard" tab, set Status to "Enabled", hit done. Deploy the application from right top corner.

Note: Below screenshot shows how to use dummy data generator if you are not able to provide a sensor.

Finally, navigate to "Your_External_IP":30002/ui or nodered.teknopark-hyperledger-iot.us-south.containers.appdomain.cloud/ui to see your dashboard. Now you are able to see your sensor data live on a gauge. Besides, you can query sensor data history from navigating to "Sensor History" tab from the hamburger menu. Remember that, the data history is coming from the Ledger where the data is storing immutablly in the blockchain.

Here is the screenshots of the final views of the application.

Extending Code Pattern

Instead developing an application with full capabilities, this minimum viable product is much more understandable and instructive. However, it can be extended with several modifications.

  • Adding new functions to chaincode will bring new features to the application
  • According to new functions, API endpoints needed to be updated to fulfill the HTTP requests
  • Dashboard must be modified depending on the upcoming data

License

Apache License 2.0

About

This code pattern introduces you to Hyperledger Fabric which is an open-source blockchain framework and Watson IoT platform by using Node-RED in order to store the upcoming data on a distributed network immutably.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages