主题
02 · D1-P0:管理平面(3 台)
| 项目 | 内容 |
|---|---|
| 所属阶段 | D1 上午 |
| 前置条件 | D0 完成:模板 9000 可克隆、vmbr1 已通、EIP 6 条已生效、nvme-lvm 已就绪 |
| 预计耗时 | 40 分钟 |
| 完成标志 | ansible ... -m ping 三台全 pong;dig mysql-01.lab.local 能解析 |
| 下一步 | 03-D1-P1最小链路.md |
这一步建哪 3 台:
| VMID | 主机名 | 角色 | 规格 |
|---|---|---|---|
| 100 | bastion | 跳板 + DNS + Ansible 主控 | 2C/2G/25G |
| 101 | ansible-02 | Ansible 备控 | 1C/1G/15G |
| 102 | ansible-03 | Ansible 执行节点 | 1C/1G/15G |
本步需要的资料
| 类型 | 名称 | 从哪来 |
|---|---|---|
| 仓库 | p0-node-init.sh | 03-集群部署/ |
| 仓库 | ansible/(inventory、cfg、group_vars) | 04-工具与自检/ansible/ |
| 前置 | 模板 9000 可克隆 | 01 的产物 |
总表(含版本、下载地址、账号规则)见
00-资料清单.md。
步骤 0 · 开始前检查(3 分钟)
在哪台:PVE(root) 目的:确认 D0 的成果还在,别在错的地基上盖楼 开始前:无
操作:
bash
# 在 PVE 上执行(root)
qm status 9000
pvesm status
systemctl is-active eip.service
ip -4 a show vmbr1 | grep 10.0.0.1
/usr/local/sbin/thin-alert.sh; echo "exit=$?"应该看到:
status: stopped(模板不该开机)nvme-lvm一行activeactiveinet 10.0.0.1/24- thin-alert 无输出、
exit=0
失败怎么办:
- 模板不存在 → 回 D0 步骤 11
nvme-lvm不是 active →vgs/pvesm status看卷组- EIP 没起 →
systemctl restart eip.service再看
步骤 1 · 建 3 台虚拟机(10 分钟)
在哪台:PVE(root) 目的:从模板 9000 克隆出管理平面三台 开始前:步骤 0 全过
操作:
- 建机(VMID 就是 IP 末位):
bash
# 在 PVE 上执行(root)
bash /root/create-lab-vm.sh 100 2 2048 25
bash /root/create-lab-vm.sh 101 1 1024 15
bash /root/create-lab-vm.sh 102 1 1024 15应该看到:每台三行输出,最后是 完成。首次开机 cloud-init 约需 40-90 秒... 和 ssh 提示。
- 等 60 秒,确认三台都在跑:
bash
# 在 PVE 上执行(root)
qm list | grep -E "lab-(100|101|102)"应该看到:三行,状态 running。
- 确认 cloud-init 把 IP 配好了:
bash
# 在 PVE 上执行(root)
qm guest cmd 100 network-get-interfaces | grep -A2 "ip-address"
ping -c2 10.0.0.100应该看到:10.0.0.100;ping 通。
失败怎么办:
模板 9000 不存在→ 回 D0 步骤 11网桥 vmbr1 不存在→ 回 D0 步骤 6- ping 不通、guest agent 无响应 → 等 2 分钟再试;仍不行
qm terminal 100看启动日志
步骤 2 · 给 bastion 发私钥和脚本(5 分钟)
在哪台:Windows → PVE → bastion 目的:让 bastion 能免密连其他机器,并拿到初始化脚本和 Ansible 清单 开始前:步骤 1 的 ping 已通
操作:
2.1 从 Windows 传到 PVE
powershell
# 在 Windows PowerShell 里执行
cd ".\ops-project-files"
scp "03-集群部署\p0-node-init.sh" root@192.168.10.30:/root/
scp -r "04-工具与自检\ansible" root@192.168.10.30:/root/ansible应该看到:两次进度条,无报错。
2.2 从 PVE 传到 bastion
bash
# 在 PVE 上执行(root)
scp /root/.ssh/id_ed25519 ubuntu@10.0.0.100:/home/ubuntu/.ssh/id_ed25519
scp /root/p0-node-init.sh ubuntu@10.0.0.100:/home/ubuntu/
scp -r /root/ansible ubuntu@10.0.0.100:/home/ubuntu/ansible
ssh ubuntu@10.0.0.100 "chmod 600 ~/.ssh/id_ed25519 && ls -l ~/p0-node-init.sh ~/ansible/inventory.ini"应该看到:两个文件都存在,id_ed25519 权限 -rw-------。
失败怎么办:
Permission denied→ PVE 的/root/.ssh/id_ed25519是否存在于 D0 步骤 9.1No such file→ 2.1 没传成功,回 Windows 重传
步骤 3 · 三台初始化(5 分钟)
在哪台:bastion(ubuntu) 目的:统一主机名、写 37 条 hosts、关 IPv6、装基础包 开始前:步骤 2 完成
操作:
- 登录 bastion:
bash
# 在 PVE 上执行(root)
ssh ubuntu@10.0.0.100- 在 bastion 上跑循环(一次改三台):
bash
# 在 bastion 上执行(ubuntu)
cd ~
for pair in "10.0.0.100 bastion" "10.0.0.101 ansible-02" "10.0.0.102 ansible-03"; do
set -- $pair
scp -q -o StrictHostKeyChecking=no p0-node-init.sh ubuntu@$1:/tmp/
ssh -o StrictHostKeyChecking=no ubuntu@$1 "sudo bash /tmp/p0-node-init.sh $2"
done应该看到:每台最后一行 初始化完成:<主机名> 10.0.0.x,以及 [dns] 跳过 或 [dns] 已指向。
失败怎么办:
- 卡在
cloud-init status --wait→ 正常,等 1–2 分钟 Permission denied (publickey)→ 私钥没放对,回步骤 2.2
步骤 4 · 内网 DNS(10 分钟)
在哪台:bastion(ubuntu) 目的:让 *.lab.local 在全家都能解析 开始前:步骤 3 完成
操作:
- 装 dnsmasq:
bash
# 在 bastion 上执行(ubuntu)
sudo apt install -y dnsmasq
sudo tee /etc/dnsmasq.d/lab.conf >/dev/null <<'EOF'
domain=lab.local
local=/lab.local/
expand-hosts
listen-address=10.0.0.100
bind-interfaces
server=223.5.5.5
cache-size=1000
EOF
sudo systemctl enable --now dnsmasq
sudo systemctl restart dnsmasq- 验证解析(此时 hosts 已由步骤 3 写好):
bash
# 在 bastion 上执行(ubuntu)
dig +short mysql-01.lab.local @10.0.0.100
dig +short bastion.lab.local @10.0.0.100应该看到:10.0.0.150 和 10.0.0.100(即使 mysql-01 还没建,也能解析)。
- 再跑一次初始化,把三台的 DNS 指过来(这次
[dns]会生效):
bash
# 在 bastion 上执行(ubuntu)
for pair in "10.0.0.100 bastion" "10.0.0.101 ansible-02" "10.0.0.102 ansible-03"; do
set -- $pair
ssh ubuntu@$1 "sudo bash /tmp/p0-node-init.sh $2"
done应该看到:每台出现 [dns] 已指向 10.0.0.100。
失败怎么办:
dnsmasq起不来 →sudo systemctl status dnsmasq --no-pager和ss -lntp | grep :53,多半是 53 被占dig返回空 →cat /etc/hosts | grep mysql-01看 hosts 有没有写进去
步骤 5 · 装 Ansible 并验证三节点(10 分钟)
在哪台:bastion(ubuntu) 目的:主控能批量管三台 开始前:步骤 4 的 dig 能通
操作:
- 主控装 Ansible:
bash
# 在 bastion 上执行(ubuntu)
sudo apt install -y ansible
cd ~/ansible
ansible --version | head -2- 三节点互相能通 + 跑 ping:
bash
# 在 bastion 上执行(ubuntu)
ansible ansible -m ping应该看到:三台各一段 "ping": "pong",最后 SUCCESS。
- 看主机名和时区(验证批量执行):
bash
# 在 bastion 上执行(ubuntu)
ansible ansible -m shell -a 'hostname; timedatectl | head -1'应该看到:bastion / ansible-02 / ansible-03 + Time zone: Asia/Shanghai.
失败怎么办:
UNREACHABLE→ 手工ssh ubuntu@10.0.0.101 hostname试,不通就是密钥或网络sudo: a password is required→ 本实验 ubuntu 用户免密 sudo,出现这行说明/etc/sudoers.d被动过command not found: ansible→ 装失败,看 apt 源
步骤 6 · 备控同步(5 分钟)
在哪台:bastion(root) 目的:让 101/102 每 5 分钟从主控同步 playbook 和 inventory,主控挂了能接管 开始前:步骤 5 通过
操作:
bash
# 在 bastion 上执行(ubuntu)
sudo apt install -y rsync cron
sudo crontab -l 2>/dev/null | grep -v "ansible-sync" | sudo crontab - 2>/dev/null || true
( sudo crontab -l 2>/dev/null; \
echo '*/5 * * * * rsync -a --delete -e "ssh -i /home/ubuntu/.ssh/id_ed25519 -o StrictHostKeyChecking=no" /home/ubuntu/ansible/ ubuntu@10.0.0.101:/home/ubuntu/ansible/ && rsync -a --delete -e "ssh -i /home/ubuntu/.ssh/id_ed25519 -o StrictHostKeyChecking=no" /home/ubuntu/ansible/ ubuntu@10.0.0.102:/home/ubuntu/ansible/ # ansible-sync' ) | sudo crontab -应该看到:无输出(crontab 写入成功不打印)。
验证:
bash
# 在 bastion 上执行(ubuntu)
sudo crontab -l
sudo rsync -a --delete -e "ssh -i /home/ubuntu/.ssh/id_ed25519 -o StrictHostKeyChecking=no" ~/ansible/ ubuntu@10.0.0.101:/home/ubuntu/ansible/
ssh ubuntu@10.0.0.101 "ls ~/ansible"应该看到:crontab 有一行带 # ansible-sync;101 的 ~/ansible 里也有 inventory.ini。
失败怎么办:
crontab: command not found→sudo apt install -y cron- rsync 报
Permission denied→ 101 的 ubuntu 用户目录权限,或密钥没同步
步骤 7 · P0 验收
逐条打勾:
- [ ]
qm list里 100/101/102 都是 running - [ ]
ping 10.0.0.100通 - [ ]
ansible ansible -m ping三台 pong - [ ]
dig +short bastion.lab.local @10.0.0.100返回10.0.0.100 - [ ]
dig +short nginx-01.lab.local @10.0.0.100返回10.0.0.120(机器还没建,但解析要有) - [ ] Windows 浏览器能打开
https://192.168.10.100(会显示 SSH 的拒绝页,说明端口通了) - [ ]
sudo crontab -l有ansible-sync那行
全部打勾后 → 进入 03-D1-P1最小链路.md。
黑公网安备23022102000118号