PM2
PM2는 node.js로 만들어진 앱에 대한 프로세스 관리 도구이다. 서버 인스턴스들에 대한 로드 밸런싱과 node.js의 스케일 업 / 스케일 다운을 돕는다. 그리고 프로세스들이 계속해서 실행할 수 있는 환경을 제공한다.
PM2 명령어
설치
PM2는 NPM 또는 Yarn으로 설치가 가능하다.
1
2
3
$ npm install pm2@latest -g
#or
$ yarn global add pm2
앱 시작
1
$ pm2 start app.js
CLI에 전달할 수 있는 옵션은 다음과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Specify an app name
--name <app_name>
# Watch and Restart app when files change
--watch
# Set memory threshold for app reload
--max-memory-restart <200MB>
# Specify log file
--log <log_path>
# Pass extra arguments to the script
-- arg1 arg2 arg3
# Delay between automatic restarts
--restart-delay <delay in ms>
# Prefix logs with time
--time
# Do not auto restart app
--no-autorestart
# Specify cron for forced restart
--cron <cron_pattern>
# Attach to application log
--no-daemon
프로세스 관리
1
2
3
4
$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name
app_name 대신 ‘all’ 모든 프로세스에 대한 조치 / ‘id’ 특정 프로세스 ID에 대한 조치를 할 수 있다.
상태, 로그, 측정항목의 확인
PM2로 관리되는 앱 리스트 확인하기
1
$ pm2 [list|ls|status]
PM2로 로그 확인하기
1
2
$ pm2 logs
$ pm2 logs --lines 200
PM2 대쉬보드 모니터 실행
1
2
$ pm2 monit
$ pm2 plus # 웹 기반 대쉬보드
PM2 Cluster Mode
Cluster Mode는 코드 수정없이 Node.js 앱을 사용 가능한 모든 CPU에 확장할 수 있다. Cluster Mode는 사용 가능한 CPU 수에 따라 앱의 성능과 안정성을 향상시킬 수 있다. Cluster Mode 사용법은 아래와 같다.
1
$ pm2 start app.js -i max
max 옵션은 PM2가 자동으로 사용가능한 CPU를 감지하여 가능한 프로세스를 모두 사용하는 것을 의미한다. 또는 JS Configuration File을 통해서 Cluster Mode를 사용할 수 있다.
1
2
3
4
5
6
7
module.exports = {
apps: [{
script: "api.js",
instances: "max",
exec_mode: "cluster"
}]
}
Config File을 사용할 경우 PM2가 각 인스턴스 사이에서 로드 밸런싱을 원한다는 것을 알리기 위해 Cluster Mode를 설정해줘야 한다. 그리고 Config File을 통해서 프로세스를 실행할 수 있다.
1
$ pm2 start processes.json