
一、序言
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.conf中nodaemon屬性值為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_logfile和stderr_logfile的日志檔案會自動納入主行程日志管理,自動進行日志輪轉操作,用戶無需干預,
當子行程未顯示的指明日志檔案路徑時,默認日志檔案存在于/tmp路徑下,
Supervisord 會基于 logfile_maxbytes 和 logfile_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
原文地址
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/423455.html
標籤:其他
