Supervisor
Supervisor 是一个客户端/服务器系统,
允许其用户监视和控制类似UNIX的操作系统上的多个进程。
在Python服务宕机后可以自动重启
环境
CentOS7.9
python3.8
supervisor 4.1.0supertvisor版本过低可能会报错
ImportError: cannot import name ‘escape’ from 'cgi
pip阿里源 http://pub.mirrors.aliyun.com/pypi/simple/
安装 pip3 install supervisor
参考
-
https://learnku.com/docs/python-learning/tornado-foundation-deployment/10899
-
http://shouce.jb51.net/tornado/ch8.html
-
https://www.cnblogs.com/Yunya-Cnblogs/p/14337673.html#_caption_0
简单的tornado
[root@master129 ~/tornado_test]# cat main.py
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options # 新导入的options模块
tornado.options.define("port", default=8000, type=int, help="服务器监听端口号")
class IndexHandler(tornado.web.RequestHandler):
"""主路由处理类"""
def get(self):
self.write(f"tornado port: {port}")
if __name__ == "__main__":
tornado.options.parse_command_line()
port = tornado.options.options.port
app = tornado.web.Application([
(r"/", IndexHandler),
])
app.listen(port)
tornado.ioloop.IOLoop.current().start()
supervisor
配置
运行 echo_supervisord_conf 命令输出默认的配置项,可以如下操作将默认配置保存到文件中
echo_supervisord_conf > supervisord.conf
vim 打开编辑 supervisord.conf 文件,修改
[include]
files = relative/directory/*.ini
为
[include]
files = /etc/supervisor/*.conf
include 选项指明包含的其他配置文件。
将编辑后的 supervisord.conf 文件复制到 /etc/ 目录下
sudo cp supervisord.conf /etc/
然后我们在 /etc 目录下新建子目录 supervisor(与配置文件里的选项相同),并在 /etc/supervisor/ 中新建 tornado 管理的配置文件 tornado.conf。
[root@master129 /etc/supervisor]# cat tornado.conf
[group:tornadoes]
programs=tornado-8000,tornado-8001,tornado-8002,tornado-8003
[program:tornado-8000]
command=python3 /root/tornado_test/main.py --port=8000
directory=/root/tornado_test
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado_test/tornado.log
loglevel=info
[program:tornado-8001]
command=python3 /root/tornado_test/main.py --port=8001
directory=/root/tornado_test
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado_test/tornado.log
loglevel=info
[program:tornado-8002]
command=python3 /root/tornado_test/main.py --port=8002
directory=/root/tornado_test
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado_test/tornado.log
loglevel=info
[program:tornado-8003]
command=python3 /root/tornado_test/main.py --port=8003
directory=/root/tornado_test
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado_test/tornado.log
loglevel=info
启动
supervisord -c /etc/supervisord.conf
查看 supervisord 是否在运行:
ps aux | grep supervisord
利用 supervisorctl 来管理 supervisor。
supervisorctl
status # 查看程序状态
stop tornadoes:* # 关闭 tornadoes组 程序
start tornadoes:* # 启动 tornadoes组 程序
restart tornadoes:* # 重启 tornadoes组 程序
update # 重启配置文件修改过的程序
执行 status 命令时,显示如下信息说明 tornado 程序运行正常:
效果
服务宕机后自动重启
[root@master129 /etc/supervisor]# ps aux | grep python3
root 1481 0.0 0.9 225684 16880 ? Ss 00:31 0:00 /usr/local/python3.8/bin/python3 /usr/local/python3.8/bin/supervisord -c /etc/supervisord.conf
root 1482 0.0 1.0 225704 19476 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8000
root 1483 0.0 1.0 225704 19428 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8001
root 1484 0.0 1.0 225704 19488 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8002
root 1485 0.0 1.0 225704 19524 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8003
root 1512 0.0 0.0 112824 984 pts/0 R+ 01:00 0:00 grep --color=auto python3
[root@master129 /etc/supervisor]# kill -9 1482
[root@master129 /etc/supervisor]# ps aux | grep python3
root 1481 0.0 0.9 225684 16980 ? Ss 00:31 0:00 /usr/local/python3.8/bin/python3 /usr/local/python3.8/bin/supervisord -c /etc/supervisord.conf
root 1483 0.0 1.0 225704 19428 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8001
root 1484 0.0 1.0 225704 19488 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8002
root 1485 0.0 1.0 225704 19524 ? S 00:31 0:00 python3 /root/tornado_test/main.py --port=8003
root 1527 4.5 1.0 225704 19172 ? S 01:01 0:00 python3 /root/tornado_test/main.py --port=8000
root 1529 0.0 0.0 112824 984 pts/0 R+ 01:01 0:00 grep --color=auto python3
Nginx反向代理
访问一个IP, 将请求分发到上面的四个服务中
nginx参考文档
https://www.cnblogs.com/LiuQizhong/p/11757420.html
https://besterwin.gitee.io/blogs/knowledge/middle/Nginx.html
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
安装pcre和nginx
源码包安装
nginx配置文件
user root;
worker_processes 5;
events {
worker_connections 1024;
use epoll;
}
http{
upstream tornadoes {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme; # 协议 http https
proxy_pass http://tornadoes;
}
}
}
niginx基础命令
启动 sbin/nginx
重新加载配置 sbin/nginx -s reload
关闭 sbin/nginx -s stop