当前位置:   article > 正文

docker — 容器网络_docker 查看容器网络

docker 查看容器网络

目录

一、容器网络

1、Docker Native Network drivers

1. Docker 提供如下 5 种原生的 Network drivers

2. Docker 安装时,自动在host上创建了如下3个网络

2、none 网络

3、host 网络

4、bridge 网络

1. docker0 网络

2. 在宿主机上查看 docker0

3. 查看docker0 网络配置

4. 在后台运行一个名为httpd1 的 httpd 容器

5. 查看该容器的网络配置

5、user-defined Bridge 网络

1. 用户可按需创建 bridge 网桥,称为 user-defined Bridge

2. 创建一个 user-defined Bridge,命名为net1

3. 查看 net1 网桥信息,已自动配置 subnet 和 gateway

4. 创建第二个网桥,指定IP网段,命名为 net2

5. 启动3个 centos 容器,分别命名为c1、c2、c3

 6. 查看三个 centos 容器的 IP 地址信息

7. 进入容器c3,进行连通性测试

8. 为 c1 添加一块网卡,加入到 net2 网络

9. 进入 c1,验证连通性

10. 删除相关容器


一、容器网络

1、Docker Native Network drivers

1. Docker 提供如下 5 种原生的 Network drivers

模型说明
bridge

默认 网络驱动程序。主要用于多个容器在同一个Docker宿主机上进行通信

(当创建新容器时,默认就是bridge)     

host

容器加入到宿主机的Network namespace,容器直接使用宿主机网络

(注意端口不能冲突)                    网卡数和物理机网卡数量相同

nonenone 网络中的容器,不能与外部通信(只有一块lo网卡)      只有一块网卡
OverlayOverlay 网络基于 Linux 网桥和 Vxlan,实现跨主机的容器通信
MacvlanMacvlan 用于跨主机通信场景

2. Docker 安装时,自动在host上创建了如下3个网络

2、none 网络

