问题描述
该笔记将记录:在 PVE 中,如何使用 cloud-init 进行快速地操作系统安装,以及相关问题解决办法。
解决方案
参考 PVE, Admin Guide, 7.1 / Chapter 10 / 10.1 Cloud-Init Support 文档,我们记录如下学习笔记。
虽然各 Linux 发行版已经提供 Cloud Image 文件,但是 PVE 仍旧建议我们根据需要来制作自己的镜像文件;
同时 PVE 建议我们将 Cloud Image 转化为 Template 以用于快速创建虚拟机(当创建虚拟机时,仅需要简单配置);
最后,PVE 还是通过创建 ISO 文件来传递配置,所以需要为虚拟机添加 CD-ROM 驱动;
很多 Cloud Image 假设具备串口控制台,所以 PVE 也建议我们在虚拟机中添加,以用于作为虚拟机的显示器;
// ### 第一步、准备 cloud-init 模板 wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img qm create 9000 --memory 2048 --net0 virtio,bridge=vmbr1 # create a new VM qm importdisk 9000 focal-server-cloudimg-amd64.img local-lvm # import the disk to local-lvm storage qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0 # Ubuntu Cloud-Init images require the virtio-scsi-pci controller type for SCSI drives. qm set 9000 --ide2 local-lvm:cloudinit # configure a CD-ROM drive used to pass the Cloud-Init data to the VM. qm set 9000 --boot c --bootdisk scsi0 # This will speed up booting qm set 9000 --serial0 socket --vga serial0 # configure a serial console and use it as a display qm template 9000 // ### 第二步、部署 cloud-init 模板 qm clone 9000 150 --name k8s120-wn150 --full true qm resize 150 scsi0 100G # 调整磁盘为 100G qm set 150 --ipconfig0 ip=192.168.10.150/24,gw=192.168.10.1 // ### 创建多台虚拟机 for i in $(seq 1 9) do echo "# --------------- $i" vmid=16801005${i} qm clone 8001 ${vmid} --name cncicd-5${i} --full true qm resize ${vmid} scsi0 100G # 调整磁盘为 100G qm set ${vmid} --ipconfig0 ip=192.168.10.5${i}/24,gw=192.168.10.1 qm start ${vmid} done
常见问题处理
CentOS 7 Cloud Image
下载地址:CentOS Cloud images
版本选择:我们使用 CentOS-7-x86_64-GenericCloud-XXXX.qcow2 版本镜像
已知问题:
0016948: CentOS 8 cloud images have static 192.168.122.1 nameserver in /etc/resolv.conf
Q:镜像内置的 /etc/resolv.conf 存在默认值,包含 DNS Server 192.168.122.1 或 10.0.2.3 地址,导致初始化后网络访问异常
A:在导入镜像前,删除该文件:virt-sysprep -a CentOS-7-xxx.qcow2 –delete ‘/etc/resolv.conf’
How to fix Centos 7 cloud images · GitHub
Q:在 CentOS 7 中,/etc/ssh/sshd_config 的 UseDNS yes 导致 SSH 登录慢,如何关闭?
A:在导入镜像前,修改该文件:virt-sysprep -a CentOS-7-xxx.qcow2 –edit ‘/etc/ssh/sshd_config:s/^#UseDNS yes/UseDNS no/’
Q:主机名没有被自动设置为虚拟机名称,而是使用 localhost 作为主机名
A:在 Cloud Init 中,配置 DNS Domain 能够解决该问题(但我们没有找到标准的解决方案);