事情的起因是这样的,我们公司使用的是单点服务,在日常环境下,机器重启时,会造成服务一段时间内不可用,影响日常开发和测试。
于是我们想到用docker-compose来实现一台服务器做服务的分批部署
单机做分批部署的逻辑大概如下,在部署时,需要保证两个服务的启动顺序,停止服务A时,服务B在正常的提供对外服务,服务A启动好后自行启动,监听到服务A启动完成后,停止服务B,最后再启动服务B
注意,如果要做得比较细致的话,启动服务B后,是需要等待启动完成后,才能结束流程的,否则如果服务B因为网络等外部因素部署失败了,下次部署时,停止A之后,会造成服务不可用
我们要满足以下几点要求
方案详情
让我们先来看下目前的 docker-compose.yaml 文件的配置情况,
version: '2.2'
services:
serviceName:
image: ${URL}
container_name: name1
network_mode: host
restart: on-failure
volumes:
- /data:/data
mem_limit: 2G
version:Compose文件版本,与Docker引擎版本是配套的,对应关系见附录
services:定义服务列表
serviceName:服务名称,可自定义,不能重复
image:docker镜像地址
container_name:容器名称,可自定义,不能重复
network_mode:网络模式,和docker --network相同,有4种,默认是bridge,host是直接绑定到 Docker 主机,没有网络隔离
restart:重启方式,有4种,默认是no,on-failure是如果容器退出时出现异常,则重启该容器
volumes:卷挂载,配置docker和宿主机的卷路径映射,服务的日志映射需要在这里配置
mem_limit:docker容器的内存限制
改造后的 docker-compose.yaml 文件如下
version: '2.2'
services:
serviceName1:
image: ${URL}
container_name: container_name1
network_mode: host
restart: on-failure
volumes:
- /data:/data
mem_limit: 1G
serviceName2:
image: ${URL}
container_name: container_name2
network_mode: host
restart: on-failure
volumes:
- /data:/data
mem_limit: 1G
depends_on:
- serviceName1
environment:
# 该服务端口 不能与serviceName1的端口一致
- SERVER_PORT=8081
# Dubbo 端口地址 需要则打开
# - DUBBO_PROTOCOL_PORT=20042
serviceName1:采用serviceName后加序号的方式来区分多节点
mem_limit:内存减半,同台机器上,内存有限
depends_on:并不是等待 serviceName1 启动完成后启动,而是 serviceName1 开始启动后启动
environment:环境配置,如果没有使用Dubbo的话,不需要配置 DUBBO_PROTOCOL_PORT
${port} 改成 serviceName1 的端口号
#!/bin/bash
docker-compose pull
docker-compose stop serviceName1
docker-compose rm -f serviceName1
docker-compose up -d serviceName1
sh ./wait-for-it.sh 127.0.0.1:${port} --timeout=300
sleep 3
TAG=qa docker-compose stop serviceName2
TAG=qa docker-compose rm -f serviceName2
TAG=qa docker-compose up -d serviceName2
建议将 start.sh 、docker-compose.yaml 、wait-for-it.sh 文件放在同级目录
个人建议在 serviceName1运行完成之后,再等待3秒,保证资源完全初始化,如果不想等的话可以把 sleep 3 去掉
将 start.sh 、docker-compose.yaml 、wait-for-it.sh 文件放在同级目录后,执行 start.sh
sh start.sh
如果提示没有执行权限,则执行
sudo chmod u+x start.sh
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available
WAITFORIT_cmdname=${0##*/}
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
usage()
{
cat << USAGE >&2
Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}
wait_for()
{
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
else
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
fi
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
(echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
if [[ $WAITFORIT_result -eq 0 ]]; then
WAITFORIT_end_ts=$(date +%s)
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
break
fi
sleep 1
done
return $WAITFORIT_result
}
wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
wait $WAITFORIT_PID
WAITFORIT_RESULT=$?
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
fi
return $WAITFORIT_RESULT
}
# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
WAITFORIT_CHILD=1
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
;;
-s | --strict)
WAITFORIT_STRICT=1
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
shift 1
;;
-t)
WAITFORIT_TIMEOUT="$2"
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
WAITFORIT_TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
WAITFORIT_CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
# Check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
WAITFORIT_BUSYTIMEFLAG=""
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
WAITFORIT_ISBUSY=1
# Check if busybox timeout uses -t flag
# (recent Alpine versions don't support -t anymore)
if timeout &>/dev/stdout | grep -q -e '-t '; then
WAITFORIT_BUSYTIMEFLAG="-t"
fi
else
WAITFORIT_ISBUSY=0
fi
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi
if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
exit $WAITFORIT_RESULT
fi
exec "${WAITFORIT_CLI[@]}"
else
exit $WAITFORIT_RESULT
fi
wait-for-it.sh
docker-compose | Docker 引擎 |
---|---|
3.8 | 19.03.0+ |
3.7 | 18.06.0+ |
3.6 | 18.02.0+ |
3.5 | 17.12.0+ |
3.4 | 17.09.0+ |
3.3 | 17.06.0+ |
3.2 | 17.04.0+ |
3.1 | 1.13.1+ |
3.0 | 1.13.0+ |
2.4 | 17.12.0+ |
2.3 | 17.06.0+ |
2.2 | 1.13.0+ |
2.1 | 1.12.0+ |
2.0 | 1.10.0+ |
本文由 mdnice 多平台发布