Using kubernetes to develop blockchain projects on Hyperledger Fabric
Good afternoon, dear Khabrovites.
I am a developer of the IBM Science and Technology Center in Moscow. We are developing IBM products with other labs around the world. If time permits and there is a desire, then we are allowed to use part of the working time for projects that are not the main employment. This approach broadens the horizons and supports creative mood. For me, this area is the development of blockchain solutions on Hyperledger Fabric. Moreover, such projects have become in demand in our market.
I hope that the article will not be about the blockchain, but about what we use to develop such solutions, but nevertheless it is worthwhile to start with the subject area.
The article is not intended to be exhaustive and is aimed at creating a working cloud environment for developing blockchain projects on Hyperledger Composer. As a result of our practical research, we should get the following infrastructure:
A Little About Hyperledger Fabric
There are a large number of blockchain platforms that can be compared indefinitely. Most likely, a universal platform does not exist; there is one selected for the current task. For myself, from the blockchain platforms, I chose Hyperledger Fabric. On the one hand, because the platform is interesting to me from a technical point of view, on the other, IBM is actively involved in the Hyperledger consortium.
Hyperledger is a consortium of the Linux Foundation project. Hyperledger Fabric (HLF) is an implementation of blockchain technology for building various solutions. It is a permission-controlled blockchain platform. This means that adding new members is controlled. Participants are defined during the network design phase and may have different roles. The most understandable scenarios for such networks are supply chains in which many independent companies participate. As an example, you can study the experience of Walmart, JD.com, IBM, and Tsinghua University in organizing a food supply chain .
From a technical point of view, each participant has an expanded network section in his organization (or in the cloud infrastructure), but does not have access to network sections deployed in other companies.
As can be seen from the diagram, each participant has:
- own node (peer), which ensures the operation of the blockchain network and is part of the HLF;
- An application that usually consists of at least a web server and user interface
- connecting link (ordering service), which provides interaction between nodes.
In the application, users log in using their keys or a bunch of username and password and gain access to the smart contract.
A smart contract is a programmed business logic and rules for interacting with data that are placed in a distributed ledger. The registry contains a chain of transaction blocks made by network participants. Each subsequent block stores the hash of the previous block, which ensures the impossibility of changing part of the information. The process of agreeing on the correctness of a block and adding it to a distributed registry is called consensus. Different blockchain platforms have different consensus mechanisms: proof of work (PoW) , proof of stake (PoS) , byzantine fault tolerance (BFT) and others.
Starting with version 1.0, HLF is specific to the presence of the Ordering Service, which is distributed among the participants in the cluster and is responsible for the sequence of transactions in the emerging block. According to the configured parameters, it collects transactions in the network and forms a block. In the event of an Ordering Service failure, transactions on the network will no longer be logged, but the data itself will remain unchanged.
Now, having understood the basic concepts, we can move on to practice.
How to deploy your own HLF network
All HLF components work in Docker containers and can be deployed using either Docker Compose or Kubernetes . In addition, smart contracts themselves also run in Docker containers.
In order to deploy HLF, you can use the existing kubernetes network, minikube or use one of the cloud Kubernetes as a Service.
You can deploy your kubernetes network for free on the IBM cloud infrastructure .
How to deploy the kubernetes network to the IBM Cloud.
In order to start using IBM Cloud you need to:
- register ;
- Cloud the CLI to install the IBM ;
- receive and activate a coupon to activate additional functionality in IBM Cloud (coupons are issued in semi-manual mode, and there are, unfortunately, a limited number of them);
- install kubectl ;
- Install the Git client for your operating system
- read about what Docker and Kubernetes are .
After registering with IBM Cloud, you will have access to the graphical management interface of your infrastructure. You will have access to hundreds of services and dozens of approaches for hosting your applications (Cloud Foundry, Kubernetes, OpenWhisk, virtual machines and dedicated servers). To use virtual machines and dedicated servers, you need to add a credit card, but a small piece of kubernetes can be obtained for free (with a coupon above).
To do this, in the upper left corner, click on the menu button and select Containers .
You will see the kubernetes cluster control panel, Docker registry and helm charts .
Selecting Cluster from the menu on the left , you will see the kubernetes cluster creation interface and the choice of the region in which the cluster will be deployed. You can create a free cluster in each region.
After clicking the Create cluster button , select the type of cluster Free and come up with a name for it.
Creating a cluster will take some time, which you can spend switching to the console (you won’t have time to brew coffee). After installing the IBM Cloud CLI (bx tool), you need to configure it to work with your account:
- install the plugin for bx to work with kubernetes;
bx plugin install container-service -r Bluemix
- Log in to IBM Cloud
bx login -a https://api.eu-gb.bluemix.net
- indicate the region where you created your cluster (uk-south may vary);
bx cs region-set uk-south
- request a configuration for your cluster, where mycluster is the name you entered in the IBM Cloud GUI;
bx cs cluster-config mycluster
- execute the command that you will see as a result of the previous step. It will tell the kubectl utility where the keys for accessing your cluster are located (the path to the configuration file may change);
- find out the public IP of your kubernetes worker.
bx cs workers mycluster
Now you are ready to start using your kubernetes cluster for any purpose. The cluster is free, and there are some limitations in its functionality that will not prevent us from deploying our blockchain network:
- only one worker (virtual machine);
- lack of volumes;
- unavailability of application load balancer;
- the inability to use Ingres, instead of which you can use [NodePort] ( https://kubernetes.io/docs/concepts/services-networking/service/ )
For those who are using kubernetes for the first time, it might be worth experimenting with deploying the first Deployment and services . Those who are already ready to plunge into the world of developing blockchain projects can move on to the next steps.
Deploy HLF containers in kubernetes
For those who have recently been working with Docker and Kubernetes, let me remind you that in order to deploy any application in kubernetes, we need 2 things:
- Docker images, on the basis of which the container starts;
- yaml configuration of our container behavior in kubernetes.
The open source Hypeledger Fabric developer community has done the job for us. Docker Images with HLF are already on DockerHub and will automatically load into our cluster. And the whole yaml configuration is in the github repository . At the time of writing, the repository contains the configuration for Hyperledger Fabric 1.0.3. We return to the console and execute the following commands:
git clone https://github.com/IBM-Blockchain/ibm-container-service.git cd ibm-container-service/cs-offerings/scripts/ ./create_all.sh --with-couchdb
The key is
--with-couchdbnecessary so that instead of the default database, we deploy CouchDB. With it, we can use queries to retrieve data from HLF Composer.
What is Hyperledger Composer?
Composer is a tool for blockchain developers. With it, you can increase the speed of developing blockchain solutions from months to weeks. There is an online sandbox Composer Playground , but all the business logic designed in it will be launched in your browser (blockchain emulation). In our case, Composer will be connected to a working Hyperledger Fabric network deployed in kubernetes.
Another advantage of Hyperledger Composer is the automatic creation of REST interfaces based on the described assets and transactions, which allows you to immediately start writing the user interface (and accordingly get MVP).
Launch of the first blockchain project
After starting Hyperledger Composer on the IBM Cloud, you can access its web interface at
Composer is connected to the github-project , and you can deploy one of the existing projects or start developing your own.
As a result, we got a free cloud environment for developing projects based on Hyperledger Fabric.
What's next ?
As with any technically challenging project, there are always many details and additional things. This article is for guidance only and is intended to help you get started. I hope that the time and effort will allow me to write several articles with more detailed information both on kubernetes in the IBM Cloud and on development for Hyperledger Fabric.
Blockchain, Hyperledger project, IBM Blockchain
official documentation hyperledger fabric
documentation Hyperledger Composer
example project on Hyperledger
telegram channel about modern technologies and blockchain in particular