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.
Join the DZone community and get the full member experience.
Join For FreeA 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 --cloud
flag. 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
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.
Self Deploying
Now we should go back to our working directory and install the okd-runner
module 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:
- Then it will show you the namespaces available for your user to chose:
Runtime
After that, you can watch how your application deploys:
...
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!
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.
Published at DZone with permission of Cesar Valdez, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments