In this Code Pattern, we will build a cloudnative application based on microservice architecture, IBM Middlewares and following 12-factor app practices. This application is an airline booking app, and the technologies behind it are:
- Node.js (TypeScript)
- IBM Db2
- IBM ODM
- RabbitMQ
- Celery
- Angular
- Hyperledger Fabric
- Hyperledger Burrow
- Solidity
- Docker
- Kubernetes
- IBM Cloud Private
"Cloud native application" is a buzzword these days. Technologies like Kubernetes and Docker are picking up reputation and popularity in a cloud native world. This pattern will show the end-to-end process of building a highly available cloud native application on IBM Cloud Private. The application backend is written in Node.js and uses Db2 as a database. The frontend is built using Angular4. The distributed, asynchronous task queue is based on RabbitMQ and Celery. Container orchestration is managed by Kubernetes. Finally, the blockchain is based on Hyperledger Fabric, Hyperledger Burrow and Ethereum Smart Contract.
When you've completed this code pattern, you will understand how to:
- Break the application into different microservices
- Deploy microservices based application on IBM Cloud Private
- Leverage IBM Middlewares such as ODM and Db2 on Kubernetes
- Design business rules and decision making by IBM ODM
- Create distributed task queues using RabbitMQ and Celery
- Leverage Db2 as backend data storage
- Deploy Ethereum smart contract on Hyperledger fabric by leveraging Hyperledger Burrow
- Learn how to use IBM Cloud Private, Docker, and Kubernetes
- USER: End user who will access the application
- APPUI: The user interface is built using Angular4. It leverages restful endpoints by backend microservices
- Login Microservice: This Microservice allows the user to login to the application
- Signup Microservice: This Microservice allows the user to signup to the application
- Checkin Microservice: This Microservice allows the user to checkin to a given booked flight
- Listing Microservice: This Microservice allows the user to list and browse the available flights
- Booking Microservice: This Microservice allows the user to book an available flight
- Blockchain Microservice: This Microservice helps to communicate with the Blockchain
- IBM Db2: SQL Database for storing data
- ODM Microservice: This Microservice helps to communicate with the ODM Service
- ODM: IBM Operational Decision Manager is an advanced business rules management system that helps you capture, automate and govern frequent, repeatable business decisions and adds real-time decision making to your day-to-day operations
- Email Microservice: Email Microservice communicates with RabbitMQ and Celery to send email in asynchronous queues
- RabbitMQ: RabbitMQ is a messaging broker - an intermediary for messaging. It gives your applications a common platform to send and receive messages, and your messages a safe place to live until received.
- Celery: Celery is an asynchronous task queue/job queue based on distributed message passing
- Ethereum Proxy: Allows to communicate with Ethereum Virtual Machine
- Hyperledger Burrow EVM Chaincode Plugin: Allows Ethereum Capabilities on top of Hyperledger Fabric by leveraging Hyperledger Burrow
- Hyperledger Fabric: Hyperledger Fabric is a blockchain framework implementation and one of the Hyperledger projects hosted by The Linux Foundation. Intended as a foundation for developing applications or solutions with a modular architecture, Hyperledger Fabric allows components, such as consensus and membership services, to be plug-and-play
- Angular4: Angular is a platform that makes it easy to build applications with the web. Angular combines declarative templates, dependency injection, end-to-end tooling, and integrated best practices to solve development challenges
- Nodejs: An open-source JavaScript run-time environment for executing server-side JavaScript code.
- Typescript: TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language
- Docker: Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.
- Kubernetes: Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications
- IBM Db2: Db2 is a database product from IBM. It is a Relational Database Management System (RDBMS)
- IBM ODM: IBM® Operational Decision Manager is a comprehensive decision automation platform that helps you capture, analyze, automate and govern rules-based business decisions
- RabbitMQ: RabbitMQ is lightweight and easy to deploy on-premises and in the cloud. It supports multiple messaging protocols. RabbitMQ can be deployed in distributed and federated configurations to meet high-scale, high-availability requirements
- Celery: Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation but supports scheduling as well
- Hyperledger Fabric: Hyperledger Fabric is a blockchain framework implementation and one of the Hyperledger projects hosted by The Linux Foundation. Intended as a foundation for developing applications or solutions with a modular architecture, Hyperledger Fabric allows components, such as consensus and membership services, to be plug-and-play
- Hyperledger Burrow: Hyperledger Burrow is one of the Hyperledger projects hosted by The Linux Foundation. Hyperledger Burrow was originally contributed by Monax and co-sponsored by Intel. Hyperledger Burrow provides a modular blockchain client with a permission smart contract interpreter partially developed to the specification of the Ethereum Virtual Machine (EVM)
- Solidity: Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript and is designed to target the Ethereum Virtual Machine (EVM)
- Install Nodejs
- Install Kubectl
- Install Docker
- Install Angular4Cli
- Install DBVisualiser
- Install
gulpcli
$ npm i gulp-cli -g
- From your terminal type
$ sudo nano /etc/hosts
- Type in your ICP IP and provide a DNS name, quit and save the file
- Go to Docker preferences
- Navigate to Daemon tab, click on the
+
button and add an entryicpdns:85000
, clickApply & Restart
button to restart Docker service.
- In your terminal type
$ docker login icpdns:8500
- Booking Microservice
$ cd booking-microservice
$ npm i
$ docker build -t booking-microservice --no-cache .
$ docker tag booking-microservice <icpdns>:8500/default/booking-microservice:latest
$ docker push <icpdns>:8500/default/booking-microservice:latest
- Checkin Microservice
$ cd checkin-microservice
$ npm i
$ docker build -t checkin-microservice --no-cache .
$ docker tag checkin-microservice <icpdns>:8500/default/checkin-microservice:latest
$ docker push <icpdns>:8500/default/checkin-microservice:latest
- Listing Microservice
$ cd listing-microservice
$ npm i
$ docker build -t listing-microservice --no-cache .
$ docker tag listing-microservice <icpdns>:8500/default/listing-microservice:latest
$ docker push <icpdns>:8500/default/listing-microservice:latest
- Login Microservice
$ cd login-microservice
$ npm i
$ docker build -t login-microservice --no-cache .
$ docker tag login-microservice <icpdns>:8500/default/login-microservice:latest
$ docker push <icpdns>:8500/default/login-microservice:latest
- Signup Microservice
$ cd signup-microservice
$ npm i
$ docker build -t signup-microservice --no-cache .
$ docker tag signup-microservice <icpdns>:8500/default/signup-microservice:latest
$ docker push <icpdns>:8500/default/signup-microservice:latest
- Celery Microservice
$ cd celery
$ docker build -t celery --no-cache .
$ docker tag celery <icpdns>:8500/default/celery:latest
$ docker push <icpdns>:8500/default/celery:latest
- Email Microservice
$ cd email-microservice
$ npm i
$ docker build -t email-microservice --no-cache .
$ docker tag email-microservice <icpdns>:8500/default/email-microservice:latest
$ docker push <icpdns>:8500/default/email-microservice:latest
- Ethereum Microservice
$ cd ethereum-microservice
$ npm i
$ docker build -t ethereum-microservice --no-cache .
$ docker tag ethereum-microservice <icpdns>:8500/default/ethereum-microservice:latest
$ docker push <icpdns>:8500/default/ethereum-microservice:latest
- Odm Microservice
$ cd odm-microservice
$ npm i
$ docker build -t odm-microservice --no-cache .
$ docker tag odm-microservice <icpdns>:8500/default/odm-microservice:latest
$ docker push <icpdns>:8500/default/odm-microservice:latest
- Angular Frontend
$ cd icp-frontend
$ npm i
$ docker build -t frontend --no-cache .
$ docker tag frontend <icpdns>:8500/default/frontend:latest
$ docker push <icpdns>:8500/default/frontend:latest
Find all your images in
https://icpip:8443/console/manage/images
- Login to your icp instance by going to
https://icpip:8443
- Click on configure, click on copy, and paste all these commands into your terminal
- ssh to your icp proxy and create a folder in a specific directory
$ ssh icpproxyip
- Login to IBM Cloud Private by going to this link
https://icplink:8443
-
Click on
Platform
and chooseStorage
, then click onCreate PersistentVolume
-
Provide the name
shared
for persistance volume -
Provide storage capacity for around
30GB
-
Access mode change to
Read Write Many
-
Storage type to
NFS
-
Click on parameters tab and add the follow parameters
server : youricpip
path: your folder path that you created in your icp proxy
- Click
Create
- Click on
Persistent Volume Claim
tab - Click on
Create Persistent Volume Claim
- Give the name
shared-pvc
- Provide storage request
30GB
- Provide access mode
Read Write Many
- Click on Create
- Go to
Catalog
and filterdb2
- Click on configure, fill up the required field and deploy
- Follow this tutorial to deploy Db2 in IBM Cloud Private
Note: when asked for persistant volume claim give shared-pvc while filling the form to deploy Db2
- ssh to Db2 pod
$ kubectl exec -it <podname> bash
- switch the user you have created
$ su - <username>
- Connect to SAMPLE db
$ db2 connect to SAMPLE
- Download existing flight data from github and set permissions
$ wget https://raw.githubusercontent.com/SaifRehman/ICP-Airways/master/dataset/flights.csv
- Create Database and importing existing data to Flights table
- Flights Table
db2 CREATE TABLE "SAMPLE.FlightsData (ID int NOT NULL , Year varchar(255) NULL , Month varchar(255) NULL, DayofMonth varchar(255) NULL, DepTime varchar(255) NULL, CRSDepTime varchar(255) NULL, ArrTime varchar(255) NULL, CRSArrTime varchar(255) NULL, FlightNum varchar(255) NULL, TailNum varchar(255) NULL, ActualElapsedTime varchar(255) NULL, CRSElapsedTime varchar(255) NULL, Airtime varchar(255) NULL, ArrDelay varchar(255) NULL, DepDelay varchar(255) NULL, Origin varchar(255) NULL, Dest varchar(255) NULL, Distance varchar(255) NULL, PRIMARY KEY (ID))"
- User Table
db2 CREATE TABLE "SAMPLE.UserData (UserID int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) , LastName varchar(255) NULL , FirstName varchar(255) NULL, Location varchar(255) NULL, Email varchar(255) NULL, Password varchar(255) NULL, Age int NULL, Tier varchar(255) NULL, PRIMARY KEY (UserID))"
- Booking Table
db2 CREATE TABLE "SAMPLE.Booking (BookingID int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) ,TS TIMESTAMP NOT NULL , Checkin varchar(2555) NOT NULL, OfferNamePricing varchar(2555) NOT NULL, OfferTypePricing varchar(2555) NOT NULL , CostPricing varchar(2555) NOT NULL, OfferNameUpgrade varchar(2555) NOT NULL, OfferTypeUpgrade varchar(2555) NOT NULL , CostNameUpgrade varchar(2555) NOT NULL, UserID INT NOT NULL, FlightID INT NOT NULL, FOREIGN KEY (UserID) REFERENCES SAMPLE.UserData(UserID), FOREIGN KEY (FlightID) REFERENCES SAMPLE.FlightsData(ID), PRIMARY KEY (BookingID))"
- Importing existing data to flights table
$ db2 IMPORT FROM "path-where-flights.cvs-is-saved/flights.csv" OF DEL INSERT INTO SAMPLE.FlightsData
- Go to
Catalog
and filterrabbitmq
- Select
RabbitMQ
, clickConfigure
, fill the form and click on deploy - Expose ampq port of rabbitmq
$ kubectl expose deployment <rabbitmq deployment name> --name=mq-amqp --type=NodePort --port=5671
- Go to
Catalog
and filterodm
- Select
odm
, clickConfigure
, fill the form and click on deploy
-
Go to
http://icpip:odmport/teamserver/faces/login.jsp
, and login with usernamertsAdmin
, and passrtsAdmin
-
Click on
Configure
tab, then click onimport project
. Select the zip file underodm-microservice
folder -
Go to
http://icpip:odmport/decisioncenter/t/library
, and login with usernamertsAdmin
, and passrtsAdmin
-
Select
New Release
fromICPAirlines-Rules
, then selectICPAirlinesDeployment
and clickdeploy
- Navigate to
fabric-deploy/cs-offerings/scripts
$ cd fabric-deploy/cs-offerings/scripts
- Run the create all script
$ ./create_all.sh
- Give it some time to load and finish
- Navigate to
fabric-evm-proxy/fabric-evm-deployment/cs-offerings
- run
kubesctl get pods
- copy any of blockchain pod name which is deployed by
create_all.sh
script - Delete the
crypto-config
folder - run
kubectl cp <pod name>:/shared/crypto-config crypto-config
- Open
fabric-cluster.yml
infabric-evm-proxy
folder - Find and replace
<path-to-crypto-config-directory>
in the file withcrypto-config directory
- Find and replace
<cluster-ip>
in the file withcluster ip of your cluster
- Navigate to
fabric-evm-proxy
- Deploy Ethereum Proxy image to ICP Private registry
$ docker build -t ethereumproxy --no-cache .
$ docker tag ethereumproxy <icpdns>:8500/default/ethereumproxy:latest
$ docker push <icpdns>:8500/default/ethereumproxy:latest
- UID is ur database username
- PASSWORD is your database password
- SECRET is your unique secret you gave for your app for JWT authentication
- EMAILUSERNAME is your gmail email username from which the email would be sent
- EMAILPASSWORD is the email password of your gmail email
- Navigate to
secrets
folder inconfigMaps-secrets/secrets
- Deploy secrets to Kubernetes
$ kubectl create -f secrets.yml
- If you need to redeploy with certain changes
$ kubectl apply -f secrets.yml
NOTE: all these are base64 representation encoding.
$ echo -n 'admin' | base64
YWRtaW4=
- DATABASE: is your database name, which should be SAMPLE
- PORT: is your exposed Db2 port from port
5000
, get this port by running and finding Db2 servicekubectl get service
- HOSTNAME: is your icpip
- ODM: is your ODM buisness api you have created, get odm port number by running and finding the exposed port
kubectl get service
- HttpProvider: replace the ip with your icp ip. This is the provider to connect with ethereum proxy
- CONTRACTADRESS and ETHEREUMACCOUNT: get your contract address and ethereum account by running
app.js
incontracts
folder. also, replace ip with your ICP IP
$ npm i
$ node app.js
- CELERY_RESULT_BACKEND and CELERY_BROKER_URL: replace with your ICP IP, and replace it with amqp port. get amqp port by running
kubesctl get service mq-amqp
- EMAILAPI: replace ip with your icpip
- Booking Microservice
$ cd booking-microservice
$ kubectl create -f service-deployment.yml
- Checkin Microservice
$ cd checkin-microservice
$ kubectl create -f service-deployment.yml
- Listing Microservice
$ cd listing-microservice
$ kubectl create -f service-deployment.yml
- Login Microservice
$ cd login-microservice
$ npm i
$ kubectl create -f service-deployment.yml
- Signup Microservice
$ cd signup-microservice
$ kubectl create -f service-deployment.yml
- Celery Microservice
$ cd celery
$ kubectl create -f service-deployment.yml
- Email Microservice
$ cd email-microservice
$ kubectl create -f service-deployment.yml
- Ethereum Microservice
$ cd ethereum-microservice
$ kubectl create -f service-deployment.yml
- Ethereum Proxy
$ cd fabric-evm-proxy
$ kubectl create -f service-deployment.yml
- Odm Microservice
$ cd odm-microservice
$ kubectl create -f service-deployment.yml
- Angular Frontend
$ cd icp-frontend
$ kubectl create -f service-deployment.yml
- Booking-microservice [3000:30090]
- Checkin-microservice [3001:30091]
- Listing-microservice [3002:30092]
- Login-microservice [3003:30093]
- Signup-microservice [3004:30094]
- Blockchain-microservice [3006:30199]
- Angular frontend [80:30080]
- Ethereum Proxy microservice [5000:30081]
- Celery microservice [4000:30989]
- Odm Microservice [3005:30095]
Documentation of all the backend endpoints is available here