Use ip command docker container network

Start a docker container named test1

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

After creating two network namespace (ns1 and ns2) using the command ip netns will see in / var / run / netns directory ns1 and ns2 two

[the root @ localhost ~] # LS -la / var / RUN / netns 
total amount of 0 
drwxr -XR X-   2 the root the root   80 12 is dated 28  . 17 : 20 is . 
drwxr -XR X- 27 the root the root 820. 12 is dated 28  . 17 : 20 is ..
 -R & lt - R & lt - r--   . 1 the root the root    0 12 is dated 28  . 17 : 20 is NSl
 -R & lt - R & lt - r--   . 1 the root the root    0 12 is dated 28  . 17 : 20 is NS2

network namespace docker created in this directory does not create any entries, each process under linux belongs to a particular network namespace, look at what is the difference between different network namespace environment under / proc / $ PID / ns directory

/ Proc / self link to the currently running processes

Hosts in the default network namespace

[the root @ localhost ~] # LS -la / proc / Self / NS / 
total amount of 0 
DR the -X-- X - X 2 the root the root 0 12 is dated 28  . 17 : 51 is . 
DR -XR-X-XR . 9 the root the root 0 12 is dated 28  . 17 : 51 is .. 
lrwxrwxrwx . 1 the root the root 0 12 is dated 28  . 17 : 51 is IPC -> IPC: [ 4026531839 ] 
lrwxrwxrwx . 1 the root the root 0 12 is dated 28  . 17 : 51 is 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]

In the ns1

[the root @ localhost ~] # IP netns Exec NSl LS -la / proc / Self / NS 
total amount of 0 
DR the -X-- X - X 2 the root the root 0 12 is dated 28  . 17 : 52 is . 
DR -XR-X-XR . 9 the root the root 0 12 is dated 28  . 17 : 52 is .. 
lrwxrwxrwx . 1 the root the root 0 12 is dated 28  . 17 : 52 is IPC -> IPC: [ 4,026,531,839 ] 
lrwxrwxrwx . 1 the root the root 0 12 is dated 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 ~]# 

In the ns2

[the root @ localhost ~] # IP netns Exec NS2 LS -la / proc / Self / NS 
total amount of 0 
DR the -X-- X - X 2 the root the root 0 12 is dated 28  . 17 : 53 is . 
DR -XR-X-XR . 9 the root the root 0 12 is dated 28  . 17 : 53 is .. 
lrwxrwxrwx . 1 the root the root 0 12 is dated 28  . 17 : 53 is IPC -> IPC: [ 4,026,531,839 ] 
lrwxrwxrwx . 1 the root the root 0 12 is dated 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]

As long as the link network namespace file on behalf of the docker to create under / var / run / netns directory, you can use the command ip netns operate the

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

If there is a directory / var / run / netns, create

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

Create a soft link in / var / run / netns /, pointing test1 container network namespace

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

Whether the test was successful

[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

After completing the configuration, you can configure their own docker's network environment, and in addition to the ip netns command, there are some tools you can enter linux namespace.

Guess you like

Origin www.cnblogs.com/liujunjun/p/12112881.html