1. 程式人生 > >Create cloud native Node.js applications

Create cloud native Node.js applications

Cloud native technologies are built to scale and grow in public, private, and hybrid cloud environments. Because they are resilient and easy to manage, they’re becoming increasingly important in our quick-changing technology landscape.

But building cloud native applications can sometimes feel daunting. Knowing what technologies to include and how to provision them is a huge task to accomplish before you can even begin coding. According to the

Cloud Native Computing Foundation, key technologies to implement and run cloud native applications include Docker, Kubernetes, Helm, and Prometheus. Plus, cloud native applications are commonly built using a microservice architecture, which is a distributed application architecture that’s comprised of multiple, separately running services.

To simplify building cloud native Node.js apps, we’ve created an open source Yeoman generator to help you create fully functional Express-based Node.js applications. The generators correspond and align directly with the modules outlined on the CloudNativeJS.io. CloudNativeJS is a community project that provides assets and tools to help you to build cloud native Node.js applications and deploy them to Docker and Kubernetes-based clouds.

Open source Yeoman generators simplify Node.js development

The open source Yeoman generator, generator-nodeserver, creates fully functional Express-based Node.js application projects that include:

  1. Docker files for release and development containers.
  2. Helm charts with substitution values for deployment to any Kubernetes environment.
  3. Monitoring endpoints:
    1. Kubernetes liveness probes.
    2. Zipkin for end-to-end request tracing to spot bottlenecks.
    3. Prometheus for visualizing performance problems across app components.
    4. App Metrics with flame graphs for drill down into individual Node.js processes.

Build your Node.js project

So, now that you know what the generator does, let’s get you set up using it. Follow the steps below to install and use generator-nodeserver to create an Express-based Node.js server project.

You can build/deploy these projects using standard tools like npm, Docker, and Helm, or you can use IBM Cloud Developer Tools (IDT) to simplify these tasks. The projects created by the nodeserver generator include npm scripts to help you install and use IDT — just look for the idt:* scripts in your package.json file.

Watch the videos on Quick Start for IBM Cloud to get a better idea of what IDT can do for you.

Install the Yeoman generator

  1. Install Yeoman using the following command: npm install -g yo
  2. Install nodeserver using the following command: npm install -g generator-nodeserver

Run yo nodeserver

When you run ‘yo nodeserver’, the generator prompts you for the following:

  • Project name: Specify the project name. The default is the current directory name. This is a required value.
  • OpenApi document: Specify the relative or absolute file name of a Swagger document to direct the project’s code generation. A route stub will be scaffolded and registered for each route defined in the Swagger document. This is an optional value.
  • Choose IBM Cloud services: Use ‘Y’ or ‘N’ to specify whether you want to scaffold IBM Cloud service enablement into your project. If you specify ‘Y’, you can then select one or more IBM Cloud service from the list. Your options are alert, appid, cloudant, mongo, object storage, postgre, push, redis, watson conversation. For each service you select, configuration and access scaffolding code is generated. IBM Cloud service enablement is optional.

You can also create an application using the default options by running this command: yo nodeserver --headless

which defaults to:

  • Project name: the name of the current working directory
  • OpenApi Document: false
  • Choose IBM Cloud Services?: false

You can build and run the generated project locally across all operating systems (Windows, macOS, Linux etc).

Run the project

To run the project locally, follow these steps:

  1. Install dependencies with the following command: npm install
  2. Start the server using hte following: npm start

To run the project locally with containers, follow these steps. We use IBM Cloud Developer Tools, which offers a command-driven approach for creating, developing and deploying a project and running your app locally with containers:

  1. Install IDT with command: npm run idt:install
  2. Build a docker image and install dependencies with command: npm run idt:build
  3. Run the application with command: npm run idt:run

Test and debug your app

Here are a few quick ways to test and debug your application locally:

  • Run unit tests using the npm run idt:test command
  • Run your project in debug mode, so you can attach a debug client using the npm run idt:debug command
  • Collect application performance metrics during development by running your project locally then going to localhost:3000/appmetrics-dash
  • Check the health of your application by going to localhost:3000/health
  • Run your project with hot-reload: idt shell run-dev then go to localhost:3000

The generated projects include deployment configuration for the following environments, which are available on IBM Cloud:

  • Kubernetes: the projects include a Helm chart for deployment to Kubernetes
  • Cloud Foundry: the projects include a manifest for deployment to Cloud Foundry
  • Delivery Pipeline: the projects include a toolchain and pipeline definition for CI/CD deployment to the IBM Cloud. Check out the Garage Method to learn more.

Deploy to IBM Cloud

To deploy to IBM Cloud, you first need to create a free account on IBM Cloud.

Then, follow these steps:

  1. Install IDT using this command: npm run idt:install
  2. Log in to IBM Cloud by running the command bx login or bx login –sso if your organization has enabled single sign-on with IBM Cloud.
  3. Deploy to Cloud Foundry.
  4. Add a unique host by adding “host: ” to the manifest.yml file.
  5. Run the command bx target -o ORG -s SPACE to select the Org and Space for your application.
  6. Then deploy to IBM Cloud with the following command: npm run idt:deploy.
  7. Deploy to Kubernetes.
  8. Set your region by running: bx cs region-set <region>
  9. Set your terminal context to your cluster by running: bx cs cluster-config <clustername>
  10. Run the command displayed in your terminal in response to the last step to export your kubeconfig file.
  11. Deploy your application with this command: npm run idt:deploy -- --target container

Next steps

Check out IBM Cloud App Service to generate more complex applications like a MEAN web application.

Conclusion

The nodeserver Yeoman generator does a great job creating cloud-native projects, complete with all the essential ingredients. IDT is a great companion tool to simplify building, testing, and deploying your dockerized apps. Together, they make it a cinch to build and deploy cloud native apps to any Kubernetes environment, especially the IBM Cloud.