Docker network configuration and deployment of SpringCloud project

Docker network configuration

Docker network mode introduction
Docker has four network modes when creating a container: bridge/host/container/none, bridge is the default and does not need to be specified with -net, and the other three modes need to be specified with -net when creating a container
1. Bridge mode (default mode)
Use -net=bridge when docker run. This mode will assign an independent Network Namespace to each container
. All containers on the same host will be on the same network segment, and each other is Can communicate

 注1:bridge为默认模式,不需要使用参数--net去指定,使用了--net参数反而无效
 注2:bridge模式无法指定容器IP(但非绝对

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

2.
When using host mode docker run -net=host, the container will not virtual out the IP/port, but use the host's IP and port

 docker run -itd --net=host 961769676411

 注1:host模式不能使用端口映射和自定义路由规则,这些都与主机一致,-p 与-icc 参数是无效的

3.container mode (omitted)

4.none mode (omitted)

5. Cross-host communication (omitted) The
above four are not cross-host, which means that the containers are all running on one host, but the actual production environment cannot be run with only one.
I will definitely use more than one, so how do the containers between multiple hosts communicate?
1. Use routing mechanism to open up the network
2. Use Open vSwitch (OVS) to open up the network
3. Use flannel to open up the network
4. Use Quagga to realize automatic routing learning

External access to the docker container

1.bridge mode
docker run -itd -p 7101:7101 mirror ID
## -p parameter can appear multiple times, bind multiple port numbers
docker run -itd -p 8080:8080 -p 8088:8088 mirror ID

Example:
docker run -it --name mytomcat02 -p 8081:8080 882487b8be1d
http://192.168.147.142:8081/

2.host mode
docker run -itd --net=host mirror ID
instance:
docker run -itd --net=host 882487b8be1d
http://192.168.147.142: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. Related commands
# stop and delete all containers
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

4. Bridge-utils
apt install bridge-utils
brctl show

Docker deploys SpringCloud project

Insert picture description here
First make sure that the project can normally access
http://eureka2001.xieminglu.com:2001/
http://localhost:1005/student/list
http://localhost/student/list
Insert picture description here
Take these five deployments as an example

External access
to the springcloud project in the docker container idea to play jar packages

2. Modify the pom of the main module

 <version>0.0.1-SNAPSHOT</version>
 <!-- 1.注意更改为pom而不是jar -->
 <!--
 <packaging>jar</packaging>
 -->
 <packaging>pom</packaging> 

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

3. Add plugin dependencies in the pom.xml file of each submodule module

<build>
        <plugins>
            <!--添加maven插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--添加自己的启动类路径!-->
                    <mainClass>com.xieminglu.microservicestudentproviderhystrix.MicroserviceStudentProviderHystrixApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
     </build>

4. Click on the view of idea —— "Tool windows ——" maven projects
first double-click clean (remove the package target folder that you typed before) ——” then create install
Insert picture description here
Insert picture description here

5. Copy the jar package in the target directory of each sub-module of the project to the specified directory
Insert picture description here
For example: d:\temp\apps directory, and then run
cmd
d:
cd d:\temp\apps
java -jar *. jar --spring.profiles.active=xxx

E.g:

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

Docker deploys springcloud
1. The host changes the hosts file
vim /etc/hosts

 ## 在里面添加要映射的域名即可
 127.0.0.1  eureka2001.xieminglu.com
 127.0.0.1  eureka2002.xieminglu.com

2. The host creates a folder apps, rz uploads the eureka-server-cluster.jar package to apps
## This directory will be used as a data volume later to share data between the host and the container
mkdir /apps

3. Start the container with the jre:8 image, and mount the specified directory as the data volume
docker run -d
-it
–net=host
–name eureka-server-peer1
–mount type=bind,source=/xieminglu/apps,target= /xieminglu/apps
mirror ID

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

4. Enter the container and start the
microservice with the java command docker exec -it eureka-server-peer1 /bin/sh
java -jar microservice-eureka-server.jar --spring.profiles.active=eureka2001

 注1:同理可以启动eureka-server-peer2
 注2:docker start $(docker ps -aq)

Guess you like

Origin blog.csdn.net/xieminglu/article/details/103637311