關(guān)于 K8s 集群 CentOS Linux 7 節(jié)點(diǎn)批量 Kernel 升級(jí)的一些筆記

寫在前面
k8s 集群安裝一個(gè)觀測(cè)工具檢查發(fā)現(xiàn)內(nèi)核版本太低不支持,所有決定升級(jí)
操作環(huán)境為實(shí)驗(yàn)環(huán)境,所以沒什么顧慮
如果生產(chǎn)環(huán)境升級(jí),需要做錯(cuò)誤預(yù)算哈,最好用 Velero 備份,做好集群遷移的準(zhǔn)備
高內(nèi)核版本支持 cgroup2,如果新集群部署需要考慮下這塊。
理解不足小伙伴幫忙指正
對(duì)每個(gè)人而言,真正的職責(zé)只有一個(gè):找到自我。然后在心中堅(jiān)守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是對(duì)大眾理想的懦弱回歸,是隨波逐流,是對(duì)內(nèi)心的恐懼 ——赫爾曼·黑塞《德米安》

本地的 k8s 集群,CentOS Linux 7 (Core) 的系統(tǒng)

┌──[root@vms100.liruilongs.github.io]-[~]
└─$kubectl get nodes
NAME                          STATUS     ROLES           AGE    VERSION
vms100.liruilongs.github.io   Ready      control-plane   6d4h   v1.25.1
vms101.liruilongs.github.io   Ready      control-plane   6d4h   v1.25.1
vms102.liruilongs.github.io   Ready      control-plane   6d4h   v1.25.1
vms103.liruilongs.github.io   Ready      <none>          6d4h   v1.25.1
vms105.liruilongs.github.io   Ready      <none>          6d4h   v1.25.1
vms106.liruilongs.github.io   Ready      <none>          6d4h   v1.25.1
vms107.liruilongs.github.io   Ready      <none>          6d4h   v1.25.1
vms108.liruilongs.github.io   Ready      <none>          6d4h   v1.25.1
┌──[root@vms100.liruilongs.github.io]-[~]
└─$
內(nèi)核版本 Linux 3.10.0-693.el7.x86_64

┌──[root@vms100.liruilongs.github.io]-[~/ansible/pixie]
└─$hostnamectl
   Static hostname: vms100.liruilongs.github.io
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e93ae3f6cb354f3ba509eeb73568087e
           Boot ID: 5ed408a863df48ae80b51f1b6c4be85f
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.el7.x86_64
      Architecture: x86-64
┌──[root@vms100.liruilongs.github.io]-[~/ansible/pixie]
└─$
在安裝一個(gè)觀測(cè)工具時(shí),提示內(nèi)核版本太低

┌──[root@vms100.liruilongs.github.io]-[~/ansible/pixie]
└─$px deploy --check_only
Pixie CLI

Running Cluster Checks:
 ?    Kernel version > 4.14.0  ERR: kernel version for node (vms100.liruilongs.github.io) not supported ?    Kernel version > 4.14.0  ERR: kernel version for node (vms100.liruilongs.github.io) not supported. Must have minimum kernel version of (4.14.0)
Check pre-check has failed. To bypass pass in --check=false. error=kernel version for node (vms100.liruilongs.github.io) not supported. Must have minimum kernel version of (4.14.0)
決定升級(jí)內(nèi)核,

這里升級(jí)方案,先升級(jí)一臺(tái)機(jī)器,確認(rèn)沒有問題,對(duì)集群做簡(jiǎn)單測(cè)試,半小時(shí)后,如果集群運(yùn)行正常,然后通過 Ansible 批量升級(jí)其他的節(jié)點(diǎn)。

248323bk-1.png

Linux  官方內(nèi)核  需要從 https://www.kernel.org/ 下載并編譯安裝

大多數(shù) Linux 發(fā)行版提供自行維護(hù)的內(nèi)核,可以通過 yum 、df或 rpm 等包管理系統(tǒng)升級(jí)。

ELRepo 是一個(gè)為L(zhǎng)inux提供驅(qū)動(dòng)程序和內(nèi)核鏡像的存儲(chǔ)庫(kù),一個(gè)用于企業(yè) Linux 軟件包的 RPM 存儲(chǔ)庫(kù)。ELRepo 支持 Red Hat Enterprise Linux (RHEL) 及其重建項(xiàng)目.

