docker網絡高端實踐與原理解析

更多問題但願你們關注個人github: https://github.com/fanuxnode

網絡概述

  1. 端口映射:
$ docker run -p 8080:80 nginx:latest

若是沒有這個-p,會發現啓動了nginx可是沒法經過宿主機訪問到web服務,而使用了-p參數後就能夠經過訪問主機的8080斷開去訪問nginx了。
端口映射的原理是做了net轉發mysql

  1. 共享主機網絡:
$ docker run --net=host nginx:latest

這種容器沒有本身的網絡,徹底共享主機的網絡,因此能夠經過主機ip直接訪問容器服務。 壞處是容器與其它容器端口衝突linux

  1. link網絡
$ docker run --name mysql mysql:latest
$ docker run --link=mysql nginx:latest

這樣nginx能夠經過容器名去訪問mysql,其原理是在nginx容器中的/etc/hosts中加入了mysql主機名解析。這種共享不可跨主機nginx

$ docker run --rm -it --name c1 centos:latest /bin/bash
$ docker run --rm -it --name c2 --link c1  centos:latest /bin/bash
[root@178d290d873c /]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.4    c1 3b7b15fa7e20   # 看這裏
172.17.0.5    178d290d873c
  1. none模式
    容器不建立網絡,須要自行定義
  2. overlay網絡
    進羣中經常使用的網絡模式,使用vxlan等技術做了一層覆蓋,使每一個容器有本身獨立的ip並可跨主機通訊。
  3. 共享容器網絡

如kubernetes裏pod的實現,pod是多個容器的集合,這些容器都共享了同一個容器的網絡,那麼這些容器就如同在一個host上同樣。git

bridge原理

