进阶 PM2

PM2 大大简化了 Node 任务操做,除了简单的应用外,咱们还能够作一些有趣的事情。本节咱们来探讨一下 PM2 的平滑启动以及数据监控。php

若是你还不了解 PM2,能够先看看 PM2 初体验,或者查看 PM2 用法简介node

1、PM2 两种启动方式

1.cluster_mode:用 cluster 来作负载均衡,咱们不须要作任何代码的改动。 2.fork_mode:用 fork 模式启动(默认),这能够容许咱们经过改变 exec_interpreter 参数,启动 php 或者 python 服务。python

Node.js 给咱们提供了 cluster 模块,它能够生成多个工做线程来共享同一个 TCP 链接。git

2、实时扩展集群

任什么时候候,若是咱们须要增长工做线程的数量,能够经过 pm2 scale <app name> <n> 来对集群进行扩展。参数 <n> 指定工做线程的数量,被用来增长或减小集群数。github

补充:能够经过 pm2 scale app +3 的方式来指定要增长多少工做线程。web

PM2reload <app name> 功能将依次重启全部的工做线程。每个线程会等待在新的线程建立以后才会被终止掉,所以,当你在产品环境部署新的代码时,Server 会不间断地一直保持运行。shell

3、平滑重启

1.fork 模式npm

{
   "apps" : [{
     "name"         : "pc",
     "script"       : "jartto-server.js",
     "kill_timeout" : 3000,
     "instances": 2,
     "log_date_format": "YY-MM-DD HH:mm:ss Z"
  }]
}
const httpServer = server.listen(port, error => {
   if (error) {
       throw error;
  }
   process.send('ready');
});
process.on('SIGINT', () => {
   httpServer.close(error => {
       process.exit(error ? 1 : 0);
  });
});
复制代码

2.cluster 在集群模式下,有一个默认系统可在应用程序接受链接时将每一个集群设置为就绪。还有一个超时,默认为 3000 毫秒,咱们可使用 ecosystem 文件中的 listen_timeout 属性进行设置。json

生成 ecosystem.config.jsapi

pm2 ecosystem
复制代码

输出日志:

[PM2] Spawning PM2 daemon with pm2_home=/Users/jartto/.pm2
[PM2] PM2 Successfully daemonized
File /Users/jartto/Documents/project/ecosystem.config.js generated
复制代码

简单示例:

module.exports = {
  apps: [{}, {}], // 存放每个进程的配置信息
  deploy: {} // 包含部署配置的对象
}
复制代码

以后,就能够经过 startOrRestart 来启动了:

"scripts": {
    "start": "cross-env PATH_TYPE=test pm2 startOrRestart ecosystem.config.js --only jartto-test --env test",
}
复制代码

关于 appsdeploy 下面咱们来细致聊一聊。

4、apps 部分

上面 ecosystem.config.js 会生成一个简单的模版,为了更好的掌握,咱们来看看更全的一些配置:

module.exports = {
    apps : [{
        name: 'Jartto-test', // 进程名称
        script: './node_modules/nuxt-start/bin/nuxt-start.js', // 启动脚本地址
        args: '-p 8888 -H 0.0.0.0', // 启动的配置
        cwd: 
        instances: 4,
        autorestart: true,
        watch: false,
        max_restarts: 5,
        max_memory_restart: '1G',
        env: {
            NODE_ENV: 'development'
        },
        env_production: {
            NODE_ENV: 'production'
        }
    }],
    deploy : {
        production : {
            user : 'node',
            host : '212.83.163.1',
            ref : 'origin/master',
            repo : 'git@github.com:repo.git',
            path : '/var/www/production',
            post-deploy : 'npm install && pm2 reload ecosystem.config.js --env production'
        }
    }
};
复制代码

参数意义:

  • apps:json 结构,apps 是一个数组,每个数组成员就是对应一个pm2中运行的应用
  • name:应用程序的名称
  • cwd:应用程序所在的目录
  • script:应用程序的脚本路径
  • exec_interpreter:应用程序的脚本类型,这里使用的 shell,默认是 nodejs
  • min_uptime:最小运行时间,这里设置的是 60s 即若是应用程序在 60s 内退出,pm2 会认为程序异常退出,此时触发重启 max_restarts 设置数量
  • max_restarts:设置应用程序异常退出重启的次数,默认 15 次(从0开始计数)
  • exec_mode:应用程序启动模式,这里设置的是 cluster_mode(集群),默认是 fork
  • error_file:自定义应用程序的错误日志文件
  • out_file:自定义应用程序日志文件
  • pid_file:自定义应用程序的 pid 文件
  • watch:是否启用监控模式,默认是 false。若是设置成 true,当应用程序变更时,pm2 会自动重载。这里也能够设置你要监控的文件。

