supervisor&supervisor-monitor部署

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

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 可以看到以下界面
supervisor&supervisor-monitor部署


现在再为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/
supervisor&supervisor-monitor部署


另外补充两个经过测试两个应用的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

[root@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部署成功
supervisor&supervisor-monitor部署


添加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/
supervisor&supervisor-monitor部署

通过上面的停止,重启按钮可以控制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
supervisor&supervisor-monitor部署










本文转自 zhuhc1988  51CTO博客,原文链接:http://blog.51cto.com/changeflyhigh/2044307,如需转载请自行联系原作者
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
20天前
|
监控 Ubuntu Unix
Supervisor使用详解
Supervisor使用详解
21 0
|
3月前
|
监控 应用服务中间件 nginx
Supervisor快速入门 | 使用Supervisor守护Nginx进程
Supervisor快速入门 | 使用Supervisor守护Nginx进程
39 0
supervisor 安装、配置、使用
supervisor 安装、配置、使用
614 0
supervisor的使用
supervisor的使用
77 0
|
JavaScript
supervisor安装
supervisor安装
95 0
|
jenkins 持续交付
jenkins通过supervisor配置守护进程
jenkins通过supervisor配置守护进程
183 0
|
Unix Shell 应用服务中间件
supervisor 使用
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
324 0
|
Unix 应用服务中间件 Shell
Supervisor守护进程
使用Supervisor的背景: #Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
1152 0
|
Shell Linux Python
supervisor 管理进程
        Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
1134 0
Supervisor管理进程
Supervisor管理进程 转载 2016年04月14日 18:26:45 标签: supervisord 28344 Supervisor重新加载配置启动新的进程 liaojie 发布于 1年前,共有 0 条评论 一、添加好配置文件后 二、更新新的配置...
840 0