Linux開機時順序啓動項目並保證項目不掛機(systemctl+supervisor)

目錄

一、使用場景

二、具體配置

1.安裝supervisor

2.配置supervisor [program]

3.配置systemctl

三、命令簡述


一、使用場景

具體場景(可以拆分):

  • 開機立即啓動項目
  • 項目之間存在強依賴關係,需要按順序啓動
  • 項目進程不掛機

案例描述:

  • 先啓動JAVA項目sidecar,判斷它爲健康後,再啓動項目gateway(開機按順序啓動)
  • 當某個項目中途掛了,將會被supervisor自動喚起(保證項目不掛機)

啓動流程:

機器開啓 → systemctl啓動supervisord → supervisord自動啓動sidecar → systemctl調用腳本 → supervisord啓動gateway

可以使用在雲彈性伸縮自動生成機器時,按要求啓動項目等等場景。

二、具體配置

1.安裝supervisor

supervisor:用Python開發的一套通用的進程管理程序,能將一個普通的命令行進程變爲後臺daemon,並監控進程狀態,異常退出時能自動重啓。

安裝(Centos下):
yum install python-setuptools
easy_install supervisor

測試是否安裝成功:
echo_supervisord_conf

創建配置文件(如果沒權限,可以先創建文件,再move到etc下):
echo_supervisord_conf > /etc/supervisord.conf

2.配置supervisor [program]

概述:配置文件supervisord.conf,目的是配置supervisor託管子進程的方式

(1)執行命令編輯文件

vim /etc/supervisord.conf

(2)在配置文件後增加兩個 [program]

[program:sidecar] #子進程名稱
command = java -jar /data/application/sidecar/sidecar-0.0.1-SNAPSHOT.jar #啓動命令
autostart=true #立即啓動
autorestart=true #自動重啓
startsecs= 1 #重啓多久視爲啓動成功
startretries= 5 #啓動失敗重試次數
stdout_logfile = /var/log/supervisor/sidecar.log #進程日誌打印位置
stderr_logfile = /var/log/supervisor/sidecar.err.log #進程錯誤日誌打印位置
 
[program:gateway]
command = java -jar /data/application/gateway/gateway-0.0.1-SNAPSHOT.jar 
autostart=false #不立即啓動
autorestart=true
startsecs= 1
startretries= 5
stdout_logfile = /var/log/supervisor/gateway.log
stderr_logfile = /var/log/supervisor/gateway.err.log

注意:

  • 一般不會將program直接配置在supervisord.conf內,可以通過[include]指定配置的路徑
  • 在program下可以使用字段directroy指定文件目錄,但有可能導致子進程啓動失效,故本案例不使用

3.配置systemctl

概述:配置systemctl,是爲了讓服務器按順序啓動進程

(1)進入配置目錄

cd /usr/lib/systemd/system

(2)創建supervisord.service,使其開機啓動

sudo vim supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
  
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisord shutdown
ExecReload=/usr/bin/supervisord reload
killMode=process
  
[Install]
WantedBy=multi-user.target

 (3)創建gateway-start.service,使其在supervisor啓動後,再執行腳本啓動其他子進程

sudo vim gateway-start.service
[Unit]
Description=gateway-start Service
After=multi-user.target supervisord.service
Requires=supervisord.service
 
[Service]
Type=forking
ExecStart=/data/application/gateway-start.sh
StandardOutput=syslog
StandardError=inherit
Restart=on-failure
RestartSec=3s
 
[Install]
WantedBy=multi-user.target

注意:

  • After=multi-user.target supervisord.service  表示在supervisord啓動後執行
  • Requires=supervisord.service 表示他們是強依賴關係
  • Restart=on-failure 表示當腳本返回值不爲"0"時,進行重試
  • RestartSec 表示重試時間間隔

/data/application/目錄下需要創建腳本gateway-start.sh

cd /data/application # 進入目錄
sudo touch gateway-start.sh # 創建文件
sudo chmod +x ./gateway-start.sh  #授權可執行
#!/bin/bash
echo 'About to configure gateway'
start_status=`curl http://localhost:7101/status/sync` #獲取健康檢查狀態(自己寫好接口)
if [[ $start_status == 'synced' ]] #判斷通過健康檢查
then
        echo 'Sidecar status is ok,About to configure gateway'
        supervisorctl start gateway #使用supervisor啓動另一個項目
        echo 'Start up complete!'
        exit 0 #返回 0 時,systemctl視爲啓動成功,便不再重試
else
        echo 'Error! sidecar status is '${start_status}'! Stop start gateway'
        exit 2 #返回非 0 ,systemctl 將會重新調用該腳本 (因爲子進程的啓動需要時間,故需要做重試)
fi

 此時,需要的配置項都已完成。

三、命令執行

(1)配置開機啓動命令

概述:配置完成後,需要執行命令使得配置生效

//重新加載配置
sudo systemctl daemon-reload
 
//配置開機啓動
sudo systemctl enable supervisord
sudo systemctl enable zuul-start

(2)擴展

概述:一些常用的命令,用來使用工具

systemctl:

//配置開機啓動
sudo systemctl enable supervisord
 
//直接啓動項目(不重啓服務器)
sudo systemctl start supervisord

//重啓項目
sudo systemctl restart supervisord

//查看服務狀態
systemctl status supervisord.service

supervisor:

//直接啓動
sudo supervisord

//進入控制界面
sudo supervisorctl

//啓動項目
sudo supervisorctl start sidecar

//停止項目
sudo supervisorctl stop sidecar

//停止所有項目
sudo supervisorctl stop all

//查看日誌
cat /tmp/supervisord.log

 另外,還能通過配置supervisord.conf文件直接進入其控制檯

[inet_http_server]
port=192.168.196.216:9001
username=user
password=xbl

 

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