NodeConf EU Workshop
James M Snell <jasnell@us.ibm.com> <jasnell@gmail.com>
A Node.js Developer's Guide to
Bluemix
Getting Started
James M Snell
@jasnell on Github and Twitter
IBM Technical Lead for Node.js
TSC Member
3
http://bluemix.net/
Sign up for Free Trial Account:
http://ibm.co/1M05AMX
(go ahead, it only takes a couple minutes)
4
Node.js v0.12.7 or higher
Bluemix supports Node.js v0.12.7 by default
(If you want to use Node v4.0.0 locally, go for it.)
5
6
$ npm install –g bluemix-workshop
$ bluemix-workshop
(This is a nodeschool style workshopper that runs with a minimal UI.)
7
What is Bluemix?
The Five Minute or Less Introduction
Bluemix is IBM's Platform-as-a-
Service Offering.
It's built on top of Cloud Foundry.
It also uses Docker and OpenStack
(but we're not covering those today)
9
With Bluemix, you write
applications and deploy ("push")
them to the server.
Bluemix manages the details
– Provisioning VMs
– Setting up the Containers
– Routing,
– Load-Balancing,
– Scaling,
– Configuring Services, etc
10
11
12
13
Bluemix can run in a Shared or Dedicated Environment
14
Services Services Services Services
"Buildpack" – a collection of scripts
that prepare code for execution in
Bluemix.
…tells Bluemix how to set up the
VM, Container and Runtime for
your application to run.
For Node.js applications deployed
to Bluemix, the default Buildpack is
"sdk-for-nodejs"
15
Install the CLI
You will interact with Bluemix using
both the browser-based UI and the
Cloud Foundry cf command line
client.
17
Visit:
https://github.com/cloudfoundry/cli/releases
Once installed, cf will be added to your PATH. You may need
to restart your terminal session to pick up the change.
$ cf -version
Setting the API Endpoint
Once the cf command line tool is
installed, you have to tell it about
the Bluemix endpoint.
19
First, what region are you in?
US South (`us-south`):
https://api.ng.bluemix.net
Europe United Kingdom (`eu-gb`):
https://api.eu-gb.bluemix.net ✔
Once the cf command line tool is
installed, you have to tell it about
the Bluemix endpoint.
20
Second, set the API endpoint and Login
$ cf api https://api.eu-gb.bluemix.net
$ cf login
Enter your Bluemix User ID and Password to authenticate
A Simple App
22
$ mkdir helloworld && cd helloworld && npm init
…
name: (helloworld)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
…
Is this ok? (yes)
Remember what you choose here
23
$ vi index.js
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,
{'Content-Type': 'text/plain'});
res.end('Hello World');
});
server.listen(8888, function() {
console.log('The server is running');
});
24
$ vi package.json
{
"name": "helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index"
},
"author": "",
"license": "ISC"
}
Add this part!
25
$ npm start
> helloworld@1.0.0 start /Users/james/tmp/helloworld
> node index
The server is running
Adding Bluemix Support
27
$ vi index.js
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,
{'Content-Type': 'text/plain'});
res.end('Hello World');
});
server.listen(
process.env.VCAP_APP_PORT || 8888,
function() {
console.log('The server is running');
}
);
28
$ vi manifest.yml
---
applications:
- name: {yourappname}
mem: 128M
instances: 1
path: .
host: {yourappname}
Pick something unique for
the application name
http://{yourappname}.mybluemix.net
29
$ vi .cfignore
node_modules
$ ls –a
.cfignore
index.js
package.json
manifest.yml
This tells cf not to upload your
local node_modules, Bluemix will
install them for you.
30
$ cf push
31
http://{yourappname}.mybluemix.net
32
http://snellworkshop.mybluemix.net
Provisioning and Binding
Services
Services are additional managed
capabilities that can be added to an
application.
34
$ cf marketplace
$ cf marketplace –s rediscloud
$ cf create-service rediscloud 30mb myrediscloud
Creates an instance of the rediscloud
service within your account.
Services are additional managed
capabilities that can be added to an
application.
35
$ cf bind-service {yourappname} rediscloud
$ npm install --save redis
Attach the myrediscloud service instance to your application
Add the redis client module to your application.
36
$ vi index.js
var http = require('http');
var redis = require('redis');
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service =
JSON.parse(vcap_services)["rediscloud"][0];
var credentials =
rediscloud_service.credentials;
var client = redis.createClient(
credentials.port,
credentials.hostname,
{no_ready_check: true});
client.auth(credentials.password);
client.on('connect', function() {
console.log('Connected to Redis');
});
var server = http.createServer(function(req,res) {
client.get('last-visit', function(err, reply) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.set('last-visit', new Date().toISOString());
res.write('Last Visit: ' + reply + 'n');
res.end('Hello World');
});
});
server.listen(process.env.VCAP_APP_PORT || 8888, function() {
console.log('The server is running');
});
37
$ vi index.js
var http = require('http');
var redis = require('redis');
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service =
JSON.parse(vcap_services)["rediscloud"][0];
var credentials =
rediscloud_service.credentials;
var client = redis.createClient(
credentials.port,
credentials.hostname,
{no_ready_check: true});
client.auth(credentials.password);
client.on('connect', function() {
console.log('Connected to Redis');
});
var server = http.createServer(function(req,res) {
client.get('last-visit', function(err, reply) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.set('last-visit', new Date().toISOString());
res.write('Last Visit: ' + reply + 'n');
res.end('Hello World');
});
});
server.listen(process.env.VCAP_APP_PORT || 8888, function() {
console.log('The server is running');
});
38
$ vi index.js
var http = require('http');
var redis = require('redis');
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service =
JSON.parse(vcap_services)["rediscloud"][0];
var credentials =
rediscloud_service.credentials;
var client = redis.createClient(
credentials.port,
credentials.hostname,
{no_ready_check: true});
client.auth(credentials.password);
client.on('connect', function() {
console.log('Connected to Redis');
});
var server = http.createServer(function(req,res) {
client.get('last-visit', function(err, reply) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.set('last-visit', new Date().toISOString());
res.write('Last Visit: ' + reply + 'n');
res.end('Hello World');
});
});
server.listen(process.env.VCAP_APP_PORT || 8888, function() {
console.log('The server is running');
});
39
$ vi index.js
var http = require('http');
var redis = require('redis');
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service =
JSON.parse(vcap_services)["rediscloud"][0];
var credentials =
rediscloud_service.credentials;
var client = redis.createClient(
credentials.port,
credentials.hostname,
{no_ready_check: true});
client.auth(credentials.password);
client.on('connect', function() {
console.log('Connected to Redis');
});
var server = http.createServer(function(req,res) {
client.get('last-visit', function(err, reply) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.set('last-visit', new Date().toISOString());
res.write('Last Visit: ' + reply + 'n');
res.end('Hello World');
});
});
server.listen(process.env.VCAP_APP_PORT || 8888, function() {
console.log('The server is running');
});
40
$ vi index.js
var http = require('http');
var redis = require('redis');
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service =
JSON.parse(vcap_services)["rediscloud"][0];
var credentials =
rediscloud_service.credentials;
var client = redis.createClient(
credentials.port,
credentials.hostname,
{no_ready_check: true});
client.auth(credentials.password);
client.on('connect', function() {
console.log('Connected to Redis');
});
var server = http.createServer(function(req,res) {
client.get('last-visit', function(err, reply) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.set('last-visit', new Date().toISOString());
res.write('Last Visit: ' + reply + 'n');
res.end('Hello World');
});
});
server.listen(process.env.VCAP_APP_PORT || 8888, function() {
console.log('The server is running');
});
41
$ cf push
The Bluemix Environment
Bluemix uses environment
variables to configure your
application
43
VCAP_APP_PORT - The TCP Port
VCAP_APP_HOST - The Hostname
VCAP_SERVICES - *JSON* File with
Service Details
VCAP_APPLICATION - *JSON* file with
Application details
Bluemix uses environment
variables to configure your
application
44
VCAP_SERVICES:
{
"rediscloud": {
"credentials": { ... },
"label": "rediscloud",
"name": "myrediscloud",
"plan": "30mb",
...
}
}
Bluemix uses environment
variables to configure your
application
45
VCAP_APPLICATION:
{
"application_name": "snellworkshop",
"application_urls": [
"snellworkshop.mybluemix.net"
],
...
}
Bluemix uses environment
variables to configure your
application
46
$ cf set-env {myappname} MYENVVAR value
$ cf env {myappname}
$ cf unset-env {myappname} MYENVVAR
Enabling Application
Management
The Application Management Tools
are additional utilities for debugging
and managing applications
48
$ cf set-env {yourappname} BLUEMIX_APP_MGMT_ENABLE
proxy+devconsole+shell+inspector+trace
$ cf restage {yourappname}
Enables:
• a terminal shell for command line
access to your application's container
• a node-inspector instance
• debug tracing using log4js, bunyan, etc
http://{yourappname}.mybluemix.net/bluemix-
debug/manage/
49
http://{yourappname}.mybluemix.net/bluemix-
debug/manage/
50
http://{yourappname}.mybluemix.net/bluemix-
debug/manage/
51
You can enable tracing using log4js
or bunyan
52
$ npm install --save log4js@0.6.22
$ vi index.js
...
var log4js = require('log4js');
log4js.loadAppender('console');
var logger = log4js.getLogger('app');
logger.setLevel('ERROR');
...
logger.info('running...');
...
Set the trace level from within the Bluemix
Dashboard…
53
54
Set the trace level from within the Bluemix
Dashboard…
Use cf logs to view the trace output
55
$ cf logs {yourappname}
$ cf logs --recent {yourappname}
Tails the logs to the console
Prints the most recent logs
Using an alternative buildpack
(or, how to use newer versions of Node.js)
The default Node.js Buildpack uses
Node.js v0.12.7, which is good.
But I want to use the new stuff.
57
The IBM Node.js Buildpack is based on the open source
Cloud Foundry Node.js Buildpack. Nearly all Cloud Foundry
Buildpacks can work on Bluemix out of the box.
58
The Heroku Node.js Buildpack:
https://github.com/heroku/heroku-buildpack-nodejs.git
Includes support for the latest Node.js/io.js Binaries
You specify the alternate buildpack in the
manifest.yml
59
$ vi manifest.yml
---
applications:
- name: {yourappname}
mem: 128M
instances: 1
path: .
host: {yourappname}
buildpack: https://github.com/heroku/heroku-buildpack-
nodejs.git
command: node app
And tell package.json which version of
the Node runtime to use
60
$ vi package.json
{
"name": "helloworld",
"version": "1.0.0",
"main": "./app.js",
"scripts": {
"start": "node app"
},
...,
"engines": {
"iojs" : "*"
}
}
61
$ cf push
User-Provided Services
Bluemix supports User-Provided
Services.
63
User Provided Services are essentially service configurations
you provide. They are kept separate from your application so
that they can be shared and managed just like the services
provided by Bluemix itself.
User Provided Services provide an excellent way of providing
runtime configuration setting to your applications.
For instance, suppose we need to
configure a secret for our
applications:
64
$ cf cups session-secret -p secret
secret> this is the secret
Creating user provided service session-secret in org jasnell / space
dev as jasnell...
$ cf bind-service {yourappname} session-secret
$ cf restage
65
User-Provide Service properties are included in
the VCAP_SERVICES environment variable, just
like any other service.
Using a Custom Domain Name
Bluemix supports Custom Domain
Names
67
By default, Bluemix will create a URL for your application
automatically.
This usually follows the pattern:
http://{yourappname}.mybluemix.net
But what if you want to use your own domain name?
A few command line calls is all you
need.
68
$ cf create-domain myorg example.org
$ cf map-route {yourappname} example.org --n foo
Then create a CNAME DNS record for your sub-domain:
foo.example.org CNAME {yourappname.mybluemix.net}
Once the DNS records propagate, your application will be available at:
http://foo.example.org
You can upload your SSL/TLS
certificates via the dashboard:
69
No-downtime deployment
Updating an application on Bluemix
will cause the application to restart,
forcing a small downtime period.
You can avoid it.
71
$ cf rename {yourappname} {youappname}-old
$ cf push
$ cf delete {yourappname}-old
Resources:
• Bluemix Documentation -
https://www.ng.bluemix.net/docs
• developerWorks -
http://www.ibm.com/developerworks/cloud/bluemix
/
Q&A
Thank you
James M Snell
jasnell@us.ibm.com
jasnell@gmail.com
twitter/github @jasnell

