使用Supervisord软件管理go服务进程

一、介绍Supervisord软件
1、什么是Supervisord?
Supervisord是一个非常优秀的进程管理工具,使用Python开发。它可以在类UNIX系统的方式让用户来准确地监视和控制后台一定数量的服务进程。并作为一个天使进程让后台进程在当发生内部错误退出、或者进程被意外杀死时自动重启。除此之外,supervisord可以监控TCP端口,让其他主机通过客户端了命令supervisorctl通过HTTP协议直接对Server端进程进行启停,避免让进程/服务器管理者直接接触Shell或root用户。进程之间也有一个优先级和进程组关系,让管理员使用start all和stop all的关系来启动。

2、安装Supervisord软件

[root@yyljxl ~]# cat /etc/redhat-release
CentOS Linux release 7.x

[root@www ]# yum -y install supervisor
[root@yyljxl ~]# supervisorctl -h
supervisorctl -- control applications run by supervisord from the cmd line.

Usage: /usr/bin/supervisorctl [options] [action [arguments]]

Options:
-c/--configuration -- configuration file path (default /etc/supervisord.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
(default "http://localhost:9001").
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)

action [arguments] -- see below

Actions are commands like "tail" or "stop". If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started. Use the action "help" to find out about available
actions.
出现上面的帮助信息表示安装成功了。

#启动服务
systemctl start supervisord
systemctl enable supervisord
systemctl status supervisord

#supervisord监控的软件的配置说明
vim /etc/supervisor/conf.d
[program:项目名]
command=/data/www/go/src/test/test.bin
程序启动命令
autostart=true
在supervisord启动的时候也自动启动
startsecs=10
启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true
程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3
启动失败自动重试次数,默认是3
user=root
用哪个用户启动进程,默认是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=/data/logs/test/test.log
日志输出的文件地址
stopasgroup=false
默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false
默认为false,向进程组发送kill信号,包括子进程。

#官方文档
http://supervisord.org/

3、使用案例
示例:编写一个简单的go web项目
[root@web scripts]# cat mail.go
package main

import (
"fmt"
"net/http"
)

//w, 给客户端回复数据
//r, 读取客户端发送的数据
func HandConn(w http.ResponseWriter, r *http.Request) {
fmt.Println("r.Method = ", r.Method)
fmt.Println("r.URL = ", r.URL)
fmt.Println("r.Header = ", r.Header)
fmt.Println("r.Body = ", r.Body)

w.Write([]byte("hello go")) //给客户端回复数据
}

func main() {
//注册处理函数,用户连接,自动调用指定的处理函数
http.HandleFunc("/", HandConn)

//监听绑定
http.ListenAndServe(":8000", nil)
}

#上传程序到Centos7.x系统
yum install go -y

#build程序
[root@yyljxl scripts]# go build -o test

#生成可执行文件
[root@yyljxl scripts]# ll
total 6416
-rw-r--r-- 1 root root 542 Jun 24 09:15 mail.go
-rwxr-xr-x 1 root root 6562362 Jun 24 09:37 test

#运行程序
[root@yyljxl scripts]# nohup ./test &
[1] 14132
[root@yyljxl scripts]# nohup: ignoring input and appending output to ‘nohup.out’

#查看进程
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8000 :::* LISTEN 14132/ ./test

4、配置test程序用supervisord监控
[root@www scripts]#
mkdir -p /nulige/scripts/logs
touch /nulige/scripts/logs/test.log
#配置supervisor监控test
cat > /etc/supervisord.d/test.ini <<EOF
[program:test]
command=/nulige/scripts/test
autostart=true
startsecs=10
autorestart=true
startretries=3
user=root
priority=999
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups = 20
stdout_logfile=/nulige/scripts/logs/test.log
stopasgroup=false
killasgroup=false
EOF

#执行命令
[root@yyljxl scripts]# supervisorctl update
test: added process group
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09

#端口占用,导致没有载入成功,kill掉原端口程序,再重新载入
[root@yyljxl scripts]# supervisorctl status
test FATAL Exited too quickly (process log may have details)

#重新载入
[root@yyljxl scripts]# supervisorctl reload
Restarted supervisord

#更新监控的程序
[root@yyljxl scripts]# supervisorctl update

#查看运行状态
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09

#停止服务
[root@yyljxl scripts]# supervisorctl stop test
test: stopped

#测试杀当前程序的pid,再观察pid变化。

#查看进程
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name             
tcp6       0      0 :::8000                 :::*                    LISTEN      15385/test          

#杀进程
[root@yyljxl ~]# kill 15385
[root@yyljxl ~]# kill 15385
-bash: kill: (15385) - No such process

#查看进程,发现PID已变化,说明监控成功,服务停止后,会自启动。
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name        
tcp6       0      0 :::8000                 :::*                    LISTEN      15416/test          

5、supervisord常用命令

supervisorctl status #监控的程序的运行状态的列表
supervisorctl stop test #停止监控的程序
supervisorctl start test #启动监控的程序
supervisorctl restart test #重启监控的程序
supervisorctl update #更新监控的程序,如有新的配置添加一定要先执行update
supervisorctl reload #载入最新的配置文件,并按新的配置启动、管理所有进程。

原文地址:https://www.cnblogs.com/nulige/p/11075596.html