案例分析Centos7内核升级
1. 规划节点
节点规划,见表1。
IP | 主机名 | 节点 |
---|---|---|
10.24.2.5 | k8s-master-node1 | master节点、仓库节点 |
表1 节点规划
2. 基础准备
(1)删除官方yum源
[root@k8s-worker-node1 ~]# rm -rf /etc/yum.repos.d/*
(2)下载centos7阿里云源
[root@k8s-worker-node1 ~]# curl -o /etc/yum.repos.d/centos.repo https://mirrors.aliyun.com/repo/Centos-7.repo
3. 在线升级内核
(1)检查当前内核版本
[root@k8s-master-node1 ~]# uname -rs
Linux 3.10.0-1160.el7.x86_64
查看已安装的RPM包
[root@k8s-master-node1 ~]# rpm -qa | grep kernel
kernel-tools-3.10.0-1160.el7.x86_64
kernel-3.10.0-1160.el7.x86_64
kernel-headers-3.10.0-1160.92.1.el7.x86_64
kernel-tools-libs-3.10.0-1160.el7.x86_64
(2)添加阿里云源
cat > /etc/yum.repos.d/elrepo.repo << EOF
[elrepo]
name=elrepo
baseurl=https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64
gpgcheck=0
enabled=1
EOF
快速建立缓存
[root@k8s-master-node1 ~]# yum clean all && yum makecache fast
查看内核版本信息
[root@k8s-master-node1 ~]# yum list kernel* --showduplicate | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
kernel.x86_64 3.10.0-1160.el7 updates
...
(3)安装内核
[root@k8s-master-node1 ~]# yum remove -y kernel-tools* kernel-headers*
[root@k8s-master-node1 ~]# yum install -y kernel-lt \
kernel-lt-devel \
kernel-lt-doc \
kernel-lt-headers \
kernel-lt-tools \
kernel-lt-tools-libs \
kernel-lt-tools-libs-devel
查看已安装的RPM包
[root@k8s-master-node1 ~]# rpm -qa | grep kernel
kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64
kernel-lt-headers-5.4.278-1.el7.elrepo.x86_64
kernel-3.10.0-1160.el7.x86_64
kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64
kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64
kernel-lt-tools-libs-devel-5.4.278-1.el7.elrepo.x86_64
kernel-lt-5.4.278-1.el7.elrepo.x86_64
kernel-lt-doc-5.4.278-1.el7.elrepo.noarch
查看启动内核
[root@k8s-master-node1 ~]# cat /etc/grub2.cfg | grep menuentry|awk -F "\'" '$1=="menuentry " {print i++ " : " $2}'
awk: warning: escape sequence `\'' treated as plain `''
0 : CentOS Linux (5.4.278-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-06c467b705a148d5a4378c72b1379223) 7 (Core)
设置启动内核版本
[root@k8s-master-node1 ~]# grub2-set-default 0
检查默认内核
[root@k8s-master-node1 ~]# grubby --default-kernel
/boot/vmlinuz-5.4.278-1.el7.elrepo.x86_64
重启查看
[root@k8s-master-node1 ~]# reboot
开机界面可以看到3.10内核版本还是存在
命令行查看
[root@k8s-master-node1 ~]# uname -rs
Linux 5.4.278-1.el7.elrepo.x86_64
删除3.10内核版本(可选)
[root@k8s-master-node1 ~]# yum remove -y kernel-3.10.0-1160*
查看已安装的RPM包
[root@k8s-master-node1 ~]# rpm -qa | grep kernel
kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64
kernel-lt-headers-5.4.278-1.el7.elrepo.x86_64
kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64
kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64
kernel-lt-tools-libs-devel-5.4.278-1.el7.elrepo.x86_64
kernel-lt-5.4.278-1.el7.elrepo.x86_64
kernel-lt-doc-5.4.278-1.el7.elrepo.noarch
查看grub2
[root@k8s-master-node1 ~]# sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.4.278-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (0-rescue-06c467b705a148d5a4378c72b1379223) 7 (Core)
重新安装依赖
[root@k8s-master-node1 ~]# yum install -y gcc gcc-c++ glibc-devel glibc-headers
3. 离线升级内核
(1)从官网下载内核rpm包
官方镜像站:https://elrepo.org/linux/kernel/el7/x86_64/RPMS/
阿里云镜像站:https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64/RPMS/?spm=a2c6h.25603864.0.0.532979e8bvvK1A
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-5.4.278-1.el7.elrepo.x86_64.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-doc-5.4.278-1.el7.elrepo.noarch.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-headers-5.4.278-1.el7.elrepo.x86_64.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64.rpm
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-tools-libs-devel-5.4.278-1.el7.elrepo.x86_64.rpm
(2)上传rpm包并安装
rpm -ivh kernel-lt-5.4.278-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-doc-5.4.278-1.el7.elrepo.noarch.rpm
rpm -ivh kernel-lt-headers-5.4.278-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-tools-5.4.278-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-tools-libs-5.4.278-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-lt-tools-libs-devel-5.4.278-1.el7.elrepo.x86_64.rpm
查看内核是否载入到grub2
#/etc/grub2.cfg文件是/boot/grub2/grub.cfg文件的软链接,grub.cfg文件是grub引导加载器的配置文件
#注意:如果服务器使用efi模式启动,那么可能没有/boot/grub2/grub.cfg文件,其对应的配置文件可能是/boot/efi/EFI/centos/grub.cfg
[root@k8s-master-node1 ~]# sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.4.257-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.92.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-bfd5b36eb1f547e18ff398ed50be7142) 7 (Core)
通过 grub2-set-default 0 命令设置或编辑 vim /etc/default/grub 文件来设置
[root@k8s-master-node1 ~]# grub2-set-default 0
[root@k8s-master-node1 ~]# vim /etc/default/grub
GRUB_DEFAULT=0 #这句改为GRUB_DEFAULT=0 意思是GRUB初始化页面的第一个内核将作为默认内核,保存退出
(3)检查当前内核版本
[root@k8s-master-node1 ~]# grubby --default-kernel
/boot/vmlinuz-5.4.278-1.el7.elrepo.x86_64
重启查看
[root@k8s-master-node1 ~]# reboot
命令行查看
[root@k8s-master-node1 ~]# uname -rs
Linux 5.4.278-1.el7.elrepo.x86_64
安装完成之后,会在/usr/src/kernels目录生成内核的源码包
[root@k8s-master-node1 ~]# ll /usr/src/kernels/5.4.278-1.el7.elrepo.x86_64/
total 5516
drwxr-xr-x 27 root root 4096 Oct 12 23:10 arch
drwxr-xr-x 3 root root 78 Oct 12 23:10 block
drwxr-xr-x 2 root root 37 Oct 12 23:10 certs
drwxr-xr-x 4 root root 76 Oct 12 23:10 crypto
drwxr-xr-x 137 root root 4096 Oct 12 23:10 drivers
drwxr-xr-x 76 root root 4096 Oct 12 23:10 fs
drwxr-xr-x 29 root root 4096 Oct 12 23:10 include
drwxr-xr-x 2 root root 37 Oct 12 23:10 init
drwxr-xr-x 2 root root 22 Oct 12 23:10 ipc
-rw-r--r-- 1 root root 595 Jun 17 03:57 Kconfig
drwxr-xr-x 17 root root 301 Oct 12 23:10 kernel
drwxr-xr-x 18 root root 4096 Oct 12 23:10 lib
-rw-r--r-- 1 root root 62436 Jun 17 03:57 Makefile
drwxr-xr-x 3 root root 71 Oct 12 23:10 mm
-rw-r--r-- 1 root root 1425710 Jun 17 03:57 Module.symvers
drwxr-xr-x 69 root root 4096 Oct 12 23:10 net
drwxr-xr-x 28 root root 4096 Oct 12 23:10 samples
drwxr-xr-x 15 root root 8192 Oct 12 23:10 scripts
drwxr-xr-x 12 root root 209 Oct 12 23:10 security
drwxr-xr-x 26 root root 324 Oct 12 23:10 sound
-rw-r--r-- 1 root root 4102769 Jun 17 03:57 System.map
drwxr-xr-x 30 root root 4096 Oct 12 23:10 tools
drwxr-xr-x 3 root root 52 Oct 12 23:10 usr
drwxr-xr-x 4 root root 44 Oct 12 23:10 virt