小知识:详解Shell脚本控制docker容器启动顺序

1.遇到的问题

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

2.解决思路

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

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

3.端口探测

端口探测使用的命令是

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

host:目标主机的ip

port:服务监听的端口

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

4.Shell脚本

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
#chkconfig: 2345 80 90
#description:autoStartMaintenanceService.sh
#
#前提:
#1.docker必须能开机自启
#2.docker能够正常启动运维服务
#3.此脚本须运行微服务所在的机器上
#
##需要修改的配置—–开始
##数据库所在的机器IP
DATABASE_HOST=192.169.1.52
##数据库监听的端口
DATABASE_PORT=3306
##微服务所在机器IP
LOCAL_HOST=192.169.1.46
##微服务访问端口
Maintenance_Port=8180
##NACOS所在机器的ip
NACOS_HOST=192.169.1.82
##NACOS的监听端口
NACOS_PORT=8848
##微服务容器名称(NAMES列)
Maintenance_Container_Name=”umc-maintenance”
##该脚本生成的日志路径
Log_Path=/home/test/log
##需要修改的配置—–结束
##
##循环延时时间(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() {
##如果日志路径不存在则创建
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),可以这样写:

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

/dev/null文件

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

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

参考

菜鸟教程-Shell

到此这篇关于Shell脚本控制docker容器启动顺序的文章就介绍到这了,更多相关Shell脚本控制docker内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/songsongsun/archive/2021/03/05/14486371.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:如何使用docker对容器资源进行限制

2023-3-24 11:46:52

建站知识

小知识:docker安装ElasticSearch:7.8.0集群的详细教程

2023-3-24 12:01:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索