3 min read

利用Cloud-Init快速创建Debian-11

利用Cloud-Init快速创建Debian-11

前言

有时候需要多个节点环境来测试或验证,那么就可以结合 Cloud-init工具来快速实现。

环境

  • OS: Proxmox-VE 7.2
  • 虚拟机系统: debian-11

安装工具包

安装镜像编辑工具包

# apt-get install libguestfs-tools

下载镜像

# mkdir cloud_img && cd cloud_img
# wget https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-nocloud-amd64.qcow2

更多下载地址: https://cloud.debian.org/images/cloud/bullseye/latest/

配置系统镜像

export IMAGE_NAME=debian-11-nocloud-amd64.qcow2

cat>sources.list<<EOF
deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
EOF

virt-customize --hostname "debian-11-tpl.vqiu.local" \
               --ssh-inject root:file:/root/.ssh/id_rsa.pub  \
               --root-password password:Passw@rd \
               --upload ./sources.list:/etc/apt/sources.list \
               --install cloud-init,htop,vim,qemu-guest-agent,curl,wget,bash-completion \
               --timezone "Asia/Shanghai" \
               --run-command "systemctl stop postfix rpcbind.socket"  \
               --firstboot-command "dpkg-reconfigure openssh-server" \
               --delete '/var/log/*.log'. -a ${IMAGE_NAME}

创建虚拟机模板

export VM_ID=9000
export IMAGE_NAME=debian-11-nocloud-amd64.qcow2
qm create ${VM_ID} --cores 2 --memory 2048 --net0 virtio,bridge=vmbr0

将下载的qcow2镜像文件导入至 Local-lvm

qm importdisk ${VM_ID} ${IMAGE_NAME} local-lvm

添加刚导入的系统盘

qm set ${VM_ID} --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-${VM_ID}-disk-0

将系统盘扩容到50G,当前为2G

qm resize ${VM_ID} scsi0 +48G

添加cloudinit设备

qm set ${VM_ID} --ide2 local-lvm:cloudinit

设置第一启动设备为硬盘

qm set ${VM_ID} --boot c --bootdisk scsi0

设置显示设备

qm set ${VM_ID} --serial0 socket --vga serial0

转换成模板

qm template ${VM_ID}

启用qemu-agent,默认不启用

qm set ${VM_ID} --agent enabled=1

以上操作可以通过以下脚本来实现

#!/usr/bin/env bash

export VM_ID=9000
export IMAGE=debian-11-nocloud-amd64.qcow2
export CPU=2
export MEMORY=2048

qm create ${VM_ID} --cores ${CPU} --memory ${MEMORY} --net0 virtio,bridge=vmbr0
qm importdisk ${VM_ID} ${IMAGE} local-lvm
qm set ${VM_ID} --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-${VM_ID}-disk-0
qm set ${VM_ID} --ide2 local-lvm:cloudinit
qm set ${VM_ID} --boot c --bootdisk scsi0
qm set ${VM_ID} --serial0 socket --vga serial0
qm set ${VM_ID} --agent enabled=1
qm template ${VM_ID}

# 系统盘扩容,在原来的基础上新增42G
qm resize ${VM_ID} scsi0 +48G

通过模版部署一台虚拟机

export VM_ID=113
export VM_NAME=master-1
export IP_ADDR=172.30.109.201/24
export IP_GW=172.30.109.1
export DNS=114.114.114.114,114.114.115.115
export CPU_CORE=2
export MEM_SIZE=4096

qm clone 9000 $VM_ID --name $VM_NAME --full
qm set $VM_ID   --cores $CPU_CORE --memory $MEM_SIZE
qm set $VM_ID --ipconfig0 ip=${IP_ADDR},gw=${IP_GW}
qm set $VM_ID --nameserver $DNS
#qm set $VM_ID --searchdomain vqiu.local

qm set $VM_ID --sshkey ~/.ssh/id_rsa.pub

qm start $VM_ID
ssh -i ~/.ssh/id_rsa root@$VM_ID

提示:配置IP地址,机器必须要关机状态,重新开机才生效。

批量创建

#!/usr/bin/env bash
VM_ID_LIST=(
    131
    132
    133
)

for id in ${VM_ID_LIST[@]}
do
    qm clone 9000 $id --name vm-$id
    qm set $id --cores 2 --memory 4096
    qm set $id --ipconfig0 ip=172.30.109.${id}/24,gw=172.30.109.1
    qm set $id --nameserver 172.30.109.1
    
    qm set $id --sshkey ~/.ssh/id_rsa.pub

    qm start $id
done

附录

1、如何通过后台更改系统密码?

# qm guest passwd <ID> <USER>

提示:debian11默认密码为空,修改密码前提需要安装了qemu-guest-agent

2、如何查看系统已分配的磁盘?

# pvesm list local-lvm

3、如何为某VM新增1块100G数据盘?

# pvesm alloc local-lvm $VM_ID '' 100G
# qm set $VM_ID --scsi11 local-lvm:vm-$VM_ID-disk-1,discard=on
# qm config $VM_ID | grep scsi1

4、如何使用命令为虚机创建快照?

# qm snapshot 111 ready_install_k8s

参考引用