Supervisor全攻略(1)

原创
2015/09/18 14:56
阅读数 938

Supervisor是一个C/S系统,它允许用户在类UNIX系统上控制一些进程。它具有以下特性:

    1    简单

            Supervisor通过INI格式配置文件进行配置,很容易掌握,它为每个进程提供了很多配置选项,可以使你很容易的重启进程或者自动的轮转日志。

    2    统一

            Supervisor提供了一种统一的方式来start、stop、monitor你的进程, 进程可以单独控制,也可以成组的控制。你可以在本地或者远程命令行或者web接口来配置Supervisor。

    3    有效

            Supervisor通过fork/exec启动它的子进程,子进程并不是守护进程。当一个进程终止的时候,操作系统会立即给Supervisor发送一个信号,而不是像其他解决方案依赖PID文件。

    4    可扩展

            Supervisor包含一个简单的事件通知协议,因此任何程序都可以监控它,而且提供一个XML-RPC控制接口。

    5    兼容

            除了windows平台,其他平台都可运行。

Supervisor系统的组件:

    supervisord:

        服务会启动supervisord服务,它负责调用自己启动子程序,响应来自客户端的命令,重启crash或者退出的进程,记录进程的输出信息,收集事件信息。该服务的配置文件在/etc/supervisor/supervisord.conf

    supervisorctl:

        客户端的命令行工具,提供一个类shell接口,通过它你可以连接到不同的supervisord进程上来管理它们各自的子程序。客户端命令通过UNIX socket或者TCP来和服务通讯,服务端可以要求客户端提供身份验证之后才能进行操作([supervisorctl])。

    Web Server:

        一个小的web接口被集成进了supervisorctl,重启supervisord之后就可以访问了([inet_http_server])。    

    XML-RPC Interface:

        就像HTTP提供WEB UI一样,同时还提供了XML-RPC接口来控制supervisor和由它运行的程序。

安装:

        supervisor是python编写的,显然用easy_install、pip都可以安装,我懒,直接apt-get了,在ubuntu14.04下安装完后版本是3.0b2。

Supervisor服务的启动

        其实启动Supervisor很简单,supervisord -h看看就知道了,最简单的-c根配置文件即可:

supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration FILENAME -- configuration file
-n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-d/--directory DIRECTORY -- directory to chdir to when daemonized
-l/--logfile FILENAME -- use FILENAME as logfile path
-y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
-z/--logfile_backups NUM -- number of backups to keep when max bytes reached
-e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
-i/--identifier STR -- identifier used for this instance of supervisord
-q/--childlogdir DIRECTORY -- the log directory for child process logs
-k/--nocleanup --  prevent the process from performing cleanup (removal of
                   old automatic child log files) at startup.
-a/--minfds NUM -- the minimum number of file descriptors for start success
-t/--strip_ansi -- strip ansi escape codes from process output
--minprocs NUM  -- the minimum number of processes available for start success
--profile_options OPTIONS -- run supervisord under profiler and output
                             results based on OPTIONS, which  is a comma-sep'd
                             list of 'cumulative', 'calls', and/or 'callers',
                             e.g. 'cumulative,callers')

        不过既然我这懒人是用apt-get安装的,那安装包的规范必然符合debian系的风格了,直接service supervisor start即可启动,且慢,我们还没配置supervisor的配置文件呢,启动了也没什么效果。我们后面详解配置文件的配置。

supervisorctl客户端的使用

        supervisorctl有两种模式,一种是交互模式,一种是命令行模式。在命令行输入supervisorctl直接回车,即可进入交互模式。

supervisorctl -- control applications run by supervisord from the cmd line.

Usage: /usr/bin/supervisorctl [options] [action [arguments]]

Options:
-c/--configuration -- configuration file path (default /etc/supervisor.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
     (default "http://localhost:9001").  
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)

action [arguments] -- see below

Actions are commands like "tail" or "stop".  If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started.  Use the action "help" to find out about available
actions.

Supervisor的开机自启动

        如果你是pip或者easy_install安装的,开机服务自启动还真是个麻烦事,不过官方已经给出一些rc.d脚本示例了,在github上,不过由于我是apt-get安装的,显然这个开机自启动是不用担心的,用debian的update-rc.d即可搞定。

Supervisor的进程安全

        既然是用Supervisor来保证其他进程的正常运行,但是万一Supervisor进程挂了怎么办,我们可以使用daemontools来保证Supervisor正常运行,就类似于监控的监控。

下篇看配置文件



展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部