With the launch of the new OpenShift Online Next Gen Developer Preview, I thought I'd offer a few tips for NodeJS developers who are interested in giving the new Kubernetes-based platform a try.

This guide applies to the official OpenShift NodeJS S2I base images:

To follow along, you'll need access to an OpenShift account, and a development environment with nodejs, git, and a copy of the "oc" command line tool.

Adapting your NodeJS source code to run on in a container can usually be accomplished in two easy steps:

  1. Include a package.json file with valid "dependencies" and "scripts" sections - allowing builds to be started with "npm install", and webservers to be initialized via "npm start"
  2. Your source repo should launch a single web process that listens on port "8080" in order to connect to the Kubernetes "Service" (or load-balancer)

Build and Deploy Images from the CLI

Schedule a new build and deployment, adding source code from "REPO_NAME" to a new image layer on top of "BASE_IMAGE":

oc new-app BASE_IMAGE~REPO_NAME

For example, to schedule a build and deployment, layering the pillar-base example repo on top of the system-provided nodejs base image, run:

oc new-app nodejs~http://github.com/OpenShiftDemos/pillar-base

The command output should look something like:

--> Found image 6f7f7d9 (5 weeks old) in image stream "nodejs" in project "openshift" under tag "0.10" for "nodejs"

    Node.js 0.10
    ------------
    Platform for building and running Node.js 0.10 applications

    Tags: builder, nodejs, nodejs010

    * A source build using source code from http://github.com/OpenShiftDemos/pillar-base will be created
      * The resulting image will be pushed to image stream "pillar-base:latest"
    * This image will be deployed in deployment config "pillar-base"
    * Port 8080/tcp will be load balanced by service "pillar-base"
      * Other containers can access this service through the hostname "pillar-base"

--> Creating resources with label app=pillar-base ...
    imagestream "pillar-base" created
    buildconfig "pillar-base" created
    deploymentconfig "pillar-base" created
    service "pillar-base" created
--> Success
    Build scheduled, use 'oc logs -f bc/pillar-base' to track its progress.
    Run 'oc status' to view your app

Several new kubernetes objects (service, deploymentconfig, imagestream, buildconfig) have been successfully created, as shown in the output.

To make your new Kubernetes service available via an http route, run the following command after updating it to include your service name as the last argument:

oc expose svc pillar-base

Then, run "oc status" to find the host address where your NodeJS app will be available.

Runtime configuration via Environment variables:

The container runtime environment can be configured by manipulating the following Env vars:

Name Description Default Value
NODE_ENV
NodeJS runtime mode
production
NPM_RUN
Select an alternate / custom runtime mode, defined in your package.json file’s "scripts" section
Default init command: npm run $NPM_RUN
start
DEV_MODE
When set to "true", nodemon will be used to automatically restart the server
When DEV_MODE is enabled, the default value for NODE_ENV will change to "development" (if not explicitly set), and the NPM_RUN config option will have no effect
false

Alternate NodeJS Base Images

Additional NodeJS releases are available for Source2Image and Docker ONBUILD, making it easy to build application images that will run on OpenShift, Kubernetes, or any other platform that includes Docker image support.

Ready to give OpenShift a try?

Don't have access to an OpenShift / Kubernetes cluster? No problem! There are plenty of ways to get started:

  1. Sign up for the OpenShift Online 3 Developer Preview
  2. Fire up a local OpenShift Development VM using Vagrant and Virtualbox with "vagrant init thesteve0/openshift-origin & vagrant up"
  3. Try our new OpenShift-on-Docker environment with "oc cluster up"
  4. Build your own OpenShift cluster w/ Ansible: http://github.com/openshift/openshift-ansible
  5. Sign up for OpenShift Dedicated w/ ops and support from Red Hat
  6. Run your own OpenShift Container Platform w/ support from Red Hat