Published 5月 31, 2019 by with 0 comment

4 - 03 - Adding a NIC



In this lab, We have 3 containers. Now I will following the topology.
1. Creating a Network call NET_192_168_1 and gateway is 192.168.1.1
2. Creating two containers call Busybox 1 and Busybox2. Both Buysbox 1 and 2 are in the same network (NET_192_168_1)
3. Busybox1 use DHCP to get a IP, Busybox2 is static ip 192.168.1.20/24.
4. Creating a Network call NET_192_168_2 and gateway is 192.168.2.1
5. Creating a containers call Busybox 3 and using NET_192.168_2 network.
6. Busybox3 use static IP 192.168.2.30/24.
7. Busybox3 add a new NIC which IP is 192.168.1.30.
8. Busybox1, Busybox2 and Busybox3 can ping each other.


1. Creating a Network call NET_192_168_1 and gateway is 192.168.1.1
1a. Remove old NET_192_168_1
peter@peter-KVM:~$ docker network rm NET_192_168_1
NET_192_168_1
peter@peter-KVM:~$ 

1b. Create a new bridge network NET_192_168_1
peter@peter-KVM:~$ docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 NET_192_168_1
11e6e8ed82ce33d781be26a04dc8c3d1faf78e9a62a31adc57bee154f430b902
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker network inspect NET_192_168_1
[
    {
        "Name": "NET_192_168_1",
        "Id": "11e6e8ed82ce33d781be26a04dc8c3d1faf78e9a62a31adc57bee154f430b902",
        "Created": "2019-05-31T15:44:39.396715078+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.1.0/24",
                    "Gateway": "192.168.1.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
peter@peter-KVM:~$ 

2. Creating two containers call Busybox 1 and Busybox2. Both Buysbox 1 and 2 are in the same network (NET_192_168_1)
2a. Remove all non-use container
peter@peter-KVM:~$ docker rm -v $(docker ps -aq -f status=exited)
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
peter@peter-KVM:~$ 
2b. Creating a container called Busybox1 with bridge NET_192_168_1
peter@peter-KVM:~$ docker run -itd --name Busybox1 --network NET_192_168_1 busybox sleep 3000
715d7783bce92aa31e477dd5d01249ed88faa25039f4bb2d65250990ebb1fa22
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
715d7783bce9        busybox             "sleep 3000"        4 seconds ago       Up 2 seconds                            Busybox1
peter@peter-KVM:~$ 
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker exec -it Busybox1 sh
/ # 
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
128: eth0@if129:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # exit
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
11e6e8ed82ce        NET_192_168_1       bridge              local
3aa82fb31574        bridge              bridge              local
9a8a72ebd1bf        host                host                local
c87da00cc48c        none                null                local
peter@peter-KVM:~$ 
peter@peter-KVM:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11e6e8ed82ce         8000.0242a401537a       no              veth0494abc
docker0         8000.024219252d6f       no
peter@peter-KVM:~$ 
2c. Creating a container called Busybox2 with bridge NET_192_168_1. Static IP 192.168.1.20/24
peter@peter-KVM:~$ docker run -itd --name Busybox2 --network NET_192_168_1 --ip 192.168.1.20 busybox sleep 3000
9483fb170876e9aac3ee63f2d64b4e464d8ae221e7a1fde4670a551c47f1f034
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9483fb170876        busybox             "sleep 3000"        4 seconds ago       Up 3 seconds                            Busybox2
715d7783bce9        busybox             "sleep 3000"        4 minutes ago       Up 4 minutes                            Busybox1
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker exec -it Busybox2 sh
/ # 
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
130: eth0@if131:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:14 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # exit
peter@peter-KVM:~$ 
peter@peter-KVM:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11e6e8ed82ce         8000.0242a401537a       no              veth0494abc
                                                        veth72c6f9b
docker0         8000.024219252d6f       no
peter@peter-KVM:~$ 
peter@peter-KVM:~$ 
3. Busybox1 use DHCP to get a IP, Busybox2 is static ip 192.168.1.20/24.
3a. Verify
peter@peter-KVM:~$ docker exec -it Busybox1 sh
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
128: eth0@if129:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # ping -c 3 192.168.1.20
PING 192.168.1.20 (192.168.1.20): 56 data bytes
64 bytes from 192.168.1.20: seq=0 ttl=64 time=0.150 ms
64 bytes from 192.168.1.20: seq=1 ttl=64 time=0.156 ms
64 bytes from 192.168.1.20: seq=2 ttl=64 time=0.124 ms

--- 192.168.1.20 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.124/0.143/0.156 ms
/ # 
/ # exit
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker exec -it Busybox2 sh
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
130: eth0@if131:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:14 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # ping -c 3 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.137 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.153 ms
64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.438 ms

--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.137/0.242/0.438 ms
/ # 
4. Creating a Network call NET_192_168_2 and gateway is 192.168.2.1
peter@peter-KVM:~$ docker network create --driver bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 NET_192_168_2
4e4d06e66f42acd2ec10016674eb0d3367ac20d5fed40050871d584af57c3785
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
11e6e8ed82ce        NET_192_168_1       bridge              local
4e4d06e66f42        NET_192_168_2       bridge              local
3aa82fb31574        bridge              bridge              local
9a8a72ebd1bf        host                host                local
c87da00cc48c        none                null                local
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker network inspect NET_192_168_2
[
    {
        "Name": "NET_192_168_2",
        "Id": "4e4d06e66f42acd2ec10016674eb0d3367ac20d5fed40050871d584af57c3785",
        "Created": "2019-05-31T16:05:21.390356202+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.2.0/24",
                    "Gateway": "192.168.2.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
peter@peter-KVM:~$ 
5. Creating a containers call Busybox 3 and using NET_192.168_2 network.
peter@peter-KVM:~$ docker run -itd --name Busybox3 --network NET_192_168_2 --ip 192.168.2.30 busybox sleep 3000
b81c5588f9c5edfbcd861bad10aa4993ac0bf92f86cbfc11b3e28cfec299cb14
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b81c5588f9c5        busybox             "sleep 3000"        5 seconds ago       Up 3 seconds                            Busybox3
9483fb170876        busybox             "sleep 3000"        11 minutes ago      Up 11 minutes                           Busybox2
715d7783bce9        busybox             "sleep 3000"        15 minutes ago      Up 15 minutes                           Busybox1
peter@peter-KVM:~$ 
peter@peter-KVM:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11e6e8ed82ce         8000.0242a401537a       no              veth0494abc
                                                        veth72c6f9b
br-4e4d06e66f42         8000.02420c6f213f       no              veth8cf5b23
docker0         8000.024219252d6f       no
peter@peter-KVM:~$ 
6. Busybox3 use static IP 192.168.2.30/24.
6a. Verify
peter@peter-KVM:~$ docker exec -it Busybox3 sh
/ # 
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
134: eth0@if135:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:02:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.30/24 brd 192.168.2.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.101 ms
64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.116 ms
64 bytes from 192.168.1.1: seq=2 ttl=64 time=0.111 ms

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.101/0.109/0.116 ms
/ # 
7. Busybox3 add a new NIC which IP is 192.168.1.30. 7a. I will add bridge NET_192.168_1 in to container Busybox3 and ip address is 192.168.1.30
peter@peter-KVM:~$ docker network connect --ip 192.168.1.30 NET_192_168_1 Busybox3
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker exec -it Busybox3 sh
/ # ip a
1: lo:  mtu 65536 qdisc noqueue qlen 1000
    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
134: eth0@if135:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:02:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.30/24 brd 192.168.2.255 scope global eth0
       valid_lft forever preferred_lft forever
136: eth1@if137:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.30/24 brd 192.168.1.255 scope global eth1
       valid_lft forever preferred_lft forever
/ # 
/ # exit
peter@peter-KVM:~$ 
peter@peter-KVM:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11e6e8ed82ce         8000.0242a401537a       no              veth0494abc
                                                        veth72c6f9b
                                                        veth751dff8
br-4e4d06e66f42         8000.02420c6f213f       no              veth8cf5b23
docker0         8000.024219252d6f       no
peter@peter-KVM:~$ 
8. Busybox1, Busybox2 and Busybox3 can ping each other.
8a. Busybox1(192.168.1.2) ping Busybox3(192.168.1.30)
peter@peter-KVM:~$ docker exec -it Busybox1 sh
/ # 
/ # ping -c 3 192.168.1.30
PING 192.168.1.30 (192.168.1.30): 56 data bytes
64 bytes from 192.168.1.30: seq=0 ttl=64 time=0.130 ms
64 bytes from 192.168.1.30: seq=1 ttl=64 time=0.128 ms
64 bytes from 192.168.1.30: seq=2 ttl=64 time=0.131 ms

--- 192.168.1.30 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.128/0.129/0.131 ms
/ # 
/ # exit
peter@peter-KVM:~$ 
8b. Busybox3(192.168.1.30) ping NET_192_168_1 gateway(192.168.1.1) and Busybox1(192.168.1.2) and Busybox2 (192.168.1.20)
peter@peter-KVM:~$ docker exec -it Busybox3 sh
/ #  
/ # ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.135 ms
64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.124 ms
64 bytes from 192.168.1.1: seq=2 ttl=64 time=0.115 ms

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.115/0.124/0.135 ms
/ # 
/ # ping -c 3 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.110 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.125 ms
64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.135 ms

--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.110/0.123/0.135 ms
/ # 
/ # ping -c 3 192.168.1.20
PING 192.168.1.20 (192.168.1.20): 56 data bytes
64 bytes from 192.168.1.20: seq=0 ttl=64 time=0.138 ms
64 bytes from 192.168.1.20: seq=1 ttl=64 time=0.116 ms
64 bytes from 192.168.1.20: seq=2 ttl=64 time=0.117 ms

--- 192.168.1.20 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.116/0.123/0.138 ms
/ # 
/ # exit
peter@peter-KVM:~$ 
最初發表 / 最後更新: 2019.05.31 / 2019.05.31

0 comments:

張貼留言