使用ip命令配置docker容器网络

启动一个名为test1的docker容器

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

使用ip netns命令创建了两个network namespace(ns1和ns2)后会在/var/run/netns目录下看到ns1和ns2两项

[ ~]# 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中

[ ~]# 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中

[ ~]# 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]
[ ~]#

在ns2中

[ ~]# 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命令进行操作了

[ ~]# docker inspect --format ‘{{ .State.Pid }}‘ test1
14450

若不存在目录/var/run/netns,则创建

[ ~]# mkdir /var/run/netns/

在/var/run/netns/下创建软链接,指向test1容器的network namespace

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

测试是否成功

[ ~]# ip netns list
test1 (id: 1)
ns2
ns1 (id: 0)
[ ~]# 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: : <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。

相关推荐