Modernize a monolithic Node.js application into a microservices architecture using IBM Cloud Pak for Applications

This tutorial shows how to transform a traditional monolithic core banking application, which is implemented in Node.js, into a modern microservices architecture by using IBM Cloud Pak for Applications.

Cloud Pak for Applications speeds the development of applications that are built for Kubernetes by using agile DevOps processes. Running on Red Hat OpenShift, the Cloud Pak provides a hybrid, multicloud foundation that is built on open standards, enabling workloads and data to run anywhere. It integrates two main open source projects: Kabanero and Appsody.

This tutorial uses a sample monolithic banking application, which is illustrated in the following architecture diagram:

Architecture diagram of sample monolithic application

There are five tightly coupled services within this application:

  • Admin login (admin_login.ejs)
  • Admin dashboard (admin.ejs)
  • User login (user_login.ejs)
  • User dashboard (users.ejs)
  • Not found (notfound.ejs)

If too much workload or user traffic occurs on one service, then all of the other interconnected services can be affected. Or the complete project can go down, which is one of the major disadvantages of monolithic architectures.

To break down this monolithic application, you separate the admin services (admin_login.ejs and admin.ejs) and user services (user_login.ejs and users.ejs) into microservices so they can run independently. Both services have different functions, so the new application is able to scale them depending on the workload. The two new microservices are:

To do this, you put the admin services into one project and the user services into another, and then deploy them both to a central GitHub repo. Both have their own dependencies and run independently, as you can see in the following architecture diagram. (Don’t worry if this does not fully make sense to you right now. The tutorial steps explain it further.)

Architecture diagram of the new microservices architecture

Prerequisites

To complete the steps in this tutorial, you need:

Estimated time

After the prerequisites are installed, this tutorial will take about 90 minutes to complete the steps.

Steps

  1. Clone the GitHub repository
  2. Install Codewind in Visual Studio to create a microservice test and deploy to GitHub
  3. Create GitHub tokens
  4. Initialize Tekton and integrate with the central GitHub repository
  5. Verify that the microservices are up and running

Step 1. Clone the GitHub repository

  1. Open your terminal and change your directory by using the cd downloads command. (Or any other directory in which you want to clone the project.)
  2. Run the command: git clone https://github.com/IBM/modernize-monolithic-architecture-using-IBM-CP4A.git.
  3. Open the project in Visual Studio.

    Screen capture of project in Visual Studio Code

Step 2. Install Codewind in Visual Studio to create a microservice test and deploy to GitHub

What is Codewind and why does this tutorial use it?

In the present era, one of the biggest challenges for a developer is to build and deploy cloud-native applications. Many actions are required to build a perfect solution on the cloud and you need to build images, create containers, debug, analyze the different logs, assess performance metrics, and rebuild the containers with each code change. That’s why this tutorial uses Eclipse Codewind, an opensource project that helps you achieve all of the above actions quickly with ready-made, container-based project templates and can easily be integrated with your visual code integrated development environment (IDE). Learn more about Codewind.

Since you know which services will be converted into microservices, start by initializing Codewind in Visual Studio with the following tasks:

  1. Open Visual Studio.
  2. Select Extensions and search for Codewind.
  3. Select Install and kindly wait, since it will take some time to initialize.
  4. Once successfully installed, you will see the Codewind section.
  5. Select Codewind and start the local Codewind.
  6. Right-click local and select Create New Project.
  7. Select Kabanero Node.js Express simple template.
  8. Select the folder where you want to initialize the template and name it micro-admin. (This step can take five to ten minutes to initalize.)
  9. Once your template is initalized successfully, kindly open the folder where you created micro-admin. You will see the newly created template.

    Screen capture of new template

    Next, you will break down the monolithic application in three stages.

  10. First, visit the folder where you cloned the monolithic application in Step 1. In that folder, open the app.js file and copy the following lines:

    const express = require("express")
    const path = require('path');
    const app = express();
    app.set('view engine', 'ejs');
    app.set('views', path.join(__dirname, 'views'));
    app.use(express.static(path.join(__dirname, 'node_modules')));
    app.use(express.static(path.join(__dirname, 'public')));
    
    app.get("/", function(req,res){
       res.render("admin_login");
    });
    app.get("/admin_login", function(req,res){
       res.render("admin_login");
    });
    app.get("/admin_in", function(req,res){
       var Name = req.query.name;
       var Password = req.query.pass;
    if (Password =="123")
    {
     console.log("Successfully logged in as admin");
     res.render("admin");
    }
    else{
       res.render("notfound.ejs");
    }
    });
    module.exports.app = app;
    

    Then, go to your new micro-admin folder and replace the app.js file with the copied version.

  11. Second, copy the complete public folder located within the folder where you cloned the monolithic application, and paste it into your new micro-admin folder.

  12. Third, open the views folder located within the folder where you cloned the monolithic application, and copy only the admin.ejs, admin_login.ejs, and notfound.ejs files. Paste those files into your new micro-admin folder.

    Your structure should now look like the following:

    Screen capture of updated micro-admin folder

  13. Open your terminal inside Visual Studio and run the command npm install ejs. This will install the Embedded JavaScript templating that you will use for front-end styling.

  14. Go to Codewind in Visual Studio and look for your project there, running as micro-admin. Right-click it and select Open Application to open the page. From there, select enable project (if it is disabled) and then select build. Check Application Endpoint to see where your application is running.
  15. To test your application, right-click micro-admin, select Application Monitor, and hit the application two or three times to see the changes.

    Screen capture of Application Metrics for Node.js

  16. Run appsody build in your Visual Studio terminal. You don’t have to worry and spend your time on a deployment configuration file since Codewind will create it for you. You only need to focus on your application development.

  17. After the above command executes successfully, you will see a new generated file called app-deploy.yaml on the left hand side of your screen. This file will help you in a later step to deploy the application on Cloud Pak for Applications.

    Note: If you do not have a namespace section, please add it as follows:

    apiVersion: appsody.dev/v1beta1
    kind: AppsodyApplication
    metadata:
     namespace: kabanero
     creationTimestamp: null
     labels:
       image.opencontainers.org/title: micro-admin
       stack.appsody.dev/id: nodejs-express
       stack.appsody.dev/version: 0.2.8
     name: micro-admin
     ....
    

    You successfully created the Admin microservice.

  18. Go back to the beginning of this Step and repeat tasks 6 to 17 to create the second microservice, naming it micro-user.

    This time, your app.js file will be for users, so copy the code below during task 10:

    const express = require("express")
    const path = require('path');
    const app = express();
    app.set('view engine', 'ejs');
    app.set('views', path.join(__dirname, 'views'));
    app.use(express.static(path.join(__dirname, 'node_modules')));
    app.use(express.static(path.join(__dirname, 'public')));
    
    app.get("/user_login", function(req,res){
       res.render("user_login");
       console.log("User login");
    });
    app.get("/user_in", function(req,res){
       var Name = req.query.name;
       var Password = req.query.pass;
    if (Password =="123")
    {
     console.log("Successfully logged in as user");
     res.render("users");
    }
    else{
       res.render("notfound.ejs");
    }
    });
    app.listen(3000 , function(){
       console.log("App is running");
    });
    

    Also, after task 12, you should see a structure like this for your new micro-user folder:

    Screen capture of updated micro-user folder

    Once you finish testing and creating the User microservice, individually upload both microservices to the central GitHub repository.

Note: If you have any difficulty executing this step to create both microservices, please check out the following sample repositories that were created using Codewind:

Step 3. Create GitHub tokens

Before you initialize Tekton, it is really important to create two GitHub tokens for your admin and user microservices:

  1. Open GitHub and log into your account.
  2. Click your profile photo to expand the account profile menu.
  3. Within the menu, click Settings > Developer settings > Personal access tokens.
  4. Click the Generate new token button.
  5. Give your first token a descriptive name by typing tekton-app-user into the Note field.
  6. Select the scopes, or permissions, you’d like to grant this token. To use your token to access repositories from the command line, select the repo checkbox.

    Screen capture of Developer settings page in GitHub

  7. Click the Generate token button.

  8. Copy the token to your clipboard. It is important that you do this. For security reasons, after you navigate off the page, you will not be able to see the token again.
  9. To create your second token, click the Generate new token button again.
  10. Give your second token a descriptive name by typing tekton-app-admin into the Note field.
  11. Select the scopes, or permissions, you’d like to grant this token. To use your token to access repositories from the command line, select the repo checkbox.
  12. Click the Generate token button.
  13. Copy the second token to your clipboard. It is important that you do this for both tokens.

    Once both tokens are created, you will see a page similar to the one below:

    Screen capture of Personal access token page in GitHub

