Scaling Nodejs

on

OpenShift


http://bit.ly/19GST1p

http://socuteurl.com/gleefulkitty

presented by

ryan jarvinen / @ryanj
Open Platform Evangelist
at
Red Hat

ryanj@redhat.com

Agenda

  1. Cloud Basics and OpenShift
  2. Horizontally scalable application architecture
  3. Spin up a new app from source
  4. Configure OpenShift / HAProxy for scaling

Cloud Overview

OpenShift

  • Auto-scaling application hosting
  • Web-service architectures on-demand
  • Free as in beer (no cost, no time limit)
  • Free as in freedom (open source)
  • 512 MB RAM / 1 GB storage per gear (3 free)
  • Need more resources? (just ask!)
  • REALLY a Developer Preview (GA this summer)

Secure and Efficient

One Source to Bind Them All

OpenShift Online

free OpenShift Online accounts:

http://openshift.com

OpenShift Origin

http://openshift.github.io

Open cartridge format

OpenShift Cartridge

Nodejs Cartridge Updates

github.com/ryanj/nodejs-custom-version-openshift

github.com/openshift/origin-server/pull/1624

The big picture

Open client interface

https://github.com/openshift/rhc

sudo gem install rhc
and
rhc setup

Will automatically:

  • link your dev env with your openshift account
  • verify your ssh key configuration
  • configure your application namespace

Scaling - Two ways

Vertical - bigger, taller, more expensive

Horizontal - distributed, cheap, linear

What you need for horizontal

  1. Commodity "hardware"
  2. Easy instance creation and deletion
  3. Software built to grow sideways

OpenShift Architecture

OpenShift Scaling

Scaling Tips

  • Go Stateless, Move sessions to the back-end
  • Build with reusable, loosely coupled pieces
  • Design for retries and small pieces failing
  • Borrow the best technologies
  • Profile your data, evaluate your need for speed, durability, and read vs write performance
  • Caching is your friend (when used correctly)

Scaling

Setting a min and max scale from the command line

rhc cartridge-scale nodejs-0.6 -a nodeapp --min 3 --max 5

Scaling Web UI

Application scaling settings

HAProxy Scaling Hooks

Adding and removing instances / scaling up and down

haproxy_ctld --up

haproxy_ctld --down

HAProxy on / off switches

haproxy_ctld_daemon start
haproxy_ctld_daemon stop
haproxy_ctld_daemon restart

These hooks are available on the command line within your gear, and via a REST API

HAProxy Web UI

http://$YOUR_APP_DNS/haproxy-status/


HAProxy raw data

http://$YOUR_APP_DNS/haproxy-status/;csv

Demo time!

https://github.com/openshift-quickstart/irc-leaderboard-quickstart

Gearing up a new app on OpenShift

rhc app create APP_NAME APP_CARTRIDGE CART_2 CART_3
or
rhc app create -s nodepdxbot nodejs-0.6 mongodb-2.2 --from-code=https://github.com/openshift-quickstart/irc-leaderboard-quickstart.git
Application Options
-------------------
Namespace:   rjdemo
Cartridges:  nodejs-0.6, mongodb-2.2
Source Code:
https://github.com/openshift-quickstart/openshift-meteorjs-quickstart.git
Gear Size:   default
Scaling:     no

Creating application 'instantappstore' ... done

Waiting for your DNS name to be available ... done

Downloading the application Git repository ...
Cloning into 'instantappstore'...

Your application code is now in 'instantappstore'
instantappstore @ http://intstantappstore-rjdemo.rhcloud.com/ (uuid: 5175981a5973ca7a69000501)
-------------------------------------------------------------
Created:         1:05 PM
Gears:           1 (defaults to small)
Git URL:        
ssh://5175981a5973ca7a69000501@instantappstore-rjdemo.rhcloud.com/~/git/instantappstore.git/
Initial Git URL:
https://github.com/openshift-quickstart/openshift-meteorjs-quickstart.git
SSH:             5175981a5973ca7a69000501@instantappstore-rjdemo.rhcloud.com

nodejs-0.6 (Node.js 0.6)
------------------------
  Gears: Located with mongodb-2.2

mongodb-2.2 (MongoDB NoSQL Database 2.2)
----------------------------------------
  Gears:          Located with nodejs-0.6
  Connection URL:
mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/
  Database Name:  instantappstore
  Password:       PTk4cCetTj2w
  Username:       admin

RESULT:
Application instantappstore was created.

Success!

You should now have MongoDB and a basic node.js app running on OpenShift!


Your gear is now configured with:

  • it's own git repo
  • it's own web server
  • ssh access
  • logging
  • MongoDB
  • publicly accessible DNS
  • a custom build of nodejs

Run your existing applications on OpenShift

Follow this git-based approach for porting your apps to OpenShift

https://gist.github.com/ryanj/5267357

Make sure that you bind to the correct IP_address and port number...

Environment Variables

var ip_addr  = process.env.OPENSHIFT_INTERNAL_IP   || '127.0.0.1';
var port     = process.env.OPENSHIFT_INTERNAL_PORT || 8000;
var hostname = process.env.OPENSHIFT_APP_DNS       || 'localhost';

Cloud Env

config-multipaas is a config-chain wrapper that provides a consistent naming scheme for cloud server configs (from OpenShift, Heroku)

npm install --save config-multipaas

Add your own configs using the config-chain API

var cc = require('config-multipaas')
var config = cc({ additional_configs: "just like config-chain!" });

When cloud configs are not available, local development configs are returned

app.listen(config.get('PORT'), config.get('IP'), function () {
  console.log( "Listening on " + config.get('IP') + ", port " + config.get('PORT') )
})

Adding cartridges to existing applications

This command should make mongodb services avilable to your application if run from within your local application source folder

rhc cartridge add mongodb-2.2

generating load

For scalable applications, generate load from the command line to see automatic scaling in action:

while true ; do ab -n 10000 -c 400 https://nodepdxbot-rjdemo.rhcloud.com/ ; done

Run the above command from within an openshift gear to bypass wifi issues.

Connect to one of your OpenShift apps from the command line:

rhc ssh APP_NAME

Hacking from last night

Cluster sibling awareness now available in Nodejs:

https://github.com/ryanj/openshift.npm

Come hang out with us:

#openshift on freenode irc

More information about scaling with OpenShift:

scale up

https://www.openshift.com/blogs/step-by-step-nodejs-guide-for-realtime-monitoring-and-scaling


Thanks for following along!   --ryanj

Signup with Code - NODEPDX