关于云计算:Linux进程管理工具-Supervisor

6次阅读

共计 6000 个字符,预计需要花费 15 分钟才能阅读完成。

Supervisor 装置与配置(linux/unix 过程管理工具)
Supervisor(http://supervisord.org)是用 Python 开发的一个 client/server 服务,是 Linux/Unix 零碎下的一个过程管理工具,不反对 Windows 零碎。它能够很不便的监听、启动、进行、重启一个或多个过程。用 Supervisor 治理的过程,当一个过程意外被杀死,supervisort 监听到过程死后,会主动将它从新拉起,很不便的做到过程主动复原的性能,不再须要本人写 shell 脚本来管制。

因为 Supervisor 是 Python 开发的, 装置前先检查一下零碎否装置了 Python2.4 以上版本。上面以 CentOS7.6,Python2.7.5 版本环境下, 介绍 Supervisor 的装置与配置步聚:

试验环境

零碎平台

cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

Python 版本

python -V
Python 2.7.5

如果 python 版本低于 2.6 请降级,上面贴出一个装置 python3.6.8 的装置示例

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y libffi-devel
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
tar xf Python-3.6.8.tar.xz
cd Python-3.6.8
./configure --prefix=/usr/local/python368
make && make install
echo 'export PATH=/usr/local/python368/bin:$PATH' >> /etc/profile
source /etc/profile
python3 -V

装置 Supervisor

装置 Supervisor 的办法很多,以下介绍三种,我这里所采纳的为第三种

1、easy_install 装置 supervisor

装置 Python 包管理工具(easy_install)
easy_install 是 setuptools 包里带的一个命令, 应用 easy_install 实际上是在调用 setuptools 来实现装置模块的工作, 所以装置 setuptools 即可:

wget https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
unzip setuptools-33.1.1.zip
cd setuptools-33.1.1
python setup.py install

easy_install supervisor

2、pip 装置 supervisor

应用 pip 来装置,前提要保障 pip 版本大于 2.6

pip install supervisor

3、yum epel-release 装置 supervisor

yum install -y epel-release && yum install -y supervisor

superviso 命令

supervisor 装置实现后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf:

  • supervisortd:用于治理 supervisor 自身服务
  • supervisorctl:用于治理咱们须要委托给 superviso 工具的服务
  • echo_supervisord_conf:用于生成 superviso 的配置文件
  • supervisor 的守护过程服务(用于接管过程治理命令)
  • 客户端(用于和守护过程通信, 发送治理过程的指令)
[root@Jumpserver /]# which supervisord
/bin/supervisord
[root@Jumpserver /]# which supervisorctl
/bin/supervisorctl
[root@Jumpserver /]# which echo_supervisord_conf
/bin/echo_supervisord_conf

配置 Supervisor

通过运行 echo_supervisord_conf 程序生成 supervisor 的初始化配置文件

如果应用 yum 装置则此步骤省略,间接进行批改配置文件步骤

mkdir /etc/supervisord.d
echo_supervisord_conf > /etc/supervisord.conf

批改配置文件

supervisor 的配置文件内容有很多,不过好多都不须要批改就行应用,我这里只批改了以下两项

# 批改 socket 文件的 mode,默认是 0700
sed -i 's/;chmod=0700/chmod=0766/g' /etc/supervisord.conf   

#在配置文件最初增加以下两行内容来蕴含 /etc/supervisord 目录
sed -i '$a [include] \
files = /etc/supervisord.d/*.conf' /etc/supervisord.conf

编写须要被 Supervisor 治理的过程

Supervisor 只能治理非 dameon 过程,像默认的 redis 为前台运行、Tomcat 其实是 startup.sh shutdown.sh 来调用 catalina.sh 进行后盾运行的,默认 catalina.sh 为前台运行的程序,不能治理像 Nginx 一样的非 dameon 过程

Tomcat 被 Supervisor 治理

Tomcat 装置如下:

wget http://us.mirrors.quenda.co/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 -y
tar xf apache-tomcat-8.5.47.tar.gz  -C /usr/local/
mv /usr/local/apache-tomcat-8.5.47 /usr/local/tomcat

想要咱们的利用被 Supervisor 治理,就须要在 /etc/supervisord 目录下编写配置文件,Tomcat 案例如下:

vim /etc/supervisord.d/tomcat.conf
[program:tomcat]                                        #程序惟一名称
directory=/usr/local/tomcat                             #程序门路
command=/usr/local/tomcat/bin/catalina.sh run           #运行程序的命令
autostart=true                                          #是否在 supervisord 启动后 tomcat 也启动
startsecs=10                                            #启动 10 秒后没有异样退出,就示意过程失常启动了,默认为 1 秒
autorestart=true                                        #程序退出后主动重启, 可选值:[unexpected,true,false],默认为 unexpected,示意过程意外杀死后才重启;意思为如果不是 supervisord 来敞开的该过程则认为不正当敞开,supervisord 会再次把该过程给启动起来,只能应用该 supervisorctl 来进行敞开、启动、重启操作 
startretries=3                                          #启动失败主动重试次数,默认是 3
user=root                                               #用哪个用户启动过程,默认是 root
priority=999                                            #过程启动优先级,默认 999,如果 Supervisord 须要治理多个过程,那么值小的优先启动
stopsignal=INT
redirect_stderr=true                                    #把 stderr 重定向到 stdout 规范输入,默认 false
stdout_logfile_maxbytes=200MB                           #stdout 规范输入日志文件大小,日志文件大小到 200M 后则进行切割,切割后的日志文件会标示为 catalina.out1,catalina.out2,catalina.out3...,默认 50MB
stdout_logfile_backups = 100                            #stdout 规范输入日志文件备份数,保留 100 个 200MB 的日志文件,超过 100 个后老的将被删除,默认为 10 保留 10 个
stdout_logfile=/usr/local/tomcat/logs/catalina.out      #规范日志输入地位,如果输入地位不存在则会启动失败
stopasgroup=false                                       #默认为 false, 过程被杀死时,是否向这个过程组发送 stop 信号,包含子过程
killasgroup=false                                       #默认为 false,向过程组发送 kill 信号,包含子过程

启动过程
应用 supervisord 治理启动后,当你应用 /usr/local/tomcat/shutdown.sh 或者 kill $PID 的时候,supervisord 都会认为是意外敞开,会主动再次把过程拉起,除非是应用 supervisord 命令敞开

#supervisord 启动
supervisord -c /etc/supervisord.conf                    #启动 supervisord 过程,咱们在配置文件中设置了 autostart=true 参数,在 supervisord 启动的时候 tomcat 也随之启动
ps -ef|grep java                                        #查看 tomcat 是否启动 

程序管理

supervisorctl status tomcat                             #tomcat 状态
supervisorctl stop tomcat                               #进行 tomcat
supervisorctl start tomcat                              #启动 tomcat
supervisorctl restart tomcat                            #重启 tomcat
supervisorctl reoload tomcat                            #重仔 tomcat

Redis 被 Supervisor 治理

redis 默认不在配置文件中增加 daemonize yes 参数则是前台启动的,所以也能够被咱们的的 Supervisor 所治理
redis 配置文件如下:

cat redis6001.conf
port 6001
bind 192.168.31.230
protected-mode yes
pidfile "/usr/local/redis/run/redis6001.pid"
loglevel notice
logfile "/usr/local/redis/logs/redis6001.log"
save 900 1
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum  yes
dbfilename dump.rdb
dir "/usr/local/redis/data/rdb/"
timeout 0
tcp-keepalive 300

编写 redis 被 Supervisor 治理的案例

vim /etc/supervisord.d/redis.conf
[program:redis]
directory=/usr/local/redis
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis6001.conf
autostart=true
startsecs=10
autorestart=true
startretries=3
user=root
priority=999
stopsignal=INT
redirect_stderr=true
stdout_logfile_maxbytes=200MB
stdout_logfile_backups = 100
stdout_logfile=/usr/local/redis/logs/redis6001.log
stopasgroup=false
killasgroup=false

应用 super 启动 redis

# 敞开 tomcat
supervisorctl stop tomcat
tomcat: stopped

#杀掉 supervisord
ps -ef|grep supervisord
root     26927     1  0 10:47 ?        00:00:00 /usr/bin/python /bin/supervisord -c /etc/supervisord.conf
root     27549 27402  0 11:07 pts/2    00:00:00 grep --color=auto super
kill -9 26927

#重新启动 supervisord 使其从新加载配置文件,supervisord 默认会把 redis 和 tomcat 都拉起来
supervisord -c /etc/supervisord.conf

程序管理

supervisorctl status redis                              #redis 状态
supervisorctl stop redis                                #进行 redis
supervisorctl start redis                               #启动 redis
supervisorctl restart reids                             #重启 redis
supervisorctl reoload redis                             #重载 redis

程序管理

程序管理

supervisorctl status all                            #查看所有过程状态
supervisorctl stop   all                            #进行所有过程
supervisorctl start  all                            #启动所有过程
supervisorctl restart all                           #重启所有过程
supervisorctl reoload all                           #重载所有过程

Supervisord 开启启动配置

vim /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target
systemctl enable supervisord
systemctl is-enabled supervisord

有段时间没跟大家分享资源福利了,看了下本人的资料夹,整顿了一些我认为比拟好的 Python 学习材料了。置信这套材料能够对你进阶高级工程师有帮忙

学习工具

大厂实战手册

自学视频(局部)

【材料收费支付形式】: 点这里:2020Python 高薪实战学习大合集

正文完
 0