3.0 proxmox-物理机部署服务多容器-家用(本地)服务器的安装

内容主要出处:
作者:狐狸Nomad https://www.bilibili.com/read/cv17670431 出处:bilibili

本转载已经过作者本人同意,原作者博文gitee同步地址:
https://gitee.com/callmer/pve_toss_notes
以下是正文:


Proxmox,全称 Proxmox Virtual Environment,简称 PVE,是一个基于 Debian 的开源免费的 QEMU 虚拟机平台,对于机器配置较低,系统运行稳定。

之前配置ubuntu系统,然后安装虚拟机方案,后请教运维大佬。直接将proxmox当做系统盘安装即可。
官网下载
https://www.proxmox.com/en/downloads/category/iso-images-pve

国内镜像
http://mirrors.ustc.edu.cn/proxmox/iso/
然后通过UltralISO或者Rufus将ISO写入U盘,制作硬盘镜像。

开机选择从U盘启动进入系统安装


8e8c1f869ea91dda54569f7a4951ea0.jpg

首先是一个使用许可说明。


bf599fd2ffa4c6038fcb8c640d6d1a7.jpg

同意后,选择系统安装磁盘位置后 Next 继续


45642c37159b9b33cee11adda35c25f.jpg

选择所在地区 Next 继续


3839e45c2f376ab9482728704c53f64.jpg

设置登陆密码和邮箱地址 Next 继续


44a03bc8365408c4ec7a79f9b372b78.jpg

设置主机名和IP地址后 Next 继续


bc43acb85d88da089b13632b6d01b57.jpg

再次确认配置,无误则点击 install 开始安装 Proxmox 系统


a5d9b0a9cd96b7dc53578158419563f.jpg

等待片刻后系统将完成安装,并且会自动重启

系统安装完重启后会显示PROXMOX系统的登陆地址,用户名root,用之前登陆的密码可以直接登陆系统。此时在同网段的PC上打开浏览器输入访问地址就可以进行配置虚拟机了。


image.png
注意必须使用https 访问,端口8006,用户名root 密码是之前设置的密码,语言可以选择 Chinese

比如我的地址是https://192.168.3.131:8006/

image.png

image.png

1.1.系统软件源替换

首先对现有的软件源配置进行备份:

进入系统软件源配置文件目录

cd /etc/apt

将默认软件源配置文件进行备份

cp sources.list sources.list.bak

这里我将使用中国科技大(USTC)的镜像仓库进行替换,使用如下命令:

注意:该命令为两行,在输入时请逐行输入并回车执行

替换系统软件仓库

sed -i 's|^deb http://ftp.debian.org|deb https://mirrors.ustc.edu.cn|g' /etc/apt/sources.list

sed -i 's|^deb http://security.debian.org|deb https://mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list

执行完成后,检查是否执行正确:

输出系统源配置文件,检查是否正确

cat /etc/apt/sources.list

如果输出结果中有 USTC 的镜像地址,则表示命令已经正确执行:

输出内容参考:

deb https://mirrors.ustc.edu.cn/debian bullseye main contrib

deb https://mirrors.ustc.edu.cn/debian bullseye-updates main contrib

# security updates
deb https://mirrors.ustc.edu.cn/debian-security bullseye-security main contrib

此处放出 Debian Bullseye 的完整镜像源以供参考, 非常不建议 将 PVE 的系统源替换成完整的 Debian 源,以避免出现问题。

Debian Bullseye 完整源 (USTC)

deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
#deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free

deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
#deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
#deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free

deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
#deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free

可以看到,PVE 的系统源和 Debian 完整源的差异在于 non-free 和 bullseye-backports 。

如果小伙伴在一些软件或者驱动(比如闭源 GPU 驱动)上遇到问题,可以尝试将 PVE 的系统源替换成 Debian 的完整源来尝试解决问题。

1.2.PVE 订阅源替换

默认情况下,PVE 启用了一个官方的源,而该源为订阅制收费,我们需要替换为免费的源。

进入订阅源的目录:

注意:rm 命令为高危险操作命令,请正确使用,请勿手抖,请勿手抖

进入订阅源目录

cd /etc/apt/sources.list.d

删除该目录下的所有配置

rm -rvf *.list

创建 PVE 免费源:

注意:该命令为两行,在输入时请逐行输入并回车执行

创建 PVE 免费源

source /etc/os-release
echo "deb https://mirrors.ustc.edu.cn/proxmox/debian/pve $VERSION_CODENAME pve-no-subscription" > /etc/apt/sources.list.d/pve-no-subscription.list

创建完成后对其进行检查:

检查PVE免费源

cat /etc/apt/sources.list.d/pve-no-subscription.list

如果输出结果中有 USTC 的镜像地址,则表示命令已经正确执行:

输出内容参考:

deb https://mirrors.ustc.edu.cn/proxmox/debian/pve bullseye pve-no-subscription 

1.3.PVE CT Templates 替换

如果需要使用 Proxmox 网页端下载 CT Templates,可以替换 CT Templates 的源。

