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
"dependencies"
and"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
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 applicationsTags: 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:
- 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
关于作者
产品
工具
试用购买与出售
沟通
关于红帽
我们是世界领先的企业开源解决方案供应商,提供包括 Linux、云、容器和 Kubernetes。我们致力于提供经过安全强化的解决方案,从核心数据中心到网络边缘,让企业能够更轻松地跨平台和环境运营。