1. none 网络的 driver 类型是 null,IPAM字段为空

    挂载 none 网络上的容器只有 lo,无法与外界通信

  1. # docker inspect none
  2. [
  3. {
  4. "Name": "none",
  5. "Id": "8a84fded05e5362b29b80ea97f793528b04c85d78f61de261fa63b34f574d6b6",
  6. "Created": "2022-08-21T08:05:09.923418335Z",
  7. "Scope": "local",
  8. "Driver": "null", #驱动类型
  9. "EnableIPv6": false,
  10. "IPAM": {
  11. "Driver": "default",
  12. "Options": null,
  13. "Config": [] #字段为空
  14. },
  15. ...输出省略

2. 测试

  1. # docker run -itd --network none centos
  2. # docker ps #查看容器是否运行
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. b355f02ca0d6 centos "/bin/bash" 19 seconds ago Up 15 seconds bold_williamson
  5. # docker exec -it b355 /bin/bash
  6. [root@b355f02ca0d6 /]# ip a
  7. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  8. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  9. inet 127.0.0.1/8 scope host lo
  10. valid_lft forever preferred_lft forever

3、host 网络

1. 挂在 host 网络上的容器共享宿主机的 network namespace

    即容器的网络配置与 host 网络配置完全一样

  1. # docker run -itd --network host --name h1 centos #以host网络运行容器
  2. # docker run -itd --network host --name h2 centos
  3. # docker ps #查看容器是否运行
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 8a1164f599a4 centos "/bin/bash" 5 minutes ago Up 5 minutes h2
  6. 158ba0eb2438 centos "/bin/bash" 5 minutes ago Up 5 minutes h1
  7. ~# ip a #查看当前网络
  8. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  9. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  10. inet 127.0.0.1/8 scope host lo
  11. valid_lft forever preferred_lft forever
  12. inet6 ::1/128 scope host
  13. valid_lft forever preferred_lft forever
  14. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  15. link/ether 00:0c:29:e3:d1:38 brd ff:ff:ff:ff:ff:ff
  16. altname enp2s1
  17. inet 192.168.147.102/24 brd 192.168.147.255 scope global ens33
  18. valid_lft forever preferred_lft forever
  19. inet6 fe80::20c:29ff:fee3:d138/64 scope link
  20. valid_lft forever preferred_lft forever
  21. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  22. link/ether 02:42:58:c8:a7:47 brd ff:ff:ff:ff:ff:ff
  23. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  24. valid_lft forever preferred_lft forever
  1. k8s-master:~# docker exec -it h1 /bin/bash #发现网卡相关信息与宿主机一致
  2. [root@k8s-master /]# ip a s
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  10. link/ether 00:0c:29:e3:d1:38 brd ff:ff:ff:ff:ff:ff
  11. altname enp2s1
  12. inet 192.168.147.102/24 brd 192.168.147.255 scope global ens33
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::20c:29ff:fee3:d138/64 scope link
  15. valid_lft forever preferred_lft forever
  16. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  17. link/ether 02:42:58:c8:a7:47 brd ff:ff:ff:ff:ff:ff
  18. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  19. valid_lft forever preferred_lft forever
  20. '容器h2与h1一致'

4、bridge 网络

1. docker0 网络

        容器创建时,默认挂载在 docker0 上

        ② docker0 是一个 linux bridge

        ③ docker0 网络创建时已默认配置了 Subnet

2. 在宿主机上查看 docker0

  1. # ifconfig docker0
  2. docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  3. inet '172.17.0.1' netmask 255.255.0.0 broadcast 172.17.255.255
  4. ether 02:42:58:c8:a7:47 txqueuelen 0 (Ethernet)
  5. RX packets 0 bytes 0 (0.0 B)
  6. RX errors 0 dropped 0 overruns 0 frame 0
  7. TX packets 0 bytes 0 (0.0 B)
  8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3. 查看docker0 网络配置

  1. # docker network inspect bridge
  2. [
  3. {
  4. "Name": "bridge",
  5. "Id": "7a84c0c796f04413629020321d2adc0d35fbfdef419d5b9cee78998b0c494274",
  6. 'bridge-ID与容器network-ID对应'
  7. "Created": "2022-10-26T00:43:27.914929427Z",
  8. "Scope": "local",
  9. "Driver": "bridge",
  10. "EnableIPv6": false,
  11. "IPAM": {
  12. "Driver": "default",
  13. "Options": null,
  14. "Config": [
  15. {
  16. "Subnet": "172.17.0.0/16",
  17. "Gateway": "172.17.0.1"
  18. }
  19. ...输出省略

4. 在后台运行一个名为httpd1 的 httpd 容器

# docker run -itd --name httpd1 httpd

5. 查看该容器的网络配置

确认"NetworkID"和docker0的ID相同,"IPAddress"同网段

  1. # docker inspect httpd1
  2. ...输出省略
  3. "Networks": {
  4. "bridge": {
  5. "IPAMConfig": null,
  6. "Links": null,
  7. "Aliases": null,
  8. "NetworkID": "7a84c0c796f04413629020321d2adc0d35fbfdef419d5b9cee78998b0c494274",
  9. #networkID对应brigde-ID
  10. "EndpointID": "94c254fa5a2cb2da2c475ee4a24f5f00a8a6975d7c8cdb338e0fc0226389a7b9",
  11. "Gateway": "172.17.0.1",
  12. "IPAddress": "172.17.0.2", #与网桥在同一网段
  13. "IPPrefixLen": 16,
  14. "IPv6Gateway": "",
  15. "GlobalIPv6Address": "",
  16. "GlobalIPv6PrefixLen": 0,
  17. "MacAddress": "02:42:ac:11:00:02",
  18. "DriverOpts": null
  19. ...输出省略

5、user-defined Bridge 网络

1. 用户可按需创建 bridge 网桥,称为 user-defined Bridge

根据实际情况可以创建多个bridge

2. 创建一个 user-defined Bridge,命名为net1

  1. # docker network create --driver bridge net1 #通过bridge桥接设备驱动创建net1
  2. 324c90cd97719e363e2e2c2ce0508f8a2d964bc41898ebcaf8bc827db3627fa8

3. 查看 net1 网桥信息,已自动配置 subnet 和 gateway

  1. root@k8s-master:~# docker network inspect net1
  2. ...输出省略
  3. "Name": "net1",
  4. "Id": "324c90cd97719e363e2e2c2ce0508f8a2d964bc41898ebcaf8bc827db3627fa8",
  5. "Created": "2022-10-26T08:10:28.784844522Z",
  6. "Scope": "local",
  7. "Driver": "bridge",
  8. "EnableIPv6": false,
  9. "IPAM": {
  10. "Driver": "default",
  11. "Options": {},
  12. "Config": [
  13. {
  14. "Subnet": "172.18.0.0/16",
  15. "Gateway": "172.18.0.1"
  16. ...输出省略

4. 创建第二个网桥,指定IP网段,命名为 net2

  1. # docker network create --driver bridge --subnet 172.10.10.0/24 --gateway 172.10.10.1 net2
  2. 6eecbf049df44ecbd7aad0978115aac0cad4942831fa76af2e34f99f8d09fcd5

5. 启动3个 centos 容器,分别命名为c1、c2、c3

其中c1加入net1c2加入 net2c3 加入 net2 并配置静态IP

  1. # docker run -itd --name c1 --network net1 centos
  2. # docker run -itd --name c2 --network net2 centos
  3. # docker run -itd --name c3 --network net2 --ip 172.10.10.10 centos

 6. 查看三个 centos 容器的 IP 地址信息

  1. # docker inspect c1 c2 c3 | grep -A 12 Networks | grep "IPAddress" #依次对应c1、c2、c3
  2. "IPAddress": "172.18.0.2",
  3. "IPAddress": "172.10.10.2",
  4. "IPAddress": "172.10.10.10",

7. 进入容器c3,进行连通性测试

结论:c3 与 c2 可以通信但c1不能通信(c2和c3在一个网桥)

  1. ~# docker exec -it c2 ping -c 3 172.10.10.10 #c2和c3可通信
  2. PING 172.10.10.10 (172.10.10.10) 56(84) bytes of data.
  3. 64 bytes from 172.10.10.10: icmp_seq=1 ttl=64 time=0.151 ms
  4. 64 bytes from 172.10.10.10: icmp_seq=2 ttl=64 time=0.050 ms
  5. 64 bytes from 172.10.10.10: icmp_seq=3 ttl=64 time=0.071 ms
  6. --- 172.10.10.10 ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss, time 2033ms
  8. rtt min/avg/max/mdev = 0.050/0.090/0.151/0.044 ms
  9. # docker exec -it c2 ping -c 3 172.18.0.2 ##c2和c1无法通信
  10. PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
  11. --- 172.18.0.2 ping statistics ---
  12. 3 packets transmitted, 0 received, 100% packet loss, time 2035ms

8. 为 c1 添加一块网卡,加入到 net2 网络

  1. ~# docker network connect net2 c1
  2. '查看容器内网卡'
  3. docker inspect c1 | grep -wA `docker inspect c1|wc -l` "Networks" | sed -n '/".*{$/ s/": {//p'|sed -n '2,3s/.*"//p'
  4. net1
  5. net2

9. 进入 c1,验证连通性

  1. # docker exec -it c1 ping -c 3 172.10.10.1
  2. PING 172.10.10.1 (172.10.10.1) 56(84) bytes of data.
  3. 64 bytes from 172.10.10.1: icmp_seq=1 ttl=64 time=0.170 ms
  4. 64 bytes from 172.10.10.1: icmp_seq=2 ttl=64 time=0.073 ms
  5. 64 bytes from 172.10.10.1: icmp_seq=3 ttl=64 time=0.042 ms
  6. --- 172.10.10.1 ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss, time 2030ms
  8. rtt min/avg/max/mdev = 0.042/0.095/0.170/0.054 ms

10. 删除相关容器

  1. '注意停止和删除顺序,不然无法删除'
  2. # docker stop c1 c2 c3 h1 h2 #停止容器
  3. # docker rm c1 c2 c3 h1 h2 #删除容器
  4. # docker network rm net1 net2 #删除bridge
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/500861
推荐阅读
相关标签
  

闽ICP备14008679号