2021-03-05

Shell脚本控制docker容器启动顺序

1.遇到的问题

在分布式项目部署的过程中,经常要求服务器重启之后,应用(包括数据库)能够自动恢复使用.虽然使用docker update --restart=always containerid能够让容器自动随docker启动,但是并不能保证是在数据库启动之后启动,如果数据库未启动,那么将导致应用启动失败;网上还有一种解决方法是通过docker-compose容器编排来控制启动顺序,这个博主研究的比较少.

2.解决思路

使用Shell脚本来控制,思路大致如下

  1. 探测数据库端口来检验数据库是否启动成功.
  2. 数据库启动成功后,探测配置中心及服务注册中心的端口来检验其是否启动成功.
  3. 当数据库及配置中心都启动之后,再启动其他微服务应用.

3.端口探测

端口探测使用的命令是

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

host:目标主机的ip

port:服务监听的端口

如果服务启动了 这条命令会返回 200,未启动则返回空.

4.Shell脚本

直接贴代码了,使用的配置中心是nacos

#!/bin/bash#chkconfig: 2345 80 90#description:autoStartMaintenanceService.sh##前提:#1.docker必须能开机自启#2.docker能够正常启动运维服务#3.此脚本须运行微服务所在的机器上###需要修改的配置-----开始##数据库所在的机器IPDATABASE_HOST=192.169.1.52##数据库监听的端口DATABASE_PORT=3306##微服务所在机器IPLOCAL_HOST=192.169.1.46##微服务访问端口Maintenance_Port=8180##NACOS所在机器的ipNACOS_HOST=192.169.1.82##NACOS的监听端口NACOS_PORT=8848##微服务容器名称(NAMES列)Maintenance_Container_Name="umc-maintenance"##该脚本生成的日志路径Log_Path=/home/test/log##需要修改的配置-----结束####循环延时时间(s)秒LOOP_TIME=5at_time=""at_date=""getAtTime() { at_time="$(date +%Y-%m-%d-%H:%M:%S) --- " at_date=$(date +%Y-%m-%d)}autoStartWebService() { ##如果日志路径不存在则创建 if [ ! -d "$Log_Path" ]; then  mkdir -p $Log_Path fi while true; do  ##判断数据库是否启动  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}##启动微服务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读取数据。
  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
  • 标准错误文件(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 和结束标记 tag 之间的内容作为输入

如果希望将 stdout 和 stderr 合并后重定向到 file(即将正确信息和错误信息都输出到file),可以这样写:

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

/dev/null文件

/dev/null是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到禁止输出的效果

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

参考

菜鸟教程-Shell









原文转载:http://www.shaoqun.com/a/606585.html

跨境电商:https://www.ikjzd.com/

薇美铺:https://www.ikjzd.com/w/2312

拍拍:https://www.ikjzd.com/w/2205


1.遇到的问题在分布式项目部署的过程中,经常要求服务器重启之后,应用(包括数据库)能够自动恢复使用.虽然使用dockerupdate--restart=alwayscontainerid能够让容器自动随docker启动,但是并不能保证是在数据库启动之后启动,如果数据库未启动,那么将导致应用启动失败;网上还有一种解决方法是通过docker-compose容器编排来控制启动顺序,这个博主研究的比较少.
母婴团购网:https://www.ikjzd.com/w/716
epa认证:https://www.ikjzd.com/w/1769
csa认证:https://www.ikjzd.com/w/904
美国加州刚宣布全州"居家",亚马逊物流就禁运了?:https://www.ikjzd.com/home/118552
和乔国际商务:https://www.ikjzd.com/w/2455
亚马逊平台新产品上市的分阶段广告策略:https://www.ikjzd.com/tl/91120

No comments:

Post a Comment