前言
本文根据前面所学习的知识,使用Maven插件docker-maven-plugin
配合Docker
+ Docker Compose
完成部署微服务项目。
前期准备工作
- Linux虚拟机或云主机(云主机需要关注安全组)
- 安装好Docker
-
部署流程
Docker开启远程API
用vim编辑修改docker.service文件
vim /usr/lib/systemd/system/docker.service
需要修改的部分
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
修改后的内容
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
重启Docker
systemctl restart docker
引入插件
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<executions>
<!--如果想在项目打包时构建镜像添加-->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
<!--<goal>push</goal>-->
</goals>
</execution>
</executions>
<configuration>
<!-- Docker 远程管理地址-->
<dockerHost>http://192.168.1.10:2375</dockerHost>
<!-- Docker 推送镜像仓库地址-->
<!--<pushRegistry>http://192.168.1.10:5000</pushRegistry>-->
<images>
<image>
<!--由于推送到私有镜像仓库,镜像名需要添加仓库地址-->
<!--<name>192.168.1.10:5000/ronan/${project.name}:${project.version}</name>-->
<!--<name>192.168.1.10:5000/ronan/${project.name}:v1.0</name>-->
<name>ronan/${project.name}:${project.version}</name>
<!--定义镜像构建行为-->
<build>
<!--定义基础镜像-->
<from>java:8</from>
<args>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</args>
<!--定义哪些文件拷贝到容器中-->
<assembly>
<!--定义拷贝到容器的目录-->
<targetDir>/</targetDir>
<!--只拷贝生成的jar包-->
<descriptorRef>artifact</descriptorRef>
</assembly>
<!--定义容器启动命令-->
<entryPoint>["java","-jar","-Xms256m","-Xmx256m","-XX:+UseG1GC","/${project.build.finalName}.jar"]</entryPoint>
<!--定义维护者-->
<maintainer>ronan</maintainer>
</build>
<!--定义容器启动行为-->
<!--<run>
<!–设置容器名,可采用通配符–>
<containerNamePattern>${project.artifactId}</containerNamePattern>
<!–设置端口映射–>
<ports>
<port>8022:8022</port>
</ports>
<!–设置容器间连接–>
<!–<links>
<link>mysql:db</link>
</links>–>
<!–设置容器和宿主机目录挂载–>
<volumes>
<bind>
<volume>/etc/localtime:/etc/localtime</volume>
<volume>/ronan/app/${project.artifactId}/logs:/var/logs</volume>
</bind>
</volumes>
</run>-->
</image>
</images>
</configuration>
</plugin>
使用脚本
# 在微服务的根目录下运行以下命令,maven插件则会开始构建镜像
mvn clean package
编写Docker Compose脚本
docker-compose-env.yml
脚本,负责基础环境version: '3'
services:
ronan-nacos:
image: nacos/nacos-server:1.4.2
container_name: ronan-nacos
ports:
- "8848:8848"
- "9848:9848"
- "9555:9555"
restart: always
networks:
- ronan-net
volumes:
- "/ronan/nacos/logs:/home/nacos/logs"
- "/ronan/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties"
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=hs-jiangke.com
- MYSQL_SERVICE_DB_NAME=ronan-nacos
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=SophSell_99
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
- JVM_XMS=512m
- JVM_XMX=512m
- JVM_XMN=64m
ronan-sentinel:
image: bladex/sentinel-dashboard:latest
container_name: ronan-sentinel
ports:
- "8858:8858"
restart: always
networks:
- ronan-net
volumes:
- "/ronan/logs:/logs/csp"
environment:
- 'TZ="Asia/Shanghai"'
- JVM_XMS=256m
- JVM_XMX=256m
- JVM_XMN=64m
- JAVA_OPTS=-XX:+UseG1GC -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -Djava.security.egd=file:/dev/./urandom -Dcsp.sentinel.api.port=8719
networks:
ronan-net:
driver: bridge
docker-compose-app.yml
脚本,负责业务服务version: '3'
services:
ronan-gateway:
image: ronan/ronan-gateway:20220530
container_name: ronan-gateway
ports:
- "9090:9090"
volumes:
- "/ronan/logs:/logs"
- "/etc/localtime:/etc/localtime"
restart: always
external_links:
- ronan-nacos:ronan-nacos
networks:
- ronan-net
environment:
- 'TZ="Asia/Shanghai"'
ronan-ums-web:
image: ronan/ronan-ums-web:20220530
container_name: ronan-ums-web
ports:
- "9096:9096"
volumes:
- "/ronan/logs:/logs"
- "/etc/localtime:/etc/localtime"
restart: always
networks:
- ronan-net
external_links:
- ronan-nacos:ronan-nacos
- ronan-sentinel:ronan-sentinel
environment:
- 'TZ="Asia/Shanghai"'
ronan-third-web:
image: ronan/ronan-third-web:20220530
container_name: ronan-third-web
ports:
- "9095:9095"
volumes:
- "/ronan/logs:/logs"
- "/etc/localtime:/etc/localtime"
restart: always
networks:
- ronan-net
external_links:
- ronan-nacos:ronan-nacos
- ronan-sentinel:ronan-sentinel
environment:
- 'TZ="Asia/Shanghai"'
ronan-message-web:
image: ronan/ronan-message-web:20220530
container_name: ronan-message-web
ports:
- "9092:9092"
volumes:
- "/ronan/logs:/logs"
- "/etc/localtime:/etc/localtime"
restart: always
networks:
- ronan-net
external_links:
- ronan-nacos:ronan-nacos
- ronan-sentinel:ronan-sentinel
environment:
- 'TZ="Asia/Shanghai"'
networks:
ronan-net:
external:
name: "ronan_ronan-net"
上传脚本文件到服务器
```bash
执行env脚本
docker-compose -f docker-compose-env.yml up -d
查看env运行容器日志
docker-compose -f docker-compose-env.yml logs —tail 10 -f 容器名
执行app脚本
docker-compose -f docker-compose-app.yml up -d
停止并且删除容器
docker-compose -f docker-compose-app.yml down ```
总结
至此,部署完成