×

Supervisor的作用与配置

admin admin 发表于2018-06-29 17:28:32 浏览1869 评论0

抢沙发发表评论

1.安装
主机环境:(Centos7)
  1. [root@localhost ~]# cat /proc/version

  2. Linux version 3.10.0-229.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Fri Mar 6 11:36:42 UTC 2015

安装Supervisor
  1. [root@localhost ~]# yum install python-setuptools

  2. [root@localhost ~]# easy_install supervisor

测试安装是否成功:(有以下配置文件说明安装成功了)
  1. [root@localhost ~]# echo_supervisord_conf

  2. ; Sample supervisor config file.

  3. ;

  4. ; For more information on the config file, please see:

  5. ; http://supervisord.org/configuration.html

  6. ;

  7. ; Notes:

  8. ;  - Shell expansion ("~" or "$HOME") is not supported.  Environment

  9. ;    variables can be expanded using this syntax: "%(ENV_HOME)s".

  10. ;  - Quotes around values are not supported, except in the case of

  11. ;    the environment= options as shown below.

  12. ;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

  13. ;  - Command will be truncated if it looks like a config file comment, e.g.

  14. ;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".

  15. [unix_http_server]

  16. file=/tmp/supervisor.sock   ; the path to the socket file

  17. ;chmod=0700                 ; socket file mode (default 0700)

  18. ;chown=nobody:nogroup       ; socket file uid:gid owner

  19. ;username=user              ; default is no username (open server)

  20. ;password=123               ; default is no password (open server)

  21. ;[inet_http_server]         ; inet (TCP) server disabled by default

  22. ;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface

  23. ;username=user              ; default is no username (open server)

  24. ;password=123               ; default is no password (open server)

  25. ...

2.创建配置文件创建supervisor配置文件目录/etc/supervisor/
  1. [root@localhost ~]# mkdir -m 755 -p /etc/supervisor/

  2. [root@localhost ~]# cd /etc/

  3. [root@localhost etc]# ll |grep supervisor

  4. drwxr-xr-x   3 root root     42 Jul 11 11:09 supervisor

创建主配文件supervisord.conf
  1. [root@localhost etc]# echo_supervisord_conf > /etc/supervisor/supervisord.conf

  2. [root@localhost etc]# cd supervisor/

  3. [root@localhost supervisor]# ll

  4. total 12

  5. -rw-r--r-- 1 root root 9186 Jul 11 11:09 supervisord.conf

创建项目配置文件目录
  1. [root@localhost supervisor]# mkdir -m 755 conf.d

  2. [root@localhost supervisor]# ll

  3. total 12

  4. drwxr-xr-x 2 root root   37 Jul 11 10:20 conf.d

  5. -rw-r--r-- 1 root root 9186 Jul 11 11:09 supervisord.conf

3.调试
在/data/wwwroot/supervisor_simple 目录下创建test.c
  1. [root@localhost ~]# vim /data/wwwroot/supervisor_simple/test.c

  2. #include<stdio.h>

  3. #include<string.h>

  4. int main() {

  5.  FILE *fp = fopen("/data/wwwroot/supervisor_simple/1.txt", "a+");

  6.  if(fp==0){

  7.    printf("can't open file\n");

  8.    return 0;

  9.  }

  10.  int ix = 0;

  11.  for(;; ix++){

  12.    fseek(fp, 0, SEEK_END);

  13.    char s_add_arr[10];

  14.    memset(s_add_arr, '\0', 10);

  15.    sprintf(s_add_arr, "%i\n", ix);

  16.    fwrite(s_add_arr, strlen(s_add_arr), 1, fp);

  17.    

  18.    sleep(1);  

  19.  }

  20.  fclose(fp);

  21.  return 0;

  22. }

 编译为test
  1. [root@localhost ~]# cd /data/wwwroot/supervisor_simple/

  2. [root@localhost supervisor_simple]# gcc -o test test.c

