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:
- OCP / OSO: RHEL NodeJS v0.10 Source2Image builder
- OpenShift Origin: CentOS NodeJS v0.10 Source2Image builder
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:
- Include a package.json file with valid
"scripts"sections – allowing builds to be started with “npm install”, and webservers to be initialized via “npm start”
- 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
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:
NodeJS runtime mode
Select an alternate / custom runtime mode, defined in your
Default init command:
When set to “true”, nodemon will be used to automatically restart the server
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:
- Sign up for the OpenShift Online 3 Developer Preview
- Fire up a local OpenShift Development VM using Vagrant and Virtualbox with “vagrant init thesteve0/openshift-origin & vagrant up”
- Try our new OpenShift-on-Docker environment with “oc cluster up”
- Build your own OpenShift cluster w/ Ansible: http://github.com/openshift/openshift-ansible
- Sign up for OpenShift Dedicated w/ ops and support from Red Hat
- Run your own OpenShift Container Platform w/ support from Red Hat