当前位置:   article > 正文

docker容器的网络配置_3、创建network namespace对象ns0并添加ens5f0网口,依次输入如下命令: ip

3、创建network namespace对象ns0并添加ens5f0网口,依次输入如下命令: ip netns a

一.利用ip netns创建名称空间并配置ip使其通信

可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装

  1. [root@localhost ~]# rpm -qa | grep iproute //检查是否具有ip netns命令
  2. iproute-5.3.0-1.el8.x86_64

创建Network Namespace

通过命令创建一个名为ns0的命名空间

  1. [root@localhost ~]# systemctl stop docker //停止容器服务
  2. Warning: Stopping docker.service, but it can still be activated by:
  3. docker.socket
  4. [root@localhost ~]# ip netns add ns0
  5. [root@localhost ~]# ip netns list
  6. ns0

新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file "/var/run/netns/ns0": File exists的错误

  1. root@localhost ~]# ls /var/run/netns/
  2. ns0
  3. [root@localhost ~]# ip netns add ns0 //不能重复创建命名空间
  4. Cannot create namespace file "/var/run/netns/ns0": File exists

对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源

操作Network Namespace

  1. [root@localhost ~]# ip netns exec ns0 ping 127.0.0.1
  2. connect: 网络不可达
  3. [root@localhost ~]# ip netns exec ns0 ip link set lo up
  4. [root@localhost ~]# ip netns exec ns0 ip a
  5. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  6. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  7. inet 127.0.0.1/8 scope host lo
  8. valid_lft forever preferred_lft forever
  9. inet6 ::1/128 scope host
  10. valid_lft forever preferred_lft forever
  11. [root@localhost ~]# ip netns exec ns0 ping 127.0.0.1
  12. PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
  13. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.026 ms
  14. 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.037 ms
  15. 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.046 ms
  16. ^C
  17. --- 127.0.0.1 ping statistics ---
  18. 3 packets transmitted, 3 received, 0% packet loss, time 60ms
  19. rtt min/avg/max/mdev = 0.026/0.036/0.046/0.009 ms

转移设备

我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。

其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的

veth pair

veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。
引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来

创建veth pair

  1. [root@localhost ~]# ip link add type veth
  2. [root@localhost ~]# ip a
  3. 4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
  4. link/ether 5e:b9:53:d0:dd:50 brd ff:ff:ff:ff:ff:ff
  5. 5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
  6. link/ether 9a:3b:1d:3f:ed:a9 brd ff:ff:ff:ff:ff:ff

可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态

实现Network Namespace间通信

下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为ns0的 Network Namespace,下面再创建一个信息Network Namespace,命名为ns1

  1. [root@localhost ~]# ip netns add ns1
  2. [root@localhost ~]# ip netns list
  3. ns0
  4. ns1

然后我们将veth0加入到ns0,将veth1加入到ns1

  1. [root@localhost ~]# ip link set veth0 netns ns0
  2. [root@localhost ~]# ip link set veth1 netns ns1

然后我们分别为这对veth pair配置上ip地址,并启用它们

  1. [root@localhost ~]# ip netns exec ns0 ip link set veth0 up
  2. [root@localhost ~]# ip netns exec ns0 ip addr add 1.1.1.1/24 dev veth0
  3. [root@localhost ~]# ip netns exec ns1 ip link set lo up
  4. [root@localhost ~]# ip netns exec ns1 ip link set veth1 up
  5. [root@localhost ~]# ip netns exec ns1 ip addr add 1.1.1.10/24 dev veth1

查看这对veth pair的状态

  1. [root@localhost ~]# ip netns exec ns0 ip a
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 4: veth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  9. link/ether 5e:b9:53:d0:dd:50 brd ff:ff:ff:ff:ff:ff link-netns ns1
  10. inet 1.1.1.1/24 scope global veth0
  11. valid_lft forever preferred_lft forever
  12. inet6 fe80::5cb9:53ff:fed0:dd50/64 scope link
  13. valid_lft forever preferred_lft forever
  14. [root@localhost ~]# ip netns exec ns1 ip a
  15. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  16. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  17. inet 127.0.0.1/8 scope host lo
  18. valid_lft forever preferred_lft forever
  19. inet6 ::1/128 scope host
  20. valid_lft forever preferred_lft forever
  21. 5: veth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  22. link/ether 9a:3b:1d:3f:ed:a9 brd ff:ff:ff:ff:ff:ff link-netns ns0
  23. inet 1.1.1.10/24 scope global veth1
  24. valid_lft forever preferred_lft forever
  25. inet6 fe80::983b:1dff:fe3f:eda9/64 scope link
  26. valid_lft forever preferred_lft forever

