Files
pve_toss_notes/3.PVE系统调整.md
T

12 KiB
Raw Blame History

0.必要条件

在上一篇文章 2.PVE初始化配置 中,我们已经初始化了 PVE 系统,接下来需要对 PVE 系统进一步调整。

在 PVE 系统调整之前,请确保必要的软件包已经安装完成。

## 同步镜像仓库
apt update

## 安装系统软件
apt install htop lm-sensors unzip fail2ban vim tmux unattended-upgrades apt-listchanges powermgmt-base 

## 安装网络工具
apt install iperf iperf3 iftop net-tools ethtool

## 安装CPU调度调整工具
apt install cpufrequtils

本篇教程后续命令,均在 SSH 终端下完成。

1.Fail2ban 配置

安装好 fail2ban 后,检查其服务状态:

## 检查 Fail2ban 系统服务状态
systemctl status fail2ban.service

Loaded 行,检查是否存在 enable

## 参考输出

● fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-07-16 05:03:53 CST; 4 days ago
       Docs: man:fail2ban(1)
    Process: 777 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS)
   Main PID: 792 (fail2ban-server)
      Tasks: 5 (limit: 18904)
     Memory: 13.3M
        CPU: 4min 53.764s
     CGroup: /system.slice/fail2ban.service
             └─792 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
                                                                       
Jul 16 05:03:53 hyper systemd[1]: Starting Fail2Ban Service... 
Jul 16 05:03:53 hyper systemd[1]: Started Fail2Ban Service.  
Jul 16 05:03:53 hyper fail2ban-server[792]: Server ready  

其中第 1 个 enable 表示当前服务开机自动启动,第 2 个 enable 表示该软件的默认启用状态。

如果第 1 个不为 enable 状态,需要用以下命令进行调整:

## 开机自启 fail2ban 服务
systemctl enable fail2ban.service 

## 参考输出
Synchronizing state of fail2ban.service with SysV service script with /lib/systemd/systemd-sysv-install.  
Executing: /lib/systemd/systemd-sysv-install enable fail2ban 
Created symlink /etc/systemd/system/multi-user.target.wants/fail2ban.service → /lib/systemd/system/fail2ban.service.

执行完成后,再次通过前面的命令检查服务自启状态。

后续如果想查看 Fail2ban 有关 sshd 的执行状态,可使用如下命令:

## 检查 sshd 的执行情况
fail2ban-client status sshd 

## 参考输出
Status for the jail: sshd                  
|- Filter                                  
|  |- Currently failed: 0                  
|  |- Total failed:     0                  
|  `- File list:        /var/log/auth.log  
`- Actions                                 
   |- Currently banned: 0                 
   |- Total banned:     0                  
   `- Banned IP list:         

如果均为 0 ,表示当前 PVE 系统没有 SSH 错误密码的尝试记录。

2.系统时区配置

如果在安装 PVE 系统时选错了时区,导致系统时间和北京时间不一致,可以使用以下命令修正:

## 修改系统时区
timedatectl set-timezone Asia/Shanghai

## 检查系统时间
date -R

## 参考输出
Wed, 20 Jul 2022 16:21:28 +0800 

输出结果如果和北京时间一致,则代表修改正确。

3.CPU调度器配置

安装好 cpufrequtils 后,先检查当前 CPU 的调度器:

## 检查 CPU 当前调度器
cpufreq-info

## 参考输出
cpufrequtils 008: cpufreq-info (C) Dominik Brodowski 2004-2009                                     
Report errors and bugs to cpufreq@vger.kernel.org, please.                                         
analyzing CPU 0:                                                                                   
  driver: intel_cpufreq                                                                             
  CPUs which run at the same hardware frequency: 0                                                  
  CPUs which need to have their frequency coordinated by software: 0                                
  maximum transition latency: 20.0 us.                                                              
  hardware limits: 800 MHz - 2.90 GHz                                                               
  available cpufreq governors: conservative, ondemand, userspace, powersave, performance, schedutil  
  current policy: frequency should be within 800 MHz and 2.90 GHz.                                  
                  The governor "ondemand" may decide which speed to use            
                  within this range.                                      
  current CPU frequency is 952 MHz.    

这里面主要关注两个点:

  • driver: intel_cpufreq
  • current policy: governor "ondemand"

当然还有另外一个命令可以用来显示 CPU 调度器:

## 检查 CPU 当前调度器
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

## 参考输出
ondemand

驱动一般不建议手动调整,而 governor "ondemand" 则显示了当前 CPU 的调度器是什么。

接下来,我们需要了解当前系统 CPU 支持的调度器有哪些:

## 检查 CPU 调度器支持情况
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

## 参考输出
conservative ondemand userspace powersave performance schedutil  

这里有很多种调度器可供选择,至于每种调度器有什么优劣,欢迎大家深度挖掘。

本教程以使用 schedutil 调度器为演示。

使用 vim 编辑器来编辑 cpufrequtils 的配置文件:

## 修改 cpufrequtils 配置文件
vim /etc/init.d/cpufrequtils

## 在配置文件中修改调度器

ENABLE="true"
GOVERNOR="schedutil"    ## 修改本行的调度器为 schedutil
MAX_SPEED="0"
MIN_SPEED="0"

i 键进入编辑模式,esc 键退出编辑模式,:wq 命令保存退出。

因为该配置文件很长,教程中留下一份已配置好的文件 Fox_PVE_Cpufrequtils.conf ,以便对比。

修改完成后,需要重新启动 PVE 服务器来使参数生效。

PVE 服务器重启完成后记得重新检查当前 CPU 的调度器,看配置文件是否生效。

这里提供两个命令,分别来实时查看当前 CPU 的频率和内部温度传感器的数值:

## 查看 CPU 当前频率
watch cat /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq

## 查看内部温度
watch sensors

4.PVE 定时重启配置

有时候我们需要让 PVE 服务器周期性的定时重启,则可使用以下命令:

## 编辑系统 crontab
crontab -e 

## 选择 nano 编辑器,粘贴以下内容并保存
0 5 1,16 * * /usr/sbin/reboot

这表示每月 1、16 号的 5 点 0 分执行系统重启命令。

可以使用以下命令来查看当前系统的计划任务:

## 显示系统 crontab
crontab -l

5.PVE系统自动更新

5.1.检查系统定时器

配置系统自动更新之前,先检查当前系统定时器状态:

## 检查系统定时器
systemctl status apt-daily-upgrade.timer

## 示例输出
● apt-daily-upgrade.timer - Daily apt upgrade and clean activities                                 
     Loaded: loaded (/lib/systemd/system/apt-daily-upgrade.timer; enabled; vendor preset: enabled) 
    Drop-In: /etc/systemd/system/apt-daily-upgrade.timer.d                                         
             └─override.conf                                                                       
     Active: active (waiting) since Sat 2022-07-16 07:03:53 CST; 4 days ago                        
    Trigger: Thu 2022-07-17 06:52:50 CST; 13h left                                                
   Triggers: ● apt-daily-upgrade.service                                                       
                                                                                                
Jul 16 12:03:53 hyper systemd[1]: Started Daily apt upgrade and clean activities.

我们后续将手动调整该定时器的时间为,每 10 天的凌晨 02:00 进行触发。

5.2.配置自动更新策略

## 配置自动更新策略
dpkg-reconfigure -plow unattended-upgrades

## 选择 “是” (“YES”)
## 示例输出
Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version

执行命令后,使用“左右”方向键进行选择,“回车”键进行确认。

然后开始调整 apt 的 20auto-upgrades 配置文件:

## 进入 apt 的配置目录
cd /etc/apt/apt.conf.d

## 编辑 20auto-upgrades 配置文件
vim 20auto-upgrades

## 删除里面全部内容并填写以下内容
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "10";
APT::Periodic::AutocleanInterval "1";
APT::Periodic::CleanInterval "1";

其中,用来控制 PVE 更新周期的为 APT::Periodic::Unattended-Upgrade 这行内容,其中的 “10” 表示更新周期为 “10” 天。

接下来调整 apt 的 50unattended-upgrades 配置文件,所有修改项目汇聚如下:

## 编辑 50unattended-upgrades 配置文件
vim 50unattended-upgrades

## 取消了以下行前面的注释,代表启用
"origin=Debian,codename=${distro_codename}-updates";

## 添加了 PVE 本身的更新项目
"origin=Proxmox,codename=${distro_codename},label=Proxmox Debian Repository";

## 取消以下行的前面注释,代表启用,并调整参数
Unattended-Upgrade::AutoFixInterruptedDpkg "true";

Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";

Unattended-Upgrade::Remove-New-Unused-Dependencies "true";

Unattended-Upgrade::Remove-Unused-Dependencies "true";

Unattended-Upgrade::Automatic-Reboot "true";

Unattended-Upgrade::Automatic-Reboot-Time "05:00";

分别表示:

  • 启用了 Debian bullseye-updates 相关更新。
  • 增加并启用 PVE 自有仓库的更新,确保不会遗漏自有仓库的更新内容。
  • 自动修复被打断的Dpkg安装。
  • 自动移除无用的的内核包。
  • 自动移除因更新而出现的无用依赖包。
  • 自动移除以前的无用依赖包。
  • 自动重启:开启。
  • 自动重启时间:05:00。

因为该配置文件很长,教程中留下一份 PVE 7.2 中已配置好的文件 Fox_PVE_50unattended_Upgrades.conf,以便对比。

仔细再仔细确认无误后,esc 键退出编辑模式,:wq 命令保存退出。

5.3.重设自动更新触发器

## 重设自动更新触发器时间为凌晨 02:00
systemctl edit apt-daily-upgrade.timer

## 根据文件中的提示,在中间空白处填入以下内容
[Timer]
OnCalendar=
OnCalendar=02:00
RandomizedDelaySec=0

完整的配置文件可查看 Fox_PVE_Apt_Daily_Upgrade.conf,以便对比。

设置完成后重启自动更新的触发器:

## 重启触发器
systemctl restart apt-daily-upgrade.timer

## 再次检查触发器状态
systemctl status apt-daily-upgrade.timer

## 参考输出
● apt-daily-upgrade.timer - Daily apt upgrade and clean activities                                    
     Loaded: loaded (/lib/systemd/system/apt-daily-upgrade.timer; enabled; vendor preset: enabled)   
    Drop-In: /etc/systemd/system/apt-daily-upgrade.timer.d                                  
             └─override.conf                                                              
     Active: active (waiting) since Wed 2022-07-20 17:36:40 CST; 11s ago                 
    Trigger: Thu 2022-07-21 02:00:00 CST; 8h left                                       
   Triggers: ● apt-daily-upgrade.service                                               
                                                                                       
Jul 20 17:36:40 hyper systemd[1]: Stopped Daily apt upgrade and clean activities.        
Jul 20 17:36:40 hyper systemd[1]: Stopping Daily apt upgrade and clean activities.        
Jul 20 17:36:40 hyper systemd[1]: Started Daily apt upgrade and clean activities.   

至此 PVE 的系统调整已经完成,重启设备后,可以愉快使用了。

Plug in & Forget :)