五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

詳解Shell腳本控制docker容器啟動順序

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

1.遇到的問題

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

2.解決思路

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

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

3.端口探測

端口探測使用的命令是

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

host:目標主機的ip

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

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

4.Shell腳本

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

#!/bin/bash
#chkconfig: 2345 80 90
#description:autoStartMaintenanceService.sh
#
#前提:
#1.docker必須能開機自啟
#2.docker能夠正常啟動運維服務(wù)
#3.此腳本須運行微服務(wù)所在的機器上
#
##需要修改的配置-----開始
##數(shù)據(jù)庫所在的機器IP
DATABASE_HOST=192.169.1.52
##數(shù)據(jù)庫監(jiān)聽的端口
DATABASE_PORT=3306
##微服務(wù)所在機器IP
LOCAL_HOST=192.169.1.46
##微服務(wù)訪問端口
Maintenance_Port=8180
##NACOS所在機器的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)延時時間(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ù)庫是否啟動
  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是否啟動
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
}
##啟動微服務(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輸入輸出重定向

寫這個腳本的時候,也讓博主對Shell輸入輸出重定向更加熟悉

一般情況下,每個 Unix/Linux 命令運行時都會打開三個文件:

  • 標準輸入文件(stdin):stdin的文件描述符為0,Unix程序默認從stdin讀取數(shù)據(jù)。
  • 標準輸出文件(stdout):stdout 的文件描述符為1,Unix程序默認向stdout輸出數(shù)據(jù)。
  • 標準錯誤文件(stderr):stderr的文件描述符為2,Unix程序會向stderr流中寫入錯誤信息。

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

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

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

/dev/null文件

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

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

參考

菜鳥教程-Shell

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

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

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

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

關(guān)注
微信

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