Docker 通過服務名直接ping通 --link、自定義網絡(關鍵)、網絡連通


Docker 容器間通過服務名直接ping通

測試

$ docker exec -it tomcat02 ping tomcat01
ping: tomcat01: Name or service not known
-------------------------------------------
# 如何解決呢?
$ docker run -d -P --name tomcat03 --link tomcat02 tomcat
.baa309a3c4a6cfeb135caae7a1189a18f1671a74722db63ba2da109602586433
---------------------------------------------------------------------
$ docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.087 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.087 ms
^C
--- tomcat02 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 28ms
rtt min/avg/max/mdev = 0.087/0.090/0.097/0.009 ms
---------------------------------------------------------------------
$ docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Temporary failure in name resolution

發現使用--link就可以直接使用容器名ping通!容器順序反之卻ping不同

查看network相關信息

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
ea60398194ce        bridge              bridge              local
85ad9f57c8bd        host                host                local
22e54d43614b        none                null                local
---------------------------------------------------------------------
$ docker network inspect ea60398194ce
[
    {
        "Name": "bridge",
        "Id": "ea60398194ce0b55f6d244f364042e9c7ead486183c0dbbcc12c94191bf0a90b",
        "Created": "2020-09-08T17:05:31.148215642+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",     # 默認的docker0
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",   # 最多配置255*255-2個
                    "Gateway": "172.17.0.1"      # 配置的網關
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": { # 下面三個容器的相關配置:
            "246fb3921ac148352d61a0216e3432b04285d87cd579298b51ac41ac157d1c50": {
                "Name": "tomcat01",
                "EndpointID": "7f7178e4c6493cdfcad1c944b851b8dc6720892a8e545c2a43fd47dc48d23b01",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "baa309a3c4a6cfeb135caae7a1189a18f1671a74722db63ba2da109602586433": {
                "Name": "tomcat03",
                "EndpointID": "98bf8739c964cbd0e8138dc13aa45248cc8b9bf490c28cb3af0fed517430c1a2",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "c4917215687a203472e900458c148909f63b93d026c9cfb5a90fc5adf5af4f84": {
                "Name": "tomcat02",
                "EndpointID": "b795a8848ba753a12135a752d93541d2869a59f91c4f4ca5b752b1954bf3760d",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]
elfin@dell:~$

探究inspect

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
baa309a3c4a6        tomcat              "catalina.sh run"   17 minutes ago      Up 17 minutes       0.0.0.0:32771->8080/tcp   tomcat03
c4917215687a        tomcat              "catalina.sh run"   2 hours ago         Up 2 hours          0.0.0.0:32770->8080/tcp   tomcat02
246fb3921ac1        tomcat              "catalina.sh run"   3 hours ago         Up 3 hours          0.0.0.0:32769->8080/tcp   tomcat01

$ docker inspect c4917215687a

tomcat03里面本地配置了tomcat02的配置

$ docker exec -it tomcat03 cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.3	tomcat02 c4917215687a
172.17.0.4	baa309a3c4a6
---------------------------------------------------------------------
$ docker exec -it tomcat02 cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.3	c4917215687a

現在已經不建議使用--link了

我們推薦使用自定義網絡!
不使用docker0!
docker0問題:不支持容器名連接訪問!

自定義網絡

查看所有的docker網絡

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
555e034b8248        bridge              bridge              local
85ad9f57c8bd        host                host                local
22e54d43614b        none                null                local

網絡模式

名字 特征
bridge 橋接 docker搭橋 0.2、0.3之間要用0.1 (默認,自己創建也使用bridge模式)
none 不配置網絡
host 主機模式:和主機共享網絡
container 容器網絡連接!(用的少!有很大的局限)

查看network

當前環境變更為windows環境
> docker network --help
Usage:  docker network COMMAND
Manage networks
Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.

以下兩個命令是等價的

# 直接啟動時 默認 --net bridge,這個使用的就是docker0網橋
> docker run -it -P --name elfin01 ubuntu:18.04
> docker run -it -P --name elfin01 --net bridge ubuntu:18.04
---------------------------------------------------------------
# docker0特點是默認域名不能訪問,--link可以打通連接!
# 但是--link會有一些問題,建議自定義網絡

查看如何創建網絡及其參數

> docker network create --help
Usage:  docker network create [OPTIONS] NETWORK
Create a network
Options:
      --attachable           Enable manual container attachment
      --aux-address map      Auxiliary IPv4 or IPv6 addresses used by
                             Network driver (default map[])
      --config-from string   The network from which copying the configuration
      --config-only          Create a configuration only network
  -d, --driver string        Driver to manage the Network (default "bridge")
      --gateway strings      IPv4 or IPv6 Gateway for the master subnet
      --ingress              Create swarm routing-mesh network
      --internal             Restrict external access to the network
      --ip-range strings     Allocate container ip from a sub-range
      --ipam-driver string   IP Address Management Driver (default "default")
      --ipam-opt map         Set IPAM driver specific options (default map[])
      --ipv6                 Enable IPv6 networking
      --label list           Set metadata on a network
  -o, --opt map              Set driver specific options (default map[])
      --scope string         Control the network's scope
      --subnet strings       Subnet in CIDR format that represents a
                             network segment

創建一個網絡net-test

使用linux測試

$ docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 net-test
-------------------------------------------------------------------------------------
# --driver bridge 默認就是橋接,可以不寫
# --subnet 192.168.0.0/16 子網的地址
# --gateway 192.168.0.1 網關,路由器地址
-------------------------------------------------------------------------------------
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f15f3323f810        backend             bridge              local
af0551b97a07        bridge              bridge              local
21fbcd175d02        host                host                local
2c4fe12634cd        layoutnet           bridge              local
9f1d4650cc11        net-test            bridge              local
daccfb2f7ebd        none                null                local

-------------------------------------------------------------------------------------
$ docker network inspect net-test 
[
    {
        "Name": "net-test",
        "Id": "9f1d4650cc11549aee69d1b7521a9c51271865499b265f2295de37cc8622cfdb",
        "Created": "2020-09-12T14:48:41.414502478+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

查看添加容器后的網絡

# 啟動兩個容器並使用net-test網橋
$ docker run -it -P --name ubuntu01 --net net-test ubuntu:18.04
root@44afdcf9482d:/# elfin@dell:~$ 
$ docker run -it -P --name ubuntu02 --net net-test ubuntu:18.04
root@8ee354200bdf:/# elfin@dell:~$ 
$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                  NAMES
8ee354200bdf        ubuntu:18.04        "/bin/bash"              13 seconds ago      Up 11 seconds                                              ubuntu02
44afdcf9482d        ubuntu:18.04        "/bin/bash"              44 seconds ago      Up 41 seconds                                              ubuntu01
11fe3e37d9c3        pdflayout:1.0       "/bin/bash"              4 hours ago         Up 2 hours          0.0.0.0:10010-10011->10010-10011/tcp   layoutLM
4405afaa1d9a        mysql:5.7           "docker-entrypoint.s…"   26 hours ago        Up About an hour    33060/tcp, 0.0.0.0:13306->3306/tcp     mysql57

----------------------------------------------------------------------------------
$ docker network inspect net-test 
[
    {
        "Name": "net-test",
        "Id": "9f1d4650cc11549aee69d1b7521a9c51271865499b265f2295de37cc8622cfdb",
        "Created": "2020-09-12T14:48:41.414502478+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "44afdcf9482d82a228234054a1bd62edb1f68170289430850af077bea26993aa": {
                "Name": "ubuntu01",
                "EndpointID": "e946b840ec2084f9d46f67f9e22dfffa1bea2ec1011c841523f727f54dcf3e3d",
                "MacAddress": "*************",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "8ee354200bdf82ffd87992a307aa3d19a79b51c36cb5f37c7e21e02591adb43b": {
                "Name": "ubuntu02",
                "EndpointID": "7d21a12198596aa0b32df5032c82d1d6426140a81b50817fef524b2e86df3ef5",
                "MacAddress": "************",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

----------------------------------------------------------------
# 可以發現容器中網絡里的Containers字段有相應的顯示。只要在這個網橋下的容器,他們之間可以相互訪問!

自定義網絡的優點

  1. 部署集群時,如mysql集群使用同一個網橋,使其能夠相互訪問;
  2. 不同集群、應用使用不同的網橋,做網絡隔離;
  3. 有利於集群的健康安全,方便網絡管理。

網絡連通


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM