首页 > 其他分享 >自定义docker网络与自定义的网络之间的连通

自定义docker网络与自定义的网络之间的连通

时间:2022-09-19 11:37:45浏览次数:71  
标签:自定义 10.192 网络 master home 64 docker root

一、自定义一个docker网络

1、创建一个自定义网络
[root@master ~]# docker network create --driver bridge --subnet 10.192.0.0/24 --gateway 10.192.0.1 mynet
806b16d9d8b2c2535e28071d9dc413b1852bb3e99acbcb13477c8918fc201310
[root@master ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
e52ef1cd9c37        bridge              bridge              local
20f229ec5603        host                host                local
806b16d9d8b2        mynet               bridge              local
5e0bd1d2525c        none                null                local
[root@master ~]# 
2、使用自定义网络运行两个容器
# 先下载一个centos镜像
[root@master ~]# docker pull centos

[root@master home]# docker run -it -d --name cn1 --net mynet centos
a694e63f485a6de5a909d9b48bf3385af1980ef78a04ddc457a6dc396715cacc

[root@master home]# docker run -it -d --name cn2 --net mynet centos
43861b425af6db813bb548823de07bb4e66b4cc289ebbecfb15ed545c5a7f142
3、查看网络情况
[root@master home]# docker inspect mynet 
[
    ...
    ...
        "ConfigOnly": false,
        "Containers": {
            "43861b425af6db813bb548823de07bb4e66b4cc289ebbecfb15ed545c5a7f142": {
                "Name": "cn2",
                "EndpointID": "154af184fc7d67b714fe213d18f6a72aeac3f52b168d02d2787a489d994e6e34",
                "MacAddress": "02:42:0a:c0:00:03",
                "IPv4Address": "10.192.0.3/24",
                "IPv6Address": ""
            },
            "a694e63f485a6de5a909d9b48bf3385af1980ef78a04ddc457a6dc396715cacc": {
                "Name": "cn1",
                "EndpointID": "8a73a643c52d41ed1c5910c63c2fa89633c0e17fd89d9932dec39f790f6ae4bf",
                "MacAddress": "02:42:0a:c0:00:02",
                "IPv4Address": "10.192.0.2/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[root@master home]# 
4、测试1,宿主机ping容器,成功
[root@master home]# ping 10.192.0.2
PING 10.192.0.2 (10.192.0.2) 56(84) bytes of data.
64 bytes from 10.192.0.2: icmp_seq=1 ttl=64 time=0.059 ms
64 bytes from 10.192.0.2: icmp_seq=2 ttl=64 time=0.075 ms
^C
--- 10.192.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.059/0.067/0.075/0.008 ms

[root@master home]# ping 10.192.0.3
PING 10.192.0.3 (10.192.0.3) 56(84) bytes of data.
64 bytes from 10.192.0.3: icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from 10.192.0.3: icmp_seq=2 ttl=64 time=0.055 ms
64 bytes from 10.192.0.3: icmp_seq=3 ttl=64 time=0.051 ms
^C
--- 10.192.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.047/0.051/0.055/0.003 ms
[root@master home]# 
5、测试2,容器之间通过名称互ping,成功
# cn1 Ping cn2
[root@master home]# docker exec -it cn1 ping cn2
PING cn2 (10.192.0.3) 56(84) bytes of data.
64 bytes from cn2.mynet (10.192.0.3): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from cn2.mynet (10.192.0.3): icmp_seq=2 ttl=64 time=0.056 ms
^C
--- cn2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 4ms
rtt min/avg/max/mdev = 0.035/0.045/0.056/0.012 ms

# cn2 Ping cn1
[root@master home]# docker exec -it cn2 ping cn1
PING cn1 (10.192.0.2) 56(84) bytes of data.
64 bytes from cn1.mynet (10.192.0.2): icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from cn1.mynet (10.192.0.2): icmp_seq=2 ttl=64 time=0.055 ms
^C
--- cn1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.055/0.060/0.065/0.005 ms
[root@master home]# 
6、结论

使用自定义的网络,docker会自动帮我们建立好对应关系。

好处:当一台服务器中,部署不同的集群时,不同的集群使用不同的网络,可以保证集群的安全与健康。

二、两个相互隔离的网络的连通

1、实验要求

实验:如下图所示,如何使mn1与cn1连通:

在这里插入图片描述

2、环境介绍
网络名 容器名 ip
mynet cn1 10.192.0.2
mynet cn2 10.192.0.3
mynet2 mn1 10.193.0.2
mynet2 mn1 10.193.0.3

创建mynet2的脚本:

[root@master home]# docker network create --driver bridge --subnet 10.193.0.0/24 --gateway 10.193.0.1 mynet2

[root@master home]# docker run -it -d --name mn1 --net mynet2 centos
6f99c08f45116a8260a12037ebb98c3647751bb6bf328174aca0156e7f2f1594

[root@master home]# docker run -it -d --name mn2 --net mynet2 centos
50bc87335d51bc20ca0b4842b4b419a2fed0fca2bcbd59651ec4587e1224afc2
3、连通

3.1、直接ping,不通

[root@master home]# docker exec -it cn1 ping mn1
ping: mn1: Name or service not known
[root@master home]# 

3.2、通过帮助文档可知,可以通过网络与容器进行连接,格式为docker network connect [OPTIONS] NETWORK CONTAINER

[root@master home]# docker network connect --help

Usage:  docker network connect [OPTIONS] NETWORK CONTAINER

Connect a container to a network

Options:
      --alias strings           Add network-scoped alias for the container
      --ip string               IPv4 address (e.g., 172.30.100.104)
      --ip6 string              IPv6 address (e.g., 2001:db8::33)
      --link list               Add link to another container
      --link-local-ip strings   Add a link-local address for the container
[root@master home]# 

3.3、连通,使用mynet与mn1连通

[root@master home]# docker network connect mynet mn1
[root@master home]# 

3.4、 测试

# cn1 ping mn1
[root@master home]# docker exec -it cn1 ping mn1
PING mn1 (10.192.0.4) 56(84) bytes of data.
64 bytes from mn1.mynet (10.192.0.4): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from mn1.mynet (10.192.0.4): icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from mn1.mynet (10.192.0.4): icmp_seq=3 ttl=64 time=0.058 ms
^C
--- mn1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 5ms
rtt min/avg/max/mdev = 0.057/0.060/0.067/0.010 ms
[root@master home]# 

# cn2 ping mn1
[root@master home]# docker exec -it cn2 ping mn1
PING mn1 (10.192.0.4) 56(84) bytes of data.
64 bytes from mn1.mynet (10.192.0.4): icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from mn1.mynet (10.192.0.4): icmp_seq=2 ttl=64 time=0.054 ms
^C
--- mn1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.054/0.072/0.091/0.020 ms
[root@master home]# 


#cn1 ping mn2
[root@master home]# docker exec -it cn1 ping mn2
ping: mn2: Name or service not known
[root@master home]# 
4、结论

连通方式如下图所示:

这种情况下:

mn1与cn1和cn2相互连通,

mn2与cn1和cn2不能连通。

在这里插入图片描述

标签:自定义,10.192,网络,master,home,64,docker,root
From: https://www.cnblogs.com/likaifei/p/16707136.html

相关文章

  • 计算机网络 _ 网络层
    网际协议IP是TCP/IP体系中两个最主要的协议之一。与IP协议配套使用的还有三个协议:地址解析协议ARP(AddressResolutionProtocol)网际控制报文协议ICMP(Inter......
  • 40. 网络加载框架Retrofit完成文件上传和下载
    40.网络加载框架Retrofit完成文件上传和下载40.1文件上传服务接口packagecom.dingjiaxiong.myretrofit;importokhttp3.MultipartBody;importokhttp3.ResponseBody;......
  • 36. 网络加载框架OkHttp的同步与异步请求
    36.网络加载框架OkHttp的同步与异步请求36.1简介官方地址https://github.com/square/okhttpandroid网络框架之OKhttp一个处理网络请求的开源项目,是安卓端最火热......
  • 37. OKhttp自定义配置
    37.OKhttp自定义配置37.1Builder构建者OkHttpClientokHttpClient=newOkHttpClient.Builder().build();提供更多操作37.2拦截器OkHttpClientokHttpClient=newO......
  • 38. 网络加载框架Retrofit
    38.网络加载框架Retrofit38.1Retrofit简介Retrofit是一个RESTful的HTTP网络请求框架的封装。原因:网络请求的工作本质上是OkHttp完成,而Retrofit仅负责网络请求......
  • 39. 网络加载框架Retrofit中的转换器和适配器
    39.网络加载框架Retrofit其他39.1Retrofit中的转换器在接到服务器响应后,目前无论是OKhttp还是Retrofit都只能接收到String字符串类型的数据,在实际开发中,通常需要对字符......
  • python网络爬虫常用解析器
    BeautifulSoupBeautifulSoup是一个HTML/XML的解析器,主要用于解析和提取HTML/XML数据。它基于HTMLDOM的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,......
  • Docker 安装启动 Jenkins (docker-compose)
    Jenkins官网:https://www.jenkins.io/ 官方安装文档指导:https://www.jenkins.io/doc/book/installing/dockerhub官方镜像: https://hub.docker.com/_/jenkins/tags......
  • Docker笔记
    docker学习,作者源于这里基本概念镜像Image操作系统分为内核和用户空间,内核启动后,会挂载root文件系统为其提供用户空间支持。docker镜像就相当于是一个root文件系统。是......
  • jenkins docker安装时插件缺失
    前言今天又试着装了一下docker版的jenkins,今天用了jenkins:2.60.3这个镜像,发现某些插件没有,导致安装不成功。anerroroccurredduringinstallation:Nosuchplugin:cl......