Docker network configuration, Docker distributed deployment project

aims

1, Docker network configuration

2, Docker deployment SpringCloud project

 

Docker Network Configuration

Docker network model introduced

   Docker There are four network mode when creating a container: bridge / host / container / none, bridge do not need to be specified as the default --net, the other three models use --net need to specify when you create the container

 

   1.bridge mode (the default mode)

     When using run docker --net = bridge, this model will be assigned a unique Network Namespace for each container,

     All the same container on the host will be in the same network, they can communicate with each other

 

     Note 1: bridge is the default mode, no parameter to specify --net, but invalid parameters used --net

     Note 2: bridge mode can not be specified container IP (but not absolute

Docker run -it --name mytomcat01 -p 8081: 8080 Mirror ID

 

 

 

 

Docker run -it --name mytomcat01 -p 8082: 8080 Mirror ID

 

 

 

   2.host mode

     When using the docker run --net = host, the container will not be virtual IP / port, but the use of host IP and port

     docker run -it --name mytomcat03 --net=host [镜像id]

 

 

 

 

 

 

Note 1: host mode can not use the port mapping and custom routing rules, which are consistent with the host, -p parameter is invalid and -icc

 

 

     Several more were not across hosts, that container are running on a single host, but the actual production environment can not only be run with a single.

     We will certainly use more than one, how much container between communicating hosts

     1. Using a routing mechanism to get through the network

     2. Open vSwitch (OVS) open network

     3. Use flannel to open up network

     4. Use the Quagga routing to automatically learn  

     Note 1: For details, see

 https://www.cnblogs.com/yy-cxd/p/6553624.html

 

外部访问docker容器

   1.bridge模式

     docker run -itd -p 7101:7101 镜像ID

     ## -p参数可以出现多次,绑定多个端口号

     docker run -itd -p 8080:8080 -p 8088:8088 镜像ID

 

实例:

docker run -it --name mytomcat02 -p 8081:8080 882487b8be1d

http://192.168.238.129:8081/

 

     

   2.host模式

     docker run -itd --net=host 镜像ID

实例:

docker run -itd --net=host 882487b8be1d

http://192.168.238.129:8080/

 

 

     注1:不需要添加-p参数,因为它使用的就是主机的IP和端口,添加-p参数后,反而会出现以下警告:

          WARNING: Published ports are discarded when using host network mode

     注2:宿主机的ip路由转发功能一定要打开,否则所创建的容器无法联网! 

          echo 1 > /proc/sys/net/ipv4/ip_forward

 

   3.相关命令

     #停止并删除所有容器

     docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

 

   4.网桥查看工具bridge-utils

     apt install bridge-utils

     brctl show

 

Docker部署SpringCloud项目

 

先确保工程能够正常访问

http://eureka2001.yuan.com:2001/

 

 

 

 

 

http://localhost/student/list

 

 

 

http://localhost:1003/student/list

 

 

以这五个部署为例

 

 

idea中springcloud项目打jar

   1.在idea运行springcloud项目,不报错,均可正常访问

 

   2.修改主模块的pom

     <version>0.0.1-SNAPSHOT</version>

     <!-- 1.注意更改为pom而不是jar -->

     <!--

     <packaging>jar</packaging>

     -->

     <packaging>pom</packaging>

 

     <!-- 2.主模块不要配置插件 -->

     <build></build>

 

   3.在各个子module模块的pom.xml文件中添加插件依赖

 1 <build>
 2         <plugins>
 3             <!--添加maven插件-->
 4             <plugin>
 5                 <groupId>org.springframework.boot</groupId>
 6                 <artifactId>spring-boot-maven-plugin</artifactId>
 7                 <configuration>
 8                     <!--添加自己的启动类路径!-->
 9                <mainClass>com.yuan.microservicestudentproviderhystrix.MicroserviceStudentProviderHystrixApplication</mainClass>
10                 </configuration>
11                 <executions>
12                     <execution>
13                         <goals>
14                             <!--可以把依赖的包都打包到生成的Jar包中-->
15                             <goal>repackage</goal>
16                         </goals>
17                     </execution>
18                 </executions>
19             </plugin>
20         </plugins>
21      </build>

 

4.点击idea的view ——》Tool windows ——》maven projects

     先双击clean(去掉之前打的包target文件夹)——》再创建install

 

   5.将项目各子模块target目录下的jar包,复制到指定目录,例如:d:\temp\apps目录下,再通过java命令直接运行

     cmd

     d:

     cd d:\temp\apps   

     java -jar *.jar --spring.profiles.active=xxx

 

例如:  

1 java -jar microservice-eureka-server.jar --spring.profiles.active=eureka2001
2 java -jar microservice-eureka-server.jar --spring.profiles.active=eureka2002
3 java -jar microservice-student-provider-hystrix.jar --spring.profiles.active=provider-hystrix-1005
4 java -jar microservice-student-provider-hystrix.jar --spring.profiles.active=provider-hystrix-1006
5 java -jar microservice-student-consumer-feign-80.jar

 

 

 

 

 

 

 

 

docker部署springcloud

   1.宿主机修改hosts文件

     vim /etc/hosts

 

     ## 在里面添加要映射的域名即可

     127.0.0.1  eureka2001.yuan.com

     127.0.0.1  eureka2002.yuan.com

 

 

 

 

 

   2.宿主机创建文件夹apps,rz上传eureka-server-cluster.jar包至apps

     ## 此目录稍后作为数据卷,在宿主机和容器之间共享数据

     mkdir /apps

 

   3.使用jre:8镜像启动容器,并挂载指定目录为数据卷

     docker run -d \

       -it \

       --net=host \

       --name student-consumer-feign1 \

       --mount type=bind,source=/lys/apps,target=/lys/apps \

       747ead4f5a69    镜像ID   

 

 

 

     注1:jre:8是自定义镜像,已安装jre1.8

 

   4.进入容器,java命令启动微服务

     docker exec -it eureka-server-peer1 /bin/sh

     java -jar microservice-eureka-server.jar --spring.profiles.active=eureka2001

 

  docker exec -it 0509c0ee8689 /bin/sh

  java -jar microservice-student-consumer-feign-80.jar

 

 

 

 

 

     注1:同理可以启动eureka-server-peer2

     注2:docker start $(docker ps -aq)

推荐博客 

1、打jar包过程中出错:https://yq.aliyun.com/articles/703936

2、docker启动项目时出现MySQL,jdbc的错:https://blog.csdn.net/qq_45174759/article/details/103630070

测试:

http://192.168.238.129:2001/

 

 

 

 

http://192.168.238.129:2002/

 

配置原因,同时启动不了太多的项目,分开测试了没截图。。。

 

http://192.168.238.129:81/student/list

 

 

http://192.168.238.129:1005/info

 

 

 

 

http://192.168.238.129:81/student/hystrix

 

 

 

 

 

谢谢观看!!! 

Guess you like

Origin www.cnblogs.com/ly-0919/p/12068903.html