Check out what's in the latest release of Kabanero Learn More

Developing cloud native microservice applications with the Kabanero Node.js LoopBack Collection and Appsody CLI.

duration 20 minutes

What you will learn

In this guide, you’ll learn how to create and run a simple cloud native microservice. Then, you’ll update the microservice that you created and deploy it to Kubernetes or Knative. This process will be done by using the Kabanero Node.js LoopBack Collection with the Appsody CLI. Deployment to Knative is optional depending on whether you want to Scale to Zero.

Kabanero’s Node.js LoopBack Collection provides an application stack that enables the development and optimization of microservices. With application stacks, developers don’t need to manage full software development stacks or be experts on underlying container technologies or Kubernetes. Application stacks are customized for specific enterprises to incorporate their company standards and technology choices.

Applications in this guide are written based on the LoopBack API specifications, built and run with Node.js, and deployed to Kubernetes through a modern DevOps toolchain that is triggered in Git.

Prerequisites

  1. Docker must be installed.

  2. Appsody must be installed.

  3. Optional: If you have a repository that contains a set of custom collections, you need the URL for the index.yaml file. Use this URL to make your custom collections available with the Appsody CLI.

  4. Optional: If you are testing multiple microservices together, you must have access to a local Kubernetes cluster for local development. If you are using Docker Desktop, you can enable Kubernetes from the menu by selecting PreferencesKubernetesEnable Kubernetes. Other options include Minishift or Minikube.

Getting started

Configuring Appsody

Note: The use of the public Kabanero Collection Hub is only for the purposes of this guide. It is recommended that you make a private copy of the Kabanero Collection Hub and use it in the same way. However, this demonstration does not require that you make your own copy.

Add your Kabanero index to the Appsody CLI. The following example uses the public index for Kabanero Version 0.3.0.

  1. To check the repositories that Appsody can already access, run the following command:

    appsody repo list

    You see output similar to the following example:

    NAME        URL
    *incubator https://github.com/appsody/stacks/releases/latest/download/incubator-index.yaml
  2. Add your Kabanero index to the Appsody CLI. The following example uses the public index for Kabanero Version 0.3.0. Run the following command to add the Kabanero index:

    appsody repo add kabanero https://github.com/kabanero-io/collections/releases/download/0.3.0/kabanero-index.yaml
  3. Check the repositories again by running appsody repo list to see that the Kabanero repository was added:

    NAME        URL
    *incubator https://github.com/appsody/stacks/releases/latest/download/incubator-index.yaml
    kabanero    https://github.com/kabanero-io/collections/releases/download/0.3.0/kabanero-index.yaml

    In this example, the asterisk (*) shows that incubator is the default repository. Run the following command to set the Kabanero index as the default repository:

    appsody repo set-default kabanero
  4. Check the available repositories again by running appsody repo list to see that the default is updated:

    NAME        URL
    incubator  https://github.com/appsody/stacks/releases/latest/download/incubator-index.yaml
    *kabanero   https://github.com/kabanero-io/collections/releases/download/0.3.0/kabanero-index.yaml
  5. Recommendation: In enterprise settings, when a solution architect creates application stacks with technology choices that are in a private Collection Hub, it’s best to remove incubator from the list. These Appsody stacks are not supported by the Kabanero application cluster. Run the following command to remove the incubator repository:

    appsody repo remove incubator
  6. Check the available repositories again by running appsody repo list to see that incubator is removed:

    NAME             URL
    *kabanero        https://github.com/kabanero-io/collections/releases/download/0.3.0/kabanero-index.yaml

Your Appsody CLI is now configured to use the Kabanero Collections. Next, you need to initialize your project.

Initializing your project

  1. First, create a directory that will contain the project:

    mkdir -p ~/projects/simple-nodejs-loopback
    cd ~/projects/simple-nodejs-loopback
  2. Run the following command to initialize your Node.js LoopBack project with the Appsody CLI:

    appsody init nodejs-loopback

    The output from the command is similar to the following example:

    Running appsody init...
    Downloading nodejs-loopback template project from https://github.com/kabanero-io/collections/releases/download/0.3.0/incubator.nodejs-loopback.v0.1.6.templates.scaffold.tar.gz
    Download complete. Extracting files from /Users/suechaplain/appsody/simple-nodejs-loopback/nodejs-loopback.tar.gz
    Setting up the development environment
    Your Appsody project name has been set to simple-nodejs-loopback
    Pulling docker image kabanero/nodejs-loopback:0.1
    Running command: docker pull kabanero/nodejs-loopback:0.1
    0.1: Pulling from kabanero/nodejs-loopback
    ..
    26ad3cb5cc5c: Pull complete
    Digest: sha256:cca31a10f825c0ae7785681b21189921b12c88b4260ff17558f39c0093e1625d
    Status: Downloaded newer image for kabanero/nodejs-loopback:0.1
    docker.io/kabanero/nodejs-loopback:0.1
    [Warning] The stack image does not contain APPSODY_PROJECT_DIR. Using /project
    Running command: docker run --rm --entrypoint /bin/bash kabanero/nodejs-loopback:0.1 -c find /project -type f -name .appsody-init.sh
    Successfully initialized Appsody project

Your new project is created, built, and started inside a container.

Understanding the project layout

For context, the following image displays the structure of the project that you’re working on:

Project structure

