Docker随手记4 - CentOS7 Docker容器无法ping通宿主机ip问题解决记录

1,876 阅读1分钟

Docker服务部署启动容器发现docker容器内访问宿主机IP不通,于是进入容器内ping宿主机IP,发现无法ping通,容器IP为172.17.0.2,于是继续ping172.17.0.1也不通,ping docker0也不通,进过网上查询相关资料,有其他大佬也遇到这个坑,这里记录一下。

问题

环境

操作系统:centos 7.9 内核版本:3.10.0-1127.19.1.el7.x86_64 Docker版本:19.03

现象

Docker ping容器内host网络没有问题,但是访问ip不同,访问docker0网卡不通

原因

docker 加载内核的bridge.ko 驱动异常,导致docker0 网卡无法转发数据包,也就是系统内核的网桥模块bridge.ko 加载失败导致

解决办法

升级操作系统内核,重新安装docker

实施

centos7 升级操作系统内核

1、查看操作系统内核版本

$ uname -r
3.10.0-1127.19.1.el7.x86_64

$ uname -a 
Linux VM-0-16-centos 3.10.0-1127.19.1.el7.x86_64 #1 SMP Tue Aug 25 17:23:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 7.9.2009 (Core)
Release:	7.9.2009
Codename:	Core 

2、内核升级(离线)

下载镜像

centos官方镜像地址:elrepo.org/linux/kerne… ,选择合适版本下载。lt是长期支持版本,ml是最新发布的稳定版本,我这里选择的是lt版本

  • kernel-lt-5.4.92-1.el7.elrepo.x86_64.rpm
  • kernel-lt-devel-5.4.92-1.el7.elrepo.x86_64.rpm

安装镜像

$ yum install -y kernel-lt-devel-5.4.92-1.el7.elrepo.x86_64.rpm
$ yum install -y kernel-lt-5.4.92-1.el7.elrepo.x86_64.rpm

查看内核版本顺序

# 如下所示,5.4.92的位置在0
$ awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (5.4.92-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux 7 Rescue 1f37b745f7cc48a0a6f2eccdf2406089 (3.10.0-1160.11.1.el7.x86_64)
CentOS Linux (3.10.0-1160.11.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1127.19.1.el7.x86_64) 7 (Core)

修改内核启动顺序

如果想生效最新的内核,还需要修改内核的启动顺序为0: vim /etc/default/grub,找到GRUB_DEFAULT=saved,将saved修改为内核位置,此处为0,则改为GRUB_DEFAULT=0

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=0
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="serial console"
GRUB_TERMINAL_OUTPUT="serial console"
GRUB_CMDLINE_LINUX="crashkernel=auto console=ttyS0 console=tty0 panic=5 net.ifnames=0 biosdevname=0 intel_idle.max_cstate=1 intel_pstate=disable"
GRUB_DISABLE_RECOVERY="true"
GRUB_SERIAL_COMMAND="serial --speed=9600 --unit=0 --word=8 --parity=no --stop=1"

重新生成grup配置文件

$ grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.92-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.4.92-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.11.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.11.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1127.19.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1127.19.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1f37b745f7cc48a0a6f2eccdf2406089
Found initrd image: /boot/initramfs-0-rescue-1f37b745f7cc48a0a6f2eccdf2406089.img

重启并查看内核

$ reboot
Connection closing...Socket close.
Connection closed by foreign host.

$ uname -r
5.4.92-1.el7.elrepo.x86_64

做完上述动作,卸载重新安装docker即可

参考:centos7中docker网络docker0与容器间网络不通的坑