主题
Proxmox VE 从零到生产:完整运维指南
版本: PVE 8.x (基于 Debian 12 Bookworm)
作者: AI-EAR 运维团队
更新日期: 2026-08-26
目录
第一章:PVE 概述与架构
1.1 什么是 Proxmox VE
Proxmox Virtual Environment (PVE) 是一个开源的企业级虚拟化管理平台,基于 Debian Linux,集成了:
- KVM - 全虚拟化(虚拟机)
- LXC - 容器虚拟化(轻量级容器)
- Ceph - 分布式存储
- ZFS - 高级文件系统
- Corosync + Pacemaker - 高可用集群
1.2 PVE vs 其他虚拟化平台
| 特性 | PVE | VMware vSphere | Hyper-V | oVirt |
|---|---|---|---|---|
| 开源 | ✅ 完全开源 | ❌ 商业 | ❌ 微软 | ✅ 开源 |
| 集群 | ✅ 内置 | ✅ (需 vCenter) | ✅ (需 SCVMM) | ✅ |
| 容器支持 | ✅ LXC | ❌ | ✅ (有限) | ❌ |
| Ceph 集成 | ✅ 原生 | ❌ | ❌ | ❌ |
| 备份 | ✅ PBS | ❌ (需 Veeam) | ❌ | ❌ |
| Web UI | ✅ 内置 | ✅ | ✅ | ✅ |
| API | ✅ REST | ✅ | ✅ | ✅ |
| 成本 | 免费 | 高昂 | 中等 | 免费 |
1.3 核心组件架构
┌─────────────────────────────────────────────────────────┐
│ PVE Web Interface │
│ (port 8006, HTTPS) │
└────────────────────┬────────────────────────────────────┘
│
┌────────────┴────────────┐
│ │
┌────┴────┐ ┌────┴────┐
│ pveproxy│ │ pvedaemon│
│ (API代理)│ │ (后端服务)│
└────┬────┘ └────┬────┘
│ │
└────────────┬────────────┘
│
┌────────────┴────────────┐
│ /etc/pve (pmxcfs) │
│ (集群配置文件系统) │
└────────────┬────────────┘
│
┌─────────────────┼─────────────────┐
│ │ │
┌──┴──┐ ┌───┴───┐ ┌───┴───┐
│ KVM │ │ LXC │ │ Ceph │
│(QEMU)│ │(容器) │ │(存储) │
└─────┘ └───────┘ └───────┘关键服务:
pveproxy- Web UI 和 API 代理 (port 8006)pvedaemon- 后端任务执行pvestatd- 状态收集守护进程pmxcfs- Proxmox 集群文件系统 (基于 Corosync)corosync- 集群通信pve-ha-lrm- 本地资源管理器 (HA)pve-ha-crm- 集群资源管理器 (HA)
1.4 硬件要求
最低配置:
- CPU: Intel VT/AMD-V 支持,2 核
- 内存: 2 GB (管理) + VM 内存
- 存储: 32 GB (系统)
生产推荐:
- CPU: Intel Xeon / AMD EPYC,支持 VT-d/IOMMU
- 内存: 64 GB+ (ECC 推荐)
- 存储: SSD/NVMe (系统 + ZFS/Ceph)
- 网络: 双万兆 (管理 + 存储分离)
第二章:安装与基础配置
2.1 安装方式
方式 1:ISO 安装(推荐)
bash
# 1. 下载 ISO
wget https://www.proxmox.com/en/downloads/proxmox-virtual-environment/iso/proxmox-ve-8.2-1.iso
# 2. 制作启动盘 (Linux)
dd if=proxmox-ve-8.2-1.iso of=/dev/sdX bs=1M status=progress
# 3. BIOS 设置
# - 启用 VT-x/AMD-V
# - 启用 VT-d/IOMMU (GPU 直通需要)
# - UEFI 模式启动
# 4. 安装过程
# - 选择目标磁盘 (推荐 ZFS RAID1)
# - 设置时区、密码、邮箱
# - 配置网络 (静态 IP)
# - 完成后重启方式 2:Debian + PVE 源
bash
# 1. 安装 Debian 12 (Bookworm) 最小化
# 2. 配置网络 (静态 IP)
cat > /etc/network/interfaces << 'EOF'
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.11/24
gateway 192.168.1.1
EOF
# 3. 设置 hostname
hostnamectl set-hostname pve01
echo "192.168.1.11 pve01" >> /etc/hosts
# 4. 添加 PVE 源
echo "deb http://download.proxmox.com/debian/pve bookworm pve-no-subscription" \
> /etc/apt/sources.list.d/pve-no-subscription.list
# 5. 添加 GPG key
wget https://enterprise.proxmox.com/debian/proxmox-release-bookworm.gpg \
-O /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg
# 6. 安装 PVE
apt update && apt install proxmox-ve postfix open-iscsi chrony -y
# 7. 重启
reboot2.2 基础配置
bash
# 1. 关闭 swap (推荐)
swapoff -a
sed -i '/swap/d' /etc/fstab
# 2. 配置内核参数
cat >> /etc/sysctl.conf << 'EOF'
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
EOF
sysctl -p
# 3. 时间同步 (集群必须)
systemctl enable --now chrony
# 4. 配置邮件告警
cat > /etc/pve/datacenter.cfg << 'EOF'
email_from: pve-alert@example.com
console: vv
migration: type=secure
EOF
# 5. 更新系统
apt update && apt dist-upgrade -y2.3 Web UI 访问
bash
# 访问地址: https://<PVE-IP>:8006
# 默认登录: root / <安装时密码>
# Realm: Linux PAM standard authentication
# 建议:创建普通用户 + RBAC
# Datacenter → Permissions → Users → Add第三章:集群部署
3.1 集群架构
┌─────────────────────────────────────────────────┐
│ Corosync Cluster │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ pve01 │ │ pve02 │ │ pve03 │ │
│ │ Vote: 1 │ │ Vote: 1 │ │ Vote: 1 │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ └───────────────┴───────────────┘ │
│ │
│ Quorum: 至少 2/3 节点在线 (防脑裂) │
│ pmxcfs: 共享配置 (/etc/pve) │
└─────────────────────────────────────────────────┘3.2 创建集群
bash
# === 在第一个节点 (pve01) 执行 ===
# 1. 创建集群
pvecm create mycluster \
--link0 192.168.1.11 \
--bindnet0_addr 192.168.1.11
# 2. 查看集群状态
pvecm status
pvecm nodes3.3 加入集群
bash
# === 在其他节点执行 ===
pvecm add 192.168.1.11 \
--link0 <本机IP> \
--use_ssh
# 验证
pvecm status
pvecm nodes3.4 配置冗余网络 (Link1)
bash
# 编辑集群配置
nano /etc/pve/corosync.conf
# 添加 link1 (心跳网络)
nodelist {
node {
name: pve01
nodeid: 1
ring0_addr: 192.168.1.11
ring1_addr: 10.0.0.11
}
node {
name: pve02
nodeid: 2
ring0_addr: 192.168.1.12
ring1_addr: 10.0.0.12
}
}
# 推送配置
pvecm updateconfig
systemctl restart corosync3.5 2 节点集群 + QDevice
bash
# === 在第三方机器 (QDevice) 安装 ===
apt install corosync-qnetd -y
corosync-qnetd-tool -c -i
systemctl enable --now corosync-qnetd
# === 在 PVE 节点加入 QDevice ===
apt install corosync-qdevice -y
pvecm qdevice setup 192.168.1.100
# 验证
pvecm status
# Expected votes: 3 (2 nodes + 1 qdevice)3.6 集群管理命令
bash
pvecm status # 集群状态
pvecm nodes # 节点列表
pvecm delnode pve03 # 删除节点
pvecm updatecerts # 更新证书
pvecm updateconfig # 同步配置
# 查看集群文件系统
ls -la /etc/pve/第四章:网络架构
4.1 网络模型
┌─────────────────────────────────────────────────┐
│ PVE Node │
│ │
│ ┌────────────────────────────────────────┐ │
│ │ Physical NICs │ │
│ │ eth0 (管理) eth1 (存储) eth2 (VM)│ │
│ └───┬─────────────┬─────────────┬───────┘ │
│ │ │ │ │
│ ┌───┴───┐ ┌────┴────┐ ┌───┴────┐ │
│ │ vmbr0 │ │ vmbr1 │ │ vmbr2 │ │
│ │Bridge │ │ Bridge │ │ Bridge │ │
│ └───┬───┘ └────┬────┘ └───┬────┘ │
│ │ │ │ │
│ PVE管理 Ceph/iSCSI VM业务网络 │
└─────────────────────────────────────────────────┘4.2 基础网络配置
bash
# /etc/network/interfaces
auto lo
iface lo inet loopback
# 管理网络
auto eth0
iface eth0 inet manual
auto vmbr0
iface vmbr0 inet static
address 192.168.1.11/24
gateway 192.168.1.1
bridge-ports eth0
bridge-stp off
bridge-fd 0
# 存储网络
auto eth1
iface eth1 inet static
address 10.0.1.11/24
mtu 9000 # Jumbo frames
# VM 业务网络
auto eth2
iface eth2 inet manual
auto vmbr1
iface vmbr1 inet manual
bridge-ports eth2
bridge-stp off
bridge-fd 0
bridge-vlan-aware yes
bridge-vids 2-40944.3 VLAN 配置
bash
# 方法 1:Bridge VLAN-aware (推荐)
# VM 配置时指定 VLAN tag:
qm set 100 --net0 virtio,bridge=vmbr1,tag=10
# 方法 2:VLAN 子接口
auto eth2.10
iface eth2.10 inet manual
vlan-raw-device eth2
auto vmbr10
iface vmbr10 inet static
address 10.0.10.11/24
bridge-ports eth2.10
bridge-stp off
bridge-fd 04.4 Bonding (链路聚合)
bash
# LACP 802.3ad
auto bond0
iface bond0 inet manual
bond-slaves eth0 eth1
bond-miimon 100
bond-mode 802.3ad
bond-xmit-hash-policy layer2+3
bond-lacp-rate fast
auto vmbr0
iface vmbr0 inet static
address 192.168.1.11/24
gateway 192.168.1.1
bridge-ports bond0
bridge-stp off
bridge-fd 0
# Active-Backup
auto bond1
iface bond1 inet manual
bond-slaves eth2 eth3
bond-miimon 100
bond-mode active-backup
bond-primary eth24.5 在线迁移网络
bash
# /etc/pve/datacenter.cfg:
migration: type=insecure,network=10.0.0.0/24
# 或 CLI:
pvenode config set --migration-network 10.0.0.0/24 --migration-type insecure第五章:存储系统
5.1 存储类型对比
| 类型 | 性能 | 可靠性 | 成本 | 适用场景 |
|---|---|---|---|---|
| Local | ★★★★★ | ★★ | ★ | 测试、临时存储 |
| LVM-Thin | ★★★★ | ★★★ | ★★ | 单机生产 |
| ZFS | ★★★★★ | ★★★★★ | ★★★ | 单机生产 (推荐) |
| Ceph | ★★★★ | ★★★★★ | ★★★★ | 集群 (推荐) |
| NFS | ★★★ | ★★★ | ★★ | 简单共享存储 |
| iSCSI | ★★★★ | ★★★★ | ★★★ | 企业 SAN |
5.2 ZFS 配置
bash
# 1. 创建 ZFS Pool (镜像)
zpool create -f -o ashift=12 \
rpool mirror /dev/sda /dev/sdb
# 2. 创建数据集
zfs create rpool/data
zfs set compression=lz4 rpool/data
zfs set atime=off rpool/data
# 3. 创建 VM 存储
zfs create -V 100G rpool/vm-disks # Zvol (块设备)
# 4. 在 PVE 添加 ZFS 存储
# Datacenter → Storage → Add → ZFS (Thin Provisioning)
# 5. ZFS 优化参数
cat >> /etc/modprobe.d/zfs.conf << 'EOF'
options zfs zfs_arc_max=8589934592 # 8GB ARC 限制
options zfs zfs_prefetch_disable=1 # SSD 禁用预读
EOF
update-initramfs -u5.3 Ceph 配置 (集群)
bash
# === 每个节点安装 Ceph ===
pveceph install
# === 在 pve01 创建 Ceph 集群 ===
pveceph create \
--network 10.0.1.0/24 \
--cluster-network 10.0.2.0/24
# === 每个节点添加 OSD ===
pveceph osd create /dev/sdb
pveceph osd create /dev/sdc
# 查看 OSD 状态
ceph osd tree
ceph osd status
# === 创建 Pool ===
pveceph pool create vm-disks \
--pg_num 128 \
--pgp_num 128 \
--application rbd \
--size 3 \
--min_size 2
# === 创建 CephFS ===
pveceph fs create \
--name cephfs \
--pg_num 64 \
--add-storage
# === Ceph 调优 ===
ceph -s
ceph health detail
ceph osd pool set vm-disks pg_num 2565.4 NFS 配置
bash
pvesm add nfs nas-backup \
--server 192.168.1.100 \
--export /data/pve-backup \
--content backup,iso,vztmpl \
--maxfiles 5
# 或直接编辑 /etc/pve/storage.cfg:
nfs: nas-backup
server 192.168.1.100
export /data/pve-backup
content backup,iso,vztmpl
maxfiles 5
prune-backups keep-daily=7,keep-weekly=4,keep-monthly=65.5 iSCSI + LVM-Thin
bash
# 1. 安装 iSCSI initiator
apt install open-iscsi -y
# 2. 发现 target
iscsiadm -m discovery -t st -p 192.168.1.100:3260
# 3. 登录
iscsiadm -m node --login
# 4. 创建 LVM
pvcreate /dev/sdX
vgcreate vg-iscsi /dev/sdX
lvcreate -l 100%FREE -T vg-iscsi/vm-data
# 5. 在 PVE 添加存储
pvesm add iscsi synology-iscsi \
--portal 192.168.1.100 \
--target iqn.2000-01.com.synology:xxx
pvesm add lvmthin vm-pool \
--vgname vg-iscsi \
--thinpool vm-data \
--content images,rootdir第六章:虚拟机管理
6.1 创建虚拟机
bash
# CLI 创建 VM
qm create 100 \
--name "web-server" \
--memory 4096 \
--cores 2 \
--sockets 1 \
--cpu host \
--numa 1 \
--net0 virtio,bridge=vmbr0 \
--scsihw virtio-scsi-pci \
--scsi0 local-lvm:32,cache=writeback,discard=on \
--boot order=scsi0 \
--ostype l26 \
--agent enabled=1
# 导入 ISO
qm set 100 --ide2 local:iso/ubuntu-22.04.iso,media=cdrom
# 启动
qm start 1006.2 VM 配置优化
bash
# CPU 优化
qm set 100 --cpu host # 使用宿主机 CPU 型号
qm set 100 --numa 1 # 启用 NUMA
qm set 100 --hugepages 1024 # 大页内存 (需要预留)
# 磁盘优化
qm set 100 --scsi0 local-lvm:vm-100-disk-0,cache=writeback,discard=on,iothread=1
qm set 100 --scsihw virtio-scsi-single # 单队列优化
# 网络优化
qm set 100 --net0 virtio,bridge=vmbr0,mtu=9000 # Jumbo frames
qm set 100 --net0 virtio,bridge=vmbr0,queues=4 # 多队列
# VirtIO 驱动 (Windows 必须)
qm set 100 --scsi0 local-lvm:32,cache=writeback
qm set 100 --ide2 local:iso/virtio-win.iso,media=cdrom6.3 虚拟机迁移
bash
# 在线迁移 (需要共享存储)
qm migrate 100 pve02 --online
# 离线迁移
qm migrate 100 pve02
# 全量迁移 (包括磁盘)
qm migrate 100 pve02 --full
# 查看迁移状态
qm status 100
qm monitor 100
> info migrate6.4 快照管理
bash
# 创建快照
qm snapshot 100 daily-backup --description "auto backup"
# 列出快照
qm listsnapshot 100
# 恢复快照
qm rollback 100 daily-backup
# 删除快照
qm delsnapshot 100 daily-backup6.5 模板与克隆
bash
# 转换为模板
qm template 100
# 从模板克隆
qm clone 100 101 --name "web-server-01" --full
# 链接克隆 (节省空间)
qm clone 100 102 --name "web-server-02"
# Cloud-Init 模板
qm create 9000 --name "ubuntu-template" --memory 2048 --cores 2
qm importdisk 9000 ubuntu-22.04.qcow2 local-lvm
qm set 9000 --scsi0 local-lvm:vm-9000-disk-0
qm set 9000 --ide2 local-lvm:cloudinit
qm set 9000 --boot order=scsi0
qm set 9000 --agent enabled=1
qm template 9000
# 从 Cloud-Init 模板创建 VM
qm clone 9000 200 --name "cloud-vm"
qm set 200 --ciuser admin --cipassword password123
qm set 200 --sshkeys /root/.ssh/id_rsa.pub
qm set 200 --ipconfig0 ip=dhcp
qm resize 200 scsi0 +20G
qm start 2006.6 GPU 直通 (PCI Passthrough)
bash
# 1. 启用 IOMMU
# /etc/default/grub:
GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on iommu=pt"
# AMD:
GRUB_CMDLINE_LINUX_DEFAULT="quiet amd_iommu=on iommu=pt"
update-grub
# 2. 加载 VFIO 模块
# /etc/modules:
vfio
vfio_iommu_type1
vfio_pci
vfio_virqfd
# 3. 绑定 GPU 到 VFIO
echo "10de 1b80" > /sys/bus/pci/drivers/vfio-pci/new_id # NVIDIA GTX 1080
# 4. 配置 VM
qm set 100 --hostpci0 01:00.0,pcie=1,x-vga=1
# 5. 更新 initramfs
update-initramfs -u -k all
reboot6.7 常用 VM 命令
bash
qm list # 列出所有 VM
qm status <vmid> # VM 状态
qm start <vmid> # 启动
qm shutdown <vmid> # 优雅关机
qm stop <vmid> # 强制关机
qm reboot <vmid> # 重启
qm destroy <vmid> # 删除 (危险!)
qm config <vmid> # 查看配置
qm monitor <vmid> # QEMU monitor
qm guest cmd <vmid> ping # Guest Agent ping
qm guest exec <vmid> -- ls # Guest Agent 执行命令第七章:容器管理 (LXC)
7.1 LXC vs KVM
| 特性 | LXC | KVM |
|---|---|---|
| 虚拟化类型 | 操作系统级 | 硬件级 |
| 性能开销 | 极低 (<5%) | 低 (5-15%) |
| 启动时间 | 秒级 | 分钟级 |
| 内存占用 | 极低 | 中等 |
| 隔离性 | 较弱 | 强 |
| 内核共享 | 是 (宿主机内核) | 否 (独立内核) |
| 适用场景 | 微服务、Web、数据库 | 完整 OS、Windows、GPU |
7.2 创建容器
bash
# 1. 下载模板
pveam update
pveam download local ubuntu-22.04-standard_22.04-1_amd64.tar.zst
# 2. 创建容器
pct create 200 local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst \
--hostname "web-container" \
--memory 1024 \
--swap 512 \
--cores 2 \
--rootfs local-lvm:8 \
--net0 name=eth0,bridge=vmbr0,ip=dhcp \
--unprivileged 1 \
--features nesting=1
# 3. 启动
pct start 200
# 4. 进入容器
pct enter 2007.3 容器配置
bash
# 资源调整
pct set 200 --memory 2048 --cores 4
# 挂载宿主机目录
pct set 200 --mp0 /data/shared,mp=/mnt/shared
# 设备直通
pct set 200 --dev0 /dev/ttyUSB0
# 网络配置
pct set 200 --net0 name=eth0,bridge=vmbr0,ip=192.168.1.200/24,gw=192.168.1.1
# DNS
pct set 200 --nameserver 8.8.8.8 --searchdomain example.com7.4 特权 vs 非特权容器
bash
# 非特权 (推荐,更安全)
pct create 200 ... --unprivileged 1
# 特权 (需要特殊权限时使用)
pct create 201 ... --unprivileged 0
# UID/GID 映射 (非特权容器)
# /etc/subuid 和 /etc/subgid:
root:100000:655367.5 常用容器命令
bash
pct list # 列出所有容器
pct start <ctid> # 启动
pct shutdown <ctid> # 优雅关机
pct stop <ctid> # 强制停止
pct enter <ctid> # 进入容器
pct push <ctid> /local/file /container/path # 推送文件
pct pull <ctid> /container/file /local/path # 拉取文件
pct migrate <ctid> <node> # 迁移
pct destroy <ctid> # 删除 (危险!)第八章:高可用 (HA)
8.1 HA 架构
┌─────────────────────────────────────────────────┐
│ HA Manager │
│ │
│ ┌──────────────────────────────────────┐ │
│ │ CRM (Cluster Resource Manager) │ │
│ │ - 运行在 master 节点 │ │
│ │ - 决策资源分配和迁移 │ │
│ └──────────────────────────────────────┘ │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ LRM │ │ LRM │ │ LRM │ │
│ │ (pve01) │ │ (pve02) │ │ (pve03) │ │
│ │ 本地管理 │ │ 本地管理 │ │ 本地管理 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────┘8.2 创建 HA 组
bash
# CLI
ha-manager groupadd prefer_pve01_02 \
--nodes pve01:2,pve02:1,pve03:0 \
--nofailback 0 \
--restricted 0
# 参数说明:
# - nodes: 节点:优先级 (数字越大越优先)
# - nofailback: 1=不自动迁回高优先级节点
# - restricted: 1=只能运行在组内节点8.3 将 VM 加入 HA
bash
ha-manager add vm:100 \
--group prefer_pve01_02 \
--state started \
--max_restart 3 \
--max_relocate 2
# 参数说明:
# - state: started/stopped/enabled/disabled
# - max_restart: 节点内最大重启次数
# - max_relocate: 最大迁移次数8.4 HA 状态管理
bash
# 查看所有 HA 资源
ha-manager status
# 手动迁移
ha-manager migrate vm:100 pve02
# 临时禁用 HA (维护时)
ha-manager set vm:100 --state disabled
# 查看 HA 日志
journalctl -u pve-ha-lrm -f
journalctl -u pve-ha-crm -f8.5 脑裂防护
bash
# 查看 quorum 状态
pvecm status
# 紧急时手动设置期望投票数 (危险!)
pvecm expected 2
# 2 节点集群必须配置 QDevice
pvecm qdevice setup <qdevice-ip>
# 查看 HA Fencing
ha-manager crm-command node-fence <node> 1 # 手动 fence8.6 Fencing 机制
PVE 使用 Watchdog Fencing:
- 节点失联 → CRM 等待
ha-manager shutdown_timeout(默认 60s) - 超时后标记为 fenced
- 资源迁移到其他节点
- fenced 节点触发 watchdog 重启
bash
# 配置 watchdog
echo "softdog" >> /etc/modules
systemctl restart watchdog-mux
# 测试 (危险! 会重启)
echo c > /proc/sysrq-trigger第九章:备份与灾难恢复
9.1 备份策略
┌─────────────────────────────────────────────────┐
│ 备份层次 │
│ │
│ Level 1: 快照 (秒级恢复) │
│ └─ ZFS/Ceph 快照 (存储级) │
│ └─ VM 快照 (应用级) │
│ │
│ Level 2: 备份 (分钟级恢复) │
│ └─ vzdump (VM/CT 备份) │
│ └─ PBS (增量去重) │
│ │
│ Level 3: 异地备份 (小时级恢复) │
│ └─ PBS 远程同步 │
│ └─ rsync/rclone │
│ │
│ Level 4: 冷备份 (天级恢复) │
│ └─ 磁带/对象存储 │
└─────────────────────────────────────────────────┘9.2 vzdump 备份
bash
# 手动备份
vzdump 100 --mode snapshot --compress zstd --storage nas-backup
# 批量备份
vzdump 100 101 102 --mode snapshot --compress zstd
# 备份所有 VM
vzdump --all --mode snapshot --exclude 100,101
# 定时备份
# Datacenter → Backup → Add
# Schedule: 每天 02:00
# Mode: Snapshot
# Compression: ZSTD
# Retention: keep-daily=7, keep-weekly=4, keep-monthly=6
# 恢复备份
qmrestore /var/lib/vz/dump/vzdump-qemu-100-2026_08_26-02_00_00.vma.zst 1009.3 PBS (Proxmox Backup Server)
bash
# === 安装 PBS ===
# 独立机器或 VM
apt install proxmox-backup-server
# 创建 datastore
proxmox-backup-manager datastore create backup-store /data/backup
# 创建用户
proxmox-backup-manager user create admin@pbs
# === PVE 添加 PBS 存储 ===
# Datacenter → Storage → Add → Proxmox Backup Server
# Server: pbs01.example.com
# Datastore: backup-store
# User: admin@pbs
# Password: xxx
# === PBS 优化 ===
# 启用验证
proxmox-backup-manager datastore update backup-store \
--verify-schedule "daily" \
--gc-schedule "weekly"
# 保留策略
proxmox-backup-manager datastore update backup-store \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--keep-yearly 29.4 灾难恢复
bash
# 单 VM 恢复
qmrestore /path/to/backup.vma.zst 100
# 整个集群恢复
# 1. 重新安装 PVE
# 2. 创建集群
# 3. 添加存储
# 4. 恢复所有 VM
for backup in /backup/*.vma.zst; do
vmid=$(echo $backup | grep -oP '\d+' | head -1)
qmrestore $backup $vmid --storage local-lvm
done
# 验证恢复
for vmid in $(qm list | awk 'NR>1 {print $1}'); do
echo "Testing VM $vmid..."
qm start $vmid
sleep 30
qm guest cmd $vmid ping
qm shutdown $vmid
done第十章:性能调优
10.1 CPU 优化
bash
# 1. 使用 host CPU 型号
qm set 100 --cpu host
# 2. 启用 NUMA
qm set 100 --numa 1
qm set 100 --numa0 memory=4096,cpus=0-1
# 3. CPU pinning
# 查看 NUMA 拓扑
numactl --hardware
# 绑定 VM 到特定 CPU
taskset -c 0-3 qm start 100
# 或 /etc/pve/qemu-server/100.conf:
affinity: 0-3
# 4. 大页内存
echo 1024 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
qm set 100 --hugepages 102410.2 内存优化
bash
# 1. 内存 ballooning
qm set 100 --balloon 2048 # 最小 2GB, 最大 4GB
# 2. KSM (Kernel Same-page Merging)
echo 1 > /sys/kernel/mm/ksm/run
echo 100 > /sys/kernel/mm/ksm/sleep_millisecs
# 3. 内存过量使用 (谨慎)
# /etc/pve/datacenter.cfg:
max_workers: 410.3 存储优化
bash
# 1. VirtIO-SCSI 多队列
qm set 100 --scsihw virtio-scsi-single
qm set 100 --scsi0 local-lvm:vm-100-disk-0,iothread=1,queues=4
# 2. 缓存策略
# - none: 无缓存 (最安全, 适合 SSD)
# - writeback: 写回 (最快, 需要 UPS)
# - writethrough: 写穿 (平衡)
qm set 100 --scsi0 local-lvm:vm-100-disk-0,cache=writeback
# 3. Discard/TRIM
qm set 100 --scsi0 local-lvm:vm-100-disk-0,discard=on
# 4. ZFS 优化
zfs set compression=lz4 rpool/data
zfs set primarycache=metadata rpool/data # SSD 优化
zfs set recordsize=8k rpool/data # 数据库优化10.4 网络优化
bash
# 1. VirtIO 多队列
qm set 100 --net0 virtio,bridge=vmbr0,queues=4
# 2. MTU 优化
qm set 100 --net0 virtio,bridge=vmbr0,mtu=9000
# 3. 网络参数优化
# /etc/sysctl.conf:
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216第十一章:安全加固
11.1 访问控制
bash
# 1. 禁用 root SSH 登录
sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
systemctl restart sshd
# 2. 配置 SSH 密钥认证
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-rsa AAAA..." >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# 3. 创建普通用户 + sudo
adduser admin
usermod -aG sudo admin
# 4. 配置 sudo 免密码 (可选)
echo "admin ALL=(ALL) NOPASSWD: xxx" >> /etc/sudoers.d/admin11.2 RBAC (基于角色的访问控制)
bash
# 1. 创建角色
pveum roleadd VMAdmin -privs "VM.Allocate,VM.Audit,VM.Backup,VM.Clone,VM.Config.CDROM,VM.Config.CPU,VM.Config.Disk,VM.Config.Memory,VM.Config.Network,VM.Config.Options,VM.Console,VM.Migrate,VM.Monitor,VM.PowerMgmt,VM.Snapshot"
pveum roleadd VMUser -privs "VM.Audit,VM.Console,VM.PowerMgmt"
# 2. 创建用户组
pveum groupadd admins
pveum groupadd vm-admins
pveum groupadd vm-users
# 3. 用户加入组
pveum useradd admin@pve --groups admins
pveum useradd vmadmin@pve --groups vm-admins
pveum useradd vmuser@pve --groups vm-users
# 4. 分配权限
pveum aclmod / -group admins -role Administrator
pveum aclmod /vms -group vm-admins -role VMAdmin
pveum aclmod /vms/100 -user vmuser@pve -role VMUser11.3 防火墙配置
bash
# 1. 启用防火墙
# Datacenter → Firewall → Options → Firewall: Yes
# 2. 配置规则
# /etc/pve/firewall/cluster.fw:
[OPTIONS]
enable: 1
log_level_in: nolog
log_level_out: nolog
policy_in: DROP
policy_out: ACCEPT
[RULES]
# 允许 SSH
IN ACCEPT -p tcp -dport 22 -source 192.168.1.0/24
# 允许 Web UI
IN ACCEPT -p tcp -dport 8006 -source 192.168.1.0/24
# 允许 Corosync
IN ACCEPT -p udp -dport 5404:5405
# 3. VM 级别防火墙
# /etc/pve/firewall/100.fw:
[OPTIONS]
enable: 1
[RULES]
IN ACCEPT -p tcp -dport 80
IN ACCEPT -p tcp -dport 443
IN ACCEPT -p tcp -dport 22 -source 192.168.1.0/2411.4 证书管理
bash
# 1. 使用 Let's Encrypt
certbot certonly --standalone -d pve.example.com
# 2. 配置 PVE 使用证书
cat /etc/letsencrypt/live/pve.example.com/fullchain.pem > /etc/pve/local/pveproxy-ssl.pem
cat /etc/letsencrypt/live/pve.example.com/privkey.pem > /etc/pve/local/pveproxy-ssl.key
systemctl restart pveproxy
# 3. 自动续期
crontab -e
# 添加:
0 3 * * * certbot renew --quiet && systemctl restart pveproxy第十二章:监控与告警
12.1 内置监控
bash
# 1. 查看节点状态
pvestatd
# 2. 查看 VM 状态
qm list
qm status 100
# 3. 查看存储使用率
pvesm status
df -h
zpool list
# 4. 查看 Ceph 状态
ceph -s
ceph osd status
ceph df12.2 Prometheus + Grafana
bash
# === 安装 Prometheus Exporter ===
apt install prometheus-pve-exporter
# 配置
# /etc/prometheus/pve.yml:
modules:
default:
verify_ssl: false
# 启动
systemctl enable --now prometheus-pve-exporter
# === Prometheus 配置 ===
# /etc/prometheus/prometheus.yml:
scrape_configs:
- job_name: 'pve'
static_configs:
- targets:
- pve01:9221
- pve02:9221
- pve03:9221
# === Grafana Dashboard ===
# 导入 PVE Dashboard ID: 1004812.3 告警配置
bash
# 1. 邮件告警
# /etc/pve/datacenter.cfg:
email_from: pve-alert@example.com
# 2. 配置 SMTP
# /etc/pve/smtp.cf:
smtp_host = smtp.example.com
smtp_port = 587
smtp_user = pve-alert@example.com
smtp_password = xxx
# 3. 自定义告警脚本
# /usr/local/bin/alert.sh:
#!/bin/bash
NODE=$1
LEVEL=$2
MESSAGE=$3
# 发送企业微信/钉钉/飞书
curl -X POST "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx" \
-H "Content-Type: application/json" \
-d "{\"msgtype\": \"text\", \"text\": {\"content\": \"PVE Alert: $NODE - $LEVEL - $MESSAGE\"}}"
chmod +x /usr/local/bin/alert.sh
# 4. 配置告警触发
# /etc/pve/jobs.cfg:
job: check-health
schedule: */5 * * * *
command: /usr/local/bin/alert.sh第十三章:故障排查
13.1 集群问题
bash
# 1. 查看集群状态
pvecm status
pvecm nodes
# 2. 查看 Corosync 日志
journalctl -u corosync -f
# 3. 节点失联
# 检查网络
ping <node-ip>
# 检查 corosync
systemctl status corosync
# 强制删除节点 (危险!)
pvecm delnode <node-name>
# 4. Quorum 丢失
pvecm expected 1 # 紧急恢复 (单节点)13.2 VM 问题
bash
# 1. VM 无法启动
qm start 100
# 查看日志
journalctl -u pvedaemon -f
tail -f /var/log/pve/tasks
# 2. VM 卡住
qm stop 100 # 强制停止
qm unlock 100 # 解锁
# 3. 磁盘损坏
qemu-img check /var/lib/vz/images/100/vm-100-disk-0.qcow2
qemu-img repair /var/lib/vz/images/100/vm-100-disk-0.qcow2
# 4. 性能问题
# 查看 VM 资源使用
qm monitor 100
> info status
> info migrate
> info block13.3 存储问题
bash
# 1. ZFS Pool 故障
zpool status
zpool scrub rpool
zpool clear rpool
# 2. Ceph 问题
ceph -s
ceph health detail
ceph osd tree
ceph osd down <osd-id>
ceph osd out <osd-id>
ceph osd in <osd-id>
# 3. 存储空间不足
df -h
pvesm status
# 清理旧备份
vzdump --prune-backups keep-daily=3
# 4. 磁盘 IO 问题
iostat -x 1 5
iotop13.4 网络问题
bash
# 1. 网络不通
ip addr
ip route
ping <target>
traceroute <target>
# 2. Bridge 问题
brctl show
bridge fdb show
# 3. VLAN 问题
bridge vlan show
ip -d link show vmbr1
# 4. 迁移失败
# 检查网络连通性
ping <target-node>
# 检查端口
nc -zv <target-node> 8002
# 查看迁移日志
journalctl -u pveproxy -f第十四章:生产最佳实践
14.1 集群规划
推荐配置:
- 节点数: 3 或 5 (奇数, 避免脑裂)
- 2 节点必须配 QDevice
- 网络: 双链路 (管理+存储分离)
- 存储: Ceph (3 副本) 或 ZFS (镜像)14.2 资源规划
bash
# CPU 过量使用比例
# - Web 应用: 4:1
# - 数据库: 2:1
# - 计算密集: 1:1
# 内存规划
# - 预留 20% 给宿主机
# - VM 内存总和 ≤ 物理内存 * 0.8
# 存储规划
# - Ceph: 预留 30% 空间
# - ZFS: 预留 20% 空间
# - 监控磁盘使用率 < 80%14.3 备份策略
3-2-1 备份规则:
- 3 份数据副本
- 2 种存储介质
- 1 份异地备份
推荐备份计划:
- 快照: 每小时 (保留 24 个)
- 备份: 每天凌晨 (保留 7 天)
- 周备份: 每周日 (保留 4 周)
- 月备份: 每月 1 日 (保留 12 个月)
- 异地: 每天同步到 PBS 远程14.4 维护计划
bash
# 日常 (每天)
- 检查集群状态: pvecm status
- 检查备份: vzdump 日志
- 检查告警: 邮件/企微/钉钉
# 周维护 (每周)
- 更新系统: apt update && apt upgrade
- 检查日志: journalctl --since "7 days ago"
- 清理快照: qm listsnapshot
# 月维护 (每月)
- 性能测试: pveperf
- 安全扫描: lynis audit system
- 灾难恢复演练: 恢复 1 个 VM
# 季度维护 (每 3 个月)
- 硬件检查: SMART, IPMI
- 容量规划: 预测未来 3 个月
- 文档更新: 网络拓扑, 配置变更第十五章:企业案例
15.1 案例 1: 中小企业虚拟化平台
背景:
- 50 台服务器 → 虚拟化整合
- 预算有限, 无法购买 VMware
- 需要高可用
方案:
- 3 节点 PVE 集群
- Ceph 存储 (3 副本)
- PBS 备份
- HA 自动故障转移
结果:
- 服务器数量: 50 → 3 (节省 94%)
- 电力成本: 降低 80%
- 运维时间: 减少 70%
- RTO: 4 小时 → 5 分钟15.2 案例 2: 开发测试环境
背景:
- 200+ 开发测试环境
- 需要快速创建/销毁
- 资源利用率低
方案:
- 2 节点 PVE + QDevice
- LXC 容器 (轻量级)
- 模板化部署
- API 自动化
结果:
- 环境创建时间: 2 小时 → 2 分钟
- 资源利用率: 30% → 80%
- 开发效率: 提升 50%15.3 案例 3: GPU 计算集群
背景:
- AI/ML 训练需求
- 多 GPU 服务器
- 需要 GPU 直通
方案:
- PVE + IOMMU
- GPU Passthrough
- 每个 VM 分配 2-4 GPU
- Kubernetes on PVE
结果:
- GPU 利用率: 100%
- 训练任务隔离
- 灵活调度附录 A: 常用命令速查表
bash
# 集群管理
pvecm status / nodes / create / add / delnode
# VM 管理
qm list / create / start / shutdown / stop / migrate / snapshot / clone / template
# 容器管理
pct list / create / start / shutdown / enter / migrate
# 存储管理
pvesm status / add / remove
zpool status / create / scrub
ceph -s / osd tree / df
# 备份管理
vzdump / qmrestore
proxmox-backup-manager
# HA 管理
ha-manager status / add / migrate / set
# 网络配置
nano /etc/network/interfaces
nano /etc/pve/corosync.conf
# 日志查看
journalctl -u <service> -f
tail -f /var/log/pve/tasks附录 B: 配置文件参考
bash
# /etc/pve/corosync.conf - 集群配置
# /etc/pve/storage.cfg - 存储配置
# /etc/pve/datacenter.cfg - 数据中心配置
# /etc/pve/qemu-server/<vmid>.conf - VM 配置
# /etc/pve/lxc/<ctid>.conf - 容器配置
# /etc/pve/firewall/cluster.fw - 集群防火墙
# /etc/pve/firewall/<vmid>.fw - VM 防火墙
# /etc/pve/jobs.cfg - 定时任务
# /etc/pve/user.cfg - 用户权限文档结束
本指南涵盖 PVE 从安装到生产运维的完整知识体系,适合系统管理员和运维工程师参考学习。