从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址

  1. [root@localhost ~]# ip netns exec ns1 ping 1.1.1.1
  2. PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
  3. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.044 ms
  4. 64 bytes from 1.1.1.1: icmp_seq=2 ttl=64 time=0.077 ms
  5. ^C
  6. --- 1.1.1.1 ping statistics ---
  7. 2 packets transmitted, 2 received, 0% packet loss, time 60ms
  8. rtt min/avg/max/mdev = 0.044/0.060/0.077/0.018 ms

可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互

veth设备重命名

  1. [root@localhost ~]# ip netns exec ns0 ip link set veth0 down
  2. [root@localhost ~]# ip netns exec ns0 ip link set dev veth0 name eth0
  3. [root@localhost ~]# ip netns exec ns0 ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6. inet 127.0.0.1/8 scope host lo
  7. valid_lft forever preferred_lft forever
  8. inet6 ::1/128 scope host
  9. valid_lft forever preferred_lft forever
  10. 4: eth0@if5: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
  11. link/ether 5e:b9:53:d0:dd:50 brd ff:ff:ff:ff:ff:ff link-netns ns1
  12. inet 1.1.1.1/24 scope global eth0
  13. valid_lft forever preferred_lft forever
  14. [root@localhost ~]# ip netns exec ns0 ip link set eth0 up

二.四种网络模式配置

1.bridge模式配置

  1. [root@localhost ~]# docker pull busybox
  2. Using default tag: latest
  3. latest: Pulling from library/busybox
  4. 5cc84ad355aa: Pull complete
  5. Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
  6. Status: Downloaded newer image for busybox:latest
  7. docker.io/library/busybox:latest
  8. [root@localhost ~]# docker run -it --name b1 --rm busybox
  9. / # ifconfig
  10. eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
  11. inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
  12. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  13. RX packets:11 errors:0 dropped:0 overruns:0 frame:0
  14. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  15. collisions:0 txqueuelen:0
  16. RX bytes:962 (962.0 B) TX bytes:0 (0.0 B)
  17. lo Link encap:Local Loopback
  18. inet addr:127.0.0.1 Mask:255.0.0.0
  19. UP LOOPBACK RUNNING MTU:65536 Metric:1
  20. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  21. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  22. collisions:0 txqueuelen:1000
  23. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  24. / # exit
  25. [root@localhost ~]# docker container ls -a
  26. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  27. [root@localhost ~]# docker ps -a
  28. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  29. 在创建容器时添加--network bridge与不加--network选项效果是一致的 默认就是桥接模式
  30. [root@localhost ~]# docker run -it --name c1 --network bridge --rm busybox
  31. / # ifconfig
  32. eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
  33. inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
  34. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  35. RX packets:8 errors:0 dropped:0 overruns:0 frame:0
  36. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  37. collisions:0 txqueuelen:0
  38. RX bytes:696 (696.0 B) TX bytes:0 (0.0 B)
  39. lo Link encap:Local Loopback
  40. inet addr:127.0.0.1 Mask:255.0.0.0
  41. UP LOOPBACK RUNNING MTU:65536 Metric:1
  42. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  43. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  44. collisions:0 txqueuelen:1000
  45. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

2.none模式配置

  1. [root@localhost ~]# docker run -it --network none --rm busybox
  2. / # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever

3.container模式配置

启动第一个容器

  1. [root@localhost ~]# docker run -it --name a1 --rm busybox
  2. / # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. 14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  8. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  9. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  10. valid_lft forever preferred_lft forever

启动第二个容器

  1. [root@localhost ~]# docker run -it --name a2 --rm busybox
  2. / # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. 16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  8. link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
  9. inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
  10. valid_lft forever preferred_lft forever

可以看到名为a2的容器IP地址是172.17.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为a2的容器IP与a1容器IP一致,也即共享IP,但不共享文件系统

  1. [root@localhost ~]# docker run -it --name a2 --rm --network container:a1 busybox
  2. / # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. 14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  8. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  9. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  10. valid_lft forever preferred_lft forever

此时我们在a1容器上创建一个目录

  1. / # mkdir /tmp/data
  2. / # ls /tmp/
  3. data

到a2容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。