该功能我未使用,因此只做记录:

替换 CT Templates 源

cp /usr/share/perl5/PVE/APLInfo.pm /usr/share/perl5/PVE/APLInfo.pm.bak

sed -i 's|http://download.proxmox.com|https://mirrors.ustc.edu.cn/proxmox|g' /usr/share/perl5/PVE/APLInfo.pm
如果修改了 CT Templates 地址,需要重启 PVE 服务器才能生效。

1.4.替换后操作

更换完成系统源之后,需要更新系统源的同步:

清理

apt clean && apt autoclean

同步

apt update 

2.安装必要软件
安装软件前,同样要让 PVE 能够访问外网。

同步镜像仓库

apt update

安装系统软件

apt install htop lm-sensors neofetch fail2ban vim tmux unattended-upgrades apt-listchanges powermgmt-base

安装网络工具

apt install iperf iperf3 iftop net-tools ethtool

安装CPU调度调整工具

apt install cpufrequtils

其中 fail2ban 和 unattended-upgrades 为两个服务,后续会对其进行配置。

cpufrequtils 为 CPU 调度器的配置工具,后续会对 CPU 调度算法进行调整。

3.配置 PVE 网桥和管理网口
设置电脑的 IP 地址为静态 IP ,IP 地址段与 PVE 的 IP 地址段保持一致。

因为连的路由器,会从最小的ip开始分配给连接到的设备,我的网段是192.168.3.1,所以会从192.168.3.2开始自动分配ip。
配置网络配置如下:

PVE IP:192.168.3.131/24

PVE 网关:192.168.3.1

子网掩码:  255.255.255.0

PVE DNS:192.168.3.1
image.png

因为我的主板是双网口(2个物理网口),所以回旋线两个enpxxx,当前插的是enp5s0。

PVE系统调整

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

检查 Fail2ban 系统服务状态

systemctl status fail2ban.service

在 Loaded 行,检查是否存在 enable :

参考输出
root@bobokaka:~# systemctl status fail2ban.service
● fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-11-20 23:45:51 CST; 20h ago
       Docs: man:fail2ban(1)
   Main PID: 47352 (fail2ban-server)
      Tasks: 5 (limit: 154498)
     Memory: 14.0M
        CPU: 15.049s
     CGroup: /system.slice/fail2ban.service
             └─47352 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

Nov 20 23:45:51 bobokaka systemd[1]: Starting Fail2Ban Service...
Nov 20 23:45:51 bobokaka systemd[1]: Started Fail2Ban Service.
Nov 20 23:45:52 bobokaka fail2ban-server[47352]: Server ready
root@bobokaka:~# 

其中第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

参考输出

root@bobokaka:~# 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:
root@bobokaka:~#

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

#### 系统时区配置
如果在安装 PVE 系统时选错了时区,导致系统时间和北京时间不一致;

可以使用以下命令修正:

## 修改系统时区

timedatectl set-timezone Asia/Shanghai

## 检查系统时间
date -R

## 参考输出
Mon, 21 Nov 2022 20:32:28 +0800
输出结果如果和北京时间一致,则代表修改正确。 

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

检查 CPU 当前调度器

cpufreq-info
参考输出
root@bobokaka:~# cpufreq-info
cpufrequtils 008: cpufreq-info (C) Dominik Brodowski 2004-2009
Report errors and bugs to [email protected], 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: 1.20 GHz - 3.10 GHz
  available cpufreq governors: conservative, ondemand, userspace, powersave, performance, schedutil
  current policy: frequency should be within 1.20 GHz and 3.10 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz.
analyzing CPU 1:
  driver: intel_cpufreq
  CPUs which run at the same hardware frequency: 1
  CPUs which need to have their frequency coordinated by software: 1
  maximum transition latency: 20.0 us.
  hardware limits: 1.20 GHz - 3.10 GHz
  available cpufreq governors: conservative, ondemand, userspace, powersave, performance, schedutil
  current policy: frequency should be within 1.20 GHz and 3.10 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz.
analyzing CPU 2:
  driver: intel_cpufreq
  CPUs which run at the same hardware frequency: 2
  CPUs which need to have their frequency coordinated by software: 2
  maximum transition latency: 20.0 us.
  hardware limits: 1.20 GHz - 3.10 GHz
  available cpufreq governors: conservative, ondemand, userspace, powersave, performance, schedutil
  current policy: frequency should be within 1.20 GHz and 3.10 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz.
......
analyzing CPU 47:
  driver: intel_cpufreq
  CPUs which run at the same hardware frequency: 47
  CPUs which need to have their frequency coordinated by software: 47
  maximum transition latency: 20.0 us.
  hardware limits: 1.20 GHz - 3.10 GHz
  available cpufreq governors: conservative, ondemand, userspace, powersave, performance, schedutil
  current policy: frequency should be within 1.20 GHz and 3.10 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz.

这里面主要关注两个点:

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

这里有很多种调度器可供选择,具体什么优劣本人未知。

