17年第一次写Go项目的时候,用Go开发项目倒没没费多大劲,很快就开发完成了。到了在测试环境部署的时候,因为不知道有 Supervisord 这个软件,着实花了些功夫。总不能跟开发环境同样,直接执行编译生成的二进制文件吧,即便 后台执行了,万一它挂了,没人知道,即便测试人员发现了,开发还得登陆到服务器再次启动下这个二进制文件。很明显这个解决方案没有任何意义,后来就在网上找解决方案。 而后,咨询Go开发的前同事,发现了Supervisord,喜出望外。它就是最优的解决方案啊。html
Supervisord 是什么? Supervisord 是用 Python 实现的一款很是实用的进程管理工具,supervisord 还要求管理的程序是非 daemon 程序,supervisord 会帮你把它转成 daemon 程序,所以若是用 supervisord 来管理 nginx 的话,必须在 nginx 的配置文件里添加一行设置 daemon off 让 nginx 以非 daemon 方式启动。 程序崩溃或者退出Supervisord会自动启动程序。这样就再不也不怕go的执行文件挂掉或者退出或者死掉,Supervisord只要监控到异常状态就会重启执行文件而且记录日志。nginx
官方的解释 Supervisor: A Process Control System Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as “process id 1”. Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.web
它是进程控制系统。监控和控制 Unix系统上进程。vim
固然是在咱们的虚拟机中安装 Supervisor服务器
sudo vagrant ssh apt-get install supervisor supervisorctl -h supervisorctl -- control applications run by supervisord from the cmd line. Usage: /usr/bin/supervisorctl [options] [action [arguments]] Options: -c/--configuration FILENAME -- configuration file path (default /etc/supervisord.conf) -h/--help -- print usage message and exit 出现上面的帮助信息表示安装成功了 安装完成后,能够查看配置 vim /etc/supervisor/supervisord.conf 看到 [include] files = /etc/supervisor/conf.d/*.conf supervisord 监控的项目的配置必须部署到 /etc/supervisor/conf.d/目录下, 而且使用conf后缀
找个简单的Go Web的项目试试supervisor怎么监控软件运行的。 首先生成一个Go编译生成的二进制可执行文件 main.goapp
package main import ( "fmt" "net/http" "log" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello China!") } func main() { http.HandleFunc("/", sayhelloName) err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
编译生成可执行文件,测试下是否访问是否正常ssh
go build -o test.gobin ./test.gobin curl http://192.168.0.10:9090 Hello China! 说明Go Web是正常的
接下来咱们用使用Supervisord监控这个Go web程序。 先看下Supervisord监控的软件的配置说明 [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信号,包括子进程curl
为上面咱们生成 test.bin 的执行文件建立一个Supervisord监控的配置文件,配置文件必须在 /etc/supervisor/conf.d 目录下工具
cd /etc/supervisor/conf.d vim test.conf [program:test] command=/data/www/go/src/test/test.bin 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=/data/logs/test/test.log stopasgroup=false killasgroup=false
运行Supervisord,使之监控 test.gobin测试
添加了新的配置以后必定须要执行 supervisorctl update 会提示 test: added process group 表示test添加成功了。 查看执行状态 supervisorctl status test RUNNING pid 4354, uptime 0:00:16
这样表示运行成功了。 咱们请求试下test.bin的服务是否正常。
curl http://192.168.0.10:9090 Hello China! 整个项目监控服务部署完成。
supervisorctl status 监控的程序的运行状态的列表 supervisorctl stop test 中止监控的程序 supervisorctl start test 启动监控的程序 supervisorctl restart test 重启监控的程序 supervisorctl update 更新监控的程序,若有新的配置添加必定要先执行update
想了解更多,固然查看官方文档。 http://supervisord.org/
原文出处:https://www.cnblogs.com/feixiangmanon/p/11067700.html