Docker 集成

Docker 集成

使用容器?我们为您保驾护航。立即开始使用 pm2-runtime,它是您在生产环境中充分利用 Node.js 的完美伴侣。

pm2-runtime 的目标是将您的应用程序封装到合适的 Node.js 生产环境中。它解决了在容器内运行 Node.js 应用程序时遇到的主要问题,例如

  • 第二进程回退,实现高应用程序可靠性
  • 进程流控制
  • 自动应用程序监控,使其始终保持稳定和高性能
  • 自动源代码映射发现和解析支持

除此之外,使用 PM2 作为容器和应用程序之间的层,带来了 PM2 强大的功能,例如 应用程序声明文件可定制的日志系统 以及其他在生产环境中管理 Node.js 应用程序的强大功能。

在容器内使用 PM2

在您的 Dockerfile 中添加以下行以安装 PM2

RUN npm install pm2 -g

然后将 node 二进制文件替换为 pm2-runtime

CMD ["node", "app.js"]

CMD ["pm2-runtime", "app.js"]

您现在已经准备就绪!您的 Node.js 应用程序现在已封装到合适的 Node.js 生产环境中。

启动配置文件

您可以将原始 Node.js 应用程序声明到配置文件(或进程文件)中,并设置一些配置变量(例如启用集群模式),而不是使用 PM2 运行它。

让我们创建一个包含以下内容的 ecosystem.config.js 文件

module.exports = [{
  script: 'app.js',
  name: 'app',
  exec_mode: 'cluster',
  instances: 2
}, {
  script: 'worker.js',
  name: 'worker'
}]

所有可用选项都 在此处列出

然后,您可以将 CMD 指令替换为以下内容

CMD ["pm2-runtime", "process.yml"]

要将每个进程拆分到其自己的 Docker 中,可以使用 –only [app-name] 选项

CMD ["pm2-runtime", "process.yml", "--only", "APP"]

将 exec_mode cluster 与 nuxtjs 一起使用

在集群模式下运行 pm2 时,由于 nuxtjs 解析其 rootDir 的方式,ecosystem.config.js 将被附加到您的 cwd 路径中,要解决此问题,您必须在 args 部分中指定配置路径

module.exports = {
  apps: [
    {
      name: 'my-nuxtjs-app',
      exec_mode: 'cluster',
      instances: 2,
      cwd: '/var/www',
      script: './node_modules/nuxt-start/bin/nuxt-start.js',
      args: '-c /var/www/nuxt.config.js'
    }
  ]
}

日志格式选项

如果要更改日志输出格式,可以选择以下选项之一

  • –json:将以 JSON 格式输出日志(logstash)
  • –format:将以 = 样式格式输出日志
  • –raw:将按原样输出日志

要使用其中一个标志,您只需将它们传递给 pm2-runtime

CMD ["pm2-runtime", "--json", "process.yml"]

启用优雅关闭

当容器收到关闭信号时,PM2 会将此信号转发给您的应用程序,允许关闭所有数据库连接,等待所有查询都已处理完毕或任何其他最终处理已完成,然后再成功进行优雅关闭。

捕获关闭信号非常简单。您需要在 Node.js 应用程序中添加一个侦听器,并在停止应用程序之前执行任何必要的操作

process.on('SIGINT', function() {
   db.stop(function(err) {
     process.exit(err ? 1 : 0);
   });
});

默认情况下,PM2 将等待 1600 毫秒,然后再发送最终的 SIGKILL 信号。您可以通过在应用程序配置文件中设置 kill_timeout 选项来修改此延迟。

在此处阅读有关应用程序状态管理的更多信息 此处

开发环境

您可能希望告诉开发人员在容器内进行编程,以保持开发、测试和生产环境之间的一致性。

pm2-runtime 替换为 pm2-dev 将启用监视和重启功能。当主机文件作为 VOLUME 暴露给容器时,这在开发容器中非常有用。

使用 PM2.io

Keymetrics.io 是一种构建在 PM2 之上的监控服务,允许轻松监控和管理应用程序(日志、重启、异常监控……)。在 Keymetrics 上创建存储桶后,您将获得一个公钥和一个私钥。

要使用 pm2-runtime 启用 Keymetrics 监控,您可以使用 CLI 选项 –public XXX–secret YYY,也可以传递环境变量 KEYMETRICS_PUBLICKEYMETRICS_SECRET

通过 Dockerfile 使用 CLI 选项的示例

CMD ["pm2-runtime", "--public", "XXX", "--secret", "YYY", "process.yml"]

或通过环境变量

ENV PM2_PUBLIC_KEY=XXX
ENV PM2_SECRET_KEY=YYY

或通过 Docker run 命令

docker run --net host -e "PM2_PUBLIC_KEY=XXX" -e "PM2_SECRET_KEY=XXX" <...>

pm2-runtime 助手

这是 pm2-runtime 助手

>>> pm2-runtime -h

  Usage: pm2-runtime app.js

  pm2-runtime is a drop-in replacement node.js binary with some interesting production features

  Options:

    -V, --version              output the version number
    -i --instances <number>    launch [number] of processes automatically load-balanced. Increase overall performances and performance stability.
    --secret [key]             [MONITORING] keymetrics secret key
    --public [key]             [MONITORING] keymetrics public key
    --machine-name [name]      [MONITORING] keymetrics machine name
    --raw                      raw log output
    --json                     output logs in json format
    --format                   output logs formatted like key=val
    --delay <seconds>          delay start of configuration file by <seconds>
    --web [port]               launch process web api on [port] (default to 9615)
    --only <application-name>  only act on one application of configuration
    --no-auto-exit             do not exit if all processes are errored/stopped or 0 apps launched
    --env [name]               inject env_[name] env variables in process config file
    --watch                    watch and restart application on file change
    --error <path>             error log file destination (default disabled)
    --output <path>            output log file destination (default disabled)
    -h, --help                 output usage information


  Commands:

    *
    start <app.js|json_file>  start an application or json ecosystem file
为此页面做出贡献