在a2容器上部署一个站点

  1. / # echo 'hello world' > /tmp/index.html
  2. / # ls /tmp/
  3. index.html
  4. / # netstat -antl
  5. Active Internet connections (servers and established)
  6. Proto Recv-Q Send-Q Local Address Foreign Address State
  7. / # /bin/httpd -h /tmp/
  8. / # netstat -antl
  9. Active Internet connections (servers and established)
  10. Proto Recv-Q Send-Q Local Address Foreign Address State
  11. tcp 0 0 :::80 :::* LISTEN

在a1容器上用本地地址去访问此站点

  1. / # wget -O - -q 127.0.0.1:80
  2. hello world

由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程

4.host模式配置

启动容器时直接指明模式为host

  1. [root@localhost ~]# docker run -it --name a1 --rm --network host busybox
  2. / # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. inet6 ::1/128 scope host
  8. valid_lft forever preferred_lft forever
  9. 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel qlen 1000
  10. link/ether 00:0c:29:be:22:87 brd ff:ff:ff:ff:ff:ff
  11. inet 192.168.12.130/24 brd 192.168.12.255 scope global dynamic noprefixroute ens160
  12. valid_lft 1021sec preferred_lft 1021sec
  13. inet6 fe80::548d:cfb8:97e:668c/64 scope link noprefixroute
  14. valid_lft forever preferred_lft forever
  15. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
  16. link/ether 02:42:ef:a5:66:03 brd ff:ff:ff:ff:ff:ff
  17. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  18. valid_lft forever preferred_lft forever
  19. inet6 fe80::42:efff:fea5:6603/64 scope link
  20. valid_lft forever preferred_lft forever

此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了

  1. [root@localhost ~]# docker run -it --name a1 --rm --network host busybox
  2. / # mkdir test
  3. / # echo "hhhhhh" > /test/index.html
  4. / # /bin/httpd -h /test/
  5. / # netstat -antl
  6. tcp 0 0 :::80 :::* LISTEN
  7. //开启另一个终端关闭防火墙
  8. [root@localhost ~]# systemctl stop firewalld
  9. [root@localhost ~]# setenforce 0

效果图

 三.容器的常用操作

1.查看容器的主机名

  1. [root@localhost ~]# docker run -it --name a1 --network bridge --rm busybox
  2. / # hostname
  3. e516130fb6e4
  4. [root@localhost ~]# docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. e516130fb6e4 busybox "sh" 2 minutes ago Up 2 minutes

2.在容器启动时注入主机名

  1. [root@localhost ~]# docker run -it --name t1 --network bridge --hostname youkun --rm busybox
  2. / # hostname
  3. youkun
  4. / # cat /etc/hosts
  5. 127.0.0.1 localhost
  6. ::1 localhost ip6-localhost ip6-loopback
  7. fe00::0 ip6-localnet
  8. ff00::0 ip6-mcastprefix
  9. ff02::1 ip6-allnodes
  10. ff02::2 ip6-allrouters
  11. 172.17.0.2 youkun 注入主机名时会自动创建主机名到IP的映射关系
  12. 配置一个站点
  13. / # mkdir data
  14. / # echo 'yyds' > /data/index.html
  15. / # /bin/httpd -h /data/
  16. / # netstat -antl
  17. Active Internet connections (servers and established)
  18. Proto Recv-Q Send-Q Local Address Foreign Address State
  19. tcp 0 0 :::80 :::* LISTEN
  20. / # wget -O - -q youkun //因为主机名与ip有映射关系,所以主机名也可访问站点
  21. yyds

3.手动指定容器要使用的DNS

  1. [root@localhost ~]# docker run -it --name a1 --network bridge --hostname wangqing --dns 11.11.11.11 --rm busybox
  2. / # cat /etc/resolv.conf
  3. search localdomain
  4. nameserver 11.11.11.11

4.手动往/etc/hosts文件中注入主机名到IP地址的映射

  1. [root@localhost ~]# docker run -it --rm --hostname youkun2 --add-host youkun1:172.17.0.2 busybox
  2. / # cat /etc/hosts
  3. 127.0.0.1 localhost
  4. ::1 localhost ip6-localhost ip6-loopback
  5. fe00::0 ip6-localnet
  6. ff00::0 ip6-mcastprefix
  7. ff02::1 ip6-allnodes
  8. ff02::2 ip6-allrouters
  9. 172.17.0.2 youkun
  10. 172.17.0.2 youkun1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/719163
推荐阅读
相关标签
  

闽ICP备14008679号