人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

詳解Shell腳本控制docker容器啟動(dòng)順序

發(fā)布日期:2021-12-27 00:10 | 文章來源:源碼之家

1.遇到的問題

在分布式項(xiàng)目部署的過程中,經(jīng)常要求服務(wù)器重啟之后,應(yīng)用(包括數(shù)據(jù)庫)能夠自動(dòng)恢復(fù)使用.雖然使用docker update --restart=always containerid能夠讓容器自動(dòng)隨docker啟動(dòng),但是并不能保證是在數(shù)據(jù)庫啟動(dòng)之后啟動(dòng),如果數(shù)據(jù)庫未啟動(dòng),那么將導(dǎo)致應(yīng)用啟動(dòng)失敗;網(wǎng)上還有一種解決方法是通過docker-compose容器編排來控制啟動(dòng)順序,這個(gè)博主研究的比較少.

2.解決思路

使用Shell腳本來控制,思路大致如下

探測(cè)數(shù)據(jù)庫端口來檢驗(yàn)數(shù)據(jù)庫是否啟動(dòng)成功.數(shù)據(jù)庫啟動(dòng)成功后,探測(cè)配置中心及服務(wù)注冊(cè)中心的端口來檢驗(yàn)其是否啟動(dòng)成功.當(dāng)數(shù)據(jù)庫及配置中心都啟動(dòng)之后,再啟動(dòng)其他微服務(wù)應(yīng)用.

3.端口探測(cè)

端口探測(cè)使用的命令是

nc -w 1 host port </dev/null && echo "200"

host:目標(biāo)主機(jī)的ip

port:服務(wù)監(jiān)聽的端口

如果服務(wù)啟動(dòng)了 這條命令會(huì)返回 200,未啟動(dòng)則返回空.

4.Shell腳本

直接貼代碼了,使用的配置中心是nacos

#!/bin/bash
#chkconfig: 2345 80 90
#description:autoStartMaintenanceService.sh
#
#前提:
#1.docker必須能開機(jī)自啟
#2.docker能夠正常啟動(dòng)運(yùn)維服務(wù)
#3.此腳本須運(yùn)行微服務(wù)所在的機(jī)器上
#
##需要修改的配置-----開始
##數(shù)據(jù)庫所在的機(jī)器IP
DATABASE_HOST=192.169.1.52
##數(shù)據(jù)庫監(jiān)聽的端口
DATABASE_PORT=3306
##微服務(wù)所在機(jī)器IP
LOCAL_HOST=192.169.1.46
##微服務(wù)訪問端口
Maintenance_Port=8180
##NACOS所在機(jī)器的ip
NACOS_HOST=192.169.1.82
##NACOS的監(jiān)聽端口
NACOS_PORT=8848
##微服務(wù)容器名稱(NAMES列)
Maintenance_Container_Name="umc-maintenance"
##該腳本生成的日志路徑
Log_Path=/home/test/log
##需要修改的配置-----結(jié)束
##
##循環(huán)延時(shí)時(shí)間(s)秒
LOOP_TIME=5
at_time=""
at_date=""
getAtTime() {
 at_time="$(date +%Y-%m-%d-%H:%M:%S) --- "
 at_date=$(date +%Y-%m-%d)
}
autoStartWebService() {
 ##如果日志路徑不存在則創(chuàng)建
 if [ ! -d "$Log_Path" ]; then
  mkdir -p $Log_Path
 fi
 while true; do
  ##判斷數(shù)據(jù)庫是否啟動(dòng)
  req_message=$(nc -w 1 ${DATABASE_HOST} ${DATABASE_PORT} </dev/null && echo "200")
  if [ -n "$req_message" ]; then
   getAtTime
   echo "$at_time Database is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
   waitNacosStarting
  else
   getAtTime
   echo "$at_time Database is not running and please wait for Database starting" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
   sleep $LOOP_TIME
  fi
 done
}
##判斷Nacos是否啟動(dòng)
waitNacosStarting() {
 req_message=$(nc -w 1 ${NACOS_HOST} ${NACOS_PORT} </dev/null && echo "200")
 if test $((req_message)) -eq 200; then
  getAtTime
  echo "$at_time Nacos is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
  startMaintenanceService
  sleep $LOOP_TIME
 else
  getAtTime
  echo "$at_time Nacos is not running and please wait for nacos starting" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
  sleep $LOOP_TIME
 fi
}
##啟動(dòng)微服務(wù)
startMaintenanceService() {
 req_message=$(nc -w 1 ${LOCAL_HOST} ${Maintenance_Port} </dev/null && echo "200")
 if test $((req_message)) -eq 200; then
  getAtTime
  echo "$at_time Maintenance service is running" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
 else
  container_id=$(docker ps -a | grep $Maintenance_Container_Name | grep -v grep | awk '{print $1}')
  getAtTime
  echo "$at_time Maintenance service container id is ${container_id}" >>${Log_Path}/"$at_date"_autoStartMaintenanceService.log
  docker start ${container_id}
 fi
}
autoStartWebService

5.Shell輸入輸出重定向

寫這個(gè)腳本的時(shí)候,也讓博主對(duì)Shell輸入輸出重定向更加熟悉

一般情況下,每個(gè) Unix/Linux 命令運(yùn)行時(shí)都會(huì)打開三個(gè)文件:

  • 標(biāo)準(zhǔn)輸入文件(stdin):stdin的文件描述符為0,Unix程序默認(rèn)從stdin讀取數(shù)據(jù)。
  • 標(biāo)準(zhǔn)輸出文件(stdout):stdout 的文件描述符為1,Unix程序默認(rèn)向stdout輸出數(shù)據(jù)。
  • 標(biāo)準(zhǔn)錯(cuò)誤文件(stderr):stderr的文件描述符為2,Unix程序會(huì)向stderr流中寫入錯(cuò)誤信息。

命令說明
command > file將輸出重定向到 file且會(huì)覆蓋file
command < file將輸入重定向到 file
command >> file將輸出以追加的方式重定向到file
command 2> file將錯(cuò)誤輸出到file且會(huì)覆蓋file
command 2>> file將錯(cuò)誤以追加的方式重定向到file
<< tag將開始標(biāo)記 tag 和結(jié)束標(biāo)記 tag 之間的內(nèi)容作為輸入

如果希望將 stdout 和 stderr 合并后重定向到 file(即將正確信息和錯(cuò)誤信息都輸出到file),可以這樣寫:

command > file 2>&1
或者
command >> file 2>&1

/dev/null文件

/dev/null是一個(gè)特殊的文件,寫入到它的內(nèi)容都會(huì)被丟棄;如果嘗試從該文件讀取內(nèi)容,那么什么也讀不到。但是 /dev/null 文件非常有用,將命令的輸出重定向到它,會(huì)起到禁止輸出的效果

command > /dev/null 2>&1 可以屏蔽stdout和stderr

參考

菜鳥教程-Shell

到此這篇關(guān)于Shell腳本控制docker容器啟動(dòng)順序的文章就介紹到這了,更多相關(guān)Shell腳本控制docker內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部