运行两个gunicorn实例

运行两个gunicorn实例

本文介绍了运行两个gunicorn实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to run two sites on gunicorn editing the service from

[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.

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

what would be the best way to run two sites in this case, using the available configurations. Tried on the same line separated by space and comma each brings an error.

推荐答案

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

systemd has great support for this situation. I presume you are running a web server like Nginx in front of the apps as a reverse proxy to route traffic to the right endpoint for each Gunicorn instance using their unique socket names. Here I'll focus on the systemd configuration.

最好能够停止并启动单个 gunicorn 实例,并且还希望将它们视为可以一起停止或启动的单一服务

It's desirable to be able to stop and start an individual gunicorn instance and also desirable to consider them as a single service that can be stopped or started together.

系统d 解决方案涉及创建一个目标,该目标将把两个实例都视为一个服务。然后,将使用一个模板单元来允许您向目标添加多个gunicorn。

The systemd solution involves creating a "target" which will be used to treat both instances as a single service. Then, a single "template unit" will be used to allow you to add multiple gunicorns to the "target".

首先, /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/[email protected] ,即模板文件:

That's it. You've created a target that when "enabled" will start at boot. Now /etc/systemd/system/[email protected], the template file:

[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

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

Notice the changes I made to your file:


  1. 每个实例现在为 PartOf = 目标。

  2. 启用此服务将使其作为目标的依赖项启动,这是由于更新的 WantedBy =

  3. WorkingDirectory = 和套接字路径现在使用变量,因为该文件现在是模板。

  1. Each instance is now PartOf= the target.
  2. Enabling this service will make it boot as dependency of the target, due to an updated WantedBy=
  3. The WorkingDirectory= and socket path now use variables, because the file is now a template.

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

To start or stop all Gunicorn instances at once, we can simply:

systemctl start gunicorn.target
systemctl stop gunicorn.target

我们可以使用 enable disable 快速添加和删除新的Gunicorn实例:

We can use enable and disable to add and remove new Gunicorn instances on the fly:

systemctl enable  gunicorn@kenyabuzz
systemctl disable gunicorn@ugandabuzz

我们还可以通过以下方式停止开始本身:

We can also stop and start a specific service by itself:

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

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

The original gunicorn.service file is no longer needed in this pattern and can be removed.

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

To learn more about any of the system directives used, you can can look at man systemd.directives, which will list the specific man page that documents each directive.

这篇关于运行两个gunicorn实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:35