DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Self-Deploying Node.js Applications

Self-Deploying Node.js Applications

Want to take some of the monotony out of deploying your microservices? Read on to see how one dev did it with OpenShift and Node.js.

Cesar Valdez user avatar by
Cesar Valdez
·
Feb. 22, 19 · Tutorial
Like (8)
Save
Tweet
Share
12.38K Views

Join the DZone community and get the full member experience.

Join For Free

A few days ago I was watching a Youtube a talk given by Kelsey Hightower titled, “Self Deploying Kubernetes Applications.” In this talk, which is a bit old now (over a year), he was demoing a Golang application capable of running locally but also capable of deploying itself into a Kubernetes cluster.

This is how deploying applications into the cloud should be, instead of copy/pasting/modifying configuration files or issuing cryptic commands. It should be as simple, just like running a process on our laptop. So, after watching this talk, I started to think about how to achieve this with one of my favorite programming languages, JavaScript/Node.js.

After a few days of hacking with Kubernetes/OpenShift REST API, I wrote a small module that, when imported (or required) in a Node..js application, extends its runtime capabilities, allowing it to run in OpenShift (at the moment) by just adding a --cloudflag. From the point of view of a non-expert, it will looks like OpenShift is an global JavaScript interpreter.

Hello World

Let’s see how this works by writing a simple web server:

let count = 0
console.log('Running...')
require('http')
    .createServer((req, res) => {
        res.end(`<HTML>
                    <h1>Hello From -> ${process.platform}</h1>
                    <h2>Visitor: ${count++} </h2>
                </HTML>`)
        console.log(`response: ${Date.now()}`)
    }).listen(8080)

In these 9 lines, we defined an HTTP/Server listening in Port 8080, serving a webpage that shows a counter with the visitors and the OS platform so we can see what OS is running our script.

We save this file with the name app.js and run test it locally:

node app #app.js

Image title

Getting Started

Before continuing on, you’ll need an OpenShift cluster up and running. The easiest way is to subscribe here, OpenShift Online, for free or if you're feeling good you can set up one on your computer via Minishift or oc cluster-up.

Once you have OpenShift sorted out, you’ll need to create a project/namespace manually. You can do this by logging into the web console and clicking into the new project.

Image title

Self Deploying

Now we should go back to our working directory and install the okd-runnermodule from npm — this is the module that does the magic:

npm install install okd-runner --save

Then we add the module to our source code:

let count = 0

const run = require('okd-runner') // <- self-deploy module

require('http')
    .createServer((req, res) => {
        res.end(`<HTML>
                    <h1>Hello From -> ${process.platform}</h1>
                    <h2>Visitor: ${count++} </h2>
                </HTML>`)
        console.log(`response: ${Date.now()}`)
    }).listen(8080)

And we close the editor and run our application with the --cloud flag:

node app.js --cloud   # or -c for short
  • When your application deploys for the first time, it will ask you for your cluster credentials:

Application cluster credentials

  • Then it will show you the namespaces available for your user to chose:Application namespaces

Runtime

After that, you can watch how your application deploys:

Application deployment

...
building  ok
URL:  http://my-app-dev-01.7e14.starter-us-west-2.openshiftapps.com
...
...
npm info using node@v10.14.0
npm info lifecycle my-app@1.0.0~prestart: my-app@1.0.0
npm info lifecycle my-app@1.0.0~start: my-app@1.0.0

> my-app@1.0.0 start /opt/app-root/src
> node app.js

response: 1550511176623
...

Container Logs

Another convenient feature is to receive the logs of your container in your shell. This makes it easier to see what's happening inside the container.

...
npm info using node@v10.14.0
npm info lifecycle my-app@1.0.0~prestart: my-app@1.0.0
npm info lifecycle my-app@1.0.0~start: my-app@1.0.0

> my-app@1.0.0 start /opt/app-root/src
> node app.js

response: 1550511176623
...

Here, you can see the logs of your container plus the URL. To exit, you just need to press Ctrl-C and you will get back to your console.

Updating

From now on, everything is similar to running your application locally. Let’s see how we can update our application by adding some code to show the pod name:

let count = 0
const run = require('okd-runner')
console.log('listening in 8080')
require('http')
    .createServer((req, res) => {
        res.end(`<HTML>
                    <h1>Hello From ${process.platform}</h1>
                    <h2>Visitors ${count++} </h2>

<!-- this line -->
                    <p>Pod ${require('os').hostname()}<p>
<img src="https://media.giphy.com/media/12NUbkX6p4xOO4/giphy.gif">
<!--           -->
 </HTML>`)
        console.log(`response: ${Date.now()}`)
    }).listen(8080)

We run our application again:

  node app --cloud

And that's it!

Image title

Clean Up

If you want to remove that project from the namespace, you just need to execute your application with the -rm flag:

node app -rm

You see not a single YML file. I hope this module helps you simplify your workflow while developing Node.js microservices. Also, this module is still under development so feel free to contribute by sending suggestions, pull request, improvements or by opening an issue.

application Kubernetes Node.js

Published at DZone with permission of Cesar Valdez, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Type Variance in Java and Kotlin
  • Using JSON Web Encryption (JWE)
  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: