PyVmomi for VM Resource Controls

This is the fourth article in my series about using PyVmomi for VM build automation from Linux. In the earlier posts, we connected to vCentre, created a VM, and added SCSI controllers and drives to the VM.

The VMs I needed to create are for a storage benchmarking tool, so I needed to be sure that competition for CPU or RAM was not going to limit the storage performance. I had parameters in the script for the vCPU count and RAM amount. Now I needed to add reservations for the entire RAM footprint and a decent amount of GHz per vCPU. I was rather expecting to have a hard time with setting resource controls on my VMs since apparently simple things like adding disks to SCSI controllers seemed painful. Much to my surprise I could set reservations very easily and setting shares and limits looked easy too. I needed to add Memory and CPU allocation objects into the VM configuration specification before creating the VM. In the example below I set the RAM reservation equal to the configured RAM and CPU reservation to 1.5GHz for each vCPU.

      config = vim.vm.ConfigSpec(name=vm_name, memoryMB=RAM, numCPUs=vCPUs, files=vmx_file, guestId='ubuntu64Guest', version='vmx-09', deviceChange=devices)
      config.memoryAllocation = vim.ResourceAllocationInfo(reservation=RAM)
              ResMHz = vCPUs * 1500
      config.cpuAllocation = vim.ResourceAllocationInfo(reservation=ResMHz)
      #logging.debug("Creating VM " + vm_name)
      task = vm_folder.CreateVM_Task(config=config, pool=resource_pool)
      tasks.wait_for_tasks(service_instance, [task])

Both the MemoryAllocation and CPUAllocation objects have Shares and Limits properties as well as the Reservation property that I am using. Since I was reserving a lot of resources there was no reason for me to use shares or limits, you use case may be different.

I found PyVmomi significantly harder to work with than PowerCLI, mostly because there are far fewer examples to work from. A lot of my time was spent reading the vSphere API and object definitions. It does feel like PyVmomi is aimed more at developers than operations teams and that some formal software development training might have made it all easier. The nice part is that I was able to get everything I wanted working within Python.

© 2018, Alastair. All rights reserved.

About Alastair

I am a professional geek, working in IT Infrastructure. Mostly I help to communicate and educate around the use of current technology and the direction of future technologies.
This entry was posted in General, VMWare. Bookmark the permalink.