【问题标题】:Running and stopping gunicorn server using monit使用 monit 运行和停止 gunicorn 服务器
【发布时间】:2015-04-08 14:03:25
【问题描述】:

我正在尝试使用 monit 管理服务器上的内容。我想做的是在 3 个不同的端口上运行 3 个不同的 gunicorn 服务器。

目前我可以一次运行所有服务器,例如在屏幕中。我可以通过命令启动服务器:

gunicorn -c app1.http_server.config app1.http_server.server:app
gunicorn -c app2.http_server.config app2.http_server.server:app
gunicorn -c app3.http_server.config app3.http_server.server:app

根据我对 monit 工作原理的了解,我应该在 monitrc 文件中指定所有内容,例如:

#set mailserver localhost
#set alert myemail@gmail.com

check process app1 with pidfile /var/run/app1.pid
    start program = "gunicorn -c app1.http_server.config app1.http_server.server:app"
    stop program  = "???"
    if failed unixsocket ??? then start
    if cpu > 50% for 5 cycles then alert

# TODO app2, app3

check system resources
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if memory usage > 75% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert


check filesystem rootfs with path /
    if space usage > 80% then alert

我尝试使用各种东西来停止程序字段并启动程序,但 monit 无法启动 gunicorn 服务器。所以我的问题是如何从监视器运行和停止 gunicorn 服务器?当我启动它时,gunicorn 的 unixsocket 会是什么?谁能提供一些可以帮助我进行设置的示例?

【问题讨论】:

    标签: python flask gunicorn monit


    【解决方案1】:

    我遇到了同样的问题。 Monit 更喜欢“/virtualenv_path/bin/gunicorn”等完整路径。如果你不使用 virtualenv,只要在我放置它的地方删除它。命令很长,但它是这样工作的:

    check process pymonit with pidfile /path/to/pid/gunicorn.pid
      start program = "/virtualenv_path/bin/python /virtualenv_path/bin/gunicorn -c /project/path/gunicorn.conf.py /project/path/wsgi:application"
      stop program = "/usr/bin/pkill -f '/virtualenv_path/bin/python /virtualenv_path/bin/gunicorn -c /project/path/gunicorn.conf.py /project/path/wsgi:application'"
      if failed host 127.0.0.1 port 8011 protocol http then restart
      if 5 restarts within 5 cycles then alert
    

    在您的 gunicorn.conf 中,您应该有这样的内容:

    bind = '127.0.0.1:8011'
    ...
    pidfile = '/path/to/pid/gunicorn.pid'
    

    在您的 gunicorn.conf 中。

    【讨论】:

    • 我在使用 /project/path/wsgi:application 时遇到问题。我得到一个ImportError: No module named '/project/path/wsgi' 我只能让它接受relative 点路径project.path.wsgi:application。但这些都行不通,因为它们必须是相对的。
    • 可能需要在 gunicorn.conf 中设置chdir = /project/path
    【解决方案2】:

    您好像在使用 monit 作为启动服务?

    根据您的操作系统,您仍应为您的应用创建 sysvinit、systemd 或 upstart 文件,这也意味着它将在启动时启动,并且不依赖于 monit 来运行并首次运行。

    这也简化了您的监控配置,只需服务 app1 启动/停止。

    如果操作系统没有这些系统中的任何一个,请创建一个 shell 脚本来正确获取您的 virtualenv 并更改目录(但 gunicorns chdir 不需要),那么您有一个更具可读性的 monit 配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2021-06-30
      • 1970-01-01
      相关资源
      最近更新 更多