More Related Content

PPTX
Bluemix Technical Overview
PPT
IBM Bluemix cloudfoundry platform
PPTX
Out of the Blue: Getting started with IBM Bluemix development
PPTX
Microservices and IBM Bluemix meetup presentation
PDF
Docker, Cloud Foundry, Bosh & Bluemix
 
PDF
IBM Bluemix hands on
PDF
IBM Codename: Bluemix - Cloudfoundry, PaaS development and deployment trainin...
PDF
IBM Containers- Bluemix
Bluemix Technical Overview
IBM Bluemix cloudfoundry platform
Out of the Blue: Getting started with IBM Bluemix development
Microservices and IBM Bluemix meetup presentation
Docker, Cloud Foundry, Bosh & Bluemix
 
IBM Bluemix hands on
IBM Codename: Bluemix - Cloudfoundry, PaaS development and deployment trainin...
IBM Containers- Bluemix

What's hot (20)

PPT
Bluemix and DevOps workshop lab
PPT
Developing for Hybrid Cloud with Bluemix
PDF
100 blue mix days technical training
PDF
IBM Bluemix Workshop version 3
PPT
IBM Softlayer Bluemix Marketplace
PDF
IBM Bluemix
PDF
Hybrid Cloud with IBM Bluemix, Docker and Open Stack
PDF
IBM BlueMix Architecture and Deep Dive (Powered by CloudFoundry)
PPTX
Automated Lifecycle Management - CloudFoundry on OpenStack
PPT
Bluemix the digital innovation platform
PDF
Blue mix
PPTX
An introduction to IBM BlueMix
PDF
ETS Summer School - Introduction to Bluemix (July 4th)
PDF
Hybrid Cloud: How to Get a Return from an Investment Made Three Decades Ago (...
PDF
IBM Bluemix Dedicated – GitHub Enterprise
PDF
Elevating your Continuous Delivery Strategy Above the Rolling Clouds
PDF
Understanding Docker and IBM Bluemix Container Service
PDF
Get over the Cloud with Bluemix
PPTX
Bluemix overview v1.4
PDF
Turning up the HEAT with IBM MobileFirst for iOS Apps
Bluemix and DevOps workshop lab
Developing for Hybrid Cloud with Bluemix
100 blue mix days technical training
IBM Bluemix Workshop version 3
IBM Softlayer Bluemix Marketplace
IBM Bluemix
Hybrid Cloud with IBM Bluemix, Docker and Open Stack
IBM BlueMix Architecture and Deep Dive (Powered by CloudFoundry)
Automated Lifecycle Management - CloudFoundry on OpenStack
Bluemix the digital innovation platform
Blue mix
An introduction to IBM BlueMix
ETS Summer School - Introduction to Bluemix (July 4th)
Hybrid Cloud: How to Get a Return from an Investment Made Three Decades Ago (...
IBM Bluemix Dedicated – GitHub Enterprise
Elevating your Continuous Delivery Strategy Above the Rolling Clouds
Understanding Docker and IBM Bluemix Container Service
Get over the Cloud with Bluemix
Bluemix overview v1.4
Turning up the HEAT with IBM MobileFirst for iOS Apps
Ad

Viewers also liked (17)

PPTX
Give Your Java Apps “The Boot” With Spring Boot And Cloud Foundry
PPTX
Building Highly Scalable Apps On Bluemix
PPTX
Bluemix - Deploying a Java Web Application
PDF
IBM Bluemix Introdution for Hackathons
PDF
Twitter analytics in Bluemix
PPTX
Think Small To Go Big - Introduction To Microservices
PPTX
IAB3948 Wiring the internet of things with Node-RED
PDF
BigData processing in the cloud – Guest Lecture - University of Applied Scien...
PDF
An Overview of IBM Streaming Analytics for Bluemix
PDF
Quickly build and deploy a scalable OpenStack Swift application using IBM Blu...
PDF
デモで理解する!Bluemixモバイル・サービス
PDF
Flow based programming an overview
PDF
Using bluemix predictive analytics service in Node-RED
PDF
Deployment Automation for Hybrid Cloud and Multi-Platform Environments
PPTX
Big Data Analytics 3: Machine Learning to Engage the Customer, with Apache Sp...
PPTX
Migrating Java EE applications to IBM Bluemix Platform-as-a-Service
PDF
Flow Base Programming with Node-RED and Functional Reactive Programming with ...
Give Your Java Apps “The Boot” With Spring Boot And Cloud Foundry
Building Highly Scalable Apps On Bluemix
Bluemix - Deploying a Java Web Application
IBM Bluemix Introdution for Hackathons
Twitter analytics in Bluemix
Think Small To Go Big - Introduction To Microservices
IAB3948 Wiring the internet of things with Node-RED
BigData processing in the cloud – Guest Lecture - University of Applied Scien...
An Overview of IBM Streaming Analytics for Bluemix
Quickly build and deploy a scalable OpenStack Swift application using IBM Blu...
デモで理解する!Bluemixモバイル・サービス
Flow based programming an overview
Using bluemix predictive analytics service in Node-RED
Deployment Automation for Hybrid Cloud and Multi-Platform Environments
Big Data Analytics 3: Machine Learning to Engage the Customer, with Apache Sp...
Migrating Java EE applications to IBM Bluemix Platform-as-a-Service
Flow Base Programming with Node-RED and Functional Reactive Programming with ...
Ad

Similar to A Node.js Developer's Guide to Bluemix (20)

PPTX
Building and Scaling Node.js Applications
PDF
Getting Started with Cloud Foundry on Bluemix
PDF
Getting Started with Cloud Foundry on Bluemix
PDF
Getting Started with Cloud Foundry on Bluemix
PPTX
Developer Nirvana with IBM Bluemix™
 
PDF
Cloud Foundry Open Tour China (english)
PPTX
Achieving Developer Nirvana With Codename: BlueMix
PDF
Migrating a Monolithic App to Microservices on Cloud Foundry
PDF
Introduction to Node.js
KEY
Writing robust Node.js applications
PDF
AMS Node Meetup December presentation Phusion Passenger
PDF
Node Interactive: Node.js Performance and Highly Scalable Micro-Services
PDF
Running node.js as a service behind nginx/varnish
PDF
Running node.js as a service behind nginx/varnish
PDF
Webconf nodejs-production-architecture
PPTX
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
PPTX
Scalable apps
 
PPTX
Containers #101 Meetup: Building a micro-service using Node.js and Docker - P...
PPTX
Introduction to Microservices and Cloud Native Application Architecture
PDF
Node.js - async for the rest of us.
Building and Scaling Node.js Applications
Getting Started with Cloud Foundry on Bluemix
Getting Started with Cloud Foundry on Bluemix
Getting Started with Cloud Foundry on Bluemix
Developer Nirvana with IBM Bluemix™
 
Cloud Foundry Open Tour China (english)
Achieving Developer Nirvana With Codename: BlueMix
Migrating a Monolithic App to Microservices on Cloud Foundry
Introduction to Node.js
Writing robust Node.js applications
AMS Node Meetup December presentation Phusion Passenger
Node Interactive: Node.js Performance and Highly Scalable Micro-Services
Running node.js as a service behind nginx/varnish
Running node.js as a service behind nginx/varnish
Webconf nodejs-production-architecture
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
Scalable apps
 
Containers #101 Meetup: Building a micro-service using Node.js and Docker - P...
Introduction to Microservices and Cloud Native Application Architecture
Node.js - async for the rest of us.

More from ibmwebspheresoftware (6)

PDF
JavaOne 2015 Keynote Presentation
PPT
NodeConf EU 2015 Keynote
PDF
A Taste of Monitoring and Post Mortem Debugging with Node
PDF
Node on Guard!
PDF
Don't Miss a Thing at IBM InterConnect 2015
PDF
IBM WebSphere Application Foundation Sessions at IBM InterConnect 2015
JavaOne 2015 Keynote Presentation
NodeConf EU 2015 Keynote
A Taste of Monitoring and Post Mortem Debugging with Node
Node on Guard!
Don't Miss a Thing at IBM InterConnect 2015
IBM WebSphere Application Foundation Sessions at IBM InterConnect 2015

Recently uploaded (20)

PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
substrate PowerPoint Presentation basic one
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PPTX
Internet of Everything -Basic concepts details
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
Auditboard EB SOX Playbook 2023 edition.
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
SGT Report The Beast Plan and Cyberphysical Systems of Control
Lung cancer patients survival prediction using outlier detection and optimize...
Presentation - Principles of Instructional Design.pptx
Rapid Prototyping: A lecture on prototyping techniques for interface design
Module 1 Introduction to Web Programming .pptx
CEH Module 2 Footprinting CEH V13, concepts
4 layer Arch & Reference Arch of IoT.pdf
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
substrate PowerPoint Presentation basic one
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Co-training pseudo-labeling for text classification with support vector machi...
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
NewMind AI Weekly Chronicles – August ’25 Week IV
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
Internet of Everything -Basic concepts details
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
Auditboard EB SOX Playbook 2023 edition.
A symptom-driven medical diagnosis support model based on machine learning te...
Electrocardiogram sequences data analytics and classification using unsupervi...

A Node.js Developer's Guide to Bluemix

  • 1. NodeConf EU Workshop James M Snell <[email protected]> <[email protected]> A Node.js Developer's Guide to Bluemix
  • 3. James M Snell @jasnell on Github and Twitter IBM Technical Lead for Node.js TSC Member 3
  • 4. http://bluemix.net/ Sign up for Free Trial Account: http://ibm.co/1M05AMX (go ahead, it only takes a couple minutes) 4
  • 5. Node.js v0.12.7 or higher Bluemix supports Node.js v0.12.7 by default (If you want to use Node v4.0.0 locally, go for it.) 5
  • 6. 6 $ npm install –g bluemix-workshop $ bluemix-workshop (This is a nodeschool style workshopper that runs with a minimal UI.)
  • 7. 7
  • 8. What is Bluemix? The Five Minute or Less Introduction
  • 9. Bluemix is IBM's Platform-as-a- Service Offering. It's built on top of Cloud Foundry. It also uses Docker and OpenStack (but we're not covering those today) 9
  • 10. With Bluemix, you write applications and deploy ("push") them to the server. Bluemix manages the details – Provisioning VMs – Setting up the Containers – Routing, – Load-Balancing, – Scaling, – Configuring Services, etc 10
  • 11. 11
  • 12. 12
  • 13. 13 Bluemix can run in a Shared or Dedicated Environment
  • 15. "Buildpack" – a collection of scripts that prepare code for execution in Bluemix. …tells Bluemix how to set up the VM, Container and Runtime for your application to run. For Node.js applications deployed to Bluemix, the default Buildpack is "sdk-for-nodejs" 15
  • 17. You will interact with Bluemix using both the browser-based UI and the Cloud Foundry cf command line client. 17 Visit: https://github.com/cloudfoundry/cli/releases Once installed, cf will be added to your PATH. You may need to restart your terminal session to pick up the change. $ cf -version
  • 18. Setting the API Endpoint
  • 19. Once the cf command line tool is installed, you have to tell it about the Bluemix endpoint. 19 First, what region are you in? US South (`us-south`): https://api.ng.bluemix.net Europe United Kingdom (`eu-gb`): https://api.eu-gb.bluemix.net ✔
  • 20. Once the cf command line tool is installed, you have to tell it about the Bluemix endpoint. 20 Second, set the API endpoint and Login $ cf api https://api.eu-gb.bluemix.net $ cf login Enter your Bluemix User ID and Password to authenticate
  • 22. 22 $ mkdir helloworld && cd helloworld && npm init … name: (helloworld) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC) … Is this ok? (yes) Remember what you choose here
  • 23. 23 $ vi index.js var http = require('http'); var server = http.createServer(function(req,res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }); server.listen(8888, function() { console.log('The server is running'); });
  • 24. 24 $ vi package.json { "name": "helloworld", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node index" }, "author": "", "license": "ISC" } Add this part!
  • 25. 25 $ npm start > [email protected] start /Users/james/tmp/helloworld > node index The server is running
  • 27. 27 $ vi index.js var http = require('http'); var server = http.createServer(function(req,res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }); server.listen( process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); } );
  • 28. 28 $ vi manifest.yml --- applications: - name: {yourappname} mem: 128M instances: 1 path: . host: {yourappname} Pick something unique for the application name http://{yourappname}.mybluemix.net
  • 29. 29 $ vi .cfignore node_modules $ ls –a .cfignore index.js package.json manifest.yml This tells cf not to upload your local node_modules, Bluemix will install them for you.
  • 34. Services are additional managed capabilities that can be added to an application. 34 $ cf marketplace $ cf marketplace –s rediscloud $ cf create-service rediscloud 30mb myrediscloud Creates an instance of the rediscloud service within your account.
  • 35. Services are additional managed capabilities that can be added to an application. 35 $ cf bind-service {yourappname} rediscloud $ npm install --save redis Attach the myrediscloud service instance to your application Add the redis client module to your application.
  • 36. 36 $ vi index.js var http = require('http'); var redis = require('redis'); var vcap_services = process.env.VCAP_SERVICES; var rediscloud_service = JSON.parse(vcap_services)["rediscloud"][0]; var credentials = rediscloud_service.credentials; var client = redis.createClient( credentials.port, credentials.hostname, {no_ready_check: true}); client.auth(credentials.password); client.on('connect', function() { console.log('Connected to Redis'); }); var server = http.createServer(function(req,res) { client.get('last-visit', function(err, reply) { res.writeHead(200, {'Content-Type': 'text/plain'}); client.set('last-visit', new Date().toISOString()); res.write('Last Visit: ' + reply + 'n'); res.end('Hello World'); }); }); server.listen(process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); });
  • 37. 37 $ vi index.js var http = require('http'); var redis = require('redis'); var vcap_services = process.env.VCAP_SERVICES; var rediscloud_service = JSON.parse(vcap_services)["rediscloud"][0]; var credentials = rediscloud_service.credentials; var client = redis.createClient( credentials.port, credentials.hostname, {no_ready_check: true}); client.auth(credentials.password); client.on('connect', function() { console.log('Connected to Redis'); }); var server = http.createServer(function(req,res) { client.get('last-visit', function(err, reply) { res.writeHead(200, {'Content-Type': 'text/plain'}); client.set('last-visit', new Date().toISOString()); res.write('Last Visit: ' + reply + 'n'); res.end('Hello World'); }); }); server.listen(process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); });
  • 38. 38 $ vi index.js var http = require('http'); var redis = require('redis'); var vcap_services = process.env.VCAP_SERVICES; var rediscloud_service = JSON.parse(vcap_services)["rediscloud"][0]; var credentials = rediscloud_service.credentials; var client = redis.createClient( credentials.port, credentials.hostname, {no_ready_check: true}); client.auth(credentials.password); client.on('connect', function() { console.log('Connected to Redis'); }); var server = http.createServer(function(req,res) { client.get('last-visit', function(err, reply) { res.writeHead(200, {'Content-Type': 'text/plain'}); client.set('last-visit', new Date().toISOString()); res.write('Last Visit: ' + reply + 'n'); res.end('Hello World'); }); }); server.listen(process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); });
  • 39. 39 $ vi index.js var http = require('http'); var redis = require('redis'); var vcap_services = process.env.VCAP_SERVICES; var rediscloud_service = JSON.parse(vcap_services)["rediscloud"][0]; var credentials = rediscloud_service.credentials; var client = redis.createClient( credentials.port, credentials.hostname, {no_ready_check: true}); client.auth(credentials.password); client.on('connect', function() { console.log('Connected to Redis'); }); var server = http.createServer(function(req,res) { client.get('last-visit', function(err, reply) { res.writeHead(200, {'Content-Type': 'text/plain'}); client.set('last-visit', new Date().toISOString()); res.write('Last Visit: ' + reply + 'n'); res.end('Hello World'); }); }); server.listen(process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); });
  • 40. 40 $ vi index.js var http = require('http'); var redis = require('redis'); var vcap_services = process.env.VCAP_SERVICES; var rediscloud_service = JSON.parse(vcap_services)["rediscloud"][0]; var credentials = rediscloud_service.credentials; var client = redis.createClient( credentials.port, credentials.hostname, {no_ready_check: true}); client.auth(credentials.password); client.on('connect', function() { console.log('Connected to Redis'); }); var server = http.createServer(function(req,res) { client.get('last-visit', function(err, reply) { res.writeHead(200, {'Content-Type': 'text/plain'}); client.set('last-visit', new Date().toISOString()); res.write('Last Visit: ' + reply + 'n'); res.end('Hello World'); }); }); server.listen(process.env.VCAP_APP_PORT || 8888, function() { console.log('The server is running'); });
  • 43. Bluemix uses environment variables to configure your application 43 VCAP_APP_PORT - The TCP Port VCAP_APP_HOST - The Hostname VCAP_SERVICES - *JSON* File with Service Details VCAP_APPLICATION - *JSON* file with Application details
  • 44. Bluemix uses environment variables to configure your application 44 VCAP_SERVICES: { "rediscloud": { "credentials": { ... }, "label": "rediscloud", "name": "myrediscloud", "plan": "30mb", ... } }
  • 45. Bluemix uses environment variables to configure your application 45 VCAP_APPLICATION: { "application_name": "snellworkshop", "application_urls": [ "snellworkshop.mybluemix.net" ], ... }
  • 46. Bluemix uses environment variables to configure your application 46 $ cf set-env {myappname} MYENVVAR value $ cf env {myappname} $ cf unset-env {myappname} MYENVVAR
  • 48. The Application Management Tools are additional utilities for debugging and managing applications 48 $ cf set-env {yourappname} BLUEMIX_APP_MGMT_ENABLE proxy+devconsole+shell+inspector+trace $ cf restage {yourappname} Enables: • a terminal shell for command line access to your application's container • a node-inspector instance • debug tracing using log4js, bunyan, etc
  • 52. You can enable tracing using log4js or bunyan 52 $ npm install --save [email protected] $ vi index.js ... var log4js = require('log4js'); log4js.loadAppender('console'); var logger = log4js.getLogger('app'); logger.setLevel('ERROR'); ... logger.info('running...'); ...
  • 53. Set the trace level from within the Bluemix Dashboard… 53
  • 54. 54 Set the trace level from within the Bluemix Dashboard…
  • 55. Use cf logs to view the trace output 55 $ cf logs {yourappname} $ cf logs --recent {yourappname} Tails the logs to the console Prints the most recent logs
  • 56. Using an alternative buildpack (or, how to use newer versions of Node.js)
  • 57. The default Node.js Buildpack uses Node.js v0.12.7, which is good. But I want to use the new stuff. 57 The IBM Node.js Buildpack is based on the open source Cloud Foundry Node.js Buildpack. Nearly all Cloud Foundry Buildpacks can work on Bluemix out of the box.
  • 58. 58 The Heroku Node.js Buildpack: https://github.com/heroku/heroku-buildpack-nodejs.git Includes support for the latest Node.js/io.js Binaries
  • 59. You specify the alternate buildpack in the manifest.yml 59 $ vi manifest.yml --- applications: - name: {yourappname} mem: 128M instances: 1 path: . host: {yourappname} buildpack: https://github.com/heroku/heroku-buildpack- nodejs.git command: node app
  • 60. And tell package.json which version of the Node runtime to use 60 $ vi package.json { "name": "helloworld", "version": "1.0.0", "main": "./app.js", "scripts": { "start": "node app" }, ..., "engines": { "iojs" : "*" } }
  • 63. Bluemix supports User-Provided Services. 63 User Provided Services are essentially service configurations you provide. They are kept separate from your application so that they can be shared and managed just like the services provided by Bluemix itself. User Provided Services provide an excellent way of providing runtime configuration setting to your applications.
  • 64. For instance, suppose we need to configure a secret for our applications: 64 $ cf cups session-secret -p secret secret> this is the secret Creating user provided service session-secret in org jasnell / space dev as jasnell... $ cf bind-service {yourappname} session-secret $ cf restage
  • 65. 65 User-Provide Service properties are included in the VCAP_SERVICES environment variable, just like any other service.
  • 66. Using a Custom Domain Name
  • 67. Bluemix supports Custom Domain Names 67 By default, Bluemix will create a URL for your application automatically. This usually follows the pattern: http://{yourappname}.mybluemix.net But what if you want to use your own domain name?
  • 68. A few command line calls is all you need. 68 $ cf create-domain myorg example.org $ cf map-route {yourappname} example.org --n foo Then create a CNAME DNS record for your sub-domain: foo.example.org CNAME {yourappname.mybluemix.net} Once the DNS records propagate, your application will be available at: http://foo.example.org
  • 69. You can upload your SSL/TLS certificates via the dashboard: 69
  • 71. Updating an application on Bluemix will cause the application to restart, forcing a small downtime period. You can avoid it. 71 $ cf rename {yourappname} {youappname}-old $ cf push $ cf delete {yourappname}-old
  • 72. Resources: • Bluemix Documentation - https://www.ng.bluemix.net/docs • developerWorks - http://www.ibm.com/developerworks/cloud/bluemix /
  • 73. Q&A
  • 74. Thank you James M Snell [email protected] [email protected] twitter/github @jasnell