百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

CentOS 生产服务业务进程管理 supervisor

nanshan 2025-02-11 12:57 8 浏览 0 评论

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
supervisor 部署
测试环境:CentOS7
IP: 192.168.1.90


安装setuptools

Bash
[root@VM_108_97_centos soft] # yum install python-setuptools -y
下载meld3和supervisor
https://pypi.python.org/simple/meld3/
https://pypi.python.org/simple/supervisor/
[root@VM_108_97_centos soft] # wget?https://pypi.python.org/packages/0f/5e/3a57c223d8faba2c3c2d69699f7e6cfdd1e5cc31e79cdd0dd48d44580b50/meld3-1.0.1.tar.gz#md5=2f045abe0ae108e3c8343172cffbe21d
[root@VM_108_97_centos soft] # wget?https://pypi.python.org/packages/31/7e/788fc6566211e77c395ea272058eb71299c65cc5e55b6214d479c6c2ec9a/supervisor-3.3.3.tar.gz#md5=0fe86dfec4e5c5d98324d24c4cf944bd
[root@VM_108_97_centos soft] # tar xf meld3-1.0.1.tar.gz
[root@VM_108_97_centos soft] # cd meld3-1.0.1
[root@VM_108_97_centos soft] # python setup.py install
[root@VM_108_97_centos soft] # cd ../
[root@VM_108_97_centos soft] # tar xf supervisor-3.3.3.tar.gz
[root@VM_108_97_centos soft] # cd supervisor-3.3.3
[root@VM_108_97_centos soft] # python setup.py install
Bash
?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的初始化配置文件
[root@VM_108_97_centos soft]# mkdir /etc/supervisor
[root@VM_108_97_centos soft]# cd /etc/supervisor
[root@VM_108_97_centos soft]# 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的守护对象
[root@VM_108_97_centos soft]# cd
[root@VM_108_97_centos soft]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y
[root@VM_108_97_centos soft]# wget?https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz
[root@VM_108_97_centos soft]# tar -xf apache-tomcat-7.0.82.tar.gz??-C /data/app
[root@VM_108_97_centos soft]# cd /data/app
[root@VM_108_97_centos soft]# 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中
[root@VM_108_97_centos soft]# mkdir /etc/supervisor/config.d
[root@VM_108_97_centos soft]# vim /etc/supervisor/supervisord.conf
#添加以下内容
[include]
files = /etc/supervisor/config.d/*.ini
编辑tomcat的supervisor启动文件
[root@VM_108_97_centos soft]# 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访权限,将下面的配置:
[root@VM_108_97_centos soft]# 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
[root@VM_108_97_centos soft]# /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
只要supervisord一启动,就会把tomcat启动起来的
[root@VM_108_97_centos soft]# 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增加一个服务memcached
[root@VM_108_97_centos soft][root@VM_108_97_centos soft]# yum install memcached -y
[root@VM_108_97_centos soft]# pwd
/etc/supervisor/config.d
[root@VM_108_97_centos soft]# 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更新一下:
[root@VM_108_97_centos soft]# supervisorctl status
tomcat? ?? ?? ?? ?? ?? ?? ?? ?? ?RUNNING? ?pid 3077, uptime 0:03:36
[root@VM_108_97_centos soft]# supervisorctl update
memcached: added process group
[root@VM_108_97_centos soft]# supervisorctl status
memcached? ?? ?? ?? ?? ?? ?? ?? ?STARTING
tomcat? ?? ?? ?? ?? ?? ?? ?? ?? ?RUNNING? ?pid 3077, uptime 0:03:44
[root@VM_108_97_centos soft]# 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启动配置文件
说明:要让redis运行于前台
[root@VM_108_97_centos soft]# /etc/redis/redis.conf
daemonize no
[root@VM_108_97_centos soft]# 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@VM_108_97_centos soft]# 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安装
[root@VM_108_97_centos soft]# wget -P /etc/yum.repos.d/?http://mirrors.aliyun.com/repo/epel-7.repo
[root@VM_108_97_centos soft]# yum install php-fpm nginx -y
#查看一眼php配置,没问题
[root@VM_108_97_centos soft]# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
[root@VM_108_97_centos soft]# systemctl start php-fpm
[root@VM_108_97_centos soft]# ss -tanlp | grep 9000
#先检查nginx的配置文件有没有问题
[root@VM_108_97_centos soft]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@VM_108_97_centos soft]# systemctl start nginx
[root@VM_108_97_centos soft]#
[root@VM_108_97_centos soft]# netstat -tunlp | grep nginx
#浏览器访问?http://http://192.168.1.94/?能正常访问测试页则说明nginx部署成功


添加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;
}
[root@VM_108_97_centos soft]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@VM_108_97_centos soft]# systemctl reload nginx
vim??/usr/share/nginx/html/index.php 测试页

#浏览器访问?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
[root@VM_108_97_centos soft]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@VM_108_97_centos soft]# systemctl reload nginx
准备supervisor-monitor的页面文件
[root@VM_108_97_centos soft]# mkdir -pv /data/data/nginx/
[root@VM_108_97_centos soft]# cd??/tmp
~][root@VM_108_97_centos soft]# ls
supervisord-monitor-master-new.zip
[root@VM_108_97_centos soft]# unzip??-q??supervisord-monitor-master-new.zip??-d??/data/data/nginx/
[root@VM_108_97_centos soft]# cd /data/data/nginx/
[root@VM_108_97_centos soft]# mv supervisord-monitor-master/ supervisor
查看一下supervisord-monitor的页面目录结构
[root@VM_108_97_centos soft]# 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/


通过上面的停止,重启按钮可以控制1.90上的服务

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

使用浏览器再次访问 http://192.168.1.94 输入admin/admin

相关推荐

在 Ubuntu 上安装 Zabbix(以 Zabbix 6.4 LTS 版本为例)

Zabbix是一个流行的开源监控解决方案,能够监控各种网络参数和服务器健康状态。一、环境准备系统要求Ubuntu20.04/22.04LTS至少2GBRAM(生产环境建议4GB+)至少1...

如何在 Ubuntu 24.04 服务器上安装 Apache Solr

ApacheSolr是一个免费、开源的搜索平台,广泛应用于实时索引。其强大的可扩展性和容错能力使其在高流量互联网场景下表现优异。Solr基于Java开发,提供了分布式索引、复制、负载均衡及自...

如何在 Ubuntu 24.04 LTS 或 22.04/20.04 上安装 Apache Maven

Maven是由Apache托管的开源工具,用于管理Java项目。它包含一个项目对象模型(POM):一个配置文件(XML),其中包含项目的基本信息,包括配置、项目依赖项等。Maven可以处理...

Cursor的终极对手——Trae Pro最新系统提示词

前段时间,字节的AI编程神器Trae国际版,终于甩出了Pro订阅计划!很多对它又爱又恨的小伙伴,直呼:终于等到你。爱它,是因为Trae长期免费+体验真香;恨它?还不是那该死的排队等待,...

AI系统提示词:V0(ai代码提示)

以下是对V0系统提示词(SystemPrompt)的分部分讲解与解读,帮助你理解其核心内容和设计意图。V0系统提示词##CoreIdentity-Youarev0,Vercel&...

8岁男童失踪第13天,搜救人员发现可疑水库,更恶心的事情发生了

Lookingatyourrequest,Ineedtorewritethearticleaboutthe8-year-oldmissingboywhilemaking...

docker常用指令及安装rabbitMQ(docker安装zabbix)

一、docker常用指令启动docker:systemctlstartdocker停止docker:systemctlstopdocker重启docker:systemctlrestart...

三步教你用Elasticsearch+PyMuPDF实现PDF大文件秒搜!

面对100页以上的大型PDF文件时,阅读和搜索往往效率低下。传统关系型数据库在处理此类数据时容易遇到性能瓶颈,而Elasticsearch凭借其强大的全文检索和分布式架构,成为理想解决方案。通过...

ElasticSearch中文分词插件(IK)安装

坚持原创,共同进步!请关注我,后续分享更精彩!!!前言ElasticSearch默认的分词插件对中文支持很不友好。一段话按规则会以每个中文字符来拆解,再分别建立倒排索引。如"中华人民共和国国歌...

SpringBoot使用ElasticSearch做文档对象的持久化存储?

ElasticSearch是一个基于Lucene的开源搜索引擎,广泛应用于日志分析、全文搜索、复杂查询等领域,在有些场景中使用ElasticSearch进行文档对象的持久化存储是一个很不错的选择...

Elasticsearch数据迁移方案(elasticsearch copyto)

前言最近小编要去给客户部署一套系统涉及到了Mysql和ES数据的迁移,下面就给大家分享一下ES数据迁移的几套方案,根据具体的使用场景来选择不同的迁移方案能使你事倍功半,话多说下面就一一介绍。Elast...

Rancher部署单体ElasticSearch(rancher2.5部署)

Rancher是k8s图形管理界面,之前曾有写文章介绍如何安装。ElasticSearch是热门搜索引擎,很多地方都有用到,常规安装部署略显繁琐,本文介绍在k8s下用rancher简易部署ES。1.在...

Elasticsearch在Java项目的搜索实践:从零开始构建高效搜索系统

Elasticsearch在Java项目中的搜索实践:从零开始构建高效搜索系统在现代的Java项目中,数据量激增,传统的数据库查询方式已经无法满足快速检索的需求。这时,Elasticsearch(E...

小白入门-Kibana安装(kibana安装配置)

一Kibana基础1.1介绍Kibana是一款免费且开放的前端应用程序,其基础是ElasticStack,可以为Elasticsearch中索引的数据提供搜索和数据可视化功能。Kiban...

Docker上使用Elasticsearch,Logstash,Kibana

在对一个项目做性能测试时我需要处理我们web服务器的访问日志来分析当前用户的访问情况。因此,我想这是试用ELK的一个好机会。ELK栈首先要注意的是使用它是非常简单的。从决定使用ELK到在本机上搭一个...

取消回复欢迎 发表评论: