使用ip命令配置docker容器網絡


啟動一個名為test1的docker容器

[root@localhost ~]# docker run -itd --name test1 busybox /bin/sh
d0a13f295d7ac256aa6ba63ab5af0d4ba2ffcb7c7ae455b9e997462d363ff6cb
[root@localhost ~]# ip netns list
ns2
ns1 (id: 0)

使用ip netns命令創建了兩個network namespace(ns1和ns2)后會在/var/run/netns目錄下看到ns1和ns2兩項

[root@localhost ~]# ls -la /var/run/netns
總用量 0
drwxr-xr-x  2 root root  80 12月 28 17:20 .
drwxr-xr-x 27 root root 820 12月 28 17:20 ..
-r--r--r--  1 root root   0 12月 28 17:20 ns1
-r--r--r--  1 root root   0 12月 28 17:20 ns2

docker創建的network namespace並不在此目錄下創建任何項,linux下的每個進程都會屬於一個特定的network namespace,來看一下不同network namespace環境中/proc/$PID/ns目錄下有何區別

/proc/self鏈接到當前正在運行的進程

主機默認的network namespace中

[root@localhost ~]# ls -la /proc/self/ns/
總用量 0
dr-x--x--x 2 root root 0 12月 28 17:51 .
dr-xr-xr-x 9 root root 0 12月 28 17:51 ..
lrwxrwxrwx 1 root root 0 12月 28 17:51 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 12月 28 17:51 mnt -> mnt:[4026531840]
lrwxrwxrwx 1 root root 0 12月 28 17:51 net -> net:[4026531956]
lrwxrwxrwx 1 root root 0 12月 28 17:51 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 12月 28 17:51 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 12月 28 17:51 uts -> uts:[4026531838]

在ns1中

[root@localhost ~]# ip netns exec ns1 ls -la /proc/self/ns
總用量 0
dr-x--x--x 2 root root 0 12月 28 17:52 .
dr-xr-xr-x 9 root root 0 12月 28 17:52 ..
lrwxrwxrwx 1 root root 0 12月 28 17:52 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 12月 28 17:52 mnt -> mnt:[4026532688]
lrwxrwxrwx 1 root root 0 12月 28 17:52 net -> net:[4026532503]
lrwxrwxrwx 1 root root 0 12月 28 17:52 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 12月 28 17:52 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 12月 28 17:52 uts -> uts:[4026531838]
[root@localhost ~]# 

在ns2中

[root@localhost ~]# ip netns exec ns2 ls -la /proc/self/ns
總用量 0
dr-x--x--x 2 root root 0 12月 28 17:53 .
dr-xr-xr-x 9 root root 0 12月 28 17:53 ..
lrwxrwxrwx 1 root root 0 12月 28 17:53 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 12月 28 17:53 mnt -> mnt:[4026532688]
lrwxrwxrwx 1 root root 0 12月 28 17:53 net -> net:[4026532567]
lrwxrwxrwx 1 root root 0 12月 28 17:53 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 12月 28 17:53 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 12月 28 17:53 uts -> uts:[4026531838]

只要將代表docker創建的network namespace文件的鏈接到/var/run/netns目錄下,就可以使用ip netns命令進行操作了

[root@localhost ~]# docker inspect --format '{{ .State.Pid }}' test1
14450

若不存在目錄/var/run/netns,則創建

[root@localhost ~]# mkdir /var/run/netns/

在/var/run/netns/下創建軟鏈接,指向test1容器的network namespace

[root@localhost ~]# docker inspect --format '{{ .State.Pid }}' test1
14450
[root@localhost ~]# ln -s /proc/14450/ns/net /var/run/netns/test1

測試是否成功

[root@localhost ~]# ip netns list
test1 (id: 1)
ns2
ns1 (id: 0)
[root@localhost ~]# ip netns exec test1 ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
6: eth0@if7: <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

完成以上配置后,就可以自行配置docker的網絡環境了,除了ip netns命令外,還有一些工具可以進入linux namespace。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM