Supervisor多程式管理 異常自動重啟 視覺化管理

Java知識圖譜發表於2022-02-07

一、序言

Supervisor是多程式管理工具,在Docker中相關聯的程式能夠通過supervisor來管理。

微服務專案開發階段,可用於微服務子專案的啟動管理。

支援web視覺化管理,能夠極大方面開發者對專案狀態的監控和重啟管理。

二、安裝與使用

(一)安裝與配置

1、服務安裝

服務安裝前,建議更新Python版本,使用較新的版本有利於服務擴充,若被管理的服務依賴於較新的Python版本,需要再次重新安裝服務。

yum install -y epel-release
yum install -y supervisor

檢視版本號

supervisord -v
2、配置檔案

配置檔案路徑為/etc/supervisord.conf,其中用英文;表示註釋。將配置檔案備份,過濾註釋配置後形成新的配置檔案。

# 備份配置檔案
mv /etc/supervisord.conf /etc/supervisord.example.conf
# 保留非註釋配置,初始化為新的配置檔案
cat /etc/supervisord.example.conf | grep -v '^;' | tr -s "\n" > /etc/supervisord.conf

使用命令echo_supervisord_conf檢視預設配置

[unix_http_server]
file=/var/run/supervisor/supervisor.sock

; 視覺化web監控模組(不需要直接註釋)
[inet_http_server]
port=0.0.0.0:9001
username=root
password=root

[supervisord]
logfile=/var/log/supervisor/supervisord.log
; 最大單個日誌檔案大小
logfile_maxbytes=50MB
; 最大日誌檔案保留份數
logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid
; 如果將設定為系統服務,需要設定為false
nodaemon=false
minfds=1024
minprocs=200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock

