使用 Pulumi 创建 pve 平台的虚拟机

基础设施即代码 (Infrastructure as Code)简称 IaC,主要是通过代码的方式管理云平台资源,比如 AWS 的 vpc ec2 ecs eks elb 等等。Iac 基本上分为两种:

  • 第一种:使用声明式的编程语言进行编写,比如 yaml,Json,TCL 等等
  • 第二种:使用编程语言进行编写,比如:Python,Golang,NodeJs 等等

Pulumi 属于第二种,使用编程语言进行编写,本文将介绍如何使用 pulumi 创建 pve 平台的虚拟机。

在创建虚拟机之前需要三部:

  1. 安装依赖,本次使用需要准备:Python、Pulumi、pve 虚拟化平台的环境
  2. 选择 pulumi 的存储方式,本次将使用本地存储,执行命令 pulumi login --local
  3. 初始化项目:执行 pulumi new 然后选择一个名为 Python 的模板,按照引导走完全部过程。
  4. 创建相对应的配置。
  5. 编写 python 代码,并启动。

依赖安装

点击链接下载 pulumi 的对应安装包。

点击链接安装 Python 环境。

执行命令安装 pve 对应的 pulumi 包:

pip install pulumi_proxmoxve

配置 pulumi

每个 pulumi 的库,都有一个默认的配置参数,点击此链接可以看到完成的参数列表,创建 pve 中的虚拟机时,我们只需要注意以下部分:

    "namespaces": {
        "proxmoxve": "ProxmoxVE"
        },
    "config": {
        "variables": {
            "endpoint": {
                "type": "string",
                "description": "The endpoint for the Proxmox VE API.\n"
            },
            "insecure": {
                "type": "boolean",
                "description": "Whether to skip the TLS verification step.\n"
            },
            "password": {
                "type": "string",
                "description": "The password for the Proxmox VE API.\n",
                "secret": true
            },
            "username": {
                "type": "string",
                "description": "The username for the Proxmox VE API.\n"
            }
        }
    },

这些参数代表了以下几个参数:

  • namespaces: 配置参数的命名空间
  • endpoint:pve 的登录地址,一般是 http://x.x.x.x:8006
  • insecure: 是否要跳过 https 验证
  • password: endpoint 的登录密码
  • username: endpoint 的登录用户

了解过之后执行命令进行配置:

pulumi config set proxmoxve:endpoint http://x.x.x.x:8006
pulumi config set proxmoxve:insecure true
pulumi config set proxmoxve:password xxxxx
pulumi config set proxmoxve:username xxxxx

在配置之后就需要编写代码了。

Python 代码编写

在 pulumi 的项目中 __main__.py 文件修改为如下内容:

import pulumi

import pulumi_proxmoxve as proxmox

virtual_machine = proxmox.vm.VirtualMachine(
    resource_name="vm", ##资源名称
    node_name="pve1",   ## pve 的节点名称
    bios="seabios",  
    cpu=proxmox.vm.VirtualMachineCpuArgs(  
        cores=1,  ##虚拟机 CPU
        sockets=1
    ),
    clone=proxmox.vm.VirtualMachineCloneArgs( ##克隆的虚拟机的相关信息
        node_name="pve1", 
        vm_id=9000,
        full=True
    ),
    disks=[
        proxmox.vm.VirtualMachineDiskArgs(
            interface="scsi0",
            datastore_id="local-lvm",  ##存储名称
            size=32,  ##大小 
            file_format="qcow2"
        )
    ],
    memory=proxmox.vm.VirtualMachineMemoryArgs(
        dedicated=1024  ##虚拟机内存
    ), 
    name="proxmox-vm",  ##虚拟机名称
    network_devices=[
        proxmox.vm.VirtualMachineNetworkDeviceArgs(
            bridge="vmbr0", ##网卡名称
            model="virtio"
        )
    ],
    on_boot=True,
    operating_system=proxmox.vm.VirtualMachineOperatingSystemArgs(type="l26"),
    initialization=proxmox.vm.VirtualMachineInitializationArgs( ## 网络相关配置
        type="nocloud",
        datastore_id="local-lvm",
        dns=proxmox.vm.VirtualMachineInitializationDnsArgs(
            domain="example.com",
            server="1.1.1.1 1.0.0.1"
        ),
        ip_configs=[
            proxmox.vm.VirtualMachineInitializationIpConfigArgs(
                ipv4=proxmox.vm.VirtualMachineInitializationIpConfigIpv4Args(
                    address="10.0.0.10/24",
                    gateway="10.0.0.1"
                )
            )
        ]
    )
)

修改以上注释的部分,然后执行 pulumi up -y 命令即可启动程序,最终会在 PVE 平台中发现新创建的 proxmox-vm 的虚拟机。

后记

如果想使用镜像镜像进行创建需要添加 VirtualMachineCdromArgs 字段,可以参考此链接,另外需要注意的是 file_id 的内容格式为:local:iso/*.iso,可以在平台网页使用 F12 来进行查看相关镜像的 id。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启明真纳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值