docker使用的與Linux網絡有關的主要技術:linux
<1> 網絡命令空間docker
root@backup:~# ls -l /proc/$$/ns total 0 lrwxrwxrwx 1 root root 0 May 21 16:59 ipc -> ipc:[4026531839] lrwxrwxrwx 1 root root 0 May 21 16:59 mnt -> mnt:[4026531840] lrwxrwxrwx 1 root root 0 May 21 16:59 net -> net:[4026531957] lrwxrwxrwx 1 root root 0 May 21 16:59 pid -> pid:[4026531836] lrwxrwxrwx 1 root root 0 May 21 16:59 user -> user:[4026531837] lrwxrwxrwx 1 root root 0 May 21 16:59 uts -> uts:[4026531838]
root@karl-v1:~# ip netns add netns1 ## ip netns add <name> root@karl-v1:~# ip netns exec netns1 ip link show ## ip netns exec <name> <command> 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 root@karl-v1:~# ip netns exec netns1 bash ## ip netns exec <name> bash root@karl-v1:~# ip link show 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 root@karl-v1:~# exit exit root@karl-v1:~# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:50:56:8d:1e:55 brd ff:ff:ff:ff:ff:ff 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:59:33:fc:fc brd ff:ff:ff:ff:ff:ff root@karl-v1:~# ip link set br0 netns netns1 Cannot find device "br0" root@karl-v1:~# ip link set lo netns netns1 RTNETLINK answers: Invalid argument root@karl-v1:~# ethtool -k lo |grep netns ## 查看設備是否可轉移命名空間 netns-local: on [fixed]
<2> Veth 設備對bash
root@karl-v1:~# ip link add veth0 type veth peer name veth1 root@karl-v1:~# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:50:56:8d:1e:55 brd ff:ff:ff:ff:ff:ff 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:59:33:fc:fc brd ff:ff:ff:ff:ff:ff 154: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 5e:00:3d:72:27:76 brd ff:ff:ff:ff:ff:ff 155: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 02:89:03:d6:ab:29 brd ff:ff:ff:ff:ff:ff root@karl-v1:~# root@karl-v1:~# root@karl-v1:~# ip link set veth1 netns netns1 root@karl-v1:~# root@karl-v1:~# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:50:56:8d:1e:55 brd ff:ff:ff:ff:ff:ff 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:59:33:fc:fc brd ff:ff:ff:ff:ff:ff 155: veth0@if154: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 02:89:03:d6:ab:29 brd ff:ff:ff:ff:ff:ff root@karl-v1:~# ip netns exec netns1 ip link show 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 154: veth1@if155: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 5e:00:3d:72:27:76 brd ff:ff:ff:ff:ff:ff root@karl-v1:~# root@karl-v1:~# ip netns exec netns1 ip addr add 10.1.1.1/24 dev veth1 root@karl-v1:~# root@karl-v1:~# ip addr add 10.1.1.2/24 dev veth0 root@karl-v1:~# root@karl-v1:~# ip netns exec netns1 ip link set dev veth1 up root@karl-v1:~# ip link set dev veth0 up root@karl-v1:~# ip link show |grep veth0 155: veth0@if154: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 root@karl-v1:~# root@karl-v1:~# ping 10.1.1.1 PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data. 64 bytes from 10.1.1.1: icmp_seq=1 ttl=64 time=0.107 ms 64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.042 ms 64 bytes from 10.1.1.1: icmp_seq=3 ttl=64 time=0.042 ms root@karl-v1:~# ip netns exec netns1 ping 10.1.1.2
root@karl-v1:~# ip netns exec netns1 ethtool -S veth1 NIC statistics: peer_ifindex: 155 root@karl-v1:~# root@karl-v1:~# ip link show |grep 155 155: veth0@if154: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 root@karl-v1:~#
<3> 網橋網絡
root@karl-v1:~# root@karl-v1:~# ip link add veth999 type veth peer name veth998 root@karl-v1:~# root@karl-v1:~# root@karl-v1:~# brctl addbr br999 root@karl-v1:~# root@karl-v1:~# ip link |grep veth999 159: veth998@veth999: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 160: veth999@veth998: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 root@karl-v1:~# root@karl-v1:~# root@karl-v1:~# brctl addif br999 veth999 root@karl-v1:~# root@karl-v1:~# root@karl-v1:~# ip link |grep veth999 159: veth998@veth999: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 160: veth999@veth998: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop master br999 state DOWN mode DEFAULT group default qlen 1000 root@karl-v1:~# root@karl-v1:~# ifconfig br999 172.119.119.119 root@karl-v1:~# ifconfig br999 br999 Link encap:Ethernet HWaddr ea:e8:d7:21:0c:42 inet addr:172.119.119.119 Bcast:172.119.255.255 Mask:255.255.0.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) root@karl-v1:~#
<4> Iptables/Netfilter函數
<5> 路由oop
LOCAL表的查看 root@karl-v1:~# ip route show table local type local 10.1.1.2 dev veth0 proto kernel scope host src 10.1.1.2 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1 172.17.0.1 dev docker0 proto kernel scope host src 172.17.0.1 172.21.1.11 dev eth0 proto kernel scope host src 172.21.1.11 172.119.119.119 dev br999 proto kernel scope host src 172.119.119.119 root@karl-v1:~# 路由表的查看 1)ip route list root@karl-v1:~# ip route list default via 172.21.1.14 dev eth0 10.1.1.0/24 dev veth0 proto kernel scope link src 10.1.1.2 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 172.21.1.8/29 dev eth0 proto kernel scope link src 172.21.1.11 172.119.0.0/16 dev br999 proto kernel scope link src 172.119.119.119 root@karl-v1:~# 2) netstat -rn root@karl-v1:~# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 172.21.1.14 0.0.0.0 UG 0 0 0 eth0 10.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 veth0 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.21.1.8 0.0.0.0 255.255.255.248 U 0 0 0 eth0 172.119.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br999 root@karl-v1:~#