supervisior基础介绍
supervisord 是后台管理服务器,用来依据配置文件的策略管理后台守护进程,它会随着系统自动启动
supervisorctl 用于管理员向后台管理程序发送 启动 重启 停止 等指令
supervisorctl介绍
执行supervisorctl指令的时候指定配置文件
sudo supervisorctl -c /home/admin/supervisor/etc/supervisord.conf status
sudo supervisorctl -c /home/admin/supervisor/etc/supervisord.conf restart jlogstash
启动用户权限问题
如果是supervisior是以root用户启动的,那么普通用户就不能直接使用supervisorctl命令来操作supervisiord进程
启动说明
supervisor注册成系统服务
1.安装supervisor
2.把supervisor注册成系统服务
3.把所有的业务进程注册到supervisor中来统一管理
# taishi enterprise supervisord service for systemd (CentOS 7.0+)
[Unit]
Description=taishi Enterprise Supervisor daemon
After=taishi.service
[Service]
Type=forking
LimitNOFILE=655350
LimitNPROC=655350
ExecStart=/bin/bash __install_dir__/etc/start_taishi.sh
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=no
RestartSec=42s
[Install]
WantedBy=multi-user.target
taishi.service
#!/bin/bash
# add hostname into /etc/hosts if necessary
os_version=`cat /etc/redhat-release | grep -oE '[0-9]+\.[0-9\.]+'`
os_major_version=${os_version:0:1}
if [ $os_major_version -eq "7" ];then
hostname=`/usr/bin/hostname`
else
hostname=`/bin/hostname`
fi
ping -c 1 -q $hostname > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "127.0.0.1 $hostname" >> /etc/hosts
fi
if [ -e __install_dir__/jdk ]; then
setcap cap_net_bind_service=+epi __install_dir__/jdk/bin/java
fi
# chmod in order to support license check
chmod u+s `which dmidecode`
#elasticsearch memory locking
ulimit -l unlimited
/usr/bin/supervisord -c __install_dir__/etc/supervisord.conf
start-taishi.sh
#Hansight Enterprise supervisor config file.
[unix_http_server]
file=__install_dir__/tmp/supervisor.sock ; (the path to the socket file)
chown=__user__ ;
[inet_http_server] ; inet (TCP) server disabled by default
port=*: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)
[supervisord]
logfile=__install_dir__/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=__install_dir__/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
user=__user__ ; (default is current user, required if root)
minfds=655350 ; (min. avail startup file descriptors;default 1024)
minprocs=655350 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=hansight ; (default is current user, required if root)
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY="value" ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix://__install_dir__/tmp/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files=__install_dir__/etc/supervisord/*.ini
supervisord.conf
function Install_Supervisor()
{
#Install supervisord
cd ../src/supervisor/
tar -zxvf setuptools-24.0.2.tar.gz 2>&1 >/dev/null
cd setuptools-24.0.2/
python setup.py install >/dev/null 2>&1
cd ..
easy_install elementtree-1.2.7-20070827-preview.zip >/dev/null 2>&1
easy_install meld3-0.6.5.tar.gz 2>/dev/null 2>&1
easy_install supervisor-3.3.0.tar.gz >/dev/null 2>&1
cd ../../script/
mkdir -p ${INSTALL_DIR}/etc/
cp ../etc/supervisord.conf ${INSTALL_DIR}/etc/
sed -i "s#__install_dir__#${INSTALL_DIR}#g" ${INSTALL_DIR}/etc/supervisord.conf
sed -i "s#__user__#${USER}#g" ${INSTALL_DIR}/etc/supervisord.conf
ln -s /usr/bin/supervisorctl ${INSTALL_DIR}/hanctl
}
function Registration_System_Services()
{
cp ../etc/start_taishi.sh ${INSTALL_DIR}/etc/
sed -i "s#__install_dir__#${INSTALL_DIR}#g" ${INSTALL_DIR}/etc/start_taishi.sh
sed -i "s#__user__#${USER}#g" ${INSTALL_DIR}/etc/start_taishi.sh
chown ${USER}:${USER} ${INSTALL_DIR}/etc/start_taishi.sh
if [ ${os_major_version} -eq "7" ];then
\cp ../etc/taishi.service ../tmp/taishi.service
#注册成service
sed -i "s#__install_dir__#${INSTALL_DIR}#g" ../tmp/taishi.service
cp ../tmp/taishi.service /etc/systemd/system/
chown ${USER}:${USER} /etc/systemd/system/taishi.service
chmod 644 /etc/systemd/system/taishi.service
systemctl daemon-reload
else
#centos6上的注册服务形式
\cp ../etc/hanent ../tmp/hanent
#注册成service
sed -i "s#__install_dir__#${INSTALL_DIR}#g" ../tmp/hanent
cp ../tmp/hanent /etc/init.d/
chown ${USER}:${USER} /etc/init.d/hanent
chmod 755 /etc/init.d/hanent
chkconfig --add hanent
fi
}
安装脚本
systemctl start taishi
supervisor使用场景
supervisor比较适合监控业务应用,且只能监控前台程序,实现的daemon【后台启动】的程序不能用它监控,否则supervisor> status 会提示:BACKOFF Exited too quickly (process log may have details)
/app/chuangfa/taishi/zookeeper/bin/zkServer.sh start 后台方式运行 supervisor无法监控
/app/chuangfa/taishi/zookeeper/bin/zkServer.sh start-foreground 前台方式运行才能使用supervisor监控
/usr/sbin/nginx -c /app/chuangfa/taishi/etc/nginx.conf -g "daemon off;" 以前台方式运行nginx进程
/usr/sbin/nginx -c /app/chuangfa/taishi/etc/nginx.conf 后台方式启动nginx 无法被监控到
注册新的管理进程
supervisorctl update
重启所有进程
supervisorctl reload
Supervisor管理java应用
在supervisor的ini配置目录中添加配置启动文件
[program:admin]
directory=/home/admin/app
command=java -jar -Xmx2048m -Xms2048m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m admin-dev.jar
priority=120
process_name=%(program_name)s
autostart=true
startsecs=60
user=admin
redirect_stderr=true
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=10
stdout_logfile=/home/admin/supervisor/logs/admin_supervisord.log
admin.ini
添加好配置文件后就表示此程序已经可以被supervisor管理起来
script {
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart register'", returnStatus: true)
if(res != 0){
error("register服务启动失败,本次发布流程终止")
}
//sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-gateway.sh'"
// res = sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-gateway.sh'", returnStdout: true)
//if (res.contains("Start Fail")){
// error("gateway服务启动失败,本次发布流程终止")
//}
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart gateway'", returnStatus: true)
if(res != 0){
error("gateway服务启动失败,本次发布流程终止")
}
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart admin'", returnStatus: true)
if(res != 0){
error("admin服务启动失败,本次发布流程终止")
}
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdccollectconfig'", returnStatus: true)
if(res != 0){
error("sdccollectconfig服务启动失败,本次发布流程终止")
}
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdcruleconfig'", returnStatus: true)
if(res != 0){
error("sdc-rule-config服务启动失败,本次发布流程终止")
}
res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdces'", returnStatus: true)
if(res != 0){
error("sdces服务启动失败,本次发布流程终止")
}
}
流水线
通过流水线脚本自动重启相关服务
supervisor启动时候找不到命令
directory=/app/taishi/app/register-center
command=/bin/bash -c 'source "$0" && exec "$@"' /etc/profile java -jar -Xmx2048m -Xms2048m -Dspring.profiles.active=prod -XX:MetaspaceSize=128m - XX:MaxMetaspaceSize=256m register-center-prod.jar
成功启动java -jar
supervisor通信方式
supervisor通常有以下两种方式通信:
1、本地socket
2、http连接 默认通过rpc连接9001端口进行通信
在supervisor.conf配置文件中,启用socket连接,注释http方式,supervisorctl配置项也使用unix socket
supervisor管理进程重启策略
supervisor更新服务进程
supervisor修改管理服务的ini文件后 不能直接重启 直接重启服务不会生效
必须先执行supervisorctl update