; 子程式配置存放目錄
[include]
files = supervisord.d/*.ini

通過如下命令檢視版本號:

supervisord -v

(二)啟動

1、後臺啟動

將supervisord設定成開機自啟,保證委託其管理的服務能夠正常啟動,建議使用此方式啟動。

# 設定開機自啟
systemctl enable supervisord

# 啟動主服務
systemctl start supervisord
2、前臺啟動

在編寫Docker映象,需要在一個映象中同時管理多個服務,需要使用前臺啟動。supervisord的預設啟動方式是daemon,若要配置為前臺啟動需修改配置檔案/etc/supervisord.confnodaemon屬性值為true

# 使用指令碼替換
sed -i 's/nodaemon=false/nodaemon=true/g' /etc/supervisord.conf

前臺啟動命令如下

supervisord -c /etc/supervisord.conf
3、啟動引數

通常情況下引數都是新增在配置檔案中,有些場景下,修改配置檔案比較繁瑣(比如已經生成的映象),這時在命令列中新增執行時引數就比較方便。

引數 用途 預設值
-c 指定配置檔案路徑 /etc/supervisord.conf
-s supervisord伺服器監聽的URL http://localhost:9001
-u 用於與伺服器進行身份驗證的使用者名稱 user
-p 用於與伺服器進行身份驗證的密碼 123

三、服務管理

服務管理包含對主服務進行管理和對子服務進行管理;子服務分為單個管理和批量(分組)管理。

1、檢視主服務狀態

如果不指定子服務名稱,預設重新檢視所有的子服務狀態。指定子服務名,僅僅檢視當前子服務狀態。

# 檢視服務狀態
supervisorctl status

主程式管理

#程式管理常用命令
systemctl stop supervisord
systemctl start supervisord
systemctl restart supervisord
2、視覺化介面管理

視覺化介面在軟體的不同開發階段採用不同的策略,專案開發和測試階段,為了提高開發效率,往往開啟視覺化介面,當專案開發完畢進行交付時,為保證伺服器的安全,通常關閉視覺化介面。

開啟Web視覺化服務需要在配置檔案中新增inet_http_server模組。

(一)單服務管理

單服務管理是指標對單個子服務進行管理,所有子服務在預設分組中,但是不顯示的標出。

1、重新整理子服務列表

當新增新加入子服務時,需要重新整理列表,主服務方能納入管理範疇。

(1)reload

如果不指定子服務名稱,預設重新啟動所有的子服務列表。指定子服務名,僅僅重啟當前子服務,其它服務不受影響。

所有子服務是指不管配置是否發生修改,都會重啟。

# 重新整理服務列表
supervisorctl reload

(2)update

重啟所有配置發生更改的子服務(包含新增子服務),配置未發生變化的子服務不重啟。

# 重新整理服務列表
supervisorctl update
2、程式管理執行

此種方式管理子程式顆粒度更小。

# 啟動指定服務
supervisorctl start program_name
# 停止指定服務
supervisorctl stop program_name
# 重啟指定服務
supervisorctl restart program_name
# 啟動所有服務
supervisorctl start all
# 停止所有服務
supervisorctl stop all

(二)分組管理

當有相關聯的子服務時,可採用分組管理,一旦設定分組並新增子服務,那麼子服務名稱就會發生變化:由原來的program_name變成group_name:program_name,比如redis:redis80

分組管理需要修改主服務配置檔案。

1、檢視分組子服務列表

檢視指定分組名稱下子服務列表,

# 檢視分組子服務列表
supervisorctl status group_name:
2、分組子程式管理

以組為單位對子程式進行管理,包含啟動服務、停止服務、重啟服務。

# 啟動指定組名下服務
supervisorctl start group_name:
# 停止指定組名下服務
supervisorctl stop group_name:
# 重啟指定組名下服務
supervisorctl restart group_name:

注意組名後的冒號:

3、分組應用

將程式委託給Supervisor管理,並分組對於一組關聯程式來說很方便,比如Redis主從服務、ES叢集、ZK叢集、Kafka叢集,他們是一組關聯度較高的子服務集合。

四、編寫子程式執行配置檔案

supervisor主程式配置檔案為/etc/supervisord.conf

在目錄/etc/supervisord.d下新建以.ini為字尾的配置檔案,每一個配置檔案代表一個子程式。執行如下命令,即可新增子程式配置。

快捷指令碼傳送門

(一)引數解釋

1、directory

當子程式啟動命令不能從環境變數讀取到時,使用此引數切換到指定的工作目錄,然後執行入口命令。

2、priority

priority引數越大時,優先順序越低。

3、environment

如果子應用無法獲取系統環境變數,那麼可顯式指明特定環境的路徑。

environment=JAVA_HOME=/usr/local/java

(二)日誌管理

1、檢視子程式日誌

子程式被Supervisor管理後會產生相應的執行日誌,常見的有訪問日誌和錯誤日誌。

; 訪問日誌
stdout_logfile=/var/log/park/access.log
; 錯誤日誌
stderr_logfile=/var/log/park/error.log

在子程式配置檔案中增加日誌配置,可以在不使用視覺化介面的情況下檢視子程式日誌。視覺化Web介面檢視日誌固然方便,缺陷是不能檢視錯誤日誌。

tail -f /var/log/park/access.log

子程式配置檔案新增引數stdout_logfilestderr_logfile的日誌檔案會自動納入主程式日誌管理,自動進行日誌輪轉操作,使用者無需干預。

當子程式未顯示的指明日誌檔案路徑時,預設日誌檔案存在於/tmp路徑下。

Supervisord 會基於 logfile_maxbyteslogfile_backups 輪轉日誌,前者限制單個日誌檔案的大小,後者限制日誌備份的數量。此配置存在與主配置檔案,非子程式配置檔案。

(三)常見元件配置

1、Nginx
cat <<EOF> /etc/supervisord.d/nginx.ini
[program:nginx]
directory=/usr/local/nginx/sbin
command=/usr/local/nginx/sbin/nginx -g 'daemon off;'
; 主服務啟動時自動啟動當前子服務
autostart=true
; 子服務異常退出自動重啟
autorestart=true
; 子服務啟動時間(與時間情況儘量一致)
startsecs=5
startretries=3
redirect_stderr=true
stdout_logfile=/usr/local/nginx/logs/access.log
stderr_logfile=/usr/local/nginx/logs/error.log
EOF

子程式的命令必須是以前臺執行的方式執行,不能使用後臺執行的方式執行。

2、Redis
cat <<EOF> /etc/supervisord.d/redis.ini
[program:redis]
command=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf --daemonize no
autostart=true
autorestart=true
startsecs=5
startretries=3
redirect_stderr=true
EOF

子程式的命令必須是以前臺執行的方式執行,不能使用後臺執行的方式執行。

3、Nacos
cat <<EOF> /etc/supervisord.d/nacos.ini
[program:nacos]
command=sh /usr/local/nacos/bin/startup.sh -m standalone
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
environment=JAVA_HOME=/usr/local/java
priority=1
EOF

子程式的命令必須是以前臺執行的方式執行,不能使用後臺執行的方式執行。

4、ElasticSearch
cat <<EOF> /etc/supervisord.d/es.ini
[program:es]
command=/usr/local/elasticsearch/bin/elasticsearch -Enetwork.host=127.0.0.1
user=es
password=es.123.456
umask=002
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
priority=100
EOF
5、ZooKeeper
cat <<EOF> /etc/supervisord.d/zk.ini
[program:zk]
command=/usr/local/zookeeper/bin/zkServer.sh start-foreground
autostart=true
autorestart=true
startsecs=5
startretries=3
redirect_stderr=true
priority=100
EOF
6、Jenkins
cat <<EOF> /etc/supervisord.d/jenkins.ini
[program:jenkins]
command=/usr/local/jenkins/bin/catalina.sh run
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
priority=100
EOF
7、Kafka
cat <<EOF> /etc/supervisord.d/kafka.ini
[program:kafka]
command=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
priority=100
EOF
8、Kibaba
cat <<EOF> /etc/supervisord.d/kibana.ini
[program:kibana]
command=/usr/local/kibana/bin/kibana -H 0.0.0.0
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
EOF
9、MongoDb
cat <<EOF> /etc/supervisord.d/mongo.ini
[program:mongo]
command=/usr/local/mongo/bin/mongod --config=/usr/local/mongo/conf/config.yml
autostart=true
autorestart=true
startsecs=8
startretries=3
redirect_stderr=true
priority=100
EOF

原文地址

相關文章