如何使用supervisor管理你的应用
2018-03-02 17:21
453 查看
1、前言
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是UNIX-like系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。
提供的高可用场景,当你的程序出现异常,例如core/内存溢出等,导致服务进程被杀死,这个时候supervisort监听到进程终止后,会自动将它重新拉起。
2、安装
supervisor是基于python开发的,所以安装时首先要保证有python环境,当然最好已经安装了如下工具。
pip:python的包管理工具
virtualenv:虚拟沙盒环境
当安装方式很简单。
supervisor是一个C/S架构的工具。安装完成后,会生成三个执行程序:
1)supervisortd:守护进程服务(用于接收进程管理命令)
2)supervisorctl:客户端(用于和守护进程通信,发送管理进程的指令)
3)echo_supervisord_conf:生成初始配置文件程序
3、配置
运行supervisor服务的时候,需要指定supervisor的配置文件,有两种方式:
1)显式的指定
可以通过-c参数显式的指定supervior配置文件,灵活性高
2)非显式的指定
如果没有显示指定的话,supervisor会在如下路径下找配置文件:
初始配置文件的生成,使用如下命令
4、配置文件参数说明
一般来说,配置管理我们采用supervisor+program的方式。
将supervisor的配置文件,和管理进程的配置文件区分开,这样的好处是,当有多个进程需要管理的时候,不会互相干扰。
下面分开说明一下。
1)配置supervisor
先粘上我使用的supervisor配置,后面再详细讲解。
2)配置program进程
可以看到上面文件中有[include]项,这个项指定了在/home/kangaroo/supervisor/supervisor.d/*.conf路径下的配置文件也会被加载进来。
一般来说我们会在这些配置文件里管理进程的配置。
配置内容
这样就配置完成了。
5、启动supervisor管理服务
这样,进程就被启动并且管理起来了。
6、client的命令
最后补充上一些supervisorctl的命令供参考
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是UNIX-like系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。
提供的高可用场景,当你的程序出现异常,例如core/内存溢出等,导致服务进程被杀死,这个时候supervisort监听到进程终止后,会自动将它重新拉起。
2、安装
supervisor是基于python开发的,所以安装时首先要保证有python环境,当然最好已经安装了如下工具。
pip:python的包管理工具
virtualenv:虚拟沙盒环境
当安装方式很简单。
pip install supervisor
supervisor是一个C/S架构的工具。安装完成后,会生成三个执行程序:
1)supervisortd:守护进程服务(用于接收进程管理命令)
2)supervisorctl:客户端(用于和守护进程通信,发送管理进程的指令)
3)echo_supervisord_conf:生成初始配置文件程序
3、配置
运行supervisor服务的时候,需要指定supervisor的配置文件,有两种方式:
1)显式的指定
可以通过-c参数显式的指定supervior配置文件,灵活性高
$SUPERVISOR/bin/supervisorctl -c $SUPERVISOR/conf/supervisord.conf start CIServer
2)非显式的指定
如果没有显示指定的话,supervisor会在如下路径下找配置文件:
$CWD/supervisord.conf $CWD/etc/supervisord.conf /etc/supervisord.conf /etc/supervisor/supervisord.conf (since Supervisor 3.3.0) ../etc/supervisord.conf (Relative to the executable) ../supervisord.conf (Relative to the executable)
初始配置文件的生成,使用如下命令
echo_supervisord_conf > /etc/supervisor/supervisord.conf
4、配置文件参数说明
一般来说,配置管理我们采用supervisor+program的方式。
将supervisor的配置文件,和管理进程的配置文件区分开,这样的好处是,当有多个进程需要管理的时候,不会互相干扰。
下面分开说明一下。
1)配置supervisor
先粘上我使用的supervisor配置,后面再详细讲解。
[unix_http_server] file=/home/kangaroo/supervisor/run/supervisor.sock ;必须,UNIX socket文件,supervisorctl 会使用 ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; (default is no username (open server)) ;password=123 ; (default is no password (open server)) ;[inet_http_server] ; inet (TCP) server disabled by default ;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface) ;username=user ; (default is no username (open server)) ;password=123 ; (default is no password (open server)) [supervisord] logfile=/home/kangaroo/supervisor/log/supervisord.log ;日志文件,默认是 $CWD/supervisord.log logfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小 logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份 loglevel=info ;日志级别,默认info,其它: debug,warn,trace pidfile=/home/kangaroo/supervisor/run/supervisord.pid ;进程pid文件 supervisord.pid nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动 minfds=1024 ;可以打开的文件描述符的最小值,默认 1024 minprocs=200 ;可以打开的进程数的最小值,默认 200 ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///home/kangaroo/supervisor/run/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致 [include] files = /home/kangaroo/supervisor/supervisor.d/*.conf
2)配置program进程
可以看到上面文件中有[include]项,这个项指定了在/home/kangaroo/supervisor/supervisor.d/*.conf路径下的配置文件也会被加载进来。
一般来说我们会在这些配置文件里管理进程的配置。
vim /home/kangaroo/supervisor/supervisor.d/CIServer.conf
配置内容
[program:CIServer] ; 程序名称,可以通过ctl指定名称进行控制 directory = /home/kangaroo/build/CIServer ; 程序的启动目录 command = python manage.py runserver --noreload 0.0.0.0:8200 ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 user = xiaoju ; 用哪个用户启动 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /home/kangaroo/supervisor/log/supervior.log
这样就配置完成了。
5、启动supervisor管理服务
supervisord -c /etc/supervisor/supervisord.conf
这样,进程就被启动并且管理起来了。
6、client的命令
最后补充上一些supervisorctl的命令供参考
supervisorctl start CIServer # 启动,配置文件中要配置program:CIServer supervisorctl stop CIServer # 关闭 supervisorctl restart CIServer # 重启 supervisorctl status # 查看管理进程的状态
相关文章推荐
- 如何管理app的进程与内存分配,以及在Android开发应用的时候如何主动的减少内存的使用
- Win8如何使用应用通知功能管理应用通知
- 使用supervisor管理beego应用
- 如何在BDS2006中使用COM+应用生成精灵
- 想研究如何使用RUP来开发一个产品,一直不知道如何来应用?
- 如何使用XMLBeans兼容Java与XML应用开发
- 如何在 IIS 6.0 中使用命令行脚本管理网站和 Web 虚拟目录
- 如何使用JCA (J2EE 连接器架构)实现企业应用
- 如何使用应用日志(Application Log)
- ABAP--如何在abap中使用日志管理
- ABAP--如何在abap中使用日志管理
- 如何使用Abator利器来开发ibatis应用实例!
- 如何在VS2005中使用C#开发Silverlight 1.1应用
- 如何使用 Mmcv.exe 工具来管理群集消息队列资源[转]
- 如何使用应用日志(Application Log)
- 如何使用Lua扩展C/C++应用系列1-转自bbs.luachina.net
- 如何使用Lua扩展C/C++应用系列3
- 如何使用ASP实现网站的“目录树”管理
- ABAP--如何在abap中使用日志管理
- 如何使用Lua扩展C/C++应用系列3-转自bbs.luachina.net