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官方鏡像地址:https://elrepo.org/linux/kernel/el7/x86_64/RPMS/ ,選擇合適版本下載。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即可