051、在overlay中运行容器(2019-03-18 周一)

 
我们前面创建了overlay网络 ov_net1 ,今天我们运行一个busybox容器并连接到 ov_net1
 
root@host01:~# docker run -itd --name bbox1 --network ov_net1 busybox    #    创建容器bbox1,使用ov_net1网络
e666ab4af792055c63afde141a13286a8e108df2472994a42f22d92e4dbd4f61
root@host01:~# docker exec bbox1 ip r    #    查看容器内路由表
default via 172.18.0.1 dev eth1
10.0.0.0/24 dev eth0 scope link  src 10.0.0.2
172.18.0.0/16 dev eth1 scope link  src 172.18.0.2
root@host01:~# docker exec bbox1 ip a    #    查看容器内网络信息,比我们想象的多了一块网卡,且默认路由走这块网卡
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
    link/ether 02:42:0a:00:00:02 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.2/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
11: eth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth1
       valid_lft forever preferred_lft forever
root@host01:~# docker network ls    #    上面使用的网卡是overlay网络自动创建的一种docker网络 docker_gwbridge,专门给使用overlay网络的机器上网用
NETWORK ID          NAME                DRIVER              SCOPE
5f1cb3e7ea16        bridge              bridge              local
467a0c3b1d73        docker_gwbridge     bridge              local
a08d5e1df638        host                host                local
83f08e15caa8        none                null                local
609020e03ff4        ov_net1             overlay             global
root@host01:~# docker network inspect docker_gwbridge
[
    {
        "Name": "docker_gwbridge",
        "Id": "467a0c3b1d73a3b025ff0fd32d87158855573ee7c056060b7967d02096fc626d",
        "Created": "2019-03-18T08:55:22.63696796+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "e666ab4af792055c63afde141a13286a8e108df2472994a42f22d92e4dbd4f61": {
                "Name": "gateway_a8d468c12df8",
                "EndpointID": "d083b420b79f99a4456eccacc8b472e77a29c821cffa726afff63b484323750e",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.enable_icc": "false",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.name": "docker_gwbridge"
        },
        "Labels": {}
    }
]
root@host01:~# ifconfig docker_gwbridge
docker_gwbridge Link encap:Ethernet  HWaddr 02:42:4d:d7:0f:5f  
          inet addr:172.18.0.1  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:4dff:fed7:f5f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:648 (648.0 B)
 
root@host01:~# docker exec bbox1 ping -c 2 www.qq.com
PING www.qq.com (123.151.137.18): 56 data bytes
64 bytes from 123.151.137.18: seq=0 ttl=51 time=5.621 ms
64 bytes from 123.151.137.18: seq=1 ttl=51 time=5.695 ms
 
--- www.qq.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 5.621/5.658/5.695 ms
 
上面是如何让容器访问外网,下面学习一下如何让外面访问容器
 
和之前方法一样,把网络类型换成 overlay 网络即可
 
docker run -d -p 80:80 --network ov_net1 --name web01 httpd
 
 
posted @ 2019-03-18 09:09  三角形  阅读(160)  评论(0编辑  收藏  举报