Step 4. Initialize Tekton and integrate with the central GitHub repository

What is Tekton and why does this tutorial use it?

Tekton is a powerful, yet flexible, Kubernetes-native open source framework for creating continuous integration and continuous delivery (CI/CD) systems. This tutorial uses Tekton because it is a built-in tool for IBM Cloud Pak for Applications that connects the GitHub central repository and a webhook that lifts and shifts application source code from your local development to the cloud. Learn more about Tekton.

To initialize Tekton, perform the following tasks:

  1. Open your Red Hat OpenShift web console.
  2. Once you are logged in successfully, select Kabanero from the My Project section.
  3. Select Cloud Pak for Applications from the menu.
  4. You should see the following screen:

    Screen capture of Welcome to Cloud Pak for Applications page

  5. Click the Instance tab.

  6. Within the Tools section, select Tekton. You should see the following screen:

    Screen capture of Tekton Pipelines page

  7. Select Webhooks from the menu and proceed to create two webhooks for your microservices (micro-admin and micro-user).

  8. For the first webhook, enter w1-admin in the Name field, https://github.com/IBM/micro-admin in the Repository URL field, and micro-token-1 in the Access Token field.

    Screen capture of Webhook Settings page for creating the `w1-admin` webhook

    Click Create.

  9. For the second webhook, enter w2-user in the Name field, https://github.com/IBM/micro-user in the Repository URL field, and micro-token-2 in the Access Token field. Click Create.

    Screen capture of Webhook Settings page for creating the `w2-user` webhook

  10. Check that Tekton and GitHub are successfully connected by opening your two repositories. Go to the micro-admin repository and under settings, select Webhooks from the menu. If the pipeline is connected properly, you will see a link such as http://w1-admin-6zmvc.kabanero.. (you may have different link). Follow the same procedure to check the micro-user repository.

    Screen capture of Webhooks section of GitHub

    Important: Do not worry if you get an error notice. This will resolve after the repositry code is updated.

  11. Make some changes in the micro-admin and micro-user repositories that were created in Step 2 to trigger your Tekton pipeline. First, open the micro-admin repository. Inside the views folder, open the admin.ejs file and make some changes, such as searching for My Dashboard and capitalizing the text to be MY DASHBOARD. After you are done, commit the file.

    Perform the same type of procedure within the micro-user repository, making similar changes to some text within the user.ejs file. After you are done, commit the file.

  12. Open your Tekton dashboard. Under the Tekton dropdown list, select PipelineRuns.

  13. Wait until the rows under the Status column display All tasks completed executing, which indicates you successfully integrated your central repo to your Tekton instance on IBM Cloud Pak for Applications.

    Important: Perform the changes in each repository separately. For example, perform the changes in the User repository first and after it is successfully built and deployed, then update the Admin repository. Or vice versa.

    Screen capture of Tekton PipelineRuns page

For more details about Tekton, check out this great tutorial.

Step 5. Verify that the microservices are up and running

  1. Open the OpenShift dashboard.
  2. Select Applications from the menu.
  3. Select Routes and you should then see your two microservices up and running on the Routes page.
  4. To run the application, click the links within the Hostname column.

    Screen capture of the OpenShift Routes page

    Here is a sample screen capture of the user interface:

    Screen capture of the User interface

    Here is a sample screen capture of the admin interface:

    Screen capture of the Admin interface

Conclusion

In this tutorial, you learned how to modernize a Node.js application, transforming it from a monolithic architecture into a microservices architecture using Cloud Pak for Applications. By independently running two projects containing related services, you can scale them depending on the workload. In addition, you can integrate as many microservices as you want without affecting or scaling down the complete project.