supervisor + uwsgi 部署python服务流程

发布时间:2024年01月03日

supervisor + uwsgi 部署python服务流程

最近由于某个python服务不稳定,有时候是造成服务器down机,有时候是服务本身突然就挂了。查了一下原因,发现是内存资源占用过高导致的,

为了让服务稳定运行,便打算引入supervisor + uwsgi对服务进行分布式部署。

第一步 安装 uwsgi和supervisor

由于我们是内网环境,所以需要先去下载安装包,可以根据需要选择版本

uwsgi 安装包 uWSGI 2.0.19 — uWSGI 2.0 documentation

supervisor 安装包 supervisor · PyPI

下载之后上传到服务器,解压python setup.py install 安装

第二步 python服务中加上uwsgi和supervisor的配置文件

可以在项目中新建一个目录runconfig,来放置配置

配置有两个uwsgi.ini和supervisor.ini

第三步 根据需要添加配置

参考配置

uwsgi.ini文件

[uwsgi]

module = app_starter:app

#用以启动程序名

processes = 2

#子进程数量

chdir = /data/project

#启动程序目录

pidfile = /data/project/runconfig/uwsgi.pid

chmod-socket = 660

# 当服务器退出时自动删除unix socket文件和pid文件

vacuum = true

#http地址和端口

http = 0.0.0.0:4000

# serializer requests

thunder-lock = true

# enable threads support

enable-threads = true

buffer-size = 32768

log-maxsize = 102400000

supervisor.ini文件

[program:uwsgi]

command=bash /data/project/runconfig/restart_service.sh??????????????; the program (relative uses PATH, can take args)

directory=/data/project??????????????; directory to cwd to before exec (def no cwd)

priority=999??????????????????; the relative start priority (default 999)

autostart=true????????????????; start at supervisord start (default: true)

startsecs=10???????????????????; # of secs prog must stay up to be running (def. 1)

startretries=3????????????????; max # of serial start failures when starting (default 3)

stopsignal=INT???????????????; signal used to kill process (default TERM)

stopasgroup=true?????????????; send stop signal to the UNIX process group (default false)

killasgroup=true?????????????; SIGKILL the UNIX process group (def false)

user=test???????????????????; setuid to this UNIX account to run thell program

redirect_stderr=true??????????; redirect proc stderr to stdout (default false)

stdout_logfile=/data/project/logs/supervisor.log????????; stdout log path, NONE for none; default AUTO

stdout_logfile_maxbytes=100MB???; max # logfile bytes b4 rotation (default 50MB)

stdout_logfile_backups=10?????; # of stdout logfile backups (0 means none, default 10)

第四步,增加重启配置脚本restart_service.sh

#!/bin/bash

set -e

cd /data/project

git pull

source /etc/profile

source /home/test/.bashrc

oldservice=`ps -ef | grep 'uwsgi.ini' | grep -v grep | awk '{print $2}'`

if [ -n "${oldservice}" ]; then

????uwsgi --stop /data/project/runconfig/uwsgi.pid

fi

uwsgi --ini /data/project/runconfig/uwsgi.ini

第五步 新增supervisord.conf文件

在/etc 目录下新建目录supervisor,

使用echo_supervisord_conf > /etc/supervisor/supervisord.conf在该目录下创建文件supervisord.conf

supervisord.conf的参考配置

[unix_http_server]

file=/data/project/runconfig/supervisor.sock???; the path to the socket file

chmod=0766?????????????????; socket file mode (default 0700)

[supervisord]

logfile=/data/project/logs/supervisord.log ; main log file; default $CWD/supervisord.log

logfile_maxbytes=50MB????????; max main logfile bytes b4 rotation; default 50MB

logfile_backups=10???????????; # of main logfile backups; 0 means none, default 10

loglevel=info????????????????; log level; default info; others: debug,warn,trace

pidfile=/data/project/runconfig/supervisord.pid ; supervisord pidfile; default supervisord.pid

nodaemon=false???????????????; start in foreground if true; default false

silent=false?????????????????; no logs to stdout if true; default false

minfds=1024??????????????????; min. avail startup file descriptors; default 1024

minprocs=200?????????????????; min. avail process descriptors;default 200

user=test? ? ? ? ? ?; setuid to this UNIX account at startup; recommended if root

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///data/project/runconfig/supervisor.sock ; use a unix:// URL??for a unix socket

[include]

files = /data/project/runconfig/supervisor*.ini

第六步设置开机自启

进入到/usr/lib/systemd/system/

cd /usr/lib/systemd/system/

创建文件supervisord.service

sudo vim supervisord.service

supervisord.service参考配置

[Unit]

Description=supervisor daemon

#After=rc-local.service nss-user-lookup.target

[Service]

Type=forking

ExecStart=/usr/local/python3817/bin/supervisord(根据自己安装目录修改) -c /etc/supervisor/supervisord.conf

ExecStop=/usr/local/python3817/bin/supervisorctl(根据自己安装目录修改) shutdown

ExecReload=/usr/local/python3817/bin/supervisorctl(根据自己安装目录修改) reload

KillMode=process

Restart=on-failure

RestartSec=42s

User=test

[Install]

WantedBy=multi-user.target

启用服务

systemctl enable supervisord

启动服务

systemctl start supervisord

查看服务状态

systemctl status supervisord

查看开机自启是否启用

systemctl is-enabled supervisord

supervisorctl的一些常用命令

supervisorctl shutdown 停止supervisord服务

supervisorctl reload 重启 supervisord服务

supervisorctl update 更新 supervisord.conf文件后重启服务

supervisorctl status 查看 supervisord服务进程状态

文章来源:https://blog.csdn.net/weixin_42575811/article/details/135372863
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。