一、介紹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 #載入最新的配置文件,並按新的配置啟動、管理所有進程。