订阅内容

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

关于作者

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Original series icon

原创节目

关于企业技术领域的创客和领导者们有趣的故事