运行两个gunicorn实例

Sam*_* M. 2 django gunicorn systemd

试图在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
Run Code Online (Sandbox Code Playgroud)

[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
Run Code Online (Sandbox Code Playgroud)

日志显示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.
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*erg 12

systemd对这种情况有很大的支持.我假设你在应用程序前面运行像Nginx这样的Web服务器作为反向代理,使用它们唯一的套接字名称将流量路由到每个Gunicorn实例的右端点.在这里,我将重点介绍systemd配置.

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

systemd解决方案涉及创建"目标",该目标将用于将两个实例视为单个服务.然后,将使用单个"模板单元"允许您向"目标"添加多个gunicorns.

首先,/etc/systemd/systemd/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
Run Code Online (Sandbox Code Playgroud)

而已.您已经创建了一个目标,当"启用"将在启动时启动.现在/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
Run Code Online (Sandbox Code Playgroud)

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

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

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

systemctl start gunicorn.target
systemctl stop gunicorn.target
Run Code Online (Sandbox Code Playgroud)

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

systemctl enable  gunicorn@kenyabuzz
systemctl disable gunicorn@ugandabuzz
Run Code Online (Sandbox Code Playgroud)

我们还可以自己stopstart特定的服务:

systemctl start gunicorn@kenyabuzz
systemctl stop gunicorn@kenyabuzz
systemctl restart gunicorn@kenyabuzz
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢。最后,我只编写了两个单独的文件 - `gunicorn_live.service` 和 `gunicorn_staging.service` (以及匹配的 `.socket` 文件)。然后我就可以使用“systemctl start Gunicorn_staging.socket”。如果我需要两项以上的服务,我会回到这种方法。 (2认同)