jsconf asia pm2

48

Upload: alexandre-strzelewicz

Post on 15-Apr-2017

465 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Jsconf asia pm2
Page 2: Jsconf asia pm2

• CEO & Founder at • Author of • Full Stack Javascript developer • Technologist

Page 3: Jsconf asia pm2

My app is ready, now what?

Page 4: Jsconf asia pm2

Deploying to production is complex

• Oh noes why my app only use 10% of my server CPUs?• Oh noes which app is leaking memory?• Why the server went out of memory?• Oh noes my app crashed!• Where are my damn logs?• How do I organize my micro service app?• …. …. …. ….

Page 5: Jsconf asia pm2
Page 6: Jsconf asia pm2

Production application toolboxwith a Built-in load balancer

that Guarantees Uptimeand help you manage your processes

Page 7: Jsconf asia pm2

App #1App #2

App #3

App #4

App #1

App #3 App #4

CL1 CL2

CL3 CL4

Disorganized application & workflow

Structured application & workflowTo

Before After

Page 8: Jsconf asia pm2

App #1

App #3 App #4

UsabilityCPU

Memory Log

DeploymentModules

CL1 CL2

CL3 CL4

Ressources management

and optimization Easier

software management

Extend capabilities

Page 9: Jsconf asia pm2

PM2: Some figures

• 10.000+ stars on GitHub • 2.000.000+ downloads • 2500+ commits • 120+ contributors • 750+ tests

Page 10: Jsconf asia pm2

100+ contributorsThank you all!!

Page 11: Jsconf asia pm2
Page 12: Jsconf asia pm2

Installation

Page 13: Jsconf asia pm2

Installing PM2

$ npm install pm2 -g

Source code:https://github.com/Unitech/pm2

Documentation:http://pm2.keymetrics.io

Page 14: Jsconf asia pm2

Setup PM2No setup required! But…

$ pm2 completion install

Enabling autocompletion:

$ pm2 startup

Respawning PM2 on server boot:

http://pm2.keymetrics.io/docs/usage/quick-start/

Page 15: Jsconf asia pm2

Launching apps

Page 16: Jsconf asia pm2

Launching a Node.js application

$ pm2 start app.js

Put application in backgroundHandle logs

MonitorAuto respawn application

Page 17: Jsconf asia pm2

Launching any kind of application

$ pm2 start app.py —interpreter="python3"

$ pm2 start script.sh

Starting a bash script:

Starting a Python app & set interpreter

http://pm2.keymetrics.io/docs/usage/process-management/

Page 18: Jsconf asia pm2

Acting on processes

$ pm2 delete <app-name|app-id|'all'>

$ pm2 restart <app-name|app-id|'all'>

Restart:

Delete:

$ pm2 stop <app-name|app-id|'all'>

Stop:

http://pm2.keymetrics.io/docs/usage/process-management/

Page 19: Jsconf asia pm2

Cluster mode

Page 20: Jsconf asia pm2

Most Node.js application…

Page 21: Jsconf asia pm2

Why using the cluster mode?

Intel® Xeon® Processor E5-1410 v2 - 8 cores

Page 22: Jsconf asia pm2

Starting an application cluster mode

$ pm2 start app.js -i <instances>

http://pm2.keymetrics.io/docs/usage/cluster-mode/

Page 23: Jsconf asia pm2

Managing clustered applications

$ pm2 gracefulReload <app-name|'all'>

$ pm2 reload <app-name|'all'>

Reload without downtime:

Graceful Reload (send signal to process):

$ pm2 scale <app-name> <instances>

Scale up/down process number:

http://pm2.keymetrics.io/docs/usage/cluster-mode/

Page 24: Jsconf asia pm2

Inspecting applications

Page 25: Jsconf asia pm2

$ pm2 list

Listing processes

Page 26: Jsconf asia pm2

$ pm2 monit

Monitoring CPU & Memory

Page 27: Jsconf asia pm2

$ pm2 show <app-name|id>

More informations

Page 28: Jsconf asia pm2

Inspecting logs

$ pm2 flush

$ pm2 logs [app-name]

Display logs in realtime:

Flushing logs:

$ pm2 reloadLogs

Reloading logs (logrotate):

http://pm2.keymetrics.io/docs/usage/log-management/

Page 29: Jsconf asia pm2

Organizing applications

Page 30: Jsconf asia pm2

Starting your micro service app…

$ DEBUG=“*” NODE_ENV="production" pm2 start app-1.js —name “API” -i 8 —watch —max-memory-restart=“300M” — -a 23

$ DEBUG=“app2:*” NODE_ENV=“production" pm2 start app-2.js —name “WORKER" —watch — -a prod

$ DEBUG=“app3:*” NODE_ENV=“production" pm2 start app-3.js —name “BDD" —watch — -a malo

Page 31: Jsconf asia pm2

UGLY

Page 32: Jsconf asia pm2

Solution: JSON declaration

Application 1 (API)

Application 2 (Worker)

Application 3 (Crawler)ecos

yste

m.js

on

Page 33: Jsconf asia pm2

Using the JSON declaration

$ pm2 restart ecosystem.json

$ pm2 start ecosystem.json

Start:

Restart:

$ pm2 stop <app-name|app-id|'all'>

Stop:

http://pm2.keymetrics.io/docs/usage/application-declaration/

Page 34: Jsconf asia pm2

Deployment system

Page 35: Jsconf asia pm2

Extending the previous JSONec

osys

tem

.json

Production env

Staging env

Multi host

Page 36: Jsconf asia pm2

Deployment commands

$ pm2 deploy ecosystem.json production

$ pm2 deploy ecosystem.json production setup

Setup remote server:

Update remote application:

Revert to the n-th deployment:

$ pm2 deploy ecosystem.json production revert [n]

http://pm2.keymetrics.io/docs/usage/deployment/

Page 37: Jsconf asia pm2

Modules

Page 38: Jsconf asia pm2

What is a module?

An app that extend PM2 capabilitiesor

A worker process providing an APIor

A totally independant software

Page 39: Jsconf asia pm2

Installing a module

$ pm2 install <module-name>

It gets the module from NPMthen Start it

and Keep it alive forever

http://docs.keymetrics.io/docs/usage/building-module/

Page 40: Jsconf asia pm2

Managing modules

$ pm2 uninstall <module-name>

$ pm2 install <module-name>

Install a module:

Uninstall:

$ pm2 set <module-name>:<attr> <value>

Set configuration option:

http://docs.keymetrics.io/docs/usage/building-module/

Page 41: Jsconf asia pm2

Some modules

Page 42: Jsconf asia pm2

Module #1: pm2-logrotate

$ pm2 install pm2-logrotate

Automatically rotate all logs of applicationsmanaged by pm2

https://github.com/pm2-hive/pm2-logrotate

Page 43: Jsconf asia pm2

Module #2: pm2-webshell

$ pm2 install pm2-webshell

Expose a fully capable terminal in your browser

https://github.com/pm2-hive/pm2-webshell

Page 44: Jsconf asia pm2

Module #2: pm2-webshell

Termcaps capable Backed by TTY.js HTTPS support Pass/User configurable Port

Page 45: Jsconf asia pm2

Module #x: pm2-technology

Page 46: Jsconf asia pm2

Module #x: pm2-technology

Page 47: Jsconf asia pm2

Writing your own module

$ pm2 module:generate sample-module

Create a sample module:

Documentation: Google -> pm2 module system

Official modules repositories: http://github.com/pm2-hive/