继上一篇介绍了Celery的安装和代码工程使用后,今天介绍Supervisor,部署和使用Supervisor可以提高Celery系统的稳定性,高可用性。希望能帮助到需要的朋友们,图文创作不易,欢迎关注哦。
一,简介
Supervisor是一个进程管理工具,也可以理解为进程守护监控,当进程中断的时候Supervisor能自动重新启动它。Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
在线上环境中,为了celery高可用性,肯定需要配置celery自动失败重启和开机自启动等,保证celery服务端和客户端的可用性。
二,Supervisor安装
2.1 下载包地址
https://files.pythonhosted.org/packages/2f/43/130066cd6003233401142f5f98cd09c93165f5c6408f850dd965b4f2470e/supervisor-4.2.0-py2.py3-none-any.whl
2.2 安装下载包
pip3 install supervisor
2.3 创建配置文件
创建/etc/supervisor/supervisord.conf配置文件
vim /etc/supervisor/supervisord.conf
内容如下:
[unix_http_server]
file=/tmp/supervisor.sock ; path to your socket file
port=127.0.0.1:9001
[supervisord]
logfile=/var/log/supervisord/supervisord.log ; supervisord log file
logfile_maxbytes=50MB ; maximum size of logfile before rotation
logfile_backups=10 ; number of backed up logfiles
loglevel=info ; info, debug, warn, trace
pidfile=/var/run/supervisord.pid ; pidfile location
nodaemon=false ; run supervisord as a daemon
minfds=1024 ; number of startup file descriptors
minprocs=200 ; number of process descriptors
user=root ; default user
childlogdir=/var/log/supervisord/ ; where child log files will live
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use unix:// schem for a unix sockets.
[include]
# Uncomment this line for celeryd for Python
;files=celeryd.confi
files = /etc/supervisor/supervisord.conf.d/*.conf
2.4 创建文件
创建celeryd_worker.conf文件
vim /etc/supervisor/supervisord.conf.d/celeryd_worker.conf
[program:celery]
command=/usr/local/python3/bin/celery -A app worker -l info -n 192.168.12.150 -Q add
directory=/tmp/pycharm_project_146 # 运行命令的目录
numprocs=1
# 设置log的路径
stdout_logfile=/var/log/supervisor/celeryworker.log
stderr_logfile=/var/log/supervisor/celeryworker.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
priority=15
2.5 创建日志文件目录
mkdir -p /var/log/supervisor
mkdir -p /var/log/supervisord
2.6 创建service文件
创建/etc/systemd/system/supervisord.service文件
此文件用于在centos中通过服务器supervisord,需要赋予777权限
vim /etc/systemd/system/supervisord.service
chmod 777 supervisord.service
三,Supervisord使用
3.1 启动Supervisord
运行systemctl start supervisord,启动supervisor,之后celery会自动启动
3.2 命令查看celery运行状态
运行supervisorctl status查看可发现celery成功运行
3.3 woker启动进程数和服务器cpu核数相关
查看逻辑核数:
cat /proc/cpuinfo| grep "processor"| wc -l
本文由挨踢程序猿原创,欢迎关注,带你一起长知识!