天天看點

docker容器網絡配置docker容器網絡配置

docker容器網絡配置

文章目錄

  • docker容器網絡配置
    • @[toc]
      • ip netns指令
      • 建立Network Namespace
      • 操作Network Namespace
      • 建立veth pair
      • 實作Network Namespace間通信
      • veth裝置重命名
    • 四種網絡模式配置
      • bridge模式配置
      • none模式配置
      • container模式配置
      • host模式配置
    • 容器的常用操作
      • 檢視容器的主機名
      • 在容器啟動時注入主機名
      • 手動指定容器要使用的DNS
      • 手動往/etc/hosts檔案中注入主機名到IP位址的映射

ip netns指令

可以借助ip netns指令來完成對 Network Namespace 的各種操作。ip netns指令來自于iproute安裝包,一般系統會預設安裝,如果沒有的話,請自行安裝。

注意:ip netns指令修改網絡配置時需要 sudo 權限。

可以通過ip netns指令完成對Network Namespace 的相關操作,可以通過ip netns help檢視指令幫助資訊:

#檢視IP netns 幫助
[[email protected] ~]# ip netns help
Usage:  ip netns list
        ip netns add NAME
        ip netns attach NAME PID
        ip netns set NAME NETNSID
        ip [-all] netns delete [NAME]
        ip netns identify [PID]
        ip netns pids NAME
        ip [-all] netns exec [NAME] cmd ...
        ip netns monitor
        ip netns list-id [target-nsid POSITIVE-INT] [nsid POSITIVE-INT]
NETNSID := auto | POSITIVE-INT
[[email protected] ~]# 

           

預設情況下,Linux系統中是沒有任何 Network Namespace的,是以ip netns list指令不會傳回任何資訊。

建立Network Namespace

