CI/CD 容器服务准备
服务器规划
Linux 采用 Ubuntu Server X64 18.04 LTS 版本,配置及说明如下:
主机名 | IP | CPU / 内存 | 磁盘 | 说明 |
---|---|---|---|---|
docker-database | 192.168.1.130/24 | 2 核 512M | ||
docker-gitlab | 192.168.1.131/24 | 2 核 1.5 G | 20G | 源码管理 |
docker-nexus | 192.168.1.132/24 | 2 核 2G | 20G | 仓库管理 |
docker-harbor | 192.168.1.133/24 | 2 核 512M | 20G | 镜像管理 |
docker-service-cloud | 192.168.1.188/24 | 2 核 2G | 20G | 服务部署 |
虚拟机连接工具 Termius
docker-admin
docker-compose.yml
version: '3.1'
services:
itoken-admin:
image: harbor.seth.com/itoken/itoken-admin
container_name: itoken-admin
restart: always
ports:
- 8084:8084
extra_hosts:
- gitlab.seth.com:192.168.1.131
- service.cloud.seth.com:192.168.1.188
docker-service-cloud
CI/CD 流程图
基础设施集中部署,如下
root@docker-service-cloud:/usr/local/docker# tree -L 1
.
├── itoken-config
├── itoken-eureka
├── itoken-zipkin
└── runner
- itoken-config: 分布式配置中心
- itoken-eureka:服务注册与发现中心
- itoken-zipkin:服务链路追踪系统
Runner
- 使用 docker 部署
/usr/local/docker/runner/docker-compose.yml
- 编写服务 CI 配置文件 .gitlab-ci.yml (放在项目根目录下)
stages:
- build
- push
- deploy
- clean
# 打包项目、构建镜像
build:
stage: build
script:
- /usr/local/maven/apache-maven-3.8.1/bin/mvn clean package
- mv target/itoken-config-1.0.0-SNAPSHOT.jar docker/ && cd docker/
- docker build -t harbor.seth.com/itoken/itoken-config .
# 推送镜像
push:
stage: push
script:
- docker login harbor.seth.com -u admin -p Harbor12345
- docker push harbor.seth.com/itoken/itoken-config:latest
# 部署服务
deploy:
stage: deploy
script:
- cd docker
- docker-compose down
- docker-compose up -d
查看项目构建过程
进入容器
docker exec -it <容器id> /bin/bash
持续集成,持续部署
cd /home/gitlab-runner/builds/a9932170/0/itoken/itoken-config/
问题 & 解决
- 权限
解决:宿主机修改 docker.sock 权限
sudo chmod 666 /var/run/docker.sock
- 孤儿容器
解决:
docker/<项目名>/docker-compose
Note
Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable.