Build Your First Unikernel With Express.JS And NanoVMs

Unikernels are the next era in cloud computing. They offer a much lower footprint and improved security over containers. In this article I will walk you through deploying an Express.JS backend using Unikernels.

Under no circumstances use this for production. Unikernels are far from release stage and have a lot of features missing.

What is a Unikernel?

Unikernel is essentially a container, but without the need for a host OS. All system calls and OS functions are bundled together with the app as libraries. If you have never heard of Unikernels, I suggest you read this first.

Step 1: Create a simple backend

Firstly, we are going to create a simple Express.JS backend. Unikernels are still in the early stage of development, so we will keep it dead simple, a proof-of-concept, so to say. Begin with creating an npm project (run these in a new project folder):

> npm init
> npm i -S express

Now, put this code in index.js:

It should be pretty straightforward. We create an Express server that listens on port 3000 and responds with a text message. You can verify if everything works by running node index.js and opening http://localhost:3000 in a browser.

Step 2: Install OPS

For this tutorial, we will be using the NanoVMs unikernel, as it has JavaScript support. The build tool for this unikernel is called OPS. To install it, run this in your terminal:

> curl https://ops.city/get.sh -sSfL | sh

Follow the install and answer a few questions. After everything is done, verify the install by running ops version in a new terminal session. Your OPS version should be at least 0.1.9.

Step 3: Bundle your dependencies

Right now it is not possible to run npm i inside the unikernel, so we will have to bundle dependencies (express) together with our code. To do this, we will use rollup. Install the required dependencies:

> npm i -D rollup @rollup/plugin-commonjs @rollup/plugin-json @rollup/plugin-node-resolve

Now, create a file called rollup.config.js with the following contents:

This tells rollup to bundle everything our code needs in one big file. To run it, add the following to scripts section in package.json:

"build": "rollup -c"

Now, run npm run build. This should produce an index.js in output.

Step 4: Run the unikernel

Now we have everything ready to build and run your app on a unikernel. Surprisingly, this is the easiest step, thanks to OPS by NanoVMs. In project root, run:

> ops load node_v11.5.0 -a output/index.js -p 3000

This command tells OPS to use the base image with Node 11.5, load the output/index.js file and open port 3000. Upon running it, you should see this output:

[node output/index.js]
booting /home/mk/.ops/images/node.img ...
assigned: 10.0.2.15
Example app listening at http://localhost:3000

Congratulations, you just ran your Node app in a Unikernel! You can verify that everything works by visiting http://localhost:3000 in your browser:

Closing notes

Thank you for reading, I hope you successfully deployed your Node app with Unikernels. Stay tuned for more tutorials!

Resources

Get new content delivered to your mailbox:

leave a comment