一,為什么要關閉swap?
1,swap的用途?
swap 分區就是交換分區,(windows平台叫虛擬內存)
在物理內存不夠用時,
操作系統會從物理內存中把部分暫時不被使用的數據轉移到交換分區,
從而為當前運行的程序留出足夠的物理內存空間
2,為什么要關閉swap?
swap啟用后,在使用磁盤空間和內存交換數據時,
性能表現會較差,會減慢程序執行的速度
有的軟件的設計師不想使用交換,
例如:kubelet 在 1.8 版本以后強制要求 swap 必須關閉
否則會報錯:
Running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false
或者kubeadm init時會報錯:
[ERROR Swap]: running with swap on is not supported. Please disable swap
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看swapon/swapoff所屬的rpm包
[root@centos8 ~]# whereis swapoff swapoff: /usr/sbin/swapoff /usr/share/man/man8/swapoff.8.gz /usr/share/man/man2/swapoff.2.gz [root@centos8 ~]# whereis swapon swapon: /usr/sbin/swapon /usr/share/man/man8/swapon.8.gz /usr/share/man/man2/swapon.2.gz [root@centos8 ~]# rpm -qf /usr/sbin/swapon util-linux-2.32.1-17.el8.x86_64 [root@centos8 ~]# rpm -qf /usr/sbin/swapoff util-linux-2.32.1-17.el8.x86_64
如果誤刪除了命令可以用dnf安裝
[root@centos8 ~]# dnf install util-linux
三,查看swap當前的狀態:
查看swap的空間,共4G
[root@centos8 ~]# free -m total used free shared buff/cache available Mem: 3757 1224 1080 20 1452 2270 Swap: 4047 0 4047
用swapon查看所屬設備和大小:
#-v: --verbose 詳盡模式
[root@centos8 ~]# swapon -v NAME TYPE SIZE USED PRIO /dev/dm-1 partition 4G 0B -2
四,臨時關閉swap
1,查看swapoff的版本
[root@centos8 ~]# swapoff --version swapoff,來自 util-linux 2.32.1
2,關閉swap
#-a:all,禁用 /proc/swaps 中的所有交換區
[root@centos8 ~]# swapoff -a
3,檢查效果 :
[root@centos8 ~]# free -m total used free shared buff/cache available Mem: 3757 1221 1083 20 1451 2273 Swap: 0 0 0
swap的大小為0,已關閉
4,用swapon檢查:
[root@centos8 ~]# swapon -v
輸出為空,表示swap已關閉
五,臨時關閉swap后啟用swap
1,查看swapon的版本
[root@centos8 ~]# swapon --version swapon,來自 util-linux 2.32.1
2,啟用 swap
#-a:--all 啟用 /etc/fstab 中的所有交換區
[root@centos8 ~]# swapon -a
[root@centos8 ~]# free -m total used free shared buff/cache available Mem: 3757 1225 1079 20 1452 2269 Swap: 4047 0 4047
可以看到swap的空間顯示為4G
用swapon -v查看
[root@centos8 ~]# swapon -v NAME TYPE SIZE USED PRIO /dev/dm-1 partition 4G 0B -2
六,永久關閉swap
查看/etc/fstab
[root@centos8 ~]# more /etc/fstab
找到swap分區的記錄:
... /dev/mapper/cl-root / xfs defaults 0 0 UUID=f384615e-7c71-43b0-876c-45e8f08cfa6e /boot ext4 defaults 1 2 /dev/mapper/cl-home /home xfs defaults 0 0 /dev/mapper/cl-swap swap swap defaults 0 0
把加載swap分區的那行記錄注釋掉即可
#/dev/mapper/cl-swap swap swap defaults 0 0
重啟機器
[root@centos8 ~]# systemctl reboot
再用free -m檢查一下
[liuhongdi@centos8 ~]$ free -m total used free shared buff/cache available Mem: 3757 1127 1906 17 722 2370 Swap: 0 0 0
七,查看linux的版本:
[liuhongdi@centos8 ~]$ cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)