ELRepo 項(xiàng)目專注于硬件相關(guān)的軟件包,以增強(qiáng)您使用 Enterprise Linux 的體驗(yàn)。這包括文件系統(tǒng)驅(qū)動(dòng)程序、圖形驅(qū)動(dòng)程序、網(wǎng)絡(luò)驅(qū)動(dòng)程序、聲音驅(qū)動(dòng)程序、網(wǎng)絡(luò)攝像頭和視頻驅(qū)動(dòng)程序。

ELRepo官網(wǎng):http://elrepo.org/tiki/tiki-index.php

#查看 yum 中可升級(jí)的內(nèi)核版本
yum list kernel --showduplicates
#如果list中有需要的版本可以直接執(zhí)行 update 升級(jí),多數(shù)是沒有的,所以要按以下步驟操作

#導(dǎo)入ELRepo軟件倉(cāng)庫(kù)的公共秘鑰
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

#Centos7系統(tǒng)安裝ELRepo
yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
#Centos8系統(tǒng)安裝ELRepo
yum install https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm

#查看ELRepo提供的內(nèi)核版本
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Kernel 升級(jí)
先找一臺(tái)機(jī)器單獨(dú)升級(jí)

Centos7系統(tǒng)安裝ELRepo ,  導(dǎo)入ELRepo軟件倉(cāng)庫(kù)的公共秘鑰

┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
查看ELRepo提供的內(nèi)核版本

┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
已加載插件:fastestmirror
elrepo-kernel                                                                                                                                                                             | 3.0 kB  00:00:00
elrepo-kernel/primary_db                                                                                                                                                                  | 2.1 MB  00:01:40
Loading mirror speeds from cached hostfile
 * elrepo-kernel: ftp.yz.yamagata-u.ac.jp
可安裝的軟件包
kernel-lt.x86_64                                                                                        5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-devel.x86_64                                                                                  5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-doc.noarch                                                                                    5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-headers.x86_64                                                                                5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools.x86_64                                                                                  5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools-libs.x86_64                                                                             5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-lt-tools-libs-devel.x86_64                                                                       5.4.230-1.el7.elrepo                                                                        elrepo-kernel
kernel-ml.x86_64                                                                                        6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-devel.x86_64                                                                                  6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-doc.noarch                                                                                    6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-headers.x86_64                                                                                6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools.x86_64                                                                                  6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools-libs.x86_64                                                                             6.1.8-1.el7.elrepo                                                                          elrepo-kernel
kernel-ml-tools-libs-devel.x86_64                                                                       6.1.8-1.el7.elrepo                                                                          elrepo-kernel
perf.x86_64                                                                                             5.4.230-1.el7.elrepo                                                                        elrepo-kernel
python-perf.x86_64                                                                                      5.4.230-1.el7.elrepo                                                                        elrepo-kernel
┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$
kernel-lt:表示longterm,即長(zhǎng)期支持的內(nèi)核;當(dāng)前為5.4.
kernel-ml:表示mainline,即當(dāng)前主線的內(nèi)核;當(dāng)前為5.17.






這里我們升級(jí)長(zhǎng)期支持的版本,直接升級(jí)

#長(zhǎng)期支持的內(nèi)核
┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$yum -y  --enablerepo=elrepo-kernel install kernel-lt.x86_64
查看系統(tǒng)可用內(nèi)核,并設(shè)置啟動(dòng)項(xiàng)

┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.4.230-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux 7 Rescue e93ae3f6cb354f3ba509eeb73568087e (3.10.0-1160.83.1.el7.x86_64)
2 : CentOS Linux (3.10.0-1160.83.1.el7.x86_64) 7 (Core)
3 : CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
4 : CentOS Linux (0-rescue-80c608ceab5342779ba1adc2ac29c213) 7 (Core)
指定開機(jī)啟動(dòng)內(nèi)核版本

┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$grub2-set-default 0
生成 grub 配置文件


┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.230-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.4.230-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.83.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.83.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-80c608ceab5342779ba1adc2ac29c213
Found initrd image: /boot/initramfs-0-rescue-80c608ceab5342779ba1adc2ac29c213.img
Found linux image: /boot/vmlinuz-0-rescue-e93ae3f6cb354f3ba509eeb73568087e
Found initrd image: /boot/initramfs-0-rescue-e93ae3f6cb354f3ba509eeb73568087e.img
done
重啟系統(tǒng),驗(yàn)證

┌──[root@vms100.liruilongs.github.io]-[~/back]
└─$reboot
Connection to 192.168.26.100 closed by remote host.
Connection to 192.168.26.100 closed.
....
┌──[root@vms100.liruilongs.github.io]-[~]
└─$hostnamectl
   Static hostname: vms100.liruilongs.github.io
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e93ae3f6cb354f3ba509eeb73568087e
           Boot ID: a1150b6d97dc4afbb81dae58f131a487
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
      Architecture: x86-64
┌──[root@vms100.liruilongs.github.io]-[~]
└─$
確實(shí)沒有問題之后,對(duì)集群做簡(jiǎn)單測(cè)試,等半個(gè)小時(shí),批量升級(jí)一下

編寫升級(jí)腳本

#!/bin/bash

#@File    :   update_kernel
#@Time    :   2023/02/01 23:58:23
#@Author  :   Li Ruilong
#@Version :   1.0
#@Desc    :   contos 7 批量升級(jí)內(nèi)核腳本
#@Contact :   liruilonger@gmail.com



yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

yum -y  --enablerepo=elrepo-kernel install kernel-lt.x86_64

grub2-set-default 0

grub2-mkconfig -o /boot/grub2/grub.cfg

reboot
拷貝腳本到升級(jí)節(jié)點(diǎn)機(jī)器

┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$ansible ansible_node -m copy -a "src=./update_kernel/update_kernel.sh dest=/tmp/" -i host.yaml
┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$ansible ansible_node -m shell -a "cat /tmp/update_kernel.sh" -i host.yaml
運(yùn)行升級(jí)腳本

┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$ansible ansible_node -m shell -a "/usr/bin/bash /tmp/update_kernel.sh" -i host.yaml  -f 7 -vvv
升級(jí)完成查看內(nèi)核版本確認(rèn)

┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$ansible ansible_node  -m shell -a 'hostnamectl | grep Kernel'  -i host.yaml
192.168.26.106 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.105 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.102 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.103 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.101 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.107 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
192.168.26.108 | CHANGED | rc=0 >>
            Kernel: Linux 5.4.230-1.el7.elrepo.x86_64
查看集群信息確認(rèn)

┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$kubectl get nodes
NAME                                    STATUS      ROLES           AGE    VERSION
vms100.liruilongs.github.io   Ready    control-plane   6d5h   v1.25.1
vms101.liruilongs.github.io   Ready    control-plane   6d5h   v1.25.1
vms102.liruilongs.github.io   Ready    control-plane   6d5h   v1.25.1
vms103.liruilongs.github.io   Ready    <none>           6d4h   v1.25.1
vms105.liruilongs.github.io   Ready    <none>           6d4h   v1.25.1
vms106.liruilongs.github.io   Ready    <none>           6d4h   v1.25.1
vms107.liruilongs.github.io   Ready    <none>           6d4h   v1.25.1
vms108.liruilongs.github.io   Ready    <none>           6d4h   v1.25.1
┌──[root@vms100.liruilongs.github.io]-[~/ansible]
└─$
運(yùn)行原來(lái)的工具測(cè)試

┌──[root@vms100.liruilongs.github.io]-[~/ansible/pixie]
└─$px deploy --check_only
Pixie CLI

Running Cluster Checks:
 ?    Kernel version > 4.14.0
 ?    Cluster type is supported
 ?    K8s version > 1.16.0
 ?    Kubectl > 1.10.0 is present
 ?    User can create namespace
INFO[0002] All Required Checks Passed!
┌──[root@vms100.liruilongs.github.io]-[~/ansible/pixie]
└─$



作者:山河已無(wú)恙


歡迎關(guān)注微信公眾號(hào) :山河已無(wú)恙