目前,CPU 普遍采用 schedutil 调度器(能量感知调度(EAS))。
能量感知调度(EAS)使调度程序能够预测其决策对 CPU 消耗的电量的影响。 EAS 依赖于 CPU 的能量模型 (EM) 来为每个任务选择省电的 CPU,同时要求对执行任务的吞吐量的影响最小。因此本教程以使用 schedutil 调度器为演示。

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

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

## 找到 GOVERNOR="ondemand" 修改为以下内容
GOVERNOR="schedutil"
按 i 键进入编辑模式,esc 键退出编辑模式,:wq 命令保存退出。
image.png

image.png

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

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

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

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

查看内部温度

watch sensors 
image.png
PVE 定时重启配置

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

## 编辑系统 crontab
crontab -e

## 选择 nano 编辑器,粘贴以下内容并保存
30 4 1,16 * * /usr/sbin/reboot
这表示每月1、16号的4点30分执行系统重启命令。


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

## 显示系统 crontab
crontab -l 
image.png

nano编辑器使用:

保存对文件所做的更改,按Ctrl+o快捷键。
如果该文件尚不存在,则将在保存后立即创建该文件。
再按Ctrl+x退出。

如果你对未保存的文件按下Ctrl+x快捷键退出。
nano将询问您是否要保存更改,Save modified buffer?。

按下Ctrl+y表示保存并退出nano编辑器,
按下Ctrl+n表示不保存并退出nano编辑器。

参考输出:

root@bobokaka:~# crontab -l 
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
30 4 6,21 * * /usr/sbin/reboot
root@bobokaka:~# 
PVE系统自动更新

5.1.检查系统定时器

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

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

## 示例输出
root@bobokaka:~# 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)
     Active: active (waiting) since Mon 2022-11-21 20:58:22 CST; 19min ago
    Trigger: Tue 2022-11-22 06:30:36 CST; 9h left
   Triggers: ● apt-daily-upgrade.service

Nov 21 20:58:22 bobokaka systemd[1]: Started Daily apt upgrade and clean activities.
root@bobokaka:~# 

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

5.2.配置自动更新策略

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

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

image.png

执行dpkg-reconfigure -plow unattended-upgrades命令后,使用“左右”方向键进行选择,“回车”键进行确认。

──────────|配置无人值守升级├─────────────
经常进行更新是保证系统安全的重要组成部分。默认情况下,更新需要使用包管理手动应用具。或者,您可以选择让该系统自动下载和安装重要的更新。
│自动下载和安装稳定的更新?

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

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

编辑 20auto-upgrades 配置文件

vim 20auto-upgrades

原始内容如下:


image.png
删除里面全部内容并填写以下内容
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 "04:30";

分别表示:

启用了 Debian bullseye-updates 相关更新。 

增加并启用 PVE 自有仓库的更新,确保不会遗漏自有仓库的更新内容。

自动修复被打断的Dpkg安装。

自动移除无用的的内核包。

自动移除因更新而出现的无用依赖包。

自动移除以前的无用依赖包。

自动重启:开启。

自动重启时间:04:30。

esc 键退出编辑模式,:wq 命令保存退出

重设自动更新触发器
## 重设自动更新触发器时间为凌晨02:00
root


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

ctrl+Y,保存,然后ctrl+X ,再按N退出。

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

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

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

## 参考输出
root@bobokaka:/etc/systemd/system/apt-daily-upgrade.timer.d# 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)
     Active: active (waiting) since Mon 2022-11-21 21:53:47 CST; 4s ago
    Trigger: Tue 2022-11-22 06:00:05 CST; 8h left
   Triggers: ● apt-daily-upgrade.service

Nov 21 21:53:47 bobokaka systemd[1]: Stopped Daily apt upgrade and clean activities.
Nov 21 21:53:47 bobokaka systemd[1]: Stopping Daily apt upgrade and clean activities.
Nov 21 21:53:47 bobokaka systemd[1]: Started Daily apt upgrade and clean activities.
root@bobokaka:/etc/systemd/system/apt-daily-upgrade.timer.d# 

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

开启ssh登录访问.

修改sshd_config配置

vim /etc/ssh/sshd_config

删掉Port前面的#,删掉PermitRootLogin前面的#,并将后面的值改为yes

Port 22
PermitRootLogin yes
PasswordAuthentication yes

启动ssh服务

/etc/init.d/ssh start
#或
service ssh start

重启ssh命令

systemctl restart ssh.service

参考输出

root@bobokaka:~# /etc/init.d/ssh start
Starting ssh (via systemctl): ssh.service.
root@bobokaka:~# 

验证ssh服务状态

/etc/init.d/sshd status

查看服务器端口

netstat -ntulp |grep 22

设置开机自启

update-rc.d ssh enable

内容主要出处:
作者:狐狸Nomad https://www.bilibili.com/read/cv17670431 出处:bilibili

你可能感兴趣的:(3.0 proxmox-物理机部署服务多容器-家用(本地)服务器的安装)