zoukankan      html  css  js  c++  java
  • 网络配置

    Docker网络配置

    Docker网络模式介绍

       Docker在创建容器时有四种网络模式:bridge/host/container/nonebridge为默认不需要用--net去指定,其他三种模式需要在创建容器时使用--net去指定

       1.bridge模式(默认模式)

         docker run时使用--net=bridge,这种模式会为每个容器分配一个独立的Network Namespace,

         同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的

         1bridge为默认模式,不需要使用参数--net去指定,使用了--net参数反而无效

         2bridge模式无法指定容器IP(但非绝对

    Docker run -it --name mytomcat01 -p 8081:8080 镜像ID

       2.host模式

         docker run时使用--net=host,容器将不会虚拟出IP/端口,而是使用宿主机的IP和端口

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

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

       3.container模式()

       4.none模式()

       5.跨主机通信()

         以上四种均未跨主机,也就是说容器均运行在一台宿主机上,但实际生产环境不可能只用一台来跑。

         肯定会用到多台,那么多台主机之间的容器如何通信

         1.使用路由机制打通网络

         2.使用Open vSwitchOVS)打通网络

         3.使用flannel来打通网络

         4.使用Quagga来实现自动学习路由  

       1:详情见

       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.147.142:8081/

       2.host模式

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

    实例:

    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.相关命令

         #停止并删除所有容器

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

       4.网桥查看工具bridge-utils

         apt install bridge-utils

         brctl show

    Docker部署SpringCloud项目

    先确保工程能够正常访问

    http://eureka2001.huang.com:2001/

    http://localhost:1005/student/list

    http://localhost/student/list

    以这五个部署为例

    ideaspringcloud项目打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文件中添加插件依赖 

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

      4.点击ideaview ——》Tool windows ——》maven projects

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

     

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

     

      cmd

         d:

         cd d: empapps   

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

     例如:

    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部署springcloud

       1.宿主机修改hosts文件

         vim /etc/hosts

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

         127.0.0.1  eureka2001.huang.com

         127.0.0.1  eureka2002.huang.com

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

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

         mkdir /apps

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

           docker run -d

           -it

           --net=host

           --name eureka-server-peer1

           --mount type=bind,source=/hyc_docker/apps,target=/hyc_docker/apps

           镜像ID   

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

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

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

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

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

         2docker start $(docker ps -aq)

    测试:

    http://192.168.147.142:2001/

    http://192.168.147.142:2002/

  • 相关阅读:
    微信java封装
    解决PowerDesigner 生成Sql2005-2012 找不到sysproperties表的问题
    ASP.net解析JSON例子
    c# 遍历子控件,比如Form下的group,或者panel
    修改sql2005字段
    清除grid内容的列
    sql 2000以及2005以上获取数据库中所有的表(不包括系统表)
    获取SqlServer2005表结构(字段,主键,外键,递增,描述)
    SQL SERVER 数据库实用SQL语句
    查找所有页面中的按钮
  • 原文地址:https://www.cnblogs.com/bf6rc9qu/p/12075213.html
Copyright © 2011-2022 走看看