#通過指令建立一個名為ns0的命名空間:新建立的 Network Namespace 會出現在/var/run/netns/目錄下。如果相同名字的 namespace 已經存在,指令會報Cannot create namespace file "/var/run/netns/ns0": File exists的錯誤。
[[email protected] ~]# ip netns add ns0
[[email protected] ~]# ip netns list
ns0
[[email protected] ~]# 
[[email protected] ~]# ls /var/run/netns/
ns0
[[email protected] ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists
[[email protected] ~]# 

           

操作Network Namespace

ip指令提供了

ip netns exec

子指令可以在對應的 Network Namespace 中執行指令。

檢視新建立 Network Namespace 的網卡資訊

[[email protected] ~]# ip netns exec ns0 ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[[email protected] ~]# 

           

可以看到,新建立的Network Namespace中會預設建立一個lo回環網卡,此時網卡處于關閉狀态。此時,嘗試去 ping 該lo回環網卡,會提示Network is unreachable

[[email protected] ~]# ip netns exec ns0 ping 127.0.0.1
connect: Network is unreachable
[[email protected] ~]# 
[[email protected] ~]# ip netns exec ns0 ip link set lo up
[[email protected] ~]# ip netns exec ns0 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.050 ms

           

建立veth pair

[[email protected] ~]# ip link add type veth
[[email protected] ~]# ip a
4: [email protected]: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5a:7f:ac:3e:38:5e brd ff:ff:ff:ff:ff:ff
5: [email protected]: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether c2:cd:41:f1:55:e9 brd ff:ff:ff:ff:ff:ff
[[email protected] ~]# 
           

可以看到,此時系統中新增了一對veth pair,将veth0和veth1兩個虛拟網卡連接配接了起來,此時這對 veth pair 處于”未啟用“狀态。

實作Network Namespace間通信

下面我們利用veth pair實作兩個不同的 Network Namespace 之間的通信。剛才我們已經建立了一個名為ns0的 Network Namespace,下面再建立一個資訊Network Namespace,命名為ns1

[[email protected] ~]# ip netns add ns1
[[email protected] ~]# ip netns list
ns1
ns0
[[email protected] ~]# 
#然後我們将veth0加入到ns0,将veth1加入到ns1
[[email protected] ~]# ip link set veth0 netns ns0
[[email protected] ~]# ip link set veth1 netns ns1
           

然後我們分别為這對veth pair配置上ip位址,并啟用它們

[[email protected] ~]# ip netns exec ns0 ip link set veth0 up
[[email protected] ~]# ip netns exec ns0 ip addr add 192.168.171.1/24 dev veth0 
[[email protected] ~]# ip netns exec ns1 ip link set lo up
[[email protected] ~]# ip netns exec ns1 ip link set veth1 up
[[email protected] ~]# ip nstns exec ns1 ip addr add 192.168.171.2/24 dev veth1
[[email protected] ~]# ip netns exec ns1 ip addr add 192.168.171.2/24 dev veth1
#然後IP a檢視狀态
[[email protected] ~]# ip netns exec ns0 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
4: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 5a:7f:ac:3e:38:5e brd ff:ff:ff:ff:ff:ff link-netns ns1
    inet 192.168.171.1/24 scope global veth0
       valid_lft forever preferred_lft forever
    inet6 fe80::587f:acff:fe3e:385e/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
5: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether c2:cd:41:f1:55:e9 brd ff:ff:ff:ff:ff:ff link-netns ns0
    inet 192.168.171.2/24 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::c0cd:41ff:fef1:55e9/64 scope link 
       valid_lft forever preferred_lft forever
           

從上面可以看出,我們已經成功啟用了這個veth pair,并為每個veth裝置配置設定了對應的ip位址。我們嘗試在ns1中通路ns0中的ip位址:

[[email protected] ~]# ip netns exec ns1 ping 192.168.171.1
PING 192.168.171.1 (192.168.171.1) 56(84) bytes of data.
64 bytes from 192.168.171.1: icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from 192.168.171.1: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 192.168.171.1: icmp_seq=3 ttl=64 time=0.060 ms
64 bytes from 192.168.171.1: icmp_seq=4 ttl=64 time=0.060 ms
           

可以看到,veth pair成功實作了兩個不同Network Namespace之間的網絡互動

veth裝置重命名

[[email protected] ~]# ip netns exec ns0 ip a
[[email protected] ~]# ip netns exec ns0 ip link set veth0 down
[[email protected] ~]# ip netns exec ns0 ip link set dev veth0 name eth0
[[email protected] ~]# ip netns exec ns0 ifconfig -a
eth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.171.1  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 5a:7f:ac:3e:38:5e  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 1412 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1412 (1.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 672 (672.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 672 (672.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# 
           

四種網絡模式配置

bridge模式配置

[[email protected] ~]# docker run -it --name t1 --rm busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete 
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13 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:1182 (1.1 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
# 在建立容器時添加--network bridge與不加--network選項效果是一緻的
[[email protected] ~]# docker run -it --name t1 --network bridge --rm busybox
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 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:736 (736.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 
           

none模式配置

[[email protected] ~]# docker run -it --name t1 --network none --rm busybox
/ # ifconfig -a
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

           

container模式配置

啟動第一個容器

[[email protected] ~]# docker run -it --name b1 --rm busybox 
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9 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:806 (806.0 B)  TX bytes:0 (0.0 B)

           

啟動第二個容器

[[email protected] ~]# docker run -it --name b2 --rm busybox
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 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:736 (736.0 B)  TX bytes:0 (0.0 B)

           

可以看到名為b2的容器IP位址是172.17.0.2,與第一個容器的IP位址不是一樣的,也就是說并沒有共享網絡,此時如果我們将第二個容器的啟動方式改變一下,就可以使名為b2的容器IP與B1容器IP一緻,也即共享IP,但不共享檔案系統。

[[email protected] ~]# docker run -it --name b2 --rm --network container:b1 busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9 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:806 (806.0 B)  TX bytes:0 (0.0 B)

           

此時我們在b1容器上建立一個目錄

/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir /tmp/data
/ # ls /tmp/
data
/ # 

           

到b2容器上檢查/tmp目錄會發現并沒有這個目錄,因為檔案系統是處于隔離狀态,僅僅是共享了網絡而已。

在b2容器上部署一個站點

/ # echo 'hello world' > /tmp/index.html
/ # ls /tmp/
index.html
/ # httpd -h /tmp/
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 :::80                   :::*                    LISTEN      
/ # 

           

在b1容器上用本地位址去通路此站點

/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13 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:1086 (1.0 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # wget -O - -q 127.0.0.1:80
hello world
/ # 

           

由此可見,container模式下的容器間關系就相當于一台主機上的兩個不同程序

host模式配置

啟動容器時直接指明模式為host

[[email protected] ~]# docker run -it --name b2 --rm --network host busybox
/ # ifconfig 
docker0   Link encap:Ethernet  HWaddr 02:42:0E:4A:CC:8A  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:eff:fe4a:cc8a/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:2066 (2.0 KiB)

ens33     Link encap:Ethernet  HWaddr 00:0C:29:22:9F:4E  
          inet addr:192.168.171.134  Bcast:192.168.171.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe22:9f4e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:252476 errors:0 dropped:0 overruns:0 frame:0
          TX packets:222072 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:171141045 (163.2 MiB)  TX bytes:30471831 (29.0 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

           

此時如果我們在這個容器中啟動一個http站點,我們就可以直接用主控端的IP直接在浏覽器中通路這個容器中的站點了

容器的常用操作

檢視容器的主機名

[[email protected] ~]# docker run -it --name t1 --network bridge --rm busybox
/ # hostname
373c49c965d5
/ # 

           

在容器啟動時注入主機名

[[email protected] ~]# docker run -it --name t1 --network bridge --hostname Tatum --rm busybox
/ # hostname 
Tatum
/ # 
/ # 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.2      Tatum  	#注入主機名時會自動建立主機名到IP的映射關系
/ # cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.171.2
/ # 
/ # ping www.baidu.com
PING www.baidu.com (112.80.248.75): 56 data bytes
64 bytes from 112.80.248.75: seq=0 ttl=127 time=53.192 ms
64 bytes from 112.80.248.75: seq=1 ttl=127 time=58.016 ms
64 bytes from 112.80.248.75: seq=2 ttl=127 time=109.944 ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 53.192/73.717/109.944 ms
           

手動指定容器要使用的DNS

[[email protected] ~]# docker run -it --name t1 --network bridge --hostname tatum --dns 114.114.114.114 --rm busybox
/ # cat /etc/resolv.conf 
search localdomain
nameserver 114.114.114.114
/ # nslookup -type=a www.baidu.com
Server:         114.114.114.114
Address:        114.114.114.114:53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com
Name:   www.a.shifen.com
Address: 110.242.68.3
Name:   www.a.shifen.com
Address: 110.242.68.4

/ # 

           

手動往/etc/hosts檔案中注入主機名到IP位址的映射

[[email protected] ~]# docker run -it --name t1 --network bridge --hostname tatum --add-host www.a.com:1.1.1.1 --rm busybox
/ # 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
1.1.1.1 www.a.com
172.17.0.2      tatum
/ # 
           

Name: www.a.shifen.com

Address: 110.242.68.4

/ #

### 手動往/etc/hosts檔案中注入主機名到IP位址的映射

```bash
[[email protected] ~]# docker run -it --name t1 --network bridge --hostname tatum --add-host www.a.com:1.1.1.1 --rm busybox
/ # 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
1.1.1.1 www.a.com
172.17.0.2      tatum
/ # 
           

繼續閱讀