【问题标题】:running two instances of gunicorn运行两个 gunicorn 实例
【发布时间】:2017-07-02 08:35:36
【问题描述】:

尝试在 gunicorn 上运行两个站点,从

编辑服务
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz

ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application

[Install]
WantedBy=multi-user.target

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
WorkingDirectory=/home/ubuntu/webapps/uganda_buzz

ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/uganda_buzz/ug.sock kb.wsgi:application

[Install]
WantedBy=multi-user.target

日志显示ExecStart 无法复制

ubuntu@ip-172-31-17-122:~$ sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: error (Reason: Invalid argument)
   Active: active (running) since Tue 2017-02-14 09:36:52 EAT; 35s ago
 Main PID: 26150 (gunicorn)
   CGroup: /system.slice/gunicorn.service
           ├─26150 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           ├─26156 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           ├─26157 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           └─26160 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application

Feb 14 09:36:52 ip-172-31-17-122 systemd[1]: Started gunicorn daemon.
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Starting gunicorn 19.6.0
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Listening at: unix:/home/ubuntu/webapps/kenyabuzz/kb.sock (26150)
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Using worker: sync
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26156] [INFO] Booting worker with pid: 26156
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26157] [INFO] Booting worker with pid: 26157
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26160] [INFO] Booting worker with pid: 26160
Feb 14 09:37:15 ip-172-31-17-122 systemd[1]: gunicorn.service: Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.

在这种情况下,使用可用配置运行两个站点的最佳方式是什么。尝试在同一行上用空格和逗号分隔,每个都会出错。

【问题讨论】:

    标签: django gunicorn systemd


    【解决方案1】:

    systemd 非常支持这种情况。我假设您正在应用程序前面运行一个像 Nginx 这样的 Web 服务器作为反向代理,以使用它们唯一的套接字名称将流量路由到每个 Gunicorn 实例的正确端点。这里我将重点关注systemd 配置。

    希望能够停止和启动单独的gunicorn 实例,也希望将它们视为可以一起停止或启动的single service

    systemd 解决方案涉及创建一个“目标”,用于将两个实例视为单个服务。然后,将使用一个“模板单元”来允许您将多个独角兽添加到“目标”。

    首先,/etc/systemd/system/gunicorn.target

    # See README.md for more about this file
    [Unit]
    Description=Gunicorn
    Documentation=https://example.com/path/to/your/docs
    
    [Install]
    WantedBy=multi-user.target
    

    就是这样。您已经创建了一个“启用”时将在启动时启动的目标。现在/etc/systemd/system/gunicorn@.service,模板文件:

    [Unit]
    Description=gunicorn daemon
    After=network.target
    PartOf=gunicorn.target
    # Since systemd 235 reloading target can pass through
    ReloadPropagatedFrom=gunicorn.target
    
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/webapps/%i
    
    ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/%i/kb.sock kb.wsgi:application
    
    [Install]
    WantedBy=gunicorn.target
    

    注意我对您的文件所做的更改:

    1. 现在每个实例都是PartOf= 目标。
    2. 由于更新了WantedBy=,启用此服务将使其作为目标的依赖项启动
    3. WorkingDirectory= 和套接字路径现在使用变量,因为该文件现在是一个模板。

    要一次启动或停止所有 Gunicorn 实例,我们可以简单地:

    systemctl start gunicorn.target
    systemctl stop gunicorn.target
    

    我们可以使用enabledisable 动态添加和删除新的 Gunicorn 实例:

    systemctl enable  gunicorn@kenyabuzz
    systemctl disable gunicorn@ugandabuzz
    

    我们也可以自己stopstart一个特定的服务:

    systemctl start gunicorn@kenyabuzz
    systemctl stop gunicorn@kenyabuzz
    systemctl restart gunicorn@kenyabuzz
    

    此模式不再需要原始的 gunicorn.service 文件,可以将其删除。

    要了解有关使用的任何系统指令的更多信息,您可以查看man systemd.directives,其中将列出记录每个指令的特定手册页。

    【讨论】:

    • 原来的/etc/systemd/system/gunicorn.service会怎样?我注意到我必须在 /etc/systemd/system 中创建一个 gunicorn.target 和一个 gunicorn@.service 文件
    • 被这两个新文件取代,不再需要了。
    • 当我删除 gunicorn.service 并运行 systemctl start gunicorn 我得到:Failed to start gunicorn.service: Unit gunicorn.service not found.
    • 按照上面的模式,gunicorn.servicegunicorn.target 替换。所以试试systemctl start gunicorn.target。我已经澄清了答案中的语法。 @LewyBlue
    • 谢谢。最后,我只写了两个单独的文件 - gunicorn_live.servicegunicorn_staging.service(与匹配的 .socket 文件)。然后我可以使用systemctl start gunicorn_staging.socket。如果我需要两个以上的服务,我会回到这种方法。
    【解决方案2】:

    如果您有两个站点,合乎逻辑的解决方案是让每个站点成为不同的服务。通过这种方式,您可以重新启动其中一个,也可以将其中一个取下来进行维护,而不会影响另一个。

    【讨论】:

    • 我认为不可能或不建议同时运行两个 gunicorn 服务。
    • 当然可以,而且没有问题。这正是您接受的答案的作用。
    【解决方案3】:

    最简单的方法是使用supervisor。 在此处阅读文档; http://supervisord.org/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-31
      • 2020-02-09
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多