Kubernetes on IBM Cloud: Building a Cloud Native Application
Using Node.js, Express, MongoDB, and some imagination
In this Code Pattern, we will build a dummy digital bank composed of a set of microservices that communicate with each other. We’ll be using Node.js, Express, MongoDB, and the IBM Cloud Container Service.
Development of cloud native apps that are broken down into a set of microservices has been praised and commended as best-practice in software development methodologies. Software stacks like Kubernetes, which enable cloud native computing, have therefore picked up quite a bit of popularity.
It’s a little (a lot) more fun, however, to try build a so-called cloud native app, than to talk about one.
So here’s our attempt:
We’ll take a use-case that has a bit of real-world familiarity to it — A digital bank. Naturally inspired by Monzo. Let’s call it Innovate.
A live version deployed on a kubernetes cluster in IBM Cloud is available for you try here. To test it out, sign up for an account. A process runs periodically to dump randomized transactions and bills for user accounts, so give it a couple of minutes and refresh to see your populated profile.
Learning objectives
When you’ve completed this Code Pattern, you will understand how to:
- Break an application down to a set of microservices
- Create and manage a Kubernetes cluster on IBM Cloud
- Deploy to a Kubernetes cluster on IBM Cloud
- Deploy to IBM Cloud Private
Flow
When thinking of business capabilities, our imaginary bank will need the following set of microservices:
- Portal: Loads the UI and takes care of user sessions. Relies on all other microservices for core functionality.
- Authentication: Handles user profile creation, as well as login & logout.
- Accounts: Handles creation, management, and retrieval of a user’s banking accounts.
- Transactions: Handles creation and retrieval of transactions made against users’ bank accounts.
- Bills: Handles creation, payment, and retrieval of bills.
- Support: Handles communication with Watson Assistant to enable a support chat feature.
Included components
- IBM Cloud Container Service: IBM Bluemix Container Service manages highly available apps inside Docker containers and Kubernetes clusters on the IBM Cloud.
- Kubernetes Cluster: Create and manage your own cloud infrastructure and use Kubernetes as your container orchestration engine.
- Microservice Builder: Learn, build, run, and manage applications in a microservices framework.
- Watson Assistant: Create a chatbot with a program that conducts a conversation via auditory or textual methods.
Featured technologies
- Microservices: Collection of fine-grained, loosely coupled services using a lightweight protocol to provide building blocks in modern application composition in the cloud.
- Node.js: An open-source JavaScript run-time environment for executing server-side JavaScript code.
- Containers: Virtual software objects that include all the elements that an app needs to run.
- Databases: Repository for storing and managing collections of data.
- Hybrid Integration: Enabling customers to draw on the capabilities of public cloud service providers while using private cloud deployment for sensitive applications and data.
Watch the Video
If you want a quick walkthrough of the end result, a video is available here
Setup
You have multiple options to setup your own instance:
- Deploy to IBM with one-click (automated via toolchain)
- Run it locally
- Deploy to IBM Cloud the hard way (manual, multi-stage)
- Deploy to IBM Cloud Private
Deploy to IBM Cloud
NOTE: This is an automated setup & deployment to your own kubernetes cluster hosted on IBM Cloud; it packages all 7 microservice as one docker image, one deployment, and one multi-port service. To get a better grasp of the concept, you should follow the steps to configure your cluster and deploy each microservice independently.
You can deploy this application using a toolchain by clicking on the Deploy link below. Note that you should have a cluster ready before you start. To deploy a new one, find Containers in Kubernetes Clusters under the IBM Cloud catalog and click create. Allow it some time to deploy.
Deploy to IBM Cloud with one-click using toolchain by clicking this link!
Run Locally
1. Clone the repo
Clone the innovate-digital-bank
repository locally. In a terminal, run:
$ git clone https://github.com/amalamine/innovate-digital-bank.git
2. Create an Instance of MongoDB
This demo heavily depends on mongo as a session & data store.
From the IBM Cloud catalog, find Compose for MongoDB and click create. Give it a name, choose a region, pick the standard pricing plan and click create.
Get your mongo connection string. Almost all your microservices need it; keep it safe!
3. Configure your environment variables
Each of the 7 microservices must have a .env file that stores all credentials.
An example is already provided within each folder. From the directory of each microservice, copy the example file, rename it to .env, and fill it with the appropriate values.
For example, from within the /innovate folder, navigate into the accounts folder
$ cd accounts
Next, copy and rename the .env.example folder
$ cp .env.example .env
Finally, edit your .env folder and add your Mongodb connection string
Repeat these steps for all microservices. In addition to your mongo url, most will need the public IP address of your kubernetes cluster, You can find that under the overview of your cluster on IBM Cloud.
4. Configure your environment mode
When running the app locally without kubernetes, the microservices do not run on the NodePorts specified in our helm chart, so we need to point our portal and userbase microservices to the correct ports.
If you’re running on macOS or any linux-based system, run the following in a terminal from the git repo’s directory
$ export NODE_ENV=development
if you’re running on Windows, edit the NODE_ENV attribute in your .env file from within the /portal folder and the /userbase folder to the following:
NODE_ENV=development
5. Run
Finally, navigate to each microservice folder, and start it. Make sure you run the 7 microservice in 7 separate terminals.
$ npm start
Finally, navigate to each microservice folder, and start it. Make sure you run the 7 microservice in 7 separate terminals.
$ npm start
Deploy to IBM Cloud the Hard Way
NOTE: This guide requires a paid/upgraded account on IBM Cloud. You cannot complete the steps with a free or lite account
- Get the tools
- Clone the repo
- Login to IBM Cloud
- Create a cluster
- Create an instance of MongoDB
- Configure your deploy target
- Configure your environment variables
- Configure kubectl
- Initialize helm
- Deploy
1. Get the tools
You’ll need each of the following pre-requisits:
- IBM Cloud Developer Tools CLI
- the Kubernetes CLI
- the container service plugin
once you’ve downloaded the IBM Cloud Developer Tools CLI, in a terminal, run:
$ bx plugin install container-service -r Bluemix
- the container registry plugin
in a terminal, run:
$ bx plugin install container-registry -r Bluemix
2. Clone the repo
Clone the innovate-digital-bank
repository locally. In a terminal, run:
$ git clone https://github.com/amalamine/innovate-digital-bank.git
3. Login to IBM Cloud
Both through the console and your terminal
NOTE: If you need to specify the region you want to deploy in, you can do so by adding the -a flag followed by the region url.
$ bx login
4. Create a cluster
From the catalog, find Containers in Kubernetes Clusters and click create. Choose a region and a cluster type, and create your cluster. Allow it some time to deploy.
5. Create an instance of MongoDB
This demo heavily depends on mongo as a session & data store.
From the catalog, find Compose for MongoDB and click create. Give it a name, choose a region, pick the standard pricing plan and click create.
Get your mongo connection string. Almost all your microservices need it; keep it safe!
6. Configure your deploy target
Each of the 7 docker images needs to be pushed to your docker image registry on IBM Cloud. You need to set the correct deploy target. Depending on the region you’ve created your cluster in, your url will be in the following format
registry.<REGION_ABBREVIATION>.bluemix.net/<YOUR_NAMESPACE>/<YOUR_IMAGE_NAME>
For example, to deploy the accounts microservice to my docker image registry in the US-South region, my deploy_target will be:
registry.ng.bluemix.net/amalamine/innovate-accounts
If you need to get your namespace, run:
$ bx cr namespace-list
You can also add a new namespace by running:
$ bx cr namespace-add <NAME>
From the directory of each microservice, replace the deploy target in cli-config.yml & in /chart/innovate-<MICROSERVICE_NAME>/values.yaml with the correct one
For example, from within the /innovate folder, navigate into the accounts folder
$ cd accounts
Next, edit line 58 of cli-config.yaml file. Replace the deploy-image-target with the correct value.
deploy-image-target: "registry.ng.bluemix.net/amalamine/innovate-accounts"
Edit line 6 of the values.yaml file. Replace the repository with the correct value.
repository: registry.ng.bluemix.net/amalamine/innovate-accounts
Repeat these steps for all 7 microservices.
7. Configure your environment variables
Each of the 7 microservices must have a .env file that stores all credentials.
An example is already provided within each folder. From the directory of each microservice, copy the example file, rename it to .env, and fill it with the appropriate values.
For example, from within the /innovate folder, navigate into the accounts folder
$ cd accounts
Next, copy and rename the .env.example folder
$ cp .env.example .env
Finally, edit your .env folder and add your Mongodb connection string
Repeat these steps for all microservices. In addition to your mongo url, most will need the public IP address of your kubernetes cluster, You can find that under the overview of your cluster on IBM Cloud.
8. Configure kubectl
Run the following command:
$ bx cs cluster-config <YOUR_CLUSTER_NAME>
Then copy the output and paste it in your terminal
9. Initialize helm
$ helm init
10. Deploy
Finally, navigate to each microservice folder, and run the following command
$ bx dev deploy
Deploy to IBM Cloud Private
If you have an instance of IBM Cloud Private running, you can follow the steps to deploy the app. If you’d like to deploy your own instance of ICP, you can follow this great writeup
- Create a persistent volume
- Create a persistent volume claim
- Create an instance of MongoDB
- Configure your environment variables
- Add your ICP’s address to your hosts file
- Login to docker
- Configure kubectl
- Deploy
1. Create a persistent volume
This demo heavily depends on mongo as a session & data store. Give it a name and a capacity, choose storage type Hostpath, and add a path parameter
2. Create a persistent volume claim
Give it a name and a storage request value
3. Create an instance of MongoDB
From the catalog, choose MongoDb. Give it a name, specify the existing volume claim name, and give it a password
Get your mongo connection string; Almost all your microservices need it; keep it safe!
Your connection string will be in the following format:
mongodb://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DATABASE_NAME>
4. Configure your environment variables
Each of the 7 microservices must have a .env file.
An example is already provided within each folder. From the directory of each microservice, copy the example file, rename it to .env, and fill it with the appropriate values.
For example, from within the /innovate folder, navigate into the accounts folder
$ cd accounts
Next, copy and rename the .env.example folder
$ cp .env.example .env
Finally, edit your .env folder and add your Mongodb connection string
Repeat those steps for all microservices. In addition to your mongo url, the portal microservice will need the address of your ICP.
5. Add your ICP’s address to your hosts file
Add an entry to your /etc/hosts file as follows
<YOUR_ICP_IP_ADDRESS> mycluster.icp
6. Login to docker
$ docker login mycluster.icp:8500
7. Configure kubectl
From your ICP’s dashboard, copy the kubectl commands under admin > configure client
8. Deploy
Finally, navigate to each microservice, and run the following command
$ bx dev deploy
If you don’t have the IBM Cloud Developer Tools CLI installed, get it here first
(Optional) Adding Support with Watson Assistant
The support microservice connects to an instance of Watson Assistant on IBM Cloud to simulate a chat with a virtual support agent.
NOTE: This is an optional step. You need it if you’d like to enable the support feature on the app.
- Create an instance of Watson Assistant
- Import the support workspace
- Get your credentials
- Configure your environment variables
- Deploy
1. Create an instance of Watson Assistant
From the IBM Cloud Catalog, choose Watson Conversation, and click create.
2. Import the support workspace
Import the support workspace into your newly created Watson Assistant instance
3. Get your credentials
Navigate to the deploy tab and copy your username, password, and workspace ID
4. Configure your environment variables
From within the support folder, edit your .env to include your newly acquired credentials
5. Deploy
Redeploy the support microservice, the support feature should now be accessible through the portal.
$ bx dev deploy
What’s Next?
By now you should have your own instance running on the platform of your choice. You can try to deploy this on Minikube, Google Kubernetes Engine, kops — anywhere, really.
If you’re curious enough, head over to the documentation to get a better view about the endpoints each microservice exposes.
Learn more
- Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
- Data Analytics Code Patterns: Enjoyed this Code Pattern? Check out our other Data Analytics Code Patterns
- AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
- With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.
- Data Science Experience: Master the art of data science with IBM’s Data Science Experience
- Kubernetes on IBM Cloud: Deliver your apps with the combined the power of Kubernetes and Docker on IBM Cloud
There’s plenty of room for improvement
Naturally. I would want to define this app as one chart that has multiple subcharts. Or give our currently-not-so-useful chatbot access to MongoDB and some superpowers.
But I’ll keep it simple, for now. Contributions & pull requests are welcome, however!