docker的網絡基礎

docker使用的與Linux網絡有關的主要技術:linux

  1. Network Namespace
  2. Veth 設備對
  3. Iptables/Netfilter
  4. 網橋
  5. 路由

 

<1> 網絡命令空間docker

  • namespace的本質就是把原來全部進程全局共享的資源拆分紅了不少個一組一組進程共享的資源
  • 當一個namespace裏面的全部進程都退出時,namespace也會被銷燬,因此拋開進程談namespace沒有意義
  • Linux內核中的7種類型的namespace
  1. Cgroup
  2. IPC
  3. Network
  4. Mount
  5. PID
  6. User
  7. UTS
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> 網橋網絡

  •  網橋是一個二層的虛擬網絡設備,把若干網口「鏈接」起來,使得網口間的報文能互相轉發
  • 與單純的交換機不一樣,交換機只是一個二層設備,對於接收到的報文,要麼轉發,要麼丟棄
  • 網橋,除了轉發和丟棄,還能提交到協議棧上層(網絡層),既可將其看做二層設備,也可看做三層設備
  • 網橋能夠有一個IP地址,一個網橋(br0)能夠綁定多個以太網接口(如eth0和eth1)
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函數

  •  Linux提供了一套機制來爲用戶實現自定義的數據包處理過程
  • 在Linux網絡協議棧中有一組回調函數掛接點,經過這些掛接點掛接的鉤子函數可在Linux網絡協議棧處理數據包的過程當中對數據包進行一些操做,例如過濾、修改、丟棄等。整個掛接點技術叫作 Netfilter 和 Iptables
  • Netfilter負責在內核中執行各類掛接的規則,運行在內核模式中;而Iptables是在用戶模式下運行的進程,負責維護內核中Netfiler的各類規則表。經過二者的配合來實現整個Linux網絡協議棧中靈活的數據包處理機制
  • Netfilter能夠掛接的規則點有5個:PREROUTING , INPUT , FORWARD , OUTPUT , POSTROUTING
  • 咱們可在不一樣類型的Table中加入咱們的規則,目前主要支持的Table類型以下:
  1. RAW
  2. MANGLE
  3. NAT
  4. FILTER
  • 上述4個Table(規則鏈)的優先級是RAW最高,FILTER最低。
  • Iptables命令用於協助用戶維護各類規則,查看系統已有的規則有以下兩種方法:
  1. iptables-save: 按照命令行的方式打印Iptables的內容
  2. iptables -nvL: 已另外一種格式顯示Netfilter表的內容

 

<5> 路由oop

  • Linux的路由表至少包括兩個表(當啓用策略路由時,還會有其餘表):一個是LOCAL,另外一個是MAIN。
  • LOCAL表中包含多有本地設備地址,是在配置網絡設備地址時自動建立的,LOCAL表用於linux協議棧識別本地地址,以及進行本地不一樣網口間的數據轉發
  • MAIN表用於各種網絡IP地址的轉發。可使用靜態配置生成,也可以使用動態路由發現協議生成。
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:~#
相關文章
相關標籤/搜索