在/etc/supervisor/conf.d 目录下创建 test.ini
  1. [root@localhost ~]# cd /etc/supervisor/conf.d/

  2. [root@localhost conf.d]# # vim test.ini

  3. [program:test]

  4. command=/data/wwwroot/supervisor_simple/test

  5. stdout_logfile=/tmp/test_stdout.log;

  6. autostart=true

  7. autorestart=true

  8. startsecs=5

  9. priority=1

  10. stopasgroup=true

  11. killasgroup=true

在主配文档中引入test.ini
  1. [root@localhost supervisor]# vim supervisord.conf

  2. //找到include

  3. ;[include]

  4. ;files = relative/directory/*.ini

  5. //改为

  6. [include]

  7. files = ./conf.d/*.ini

启动supervisor
  1. [root@localhost supervisor]# supervisord -c supervisord.conf

  2. Unlinking stale socket /tmp/supervisor.sock

  3. [root@localhost supervisor]# pstree

  4. systemd─┬─NetworkManager─┬─dhclient

  5.        │                └─3*[{NetworkManager}]

  6.        ├─VBoxService───7*[{VBoxService}]

  7.        ├─agetty

  8.        ├─auditd───{auditd}

  9.        ├─crond

  10.        ├─dbus-daemon

  11.        ├─lvmetad

  12.        ├─memcached───6*[{memcached}]

  13.        ├─mysqld_safe───mysqld───23*[{mysqld}]

  14.        ├─nginx───nginx

  15.        ├─php-fpm───11*[php-fpm]

  16.        ├─polkitd───5*[{polkitd}]

  17.        ├─pure-ftpd

  18.        ├─redis-server───2*[{redis-server}]

  19.        ├─rsyslogd───2*[{rsyslogd}]

  20.        ├─sshd─┬─sshd───bash───pstree

  21.        │      ├─6*[sshd───sftp-server]

  22.        │      └─sshd───bash

  23.        ├─supervisord───test

  24.        ├─systemd-journal

  25.        ├─systemd-logind

  26.        ├─systemd-udevd

  27.        └─tuned───4*[{tuned}]

  28. [root@localhost supervisor]# pstree -p | grep supervisord

  29.           |-supervisord(6271)---test(6281)

  30. [root@localhost supervisor]#

查看supervisord.log发现program test已启动
  1. [root@localhost supervisor]# cat /tmp/supervisord.log

  2. 2017-07-11 10:47:33,440 CRIT Supervisor running as root (no user in config file)

  3. 2017-07-11 10:47:33,440 INFO Included extra file "/etc/supervisor/./conf.d/test.ini" during parsing

  4. 2017-07-11 10:47:33,480 INFO RPC interface 'supervisor' initialized

  5. 2017-07-11 10:47:33,480 CRIT Server 'unix_http_server' running without any HTTP authentication checking

  6. 2017-07-11 10:47:33,481 INFO daemonizing the supervisord process

  7. 2017-07-11 10:47:33,481 INFO supervisord started with pid 6572

  8. 2017-07-11 10:47:34,486 INFO spawned: 'test' with pid 6582

  9. 2017-07-11 10:47:39,532 INFO success: test entered RUNNING state, process has stayed up for > than 5 seconds (startsecs)

  10. 2017-07-11 11:10:06,249 INFO waiting for test, skuld to die

  11. 2017-07-11 11:10:06,251 INFO stopped: test (terminated by SIGTERM)

用 supervisorctl 查看已经被监控的program
  1. [root@localhost supervisor]# supervisorctl

  2. test                             RUNNING   pid 6735, uptime 3:08:29

  3. supervisor>

注:直接用运行supervisorctl命令没效果的话,换成下面的命令。效果一样
  1. [root@localhost supervisor]# supervisorctl -c /etc/supervisor/supervisord.conf

  2. test                             RUNNING   pid 6735, uptime 3:08:29

  3. supervisor>

增加一例监控php脚本
  1. [root@localhost supervisor]# cd conf.d/

  2. [root@localhost conf.d]# vim skuld.ini

  3. [program:skuld]

  4. command=php -f /data/wwwroot/supervisor_simple/skuld.php

  5. stdout_logfile=/tmp/skuld_stdout.log;

  6. autostart=true

  7. autorestart=true

  8. startsecs=5

  9. priority=1

  10. stopasgroup=true

  11. killasgroup=true

在/data/wwwroot/supervisor_simple目录下创建skuld.php
  1. <?php

  2. $fn = fopen('/data/wwwroot/supervisor_simple/skuld.log', 'a');

  3. $i = 0;

  4. while(1){

  5.  sleep(1);

  6.  fwrite($fh, $i++ . "\n");

  7. }

  8. fclose($fn);

重启监控服务
  1. [root@localhost conf.d]# supervisorctl reload

  2. Restarted supervisord

  3. [root@localhost conf.d]# pstree

  4. systemd─┬─NetworkManager─┬─dhclient

  5.        │                └─3*[{NetworkManager}]

  6.        ├─VBoxService───7*[{VBoxService}]

  7.        ├─agetty

  8.        ├─auditd───{auditd}

  9.        ├─crond

  10.        ├─dbus-daemon

  11.        ├─lvmetad

  12.        ├─memcached───6*[{memcached}]

  13.        ├─mysqld_safe───mysqld───23*[{mysqld}]

  14.        ├─nginx───nginx

  15.        ├─php-fpm───11*[php-fpm]

  16.        ├─polkitd───5*[{polkitd}]

  17.        ├─pure-ftpd

  18.        ├─redis-server───2*[{redis-server}]

  19.        ├─rsyslogd───2*[{rsyslogd}]

  20.        ├─sshd─┬─2*[sshd───bash]

  21.        │      ├─6*[sshd───sftp-server]

  22.        │      └─sshd───bash───pstree

  23.        ├─supervisord─┬─php

  24.        │             └─test

  25.        ├─systemd-journal

  26.        ├─systemd-logind

  27.        ├─systemd-udevd

  28.        └─tuned───4*[{tuned}]

  29. [root@localhost conf.d]#


===========================重点==================================
1. supervisor 比较适合监控业务应用,且只能监控前台程序,php fork方式实现的daemon不能用它监控,否则supervisor> status 会提示:BACKOFF  Exited too quickly (process log may have details)

 

2.每次修改配置文件后需进入supervisorctl,执行reload, 改动部分才能生效。

 

3.两个命令
supervisord : supervisor的服务器端部分,用于supervisor启动。
supervisorctl:启动supervisor的命令行窗口,在该命令行中可执行start、stop、status、reload等操作。

 

3.web管理界面
将supervisord.conf中[inet_http_server]部分做相应配置,在supervisorctl中reload即可启动web管理界面。
  1. ;[inet_http_server]         ; inet (TCP) server disabled by default

  2. ;port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

  3. ;username=user              ; (default is no username (open server))

  4. ;password=123               ; (default is no password (open server))

  5. 修改成:

  6. [inet_http_server]         ; inet (TCP) server disabled by default

  7. port=127.0.0.1:9001          ; (ip_address:port specifier, *:port for all iface)

  8. username=user              ; (default is no username (open server))

  9. password=123               ; (default is no password (open server))

  10. port:绑定访问IP和端口,这里是绑定的是本地IP和9001端口(写上你自己的服务器IP或是本地IP)

  11. username:登录管理后台的用户名

  12. password:登录管理后台的密码

4.如果9001端口拒绝访问,服务器允许9001端口访问 ,保存iptables规则 。
  1. [root@localhost ~]# iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 9001 -j ACCEPT

  2. [root@localhost ~]# service iptables save

访问web管理界面


分享到:

群贤毕至

访客