5、deploy 部分

1.生成公钥

ssh-keygen -t rsa
ssh-copy-id jartto@server.com
复制代码

补充说明一下: ssh-copy-id 命令能够把本地主机的公钥复制到远程主机的 authorized_keys 文件上,实现免密码登录。

2.配置 ecosystem 文件

module.exports = {
  apps: [{
    name: "app",
    script: "app.js"
  }],
  deploy: {
    // "production" is the environment name
    production: {
      // SSH key path, default to $HOME/.ssh
      key: "/path/to/some.pem",
      // SSH user
      user: "Jartto",
      // SSH host
      host: ["192.168.0.13"],
      // SSH options with no command-line flag, see 'man ssh'
      // can be either a single string or an array of strings
      ssh_options: "StrictHostKeyChecking=no",
      // GIT remote/branch
      ref: "origin/master",
      // GIT remote
      repo: "git@github.com:jartto/repository.git",
      // path in the server
      path: "/var/www/jartto-repository",
      // Pre-setup command or path to a script on your local machine
      'pre-setup': "apt-get install git ; ls -la",
      // Post-setup commands or path to a script on the host machine
      // eg: placing configurations in the shared dir etc
      'post-setup': "ls -la",
      // pre-deploy action
      'pre-deploy-local': "echo 'This is a local executed command'",
      // post-deploy action
      'post-deploy': "npm install",
    },
  }
}
复制代码

命令很简单,都有注释,这里就不赘述了。

3.是时候启动了

# Setup deployment at remote location
pm2 deploy production setup

# Update remote version
pm2 deploy production update

# Revert to -1 deployment
pm2 deploy production revert 1

# execute a command on remote servers
pm2 deploy production exec "pm2 reload all"
复制代码

更多配置项:

pm2 deploy <configuration_file> <environment> <command>
  Commands:
    setup                run remote setup commands
    update               update deploy to the latest release
    revert [n]           revert to [n]th last deployment or 1
    curr[ent]            output current release commit
    prev[ious]           output previous release commit
    exec|run <cmd>       execute the given <cmd>
    list                 list previous deploy commits
    [ref]                deploy to [ref], the "ref" setting, or latest tag
复制代码

了解更多,请参考官方文档

6、环境变量

pm2 经过在配置文件中经过 env_xx 来声明不一样环境的配置,而后在启动应用时,经过 --env 参数指定运行的环境。一个简单的示例可能以下:

"env": {
    "NODE_ENV": "production",
    "REMOTE_ADDR": "http://www.jartto.wang/"
  },
  "env_dev": {
    "NODE_ENV": "development",
    "REMOTE_ADDR": "http://dev.jartto.wang/"
  },
  "env_test": {
    "NODE_ENV": "test",
    "REMOTE_ADDR": "http://test.jartto.wang/"
  }
复制代码

7、负载均衡

PM2 提供了强大的负载能力,咱们能够经过以下命令来开启:

pm2 start app.js -i 3 # 开启三个进程
pm2 start app.js -i max # 根据机器CPU核数,开启对应数目的进程
复制代码

8、数据监控

PM2 提供了一个数据监控命令:pm2 monit,执行命令后,大概界面以下:

monit

看起来不错,惋惜并不实用。你们可能发现了,在实际场景下,咱们线上环境会有 N 台服务器,你会一台台上去看监控数据吗?

显然,咱们碰到了另外一种场景,那么如何才能统一监控呢?

不要着急,PM2 为咱们提供了另一种方式,经过在 Server 端运行命令:pm2 web,咱们能够在该机器启动一个监听服务:

web

以后,你能够经过 主机 IP:9615 来获取数据,以下图:

api

获取数据能够经过客户端轮询,或者是服务端 Socket 推送,It's up to you!

最后,咱们来看看数据格式:

data

有了数据,那么可视化岂不是小菜一碟,咱们就能够在本地实时监控以下数据:
1.服务器内存状况;
2.CPU 使用状况;
3.各个站点服务状况,是否正常运转,是否报错,是否频繁重启等;
4.服务器平均负载;
...

9、重置状态

既然经过 PM2 来监控数据了,那么咱们确定但愿每次的数据是准确的,因此这时候就可使用:

pm2 reset jartto-test
复制代码

来重置服务状态。

10、最终效果

有了数据,可视化就很是容易了,咱们来看一个简单的示例:

show

固然,你能够作的更好,快发挥你创造性,作一些有趣的事情吧!

相关文章
相关标签/搜索