美文网首页
Proxmox Centos cloud-init 模板制作

Proxmox Centos cloud-init 模板制作

作者: SmithCruise | 来源:发表于2020-05-08 12:38 被阅读0次

    原文地址:https://www.inlighting.org/archives/make-proxmox-centos-cloud-init-template

    因为自己使用 Proxmox 进行开发,需要频繁的创建删除虚拟机,所以制作 cloud-init 模板来方便虚拟机的创建。

    这里我没有自己在原系统上面安装 cloud-init 进行操作,而是直接使用了官方提供的 cloud image。下面的教程我以 Centos 为例,Proxmox 为全新安装的 v6.1-7 版本。

    更换源-可忽略

    从官方下载的 Promox 的 iso 文件不是最新版的,还可以通过 apt 命令进行升级,但是因为默认的源在国外,比较慢,这里换成了国内源。

    修改 proxmox 源

    我这里直接把 /etc/apt/sources.list.d/pve-enterprise.list 修改为如下内容:

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

    修改 debian 源

    修改 /etc/apt/sources.list 为如下内容:

    deb http://mirrors.aliyun.com/debian buster main contrib
    
    deb http://mirrors.aliyun.com/debian buster-updates main contrib
    
    # security updates
    deb http://mirrors.aliyun.com/debian-security buster/updates main contrib
    

    系统默认只有 vi,我用 vi 的时候存在键盘标准不符,打字有问题。我就先用 apt updateapt install vim 安装了一个 vim 编辑器。

    执行更新

    apt update
    apt update&&apt dist-upgrade #如需升级pve,则执行该命令
    

    或者你在 web 界面更新也可以。

    挂载第二块硬盘-可忽略

    使用 fdisk 工具分区,我的第二块硬盘路径为 /dev/sdb

    sudo fdisk /dev/sdb
    p  # 输入p查看信息
    g  # 新建gpt分区
    n  # 新建分区表,然后一直回车(默认值)即可
    w  # 保存并退出
    

    格式化新的分区表

    sudo mkfs -t ext4 /dev/sdb1
    

    挂载分区表

    mkdir /mnt/sdb  # 创建一个目录用于挂载
    mount /dev/sdb1 /mnt/sdb  # 将/dev/sdb1挂载至/mnt/sdb
    

    添加自动挂载配置,防止重启失效

    sudo vi /etc/fstab
    # 添加一行
    /dev/sdb1 /mnt/sdb ext4 defaults 0 0
    

    之后在 Proxmox web界面添加存储,选择 Datacenter -> Storage -> Add -> Dictionary ,输入相关信息即可完成添加。

    Cloud image 下载

    这里我是以 Centos 为例,但是实际上 Ubuntu 这类系统也提供了 Cloud image。

    首先去 Centos 官方网站下载它的 cloud image,网址为:https://cloud.centos.org/

    我们下载名称附带 GenericCloud 的模板就行了,后缀为 qcow2,因为 Proxmox 的模板和 OpenStack 是通用的。例如这里我下载 Centos 8 的 Cloud image,它的名称为 CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2

    它的下载地址为:https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2

    Cloud-init 模板制作

    首先我们将下载好的 qcow2 文件上传到 Proxmox 中,这里我就直接上传到了 ~/images 目录下。

    下面推荐使用命令行的方式操作,使用 Proxmox 的 web 面板操作会有点问题。

    这里的 VM_ID 设置为 9000 ,大点,不和创建机器时默认的 VM_ID 冲突。

    创建虚拟机

    qm create 9000 --memory 2048 --name centos-8-template --net0 virtio,bridge=vmbr0

    导入下载的镜像到 local-lvm 存储空间

    qm importdisk 9000 images/CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2 local-lvm

    将导入的磁盘以 scsi 方式挂载到虚拟机上面

    qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0

    添加 Cloud-Init CDROM 驱动

    英文注释仅供参考,直接抄命令即可。

    # The next step is to configure a CDROM drive which will be used to pass the Cloud-Init data to the VM.
    qm set 9000 --ide2 local-lvm:cloudinit
    # To be able to boot directly from the Cloud-Init image, set the bootdisk parameter to scsi0, and restrict BIOS to boot from disk only. This will speed up booting, because VM BIOS skips the testing for a bootable CDROM.
    qm set 9000 --boot c --bootdisk scsi0
    # Also configure a serial console and use it as a display. Many Cloud-Init images rely on this, as it is an requirement for OpenStack images.
    qm set 9000 --serial0 socket --vga serial0
    

    制作模板

    制作成模板可以进行 linked clone,这样比 full clone 更快,当然推荐在制作模板前 clone 测试下,不然 template 后就不能修改了。

    qm template 9000
    

    正常使用
    最后 clone 模板,修改下虚拟机配置和 cloud-init 的参数就可以开机了。

    相关文章

      网友评论

          本文标题:Proxmox Centos cloud-init 模板制作

          本文链接:https://www.haomeiwen.com/subject/jbqfnhtx.html