设为首页 收藏本站
查看: 2822|回复: 1

[经验分享] supervisor&supervisor-monitor部署

[复制链接]

尚未签到

发表于 2017-11-27 16:24:09 | 显示全部楼层 |阅读模式
                                                supervisor部署和supervisor-monitor的部署
(2017年11月24日)
参考文档:
http://www.cenhq.com/2017/04/14/centos7-install-config-supervisor/
https://segmentfault.com/a/1190000007367428

第一部分: supervisor部署
CentOS7 minimal安装,关闭selinux,iptables,安装光盘源,同步时间,安装必要的组件
IP: 192.168.1.90
安装setuptools
~]# yum install python-setuptools -y

下载meld3和supervisor
https://pypi.python.org/simple/meld3/
https://pypi.python.org/simple/supervisor/
~]# wget https://pypi.python.org/packages/0f/5e/3a57c223d8faba2c3c2d69699f7e6cfdd1e5cc31e79cdd0dd48d44580b50/meld3-1.0.1.tar.gz#md5=2f045abe0ae108e3c8343172cffbe21d
~]# wget https://pypi.python.org/packages/31/7e/788fc6566211e77c395ea272058eb71299c65cc5e55b6214d479c6c2ec9a/supervisor-3.3.3.tar.gz#md5=0fe86dfec4e5c5d98324d24c4cf944bd
~]# tar xf meld3-1.0.1.tar.gz
~]# cd meld3-1.0.1
~]# python setup.py install
~]# cd ../
~]# tar xf supervisor-3.3.3.tar.gz
~]# cd supervisor-3.3.3
~]# python setup.py install

supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。
运行supervisord服务的时候,需要指定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)
$CWD表示运行supervisord程序的目录。
可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件
~]# mkdir /etc/supervisor
~]# cd /etc/supervisor
~]# echo_supervisord_conf > supervisord.conf
配置文件参数说明
supervisor的配置参数较多,下面介绍一下常用的参数配置,详细的配置及说明,请参考官方文档介绍。
注:分号(;)开头的配置表示注释
[unix_http_server]
file=/tmp/supervisor.sock   ;UNIX socket 文件,supervisorctl 会使用
;chmod=0700                 ;socket文件的mode,默认是0700
;chown=nobody:nogroup       ;socket文件的owner,格式:uid:gid
;[inet_http_server]         ;HTTP服务器,提供web管理界面
;port=127.0.0.1:9001        ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user              ;登录管理后台的用户名
;password=123               ;登录管理后台的密码
[supervisord]
logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
loglevel=info                ;日志级别,默认info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false               ;是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024                  ;可以打开的文件描述符的最小值,默认 1024
minprocs=200                 ;可以打开的进程数的最小值,默认 200
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
; [program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序启动命令
autostart=true       ; 在supervisord启动的时候也自动启动
startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3       ; 启动失败自动重试次数,默认是3
user=tomcat          ; 用哪个用户启动进程,默认是root
priority=999         ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20   ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false     ;默认为false,向进程组发送kill信号,包括子进程
;包含其它配置文件
[include]
files = relative/directory/.ini    ;可以指定一个或多个以.ini结束的配置文件
include示例:
[include]
files = /opt/absolute/filename.ini /opt/absolute/
.ini foo.conf config.ini

安装部署tomcat7,作为supervisor的守护对象
~]# cd
~]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y
~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz
~]# tar -xf apache-tomcat-7.0.82.tar.gz  -C /data/app
~]# cd /data/app
~]# ln -sv apache-tomcat-7.0.82/ tomcat
配置管理进程
进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下包含进supervisord.conf文件中。
1> 创建/etc/supervisor/config.d目录,用于存放进程管理的配置文件
2> 修改/etc/supervisor/supervisord.conf中的include参数,将/etc/supervisor/conf.d目录添加到include中
~]# mkdir /etc/supervisor/config.d
~]# vim /etc/supervisor/supervisord.conf
#添加以下内容
[include]
files = /etc/supervisor/config.d/*.ini

编辑tomcat的supervisor启动文件
~]# vim /etc/supervisor/config.d/tomcat.ini
[program:tomcat]
user=root
command=/data/app/tomcat/bin/catalina.sh run
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
;stdout_logfile=/data/app/tomcat/logs/catalina.out

开启Supervisor Web管理界面
出于安全考虑,默认配置是没有开启web管理界面,需要修改supervisord.conf配置文件打开http访权限,将下面的配置:
~]# vim  /etc/supervisor/supervisord.conf
[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0: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))
生产环境不要使用默认的用户名和密码

启动supervisor
~]# /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
只要supervisord一启动,就会把tomcat启动起来的
~]# ss -tanlp | grep -E  "8080|9001"
说明: 8080时tomcat监听的端口, 9001时supervisord-web端监听的端口

交互终端supervisorctl
supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。
运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程。
supervisorctl
tomcat就是我们在tomcat.ini配置文件中[program:tomcat]指定的名字。
输入help可以查看可以执行的命令列表,如果想看某个命令的作用,运行help 命令名称,如:help stop
stop tomcat // 表示停止tomcat进程
stop all // 表示停止所有进程

bash终端
supervisorctl status
supervisorctl stop tomcat
supervisorctl start tomcat
supervisorctl restart tomcat
supervisorctl reread
supervisorctl update
浏览器访问 http://192.168.1.90:9001 可以看到以下界面
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=.jpg

现在再为supervisor增加一个服务memcached
~]# yum install memcached -y
~]# pwd
/etc/supervisor/config.d
~]# cat memcached.ini
[program:memcached]
command=/usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
autostart=true
autorestart=true
startsecs=5
;command=/usr/bin/memcached -p 11211 -u memcached -m 64 -P /var/run/memcached/memcached.pid
说明: 先把程序运行起来'systemctl start memcached',使用'ps aux | grep mem'过滤出mem启动命令,从而得到memcached的启动command
supervisor更新一下:
~]# supervisorctl status
tomcat                           RUNNING   pid 3077, uptime 0:03:36
~]# supervisorctl update
memcached: added process group
~]# supervisorctl status
memcached                        STARTING
tomcat                           RUNNING   pid 3077, uptime 0:03:44
~]# supervisorctl status
memcached                        RUNNING   pid 3136, uptime 0:00:11
tomcat                           RUNNING   pid 3077, uptime 0:03:52

浏览器刷新 http://192.168.1.90:9001/
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=.jpg

另外补充两个经过测试两个应用的supervisor启动配置文件
说明:要让redis运行于前台
~]# /etc/redis/redis.conf
daemonize no
~]# cat redis.ini
[program:redis]
command=/data/app/redis-stable/src/redis-server /etc/redis/redis.conf
autostart=true
autorestart=true
startsecs=5
/etc/mongod.conf 需要注释该行,让mongodb运行于前台
#fork: true
[iyunv@C69-1-93 config.d]# cat mongod.ini
[program:mongod]
command=/usr/bin/mongod -f /etc/mongod.conf
autostart=true
autorestart=true
startsecs=5

如果在多台服务器上都部署了supervisor做进程守护,每个supervisor都有各自的web管理,那么如果要管理多台服务器上的程序时,岂不是要登陆多个supervisor页面控制台吗?如果能把它们集合在一个页面上统一进行控制,那该有多好呢?是的,已经有人研发了这么一个工具--supervisord-monitor,这是一个php研发的工具,代码托管在gitlab,最近一次更新是四个月前.
https://github.com/mlazarov/supervisord-monitor
我们把supervisord-monitor部署到另外一个服务器上
centos7.4 minimal安装,安装epel源和光盘源,同步时间
192.168.1.94

先安装nginx和php-fpm环境,为节省时间,使用yum安装
~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
~]# yum install php-fpm nginx -y
#查看一眼php配置,没问题
~]# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
~]# systemctl start php-fpm
~]# ss -tanlp | grep 9000

#先检查nginx的配置文件有没有问题
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl start nginx
~]#
~]# netstat -tunlp | grep nginx
#浏览器访问 http://http://192.168.1.94/ 能正常访问测试页则说明nginx部署成功
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=.jpg

添加supervisor的配置nginx配置文件
vim /etc/nginx/nginx.conf
在server段落添加如下location
#add
location ~ .php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  SCHEME $scheme;
    include        fastcgi_params;
}
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl reload nginx
vim  /usr/share/nginx/html/index.php 测试页
<?php
        phpinfo();
?>
#浏览器访问 http://http://192.168.1.94/index.php  能正常访问php信息输出页面则说明nginx+php-fpm部署成功

添加nginx配置
cd /etc/nginx/conf.d/
vim supervisor.conf
server {
    listen       80 default_server;
    server_name  192.168.1.94;
    root         /data/data/nginx/supervisor/public_html;
    location / {
        index  index.php index.html;
    }
    location /control/ {
        index  index.php;
        rewrite  /(.*)$  /index.php?$1  last;
    }
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  SCHEME $scheme;
        include        fastcgi_params;
    }
}

vim /etc/nginx/nginx.conf
        listen       80 default_server;
        listen       [::]:80 default_server;
        servername  ;
修改为
        listen       80;
        listen       [::]:80;
        server_name  localhost;

#检查nginx的配置文件没有问题后重载nginx
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl reload nginx

准备supervisor-monitor的页面文件
~]# mkdir -pv /data/data/nginx/
~]# cd  /tmp
~]# ls
supervisord-monitor-master-new.zip
~]# unzip  -q  supervisord-monitor-master-new.zip  -d  /data/data/nginx/
~]# cd /data/data/nginx/
~]# mv supervisord-monitor-master/ supervisor
查看一下supervisord-monitor的页面目录结构
~]# tree  /data/data/nginx/supervisor/  -L  1
/data/data/nginx/supervisor/
├── application
├── composer.json
├── package.json
├── public_html
├── Readme.md
├── supervisord-monitor.png
└── system

cd /data/data/nginx/supervisor/application/config
cp supervisor.php.example supervisor.php
#change
vim  supervisor.php
$config['supervisor_servers'] = array(
        '192.168.1.90' => array(
                'url' => 'http://192.168.1.90/RPC2',
                'port' => '9001',
                'username' => 'user',
                'password' => '123'
        ),
);

访问http://192.168.1.94/
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=.jpg
通过上面的停止,重启按钮可以控制1.90上的服务

supervisor-monitor这个web控制台控制着多台服务器的程序运行,自然是非常重要,所以访问时应该输入用户名和密码
~]# yum -y install httpd-tools
~]# htpasswd -c /etc/nginx/conf.d/.htpasswd admin
New password: [admin]
Re-type new password: [admin]
Adding password for user admin
如果还想增加用户
~]# htpasswd -m /etc/nginx/conf.d/.htpasswd tom
这么重要的文件,当然要做好授权
~]# chown root.nginx .htpasswd
~]# chmod 640 .htpasswd
~]# ll .htpasswd
-rw-r----- 1 root nginx 86 Nov 24 16:18 .htpasswd

编辑配置文件
~]# vim /etc/nginx/conf.d/supervisor.conf
    location / {
        index  index.php index.html;
        auth_basic "Basic Auth";
        auth_basic_user_file "/etc/nginx/conf.d/.htpasswd";
    }
~]# systemctl reload nginx

使用浏览器再次访问 http://192.168.1.94 输入admin/admin
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=.jpg

                                       


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-419655-1-1.html 上篇帖子: centos7下配置vlan+kvm虚拟机 下篇帖子: 解决配置drbd报错rate问题

尚未签到

发表于 2018-5-3 14:55:00 | 显示全部楼层
汇总页面根据服务分组,有开源出来的代码么

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表