Saturday, February 26, 2011

Ubunt+Node+cluster on Amazon EC2 Quickstart

Just Show Me The Scripts


If you're just looking for a script to setup nodejs under ubuntu quickly, check out this set of scripts.

Walk Through


First you'll need to Signup for AWS/EC2 and generate a key pair. Google it, there should be plenty of tutorials.

Next, create an instance using the AWS Management Console and log into it using your key pair. You can find the AMI's here. I picked a 32bit 10.04 LTS AMI in US-East.

Install node's perquisites (or those that aren't the default ubuntu LTS ami):
  sudo apt-get update
sudo apt-get -y install build-essential libssl-dev

Now let's create a nodejs system user:
  sudo adduser --home /home/nodejs --shell /bin/bash --disabled-password --disabled-login --system nodejs

Now login as that user:
  sudo su nodejs

For the heavy lifting we use the excllent 'nvm' from creationix
  git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh

Install nodejs:
  nvm sync
time nvm install latest
nvm use latest

Setup the nodejs user's enviroment to have that latest nodejs in it's path
  echo -e -n "\n. ~/.nvm/nvm.sh\n" >> ~/.bashrc
echo -e -n "\nnvm use latest\n" >> ~/.bashrc

Setup npm, this makes life much easier:
  mkdir ~/git
cd ~/git
git clone https://github.com/isaacs/npm.git
cd npm/
make install

Taking Full Advantage Multiple Cores/CPUs


Assuming you want to take full advantage ec2's multiple cores then install 'cluster' - https://github.com/LearnBoost/cluster
  npm install cluster

Setup hello world example:
   mkdir ~/git/sandbox
cd ~/git/sandbox

Edit cluster.js to contain cluster example:
      var cluster = require('cluster')
, http = require('http');

var server = http.createServer(function(req, res){
console.log('%s %s', req.method, req.url);
var body = 'Hello World';
res.writeHead(200, { 'Content-Length': body.length });
res.end(body);
});

cluster(server)
.use(cluster.logger('logs'))
.use(cluster.stats())
.use(cluster.pidfiles('pids'))
.use(cluster.cli())
.use(cluster.repl(8888))
.listen(3000);

Test it!
  node cluster.js

Using the domain you ssh'd into earlier, browse to http://:3000, you should see hello world.

From the EC2 instance, login into the cluster admin interface:
  telnet 0 8888
Type help()

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home