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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章