This project contains the following artifacts:

  • DEVELOPING.md, a short guide for developing LoopBack applications in an IDE

  • README.md, an overview of how the project was generated

  • index.js, a top level file file used by Node.js

  • index.ts, a top level file used by Typescript

  • package-lock.json, the application’s npm dependency tree

  • package.json, the application’s package manifest

  • public, folder, containing static assets

  • src, folder, containing application source code

  • tsconfig.json, a Typescript configuration file

Running the Appsody development environment

  1. Run the following command to start the Appsody development environment:

    appsody run

    The Appsody CLI launches a local Docker image that contains the Node.js Loopback runtime environment that hosts the microservice. After some time, you see a message similar to the following example:

    [Container] Running command:  npm start
    [Container]
    [Container] > nodejs-loopback@0.1.6 start /project
    [Container] > node -r source-map-support/register .
    [Container]
    [Container] Server is running at http://[::1]:3000
    [Container] Try http://[::1]:3000/ping

    This message indicates that the project is started. Browse to http://localhost:3000 and you can see the LoopBack splash screen, as shown in the following image:

Project structure

Updating the application

The basic application created by the project initialization defines one API endpoint /ping.

  1. Browse to http://localhost:3000/ping/ to call the ping API. You should see the greeting Hello from LoopBack at the beginning of the content, in a similar format to the following output:

    {"greeting":"Hello from LoopBack","date":"2019-12-18T15:59:18.118Z","url":"/ping","headers": ...
  2. Edit the src/controllers/ping.controller.ts file. Change the text of the greeting in the ping object in the PingController class from Hello from LoopBack to Hello from LoopBack running in Kabanero!

  3. Save the change.

Appsody watches for file changes and automatically updates your application. Point your browser to http://localhost:3000/ping to see the new output, which displays the greeting Hello from LoopBack running in Kabanero!

If your application is currently running under Appsody control, you can stop it with Ctrl+C, or by running the command appsody stop from another terminal.

Testing the application

If you are building an application that is composed of microservices, you need to test within the context of the overall system. First, test your application and perform unit testing in isolation. To test the application as part of the system, deploy the system and then the new application.

You can choose how you want to deploy the system and application. If you have adequate CPU and memory to run MiniShift, the application, and the associated services, then you can deploy the application on a local Kubernetes that is running on your computer. Alternatively, you can enable Docker Desktop for Kubernetes, which is described in the Prerequisites section of the guide.

You can also deploy the system, application, and the associated services in a private namespace on a development cluster. From this private namespace, you can commit the microservices in Git repositories and deploy them through a DevOps pipeline, not directly to Kubernetes.

Testing locally on Kubernetes

After you finish writing your application code, the Appsody CLI makes it easy to deploy directly to a Kubernetes cluster for further local testing. The ability to deploy directly to a Kubernetes cluster is valuable when you want to test multiple microservices together or test with services that the application requires.

  1. Ensure that your kubectl command is configured with cluster details and run the following command to deploy the application:

    appsody deploy

    This command builds a new Docker image that is optimized for production deployment and deploys the image to your local Kubernetes cluster. After some time you see a message similar to the following example:

    Deployed project running at http://localhost:30262
  2. Run the following command to check the status of the application pods:

    kubectl get pods

    In the following example output, you can see that a simple-nodejs-loopback pod is running:

    NAME                                    READY   STATUS    RESTARTS   AGE
    appsody-operator-6bbddbd455-nfhnm        1/1     Running   0          26d
    simple-nodejs-loopback-775b655768-lqn6q  1/1     Running   0          3m10s

    After the simple-nodejs-loopback pod starts, go to the URL that was returned when you ran the appsody deploy command, and you see the Appsody microservice splash screen. To see the response from your application, point your browser to the <URL_STRING>/example URL, where <URL_STRING> is the URL that was returned. For example, http://localhost:30262 was returned in the previous example. Go to the http://localhost:30262/example URL to see the deployed application response.

  3. Use the following command to stop the deployed application:

    appsody deploy delete

    After you run this command and the deployment is deleted, you see the following message:

    Deployment deleted

Testing with Knative serving

You can choose to test an application that is deployed with Knative Serving to take advantage of Scale to Zero. Not all applications can be written to effectively take advantage of Scale to Zero. The Kabanero operator-based installation configures Knative on the Kubernetes cluster, specifically OKD 3.11. Because of the resources that are required to run Knative and its dependencies, testing locally can be difficult. Publish to Kubernetes by using pipelines that are described later in the guide. Your operations team can configure the pipelines so that Knative Serving is enabled for deployment.

Publishing to Kubernetes by using pipelines

After you develop and test your application in your local environment, it’s time to publish it to your enterprise’s pipeline. From your enterprise’s pipeline, you can deploy the application to the appropriate Kubernetes cluster for staging or production. Complete this process in Git.

When Kabanero is installed, deploying applications to a Kubernetes cluster always occurs through the DevOps pipeline that is triggered in Git. Using DevOps pipelines to deploy applications ensures that developers can focus on application code, not on containers or Kubernetes infrastructure. From an enterprise perspective, this deployment process ensures that both the container image build and the deployment to Kubernetes or Knative happen in a secure and consistent way that meets company standards.

To deliver your application to the pipeline, push the project to the pre-configured Git repository that has a configured webhook. This configured webhook triggers the enterprise build and deploy pipeline.

Copied to clipboard
Copy code block

Way to go! What's next?

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Where to next?