jsconf asia pm2

Post on 15-Apr-2017

455 Views

Category:

Engineering

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

My app is ready, now what?

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?• …. …. …. ….

Production application toolboxwith a Built-in load balancer

that Guarantees Uptimeand help you manage your processes

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

App #1

App #3 App #4

UsabilityCPU

Memory Log

DeploymentModules

CL1 CL2

CL3 CL4

Ressources management

and optimization Easier

software management

Extend capabilities

PM2: Some figures

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

100+ contributorsThank you all!!

Installation

Installing PM2

$ npm install pm2 -g

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

Documentation:http://pm2.keymetrics.io

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/

Launching apps

Launching a Node.js application

$ pm2 start app.js

Put application in backgroundHandle logs

MonitorAuto respawn application

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/

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/

Cluster mode

Most Node.js application…

Why using the cluster mode?

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

Starting an application cluster mode

$ pm2 start app.js -i <instances>

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

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/

Inspecting applications

$ pm2 list

Listing processes

$ pm2 monit

Monitoring CPU & Memory

$ pm2 show <app-name|id>

More informations

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/

Organizing applications

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

UGLY

Solution: JSON declaration

Application 1 (API)

Application 2 (Worker)

Application 3 (Crawler)ecos

yste

m.js

on

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/

Deployment system

Extending the previous JSONec

osys

tem

.json

Production env

Staging env

Multi host

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/

Modules

What is a module?

An app that extend PM2 capabilitiesor

A worker process providing an APIor

A totally independant software

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/

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/

Some modules

Module #1: pm2-logrotate

$ pm2 install pm2-logrotate

Automatically rotate all logs of applicationsmanaged by pm2

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

Module #2: pm2-webshell

$ pm2 install pm2-webshell

Expose a fully capable terminal in your browser

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

Module #2: pm2-webshell

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

Module #x: pm2-technology

Module #x: pm2-technology

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/

Thank you!

alexandre@keymetrics.iogithub.com/Unitech

@strzel_a

top related