zoukankan      html  css  js  c++  java
  • golang开发:环境篇(六) Go运行监控Supervisord的使用

    为什么要使用Supervisord

    17年第一次写Go项目的时候,用Go开发项目倒没没费多大劲,很快就开发完成了。到了在测试环境部署的时候,由于不知道有 Supervisord 这个软件,着实花了些功夫。总不能跟开发环境一样,直接执行编译生成的二进制文件吧,即使 后台执行了,万一它挂了,没人知道,即使测试人员发现了,开发还得登录到服务器再次启动下这个二进制文件。很明显这个解决方案没有任何意义,后来就在网上找解决方案。
    然后,咨询Go开发的前同事,发现了Supervisord,喜出望外。它就是最优的解决方案啊。

    Supervisord 是什么?
    Supervisord 是用 Python 实现的一款非常实用的进程管理工具,supervisord 还要求管理的程序是非 daemon 程序,supervisord 会帮你把它转成 daemon 程序,因此如果用 supervisord 来管理 nginx 的话,必须在 nginx 的配置文件里添加一行设置 daemon off 让 nginx 以非 daemon 方式启动。
    程序崩溃或者退出Supervisord会自动启动程序。这样就再不也不怕go的执行文件挂掉或者退出或者死掉,Supervisord只要监控到异常状态就会重启执行文件并且记录日志。

    官方的解释
    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.

    它是进程控制系统。监控和控制 Unix系统上进程。

    Supervisor 安装

    当然是在我们的虚拟机中安装 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.go

    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)
    	}
    }
    

    编译生成可执行文件,测试下是否访问是否正常

    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信号,包括子进程

    为上面我们生成 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!
    整个项目监控服务部署完成。

    Supervisord 的常用命令

    supervisorctl status 监控的程序的运行状态的列表
    supervisorctl stop test 停止监控的程序
    supervisorctl start test 启动监控的程序
    supervisorctl restart test 重启监控的程序
    supervisorctl update 更新监控的程序,如有新的配置添加一定要先执行update
    

    想了解更多,当然查看官方文档。
    http://supervisord.org/

  • 相关阅读:
    Flask11 Session、CSRF、注销session、利用端点自动跳转
    python学习笔记4-时间函数
    python学习笔记3-循环1
    python学习笔记2-条件语句
    python学习笔记1-基础语法
    sprintf系列函数
    sscanf非常的重要
    c++中.c_str和.c_data
    c++Map用法
    c语言sscanf总结
  • 原文地址:https://www.cnblogs.com/feixiangmanon/p/11067700.html
Copyright © 2011-2022 走看看