s4 Docker 網絡1

網絡基礎

Docker Networknode

單機網絡nginx

Bridge Network
Host Network
None Networkweb


多機網絡:overlay Networkdocker

網絡的分層網絡

346925984[4]

公有IP和私有IPide

Public IP:互聯網上的惟一標識,能夠訪問internetoop

Private IP:不可在互聯網上使用,僅供機構內部使用測試

A類10.0.0.0--10.255.255.255(10.0.0.0/8)spa

B類172.16.0.0--172.31.255.255(172.16.0.0/12)vagrant

C類192.168.0.0--192.168.255.255(192.168.0.0/16)

網絡地址轉換NAT

347080937[4]

Ping和telnet

Ping(ICMP):驗證IP的可達性

Linux網絡命名空間

 

Linux Network Namespace

347249562[4]140035421[4]

 

查看當前網絡命名空間
[root@docker-node1 vagrant]# ip netns list
添加網絡命名空間
[root@docker-node1 vagrant]# ip netns add test1
[root@docker-node1 vagrant]# ip netns add test2
[root@docker-node1 vagrant]# ip netns list
test2
test1
[root@docker-node1 vagrant]# sudo ip netns exec test1 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@docker-node1 vagrant]# sudo ip netns exec test1 ip link set dev lo up
[root@docker-node1 vagrant]# sudo ip netns exec test1 ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

[root@docker-node2 vagrant]# ip netns exec test1 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@docker-node2 vagrant]# sudo ip link set veth-test1 netns test1
[root@docker-node2 vagrant]# ip netns exec test1 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
6: veth-test1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 36:2d:2a:23:8e:af brd ff:ff:ff:ff:ff:ff
添加網絡命名空間
添加了一個test2的網絡命名空間
[root@docker-node2 vagrant]# ip netns add test2
綁定到veth-test2的設備
[root@docker-node2 vagrant]# sudo ip link set veth-test2 netns test2
查看命名空間的ip link
[root@docker-node2 vagrant]# ip netns exec test2 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: veth-test2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 2e:00:77:8a:33:57 brd ff:ff:ff:ff:ff:ff

給網絡命名空間添加ip並綁定
sudo ip netns exec test1 ip addr add 192.168.1.1/24 dev veth-testl
sudo ip netns exec test2 ip addr add 192.168.1.2/24 dev veth-test2

將兩個網絡命名空間綁定的ip設備啓動
sudo ip netns exec test1 ip link set dev veth-testl up
sudo ip netns exec test2 ip link set dev veth-test2 up

查看完整的命名空間ip信息
[root@docker-node2 vagrant]# ip netns exec test1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
6: veth-testl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 36:2d:2a:23:8e:af brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 scope global veth-testl
       valid_lft forever preferred_lft forever
    inet6 fe80::342d:2aff:fe23:8eaf/64 scope link
       valid_lft forever preferred_lft forever
[root@docker-node2 vagrant]# ip netns exec test2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: veth-test2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 2e:00:77:8a:33:57 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 scope global veth-test2
       valid_lft forever preferred_lft forever
    inet6 fe80::2c00:77ff:fe8a:3357/64 scope link
       valid_lft forever preferred_lft forever

測試命名空間的 互通性 ping
[root@docker-node2 vagrant]# ip netns exec test2 ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.050 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.110 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.202 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.052 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.040 ms

 

 

Docker Bridge0詳解

   每一個容器之間有獨立的命名空間(ip),相互之間能夠ping 通,

   Docker Bridge0 在宿主機上橋接出來的一塊命名空間,一般爲 172.17.0.2, 裏面容器的ip通常在該網段隨機分佈。除非容器暴露了外面的,不然外面宿主機不能訪問

[root@docker-node2 ~]# sudo docker run -d --name test1 busybox /bin/sh -c "while true;do sleep 3600;done"
8f7cbad560db6d4c604395f3ee16c15d6fc4073e3010134e2520aed780b6198d
​
[root@docker-node2 ~]# docker exec test1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
    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
9: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
​
查看網橋
brctl show

image

imageimage

 

新建立一個容器,新增了一個命名空間

Bridge Network 網橋

349663937

查看網橋

新建立一個容器後,會在網橋上有一個新的接口

142110968[4]

通訊

c651de0e-4ab4-48e9-993b-b04c2f1b4df8

在docker0網橋上面建立的容器關聯
​
[vagrantedocker-node1~]# sudo docker run -d --name test2 --link test1 busybox /bin/sh -c "while true;do
sleep 3600;done"
test2 link test1  容器test2 link 容器test1
2 --> 1 ping 通
1 --> 2 ping 不通

建立網橋
[root@docker-node2 ~]# docker network create -d bridge my-bridge
d7b1a39ddd6cd0fff8a7f2ff57a3391e9ac68f8bf09b05460f7d1200c712bc78
[root@docker-node2 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br-d7b1a39ddd6c         8000.0242ed73f4fa       no
docker0         8000.02420e28dc52       no              veth8bb0084
                                                        vethced44cb
​
​
[root@docker-node2 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
0e7556320fdd        bridge              bridge              local
0b32df326904        host                host                local
d7b1a39ddd6c        my-bridge           bridge              local
e2db49527111        none                null                local
​
​
[root@docker-node2 ~]#  sudo docker run -d --name test3 --network my-bridge busybox /bin/sh -c "while true;do sleep 3600;done"
7eb76c6ca9f6123811fc75cae03e571f669b308c2b6bad0cfbdd598508629f0d
[root@docker-node2 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br-d7b1a39ddd6c  8000.0242ed73f4fa       no              vethee72a47
docker0         8000.02420e28dc52       no              veth8bb0084
                                                        vethced44cb

docker network inspect my-bridge

5423040e-3b36-473a-9ef5-d7f23716f852[4]

 

 

sudo docker run -d --name test3 --network my-bridge busybox /bin/sh -c "while true;do sleep 3600;done"

test2 和 test 3 在同一個網橋my-bridge (本身建立的),能夠經過名稱ping

a221e978-a6b0-45b5-bf19-1fedebd843a9

0e034502-7711-42fa-b4ce-7152314cf51e[4]

docker inspect my-bridge

總結

docker network connect my-bridge test1

tes2和test 1在docker 默認的bridge上

可是互相只能經過ip來ping通

不能經過名字

本身建立的bridge 可以解決互相ip,名字都ping通

2dc08891-99ee-4017-b575-32c3cfd26222[4]

 

 

 

容器的端口映射

 docker run --name web -d -p 80:80 nginx

Container Port Map 端口映射

 

容器網絡之host和none

1. none 沒有ip,只有宿主能夠訪問

[vagrant@docker-node1~]$ sudo docker run -d --name test1 --network none busybox /bin/sh -c "while true;do sleep 3600;done"
da2143722efb01827070a4aa5ce52857f9269f5ed20f65f4cb39977d39141913

 

誰都不能訪問,只能 sudo docker exec -it test1 /bin/sh

2. host 共享主機的網絡

[vagrant@docker-node1 ~]$ sudo docker run -d --name test1 --network host busybox /bin/sh -c "while true;do sleep 3600;done"
18e175e07b5e15c941f848fa5d4105357d70dd0f03f8bdb55791c0997875074b

容器內部網絡跟宿主機同樣

 

相關文章
相關標籤/搜索