Automate Kubernetes Cluster creation in vSphere

11/26/2020

I want a script/tool/plugin that can be used to automate kubernetes cluster creation in vSphere. I want to create 3 virtual machines, one master and two worker nodes. Is there a way that this creation can be automated? Or is there a way where I won't need to configure every vm individually?

-- gunsNglory
docker
kubernetes
vsphere

1 Answer

11/27/2020

One of the ways to automate vSphrere cluster creation/managing is to use terraform.

There is a module called Terraform vSphere Module - it contains most of the advance features that are available in the resource vsphere_virtual_machine

Terraform vSphere Module:

This Terraform module deploys single or multiple virtual machines of type (Linux/Windows) with following features:

  • Ability to specify Linux or Windows VM customization.
  • Ability to add multiple network cards for the VM Ability to assign tags and custom variables.
  • Ability to configure advance features for the vm.
  • Ability to deploy either a datastore or a datastore cluster. Add extra data disk (up to 15) to the VM. Different datastores for data disks (datastore_id). Different scsi_controllers per disk, including data disks.
  • Ability to define depend on using variable vm_depends_on & tag_depends_on

Note: For module to work it needs number of required variables corresponding to an existing resources in vSphere. Please refer to variable section for the list of required variables.

Example:

# Configure the VMware vSphere Provider
provider "vsphere" {
  user           = "fill"
  password       = "fill"
  vsphere_server = "fill" 

  # if you have a self-signed cert
  allow_unverified_ssl = true
}

# Deploy 2 linux VMs
module "example-server-linuxvm" {
  source        = "Terraform-VMWare-Modules/vm/vsphere"
  version       = "X.X.X"
  vmtemp        = "VM Template Name (Should Alrerady exist)"
  instances     = 2
  vmname        = "example-server-linux"
  vmrp          = "esxi/Resources - or name of a resource pool"
  network = {
    "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]
  }
  vmgateway         = "10.13.113.1"
  dc        = "Datacenter"
  datastore = "Data Store name(use ds_cluster for datastore cluster)"
}

More information and examples:

1. Using Infrastructure as Code to Automate VMware Deployments

2. Building vSphere virtual machines with Terraform

3. How to Install Terraform for Use with vSphere

4. vsphere_virtual_machine terraform module


You can also look into Hashicorp Packer side to use it in spike with Terraform.

Packer is:

A free open-source tool to create images or VMs in multiple platforms (Azure, AWS, Google Cloud, VMware, OpenShift, etc.), perfect to create templates, build the same image in multiple environments or demos

Your preliminary plan would be:

1. Create VM templates with packer (You are here)
2. Create nodes with terraform
3. Create clusters with kubeadm

More information here:

-- Vit
Source: StackOverflow