网络基础
Docker Network
单机网络
多机网络:overlay NetworkBridge Network
Host Network None Network
网络的分层
公有IP和私有IP
Public IP:互联网上的唯一标识,可以访问internet
Private IP:不可在互联网上使用,仅供机构内部使用
A类10.0.0.0--10.255.255.255(10.0.0.0/8)
B类172.16.0.0--172.31.255.255(172.16.0.0/12)
C类192.168.0.0--192.168.255.255(192.168.0.0/16)
网络地址转换NAT
Ping和telnet
Ping(ICMP):验证IP的可达性
Linux网络命名空间
Linux Network Namespace
查看当前网络命名空间[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 listtest2test1[root@docker-node1 vagrant]# sudo ip netns exec test1 ip link1: lo: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 link1: lo: 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 link1: lo: 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 link1: lo: mtu 65536 qdisc noop state DOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:006: veth-test1: 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 link1: lo:mtu 65536 qdisc noop state DOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:005: veth-test2: 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-testlsudo 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 upsudo ip netns exec test2 ip link set dev veth-test2 up查看完整的命名空间ip信息[root@docker-node2 vagrant]# ip netns exec test1 ip a1: lo: mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:006: veth-testl: 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 a1: lo: mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:005: veth-test2: 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.1PING 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 ms64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.110 ms64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.202 ms64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.052 ms64 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 a1: lo: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 forever9: eth0: 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
新创建一个容器,新增了一个命名空间
Bridge Network 网桥
查看网桥
新创建一个容器后,会在网桥上有一个新的接口
通信
在docker0网桥上面创建的容器关联[vagrantedocker-node1~]# sudo docker run -d --name test2 --link test1 busybox /bin/sh -c "while true;dosleep 3600;done"test2 link test1 容器test2 link 容器test12 --> 1 ping 通1 --> 2 ping 不通创建网桥[root@docker-node2 ~]# docker network create -d bridge my-bridged7b1a39ddd6cd0fff8a7f2ff57a3391e9ac68f8bf09b05460f7d1200c712bc78[root@docker-node2 ~]# brctl showbridge name bridge id STP enabled interfacesbr-d7b1a39ddd6c 8000.0242ed73f4fa nodocker0 8000.02420e28dc52 no veth8bb0084 vethced44cb[root@docker-node2 ~]# docker network lsNETWORK ID NAME DRIVER SCOPE0e7556320fdd bridge bridge local0b32df326904 host host locald7b1a39ddd6c my-bridge bridge locale2db49527111 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 showbridge name bridge id STP enabled interfacesbr-d7b1a39ddd6c 8000.0242ed73f4fa no vethee72a47docker0 8000.02420e28dc52 no veth8bb0084 vethced44cb
docker network inspect my-bridge
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
docker inspect my-bridge
总结
docker network connect my-bridge test1
tes2和test 1在docker 默认的bridge上
但是互相只能通过ip来ping通
不能通过名字
自己创建的bridge 能够解决互相ip,名字都ping通
容器的端口映射
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
容器内部网络跟宿主机一样