在宿主機上ifconfig:github

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:a4ff:fe60:b79d  prefixlen 64  scopeid 0x20<link>
        ether 02:42:a4:60:b7:9d  txqueuelen 0  (Ethernet)
        RX packets 23465  bytes 3407255 (3.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24676  bytes 22031766 (21.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethcd2d45d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::c4d6:dcff:fe7d:5f44  prefixlen 64  scopeid 0x20<link>
        ether c6:d6:dc:7d:5f:44  txqueuelen 0  (Ethernet)
        RX packets 415  bytes 82875 (80.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 372  bytes 379450 (370.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0是一個虛擬網橋,相似一個交換機的存在。 veth開頭的網卡就是爲容器分配的一個設備,可是要注意這不是容器中的設備。因爲linux物理網卡只能出如今一個namespace中,因此只能用虛擬設備給容器建立獨立的網卡。web

docker network inspect bridge 看一下,這是給容器內部分配的地址:sql

"Containers": {
    "ac8c983592f06d585a75184b4dcd012338645fb7fa60b07c722f59ce43ceb807": {
        "Name": "sick_snyder",
        "EndpointID": "0755707344f30c40d686a2b4fdcabf45d6e1a64f8de8618b9a3a8c8e5b203ddc",
        "MacAddress": "02:42:ac:11:00:02",
        "IPv4Address": "172.17.0.2/16",
        "IPv6Address": ""
    }
}

再引入一個概念:linux設備對,相似管道同樣,在一端寫另外一端就能夠讀,容器內的eth0就與這個veth是一對設備對docker

docker0         eth0 -> 宿主機
        ---------------    ----
         |          |
        vethx      vethy
        ----       ----
          |          |    ---->設備對
     +----+---+ +----+---+
     |  eth0  | |  eth0  |
     +--------+ +--------+
      容器1       容器2

單有這些還不夠,還須要iptables對包做一些處理,下文細說。有了這些理論,再去順着這個思路去讀網絡模塊的代碼ubuntu

network namespace實踐

使用ip命令,若是沒有的話安裝一下:yum install net-tools

基本命令:

ip netns add nstest  # 建立一個net namespace
ip netns list        # 查看net namespace列表
ip netns delete nstest # 刪除
ip netns exec [ns name] command # 到對應的ns裏去執行命令
ip netns exec [ns name] bash # 在ns中使用bash,須要要ns中作一系列操做時方便

開啓ns中的迴環設備,以建立的nstest爲例

ip netns exec nstest ip link set dev lo up

在主機上建立兩個虛擬網卡兩張網卡是linux設備對

ip link set add veth-a type veth peer name veth-b

添加veth-b到nstest中

ip link set veth-b netns nstest

驗證:

[root@dev-86-208 ~]# ip netns exec nstest 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
251: veth-b@if252: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether aa:0a:7d:01:06:d3 brd ff:ff:ff:ff:ff:ff link-netnsid 0

爲網卡設置ip並啓動:

[root@dev-86-208 ~]# ip addr add 10.0.0.1/24 dev veth-a
[root@dev-86-208 ~]# ip link set dev veth-a up

[root@dev-86-208 ~]# ip netns exec nstest ip addr add 10.0.0.2/24 dev veth-b
[root@dev-86-208 ~]# ip netns exec nstest ip link set dev veth-b up

設置完ip,自動添加了這個路由
[root@dev-86-208 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.1.86.1       0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 veth-a # 目的地址是10.0.0.0/24的就從這張網卡發出
10.1.86.0       0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-4b03f208bc30

ns裏面的路由表
[root@dev-86-208 ~]# ip netns exec nstest ip route
10.0.0.0/24 dev veth-b  proto kernel  scope link  src 10.0.0.2

驗證相互ping:

[root@dev-86-208 ~]# ip netns exec nstest ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.032 ms

[root@dev-86-208 ~]# ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.024 ms

Docker bridge的網絡

咱們去建立兩個ns(ns1 與 ns2)模擬兩個容器,建立四張網卡(兩對設備對)模仿容器網卡。

brtest
   |          +-------------+
   |-veth1 <--|--> eth1 ns1 |  
   |          |-------------+
   |-veth2 <--|--> eth1 ns2 |  
   |          +-------------+

再在宿主機上建立一個網橋brtest模擬docker0網橋,將veth1和veth2橋接到上面。

添加namespace:

[root@dev-86-208 ~]# ip netns add ns1
[root@dev-86-208 ~]# ip netns add ns2
[root@dev-86-208 ~]# ip netns list
ns2
ns1
test1 (id: 3)
nstest (id: 2)

[root@dev-86-208 ~]# ip netns exec ns1 ip link set dev lo up
[root@dev-86-208 ~]# ip netns exec ns2 ip link set dev lo up

添加網卡對:

[root@dev-86-208 ~]# ip link add veth1 type veth peer name eth1
[root@dev-86-208 ~]# ip link set eth1 netns ns1
[root@dev-86-208 ~]# ip link add veth2 type veth peer name eth1
[root@dev-86-208 ~]# ip link set eth1 netns ns2

[root@dev-86-208 ~]# ip netns exec ns1 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
255: eth1@if256: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether ae:93:ba:2c:54:93 brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@dev-86-208 ~]# ip netns exec ns2 ip link
257: eth1@if258: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 3a:a6:f3:27:9d:83 brd ff:ff:ff:ff:ff:ff link-netnsid 0
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@dev-86-208 ~]# ip netns exec ns1 ip addr add 172.17.1.1/24 dev eth1
[root@dev-86-208 ~]# ip netns exec ns2 ip addr add 172.17.1.2/24 dev eth1

[root@dev-86-208 ~]# ip netns exec ns1 ip link set dev eth1 up
[root@dev-86-208 ~]# ip netns exec ns2 ip link set dev eth1 up

建立網橋:

[root@dev-86-208 ~]# brctl addbr brtest
[root@dev-86-208 ~]# ifconfig brtest
brtest: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 1e:60:eb:c1:e6:d0  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@dev-86-208 ~]# brctl addif brtest veth1
[root@dev-86-208 ~]# brctl addif brtest veth2

[root@dev-86-208 ~]# ifconfig brtest up
[root@dev-86-208 ~]# ifconfig veth1 up      # 主機上這兩張網卡工做在數據鏈路層,所以不須要設置ip也能通
[root@dev-86-208 ~]# ifconfig veth2 up

恭喜兩個eth1之間能夠通了:

[root@dev-86-208 ~]# ip netns exec ns1 ping 172.17.1.2
PING 172.17.1.2 (172.17.1.2) 56(84) bytes of data.
64 bytes from 172.17.1.2: icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from 172.17.1.2: icmp_seq=2 ttl=64 time=0.022 ms

[root@dev-86-208 ~]# ip netns exec ns2 ping 172.17.1.1
PING 172.17.1.1 (172.17.1.1) 56(84) bytes of data.
64 bytes from 172.17.1.1: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 172.17.1.1: icmp_seq=2 ttl=64 time=0.041 ms

固然想在主機上能ping通容器的話須要給brtest加ip:

[root@dev-86-208 ~]# ip addr add 172.17.1.254/24 dev brtest

[root@dev-86-208 ~]# route -n # 上面動做設置了路由
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.1.0      0.0.0.0         255.255.255.0   U     0      0        0 brtest

[root@dev-86-208 ~]# ping 172.17.1.1
PING 172.17.1.1 (172.17.1.1) 56(84) bytes of data.
64 bytes from 172.17.1.1: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 172.17.1.1: icmp_seq=2 ttl=64 time=0.030 ms

以上操做就是docker bridge模式的模型

用ip命令配置docker網絡

咱們若是須要對容器網絡進行配置,如修改ip地址,進入到容器裏面顯然不合適,並且有時不使用特權模式時是操做不了的,不過咱們可使用ip命令對其進行操做。

docker run -itd --name test ubuntu:14.04 /bin/bash

這時namespace其實已經創建了,不過使用ip命令看不到

docker inspect --format '{{ .State.Pid }}' test
3847
mkdir /var/run/netns # 若是不存在才建立
ln -s /proc/3847/ns/net /var/run/netns/test

測試:

# ip netns list
test

[root@dev-86-208 ~]# 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
253: eth0@if254: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
相關文章
相關標籤/搜索