1. 服务部署到目标服务器上
(多次部署同一个服务时,会失败,因为会端口冲突)
variables:
user: eric
pwd: Admin@1234
harbor: http://172.16.128.215:8080
image_hellocat: 172.16.128.215:8080/hive/hellocat
stages:
- testing
- build
- deploy_qa
deploy_to_qa:
stage: deploy_qa
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
# 在目标机器:172.16.128.153上部署服务,先登录docker harbor,再拉镜像,并部署
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
docker login -u $user -p $pwd $harbor &&
docker run -p 80:8000 -d 172.16.128.215:8080/hive/hellocat:latest
"
- 执行结果:
- 多次部署同一个服务,会导致端口冲突。
- 解决方案:
a) 使用shell脚本,找到它,然后删除它
b) 使用docker-compse来解决
具体步骤如下:
version: "3.9"
services:
cat:
image: 172.16.128.215:8080/hive/hellocat:latest
ports:
- ${APP_PORT}:8000
restart: always
- 更新gitlab.ci.yaml文件
variables:
user: eric
pwd: Admin@1234
harbor: http://172.16.128.215:8080
image_hellocat: 172.16.128.215:8080/hive/hellocat
stages:
- testing
- build
- deploy_qa
deploy_to_qa:
stage: deploy_qa
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
docker-compose down && docker-compose up -d
"
问题1:部署时报docker-compose不支持
解决方案:升级docker-compose到最新版本
解决方法:
- 查看版本:
- 升级docker-compose版本