使用terraform管理vSphere并创建虚拟机

介绍

terraform支持非常多的插件,之前介绍了k8s的,这篇介绍下VMware vSphere的插件。通过这个插件可以管理vSphere的虚拟机,网络,存储等。官方文档地址:
https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs

安装provider

之前的文章中介绍了terraform的安装方式,这里略。如果机器有外网,在init时会自动安装,下面是离线安装方式:

wget https://releases.hashicorp.com/terraform-provider-vsphere/2.4.2/terraform-provider-vsphere_2.4.2_linux_amd64.zip
mkdir -p ~/.terraform.d/plugins/local/hashicorp/vsphere/2.4.2/linux_amd64
unzip terraform-provider-vsphere_2.4.2_linux_amd64.zip -d ~/.terraform.d/plugins/local/hashicorp/vsphere/2.4.2/linux_amd64

创建虚拟机

创建目录,准备terraform文件。

mkdir -p /root/terraform/vsphere

versions.tf

该文件是指定vsphere provider的路径和版本。路径对应上面离线安装的目录。

terraform {
  required_providers {
    vsphere = {
      source = "local/hashicorp/vsphere"
      version = "2.4.2"
    }
  }
}

terraform.tfvars

该文件包括了vsphere的连接信息,虚拟机的配置信息,exsi主机信息等。

vsphere_vcenter = "1.2.3.4"
vsphere_user = "admin@admin.cn"
vsphere_password = "Root@123"
vsphere_unverified_ssl = "true"

vsphere_datacenter = "Datacenter"
vsphere_resource_pool = "pool"
vsphere_vm_template = "Centos7.9"
vsphere_cluster = "vSAN"
vsphere_datastore = "vsanDatastore"
vsphere_port_group = "Group"
vsphere_ipv4_address = "192.168.1.1"
vsphere_ipv4_netmask = "24"
vsphere_ipv4_gateway = "192.168.1.254"
vsphere_time_zone = "Asia/Shanghai"

k8s_node_name = "k8s-node0"
k8s_node_cpu = "16"
k8s_node_memory = "32768"
k8s_node_count = "1"

esxi_hosts = {
"0" = "192.168.1.4"
"1" = "192.168.1.5"
"2" = "192.168.1.6"
"3" = "192.168.1.7"
"4" = "192.168.1.8"
}

datasource.tf

该文件定义了vsphere的连接信息变量。

data "vsphere_datacenter" "datacenter" {
  name = "${var.vsphere_datacenter}"
}

data "vsphere_host" "hosts" {
  count         = "${length(var.esxi_hosts)}"
  name          = "${var.esxi_hosts[count.index]}"
  datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}

data "vsphere_resource_pool" "resource_pool" {
  name          = "${var.vsphere_resource_pool}"
  datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}

data "vsphere_virtual_machine" "template" {
  name          = "${var.vsphere_vm_template}"
  datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}

data "vsphere_datastore" "datastore" {
  name          = "${var.vsphere_datastore}"
  datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}

data "vsphere_network" "network" {
  name          = "${var.vsphere_port_group}"
  datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}

variables.tf

该文件定义了虚拟机的配置信息变量。

variable "vsphere_user" {
        description = "vSphere user name"
}

variable "vsphere_password" {
        description = "vSphere password"
}

variable "vsphere_vcenter" {
        description = "vCenter server FQDN or IP"
}

variable "vsphere_unverified_ssl" {
        description = "Is the vCenter using a self signed certificate (true/false)"
}

variable "vsphere_datacenter" {
        description = "In which datacenter the VM will be deployed"
}

variable "vsphere_vm_template" {
        description = "Where is the VM template located"
}

variable "vsphere_cluster" {
        description = "In which cluster the VM will be deployed"
}

variable "vsphere_resource_pool" {
        description = "Resource Pool"
}

variable "vsphere_vcpu_number" {
        description = "How many vCPU will be assigned to the VM (default: 1)"
        default = "1"
}

variable "vsphere_memory_size" {
        description = "How much RAM will be assigned to the VM (default: 1024)"
        default = "1024"
}

variable "vsphere_datastore" {
        description = "What is the name of the VM datastore"
}

variable "vsphere_port_group" {
        description = "In which port group the VM NIC will be configured (default: VM Network)"
        default = "OpsGroup"
}

variable "vsphere_ipv4_address" {
        description = "What is the IPv4 address of the VM"
}

variable "vsphere_ipv4_netmask" {
        description = "What is the IPv4 netmask of the VM (default: 24)"
        default = "24"
}

variable "vsphere_ipv4_gateway" {
        description = "What is the IPv4 gateway of the VM"
}

variable "vsphere_time_zone" {
        description = "What is the timezone of the VM (default: UTC)"
}

variable "k8s_node_name" {
        description = "k8s node name"
}

variable "k8s_node_cpu" {
        description = "k8s node cpu"
}

variable "k8s_node_memory" {
        description = "k8s node memory"
}

variable "k8s_node_count" {
        description = "k8s node count"
}

variable "esxi_hosts" {
        description = "ESXi HOSTs"
}

k8s.tf

该文件定义了provider信息,虚拟机配置信息,包括名字为test,cpu,内存,磁盘为200G,网络,主机名等。

provider "vsphere" {
  vsphere_server = "${var.vsphere_vcenter}"
  user = "${var.vsphere_user}"
  password = "${var.vsphere_password}"
  allow_unverified_ssl = "${var.vsphere_unverified_ssl}"
}

resource "vsphere_virtual_machine" "test" {
  count            = "${var.k8s_node_count}"
  name             = "test"
  resource_pool_id = "${data.vsphere_resource_pool.resource_pool.id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus = "${var.k8s_node_cpu}"
  memory   = "${var.k8s_node_memory}"
  guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
  enable_disk_uuid = "true"

  network_interface {
    network_id   = "${data.vsphere_network.network.id}"
  }

  disk {
    label = "disk0"
    size = "200"
  }

  clone {
    template_uuid = "${data.vsphere_virtual_machine.template.id}"

    customize {
      linux_options {
        host_name = "${var.k8s_node_name}2"
        domain    = "localhost"
        time_zone = "${var.vsphere_time_zone}"
      }

      network_interface {
        ipv4_address = "${var.vsphere_ipv4_address}"
        ipv4_netmask = "${var.vsphere_ipv4_netmask}"
      }

      ipv4_gateway    = "${var.vsphere_ipv4_gateway}"
    }
  }
}

init

terraform init

file

plan

terraform plan

虚拟机的配置信息:

file

克隆模板id,自定义的虚拟机网络信息,磁盘信息:

file

file

apply

terraform plan

虚拟机创建成功。

file

file

登录虚拟机,查看主机名,cpu,内存,磁盘信息符合上述配置。

file

0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部
0
希望看到您的想法,请您发表评论x