部署 django 应用程序时遇到 gunicorn 服务失败的问题,结果为“service-start-limit-hit”

部署 django 应用程序时遇到 gunicorn 服务失败的问题,结果为“service-start-limit-hit”

我正在 Ubuntu 18.04.3 上的 digital-ocean 上进行部署。这是我第一次部署 Django 应用程序,因此我对这种新的部署方式有很多了解,但几乎没有能力。当我启动并启用我的gunicorn服务时systemctl,检查它返回的状态

jordan@ubuntu-s-1vcpu-1gb-tor1-01:~/jycs/jycs/jycs$ sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-08-01 14:02:50 UTC; 7min ago
 Main PID: 20214 (gunicorn)
    Tasks: 4 (limit: 1152)
   CGroup: /system.slice/gunicorn.service
           ├─20214 /home/jordan/jycs/env/bin/python /home/jordan/jycs/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock jycs.ws
           ├─20230 /home/jordan/jycs/env/bin/python /home/jordan/jycs/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock jycs.ws
           ├─20232 /home/jordan/jycs/env/bin/python /home/jordan/jycs/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock jycs.ws
           └─20233 /home/jordan/jycs/env/bin/python /home/jordan/jycs/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock jycs.ws

Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20214] [INFO] Starting gunicorn 20.0.4
Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20214] [INFO] Listening at: unix:/run/gunicorn.sock (20214)
Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20214] [INFO] Using worker: sync
Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20230] [INFO] Booting worker with pid: 20230
Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20232] [INFO] Booting worker with pid: 20232
Aug 01 14:02:50 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: [2020-08-01 14:02:50 +0000] [20233] [INFO] Booting worker with pid: 20233
Aug 01 14:02:51 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: /home/jordan/jycs/jycs
Aug 01 14:02:51 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: /home/jordan/jycs/jycs
Aug 01 14:02:51 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]: /home/jordan/jycs/jycs
Aug 01 14:10:25 ubuntu-s-1vcpu-1gb-tor1-01 gunicorn[20214]:  - - [01/Aug/2020:14:10:25 +0000] "GET / HTTP/1.1" 400 143 "-" "curl/7.58.0"

然后我们做sudo journalctl -u gunicorn.socket

jordan@ubuntu-s-1vcpu-1gb-tor1-01:~/jycs/jycs/jycs$ sudo journalctl -u gunicorn.socket
-- Logs begin at Fri 2020-07-31 13:36:05 UTC, end at Sat 2020-08-01 14:04:00 UTC. --
Jul 31 17:22:11 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Jul 31 17:24:10 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jul 31 17:36:03 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Jul 31 19:22:37 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jul 31 19:44:31 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Jul 31 19:51:44 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jul 31 21:13:22 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Jul 31 22:00:40 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Aug 01 02:45:27 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Aug 01 02:50:19 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Aug 01 04:45:14 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Aug 01 04:45:40 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Aug 01 05:11:02 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.
Aug 01 06:04:45 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Aug 01 14:02:38 ubuntu-s-1vcpu-1gb-tor1-01 systemd[1]: Listening on gunicorn socket.

我的gunicorn.service文件位于/etc/systemd/system/gunicorn.service

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

[Service]
User=jordan
Group=www-data
WorkingDirectory=/home/jordan/jycs/jycs
ExecStart=/home/jordan/jycs/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          jycs.wsgi:application


[Install]
WantedBy=multi-user.target                                         

和套接字文件/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

当我尝试 curl 时它返回

jordan@ubuntu-s-1vcpu-1gb-tor1-01:~/jycs/jycs/jycs$ jordan@ubuntu-s-1vcpu-1gb-tor1-01:~/jycs/jycs/jycs$ curl --unix-socket /run/gunicorn.sock localhost

<!doctype html>
<html lang="en">
<head>
  <title>Bad Request (400)</title>
</head>
<body>
  <h1>Bad Request (400)</h1><p></p>
</body>
</html>

答案1

基本上,“start-limit-hit”消息是一个干扰信息 - service start 会尝试多次启动您提供的任何服务名称,如果在第五次尝试失败后(至少对我来说是这样),它会在第六次尝试时因“start-limit-hit”而崩溃。您需要调查实际的系统日志(可能是 /var/log/syslog),以查看阻止服务在尝试 1-5 时启动的真正错误是什么。

相关内容