Docker容器ping不通外网问题排查及解决

发布于:2024-11-29 ⋅ 阅读:(31) ⋅ 点赞:(0)

Docker容器ping不通外网问题排查及解决

解决方案在最下面,不看过程的可直接拉到最下面。

一台虚拟机里突然遇到docker容器一直访问外网失败,网上看到这个解决方案,这边记录一下。

首先需要明确docker的网桥模式,网桥工作在二层(OSI堆栈),是通用网络设备的一种,可以设置IP地址。有了IP地址,Linux便可通过路由表或IP表,在网络层定位网桥,这就相当于有了一个虚拟网卡,即docker0。docker0默认的地址划分:IP(127.17.42.1/16),在启动容器时,docker会在宿主机上创建一对虚拟网卡veth pair设备。veth设备总是成对出现的,它们组成了一个数据的通道,数据从一个设备进入,就会从另一个设备出来。因此,veth设备常用来连接两个网络设备,容器启动时,Docker引擎将veth pair设备的一端放在新创建的容器中,并命名为eth0。另一端放在宿主机中,以veth***这样类似的名字命名,并将这个网络设备加入到docker0网桥中,可以通过brctl show命令查看,从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。

一、首先检查宿主机的网络配置

1.检查网桥brctl sho

[root@localhost ~]# brctl show
bridge name     bridge id               STP enabled     interfaces  
docker0         8000.f229f45b3df2       no              vethaef304e  

2.检查ip

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
    inet 127.0.0.1/8 scope host lo  
       valid_lft forever preferred_lft forever  
    inet6 ::1/128 scope host   
       valid_lft forever preferred_lft forever  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UP group default qlen 1000  
    link/ether fa:16:3e:5d:75:0a brd ff:ff:ff:ff:ff:ff  
    inet 10.164.99.214/20 brd 10.164.111.255 scope global dynamic eth0  
       valid_lft 69570sec preferred_lft 69570sec  
    inet6 fe80::f816:3eff:fe5d:750a/64 scope link   
       valid_lft forever preferred_lft forever  
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000  
    link/ether fa:16:3e:45:a9:5e brd ff:ff:ff:ff:ff:ff  
    inet 10.122.76.71/24 brd 10.122.76.255 scope global dynamic eth1  
       valid_lft 65709sec preferred_lft 65709sec  
    inet6 fe80::f816:3eff:fe45:a95e/64 scope link   
       valid_lft forever preferred_lft forever  
128: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default   
    link/ether f2:29:f4:5b:3d:f2 brd ff:ff:ff:ff:ff:ff  
    inet 172.17.10.1/24 brd 172.17.10.255 scope global docker0  
       valid_lft forever preferred_lft forever  
    inet6 fe80::e03c:c3ff:fe90:1cee/64 scope link   
       valid_lft forever preferred_lft forever  
130: vethaef304e@if129: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default   
    link/ether f2:29:f4:5b:3d:f2 brd ff:ff:ff:ff:ff:ff link-netnsid 0  
    inet6 fe80::f029:f4ff:fe5b:3df2/64 scope link   
       valid_lft forever preferred_lft forever

可以看到docker创建的虚拟网卡docker0的网段为172.17.10.1/24

3.查看路由: ip route

[root@localhost ~]# ip route
10.160.146.0/23 via 10.122.72.1 dev eth1   
172.16.0.0/12 via 10.122.72.1 dev eth1   
172.17.0.0/20 via 10.122.72.1 dev eth1   
172.17.10.0/24 dev docker0 proto kernel scope link src 172.17.10.1 

即172.17.10.0/24网段的包均通过docker0网桥来转发.

二、再来查看容器的ip: docker exec -it <容器名称> ip a

[root@localhost ~]# docker exec -it c_name ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN   
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
    inet 127.0.0.1/8 scope host lo  
       valid_lft forever preferred_lft forever  
129: eth0@if130: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP   
    link/ether 02:42:ac:11:0a:02 brd ff:ff:ff:ff:ff:ff  
    inet 172.17.10.2/24 brd 172.17.10.255 scope global eth0  
       valid_lft forever preferred_lft forever  

可以看到虚拟网卡eth0的ip为172.17.10.2

1.查看容器内的路由: docker exec -it c_name ip route

[root@localhost ~]# docker exec -it c_name ip route
default via 172.17.10.1 dev eth0   
172.17.10.0/24 dev eth0 scope link  src 172.17.10.2 

由此可知172.17.10.0网段的包均走容器内的eth0,默认网关为172.17.10.1

2. 如果容器中无法ping通外网,首先查看能否ping通网关。如果网关都无法ping通,说明网桥或网段有问题。这里就可以用tcpdump来查看。比如监控上面第一步的网桥接口tcpdump -i vethaef304e -nn,正常的网络包应答如下:bash-4.4# ping -c 1 8.8.8.8

[root@localhost ~]#  tcpdump -i vethaef304e -nn  
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
listening on vethaef304e, link-type EN10MB (Ethernet), capture size 262144 bytes  
17:17:16.282976 IP 172.17.10.2 > 8.8.8.8: ICMP echo request, id 2304, seq 0, length 64  
17:17:16.325812 IP 8.8.8.8 > 172.17.10.2: ICMP echo reply, id 2304, seq 0, length 64  
17:17:21.297595 ARP, Request who-has 172.17.10.1 tell 172.17.10.2, length 28  
17:17:21.297656 ARP, Reply 172.17.10.1 is-at f2:29:f4:5b:3d:f2, length 28  

异常时就只有request who-has包,没有reply

三、问题解决

查看网桥,路由和ip均没发现问题,试试修改docker0的默认网段,居然成功了,步骤如下:

1.删除原有配置

service docker stop  
ip link set dev docker0 down  
brctl delbr docker0  
iptables -t nat -F POSTROUTING  

2.创建新的网桥

brctl addbr docker0  
ip addr add 172.17.10.1/24 dev docker0  
ip link set dev docker0 up  

3.修改docker配置,在/etc/docker/daemon.json中追加bip如下,注意json的格式,bip前面需要有,root@XXXXXX~# cat /etc/docker/daemon.json

vim /etc/docker/daemon.json
{
  "insecure-registries":["x.x.x"],
  "bip": "172.17.10.1/24"
}  

4.重启docker:
systemctl restart docker

[root@localhost ~]# systemctl  restart  docker