VMware Cloud Community
Mang201110141
Contributor
Contributor

Set CPU & Memory Limit for All VMs in Cluster using PowerCLI

Hello Experts,

I am new to PowerCLI, and looking for script where i can set the CPU & Memory Limit for all VMs in Cluster using PowerCLI.

For single VM i can set it using below command, however i would like to set the same amount of CPU memory allocated to the VM (If the VM has 2GB RAM & 1 CPU - Then it should set CPU limit as 1000 & Memory limit as 2048 for that VM)

Get-VMResourceConfiguration <VM-Name> | Where-Object {$_.MemLimitMB -eq '-1'} | Set-VMResourceConfiguration -MemLimitMB 2048 -CpuLimitMhz 1000

Can this be done using one liner or script?

-M

0 Kudos
13 Replies
LucD
Leadership
Leadership

You could do something like this

Get-Cluster -Name MyCLuster | Get-VM | `

   Get-VMResourceConfiguration  | Where-Object  {$_.MemLimitMB -eq '-1'} | `

   Set-VMResourceConfiguration -MemLimitMB 2048  -CpuLimitMhz 1000

You get all guests from the cluster and 'pipe' each guest to the Get-VMResourceConfiguration cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Mang201110141
Contributor
Contributor

Thanks for your response.

If i run this command it will set the same limit on all the VMs, but i have VMs with different configuration (Some are with 2 CPU's & 8GB RAM)

My requirement is to have same CPU & Memory limit set which is allocated to each VM.

For example - I have 2 VMs in cluster (VM1 & VM2) VM1 has 2CPU & 4GB RAM and VM2 has 1CPU & 8GB RAM. I am looking for the script which will set the Resource Limit on VM same as resource allocated to it.

-M

0 Kudos
a_p_
Leadership
Leadership

Welcome to the Community,

please don't mind me asking. What is the reason you want to set the limits to the allocate resources? This does not make any sense to me. I could understand if you would like to set the reservation, however the limit usually is only useful when you need a lower value than the configured/allocated ones.

André

0 Kudos
Mang201110141
Contributor
Contributor

You may be right, however I heard that VM can utilize more resource if required then whatever is allocated.

Hence i would like to Limit the VM with the allocated resource, so it won't take extra resource from the host.

-M

0 Kudos
a_p_
Leadership
Leadership

Are you sure you don't mix this up with the resource allocation/limit for a Resource Pool?

André

0 Kudos
Mang201110141
Contributor
Contributor

Yes, I am talking about resource limitation on VM and not about resource pool.

Do you know of any script which can do this on entire cluster? I want to perform this on around 1500 VMs in cluster.

Thanks,

-M

0 Kudos
avlieshout
VMware Employee
VMware Employee

Only a madman would like to do that!

Just don't, doesn't make sense at all!

A vm can never use more resources than its configured values.

Setting memory limits in production is a bad practise. Use it only for debugging an testing situations where you want to force ballooning and/or swapping.

Arnim

Sent from my iPad

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
Mang201110141
Contributor
Contributor

Yes, this is test environment. Below VMs are configured with 8192 MB Memory, however it is taking more memory, Am i missing something?

I would like to limit the VM so that it would only consume the memory which is allocated to it.

Memory_Usage more than allocated.JPG

0 Kudos
avlieshout
VMware Employee
VMware Employee

There is always some memory overhead from the virtual machine monitor. That's why you see more hostmemory usage than configured memory

Looking at your figure I conclude that your vms ARE using all of its configured memory, or at least there was a point in time where they were using it.

Must be (have been) memory intensive workloads in there.

Arnim

Sent from my iPad

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
a_p_
Leadership
Leadership

Like avlieshout said, the "Host Memory Usage" equals the "Memory Size" plus the "Memory Overhead" which depends on several configuration settings of the VM (number of vCPUs, assigned memory, ...). The Memory Overhead is shown on the General tab of the VM.

It's interesting thet all your VMs seem to allocate the full memory size. To me this looks like you either set the reservation to the full size or you don't have VMware tools installed on the VM's!? I hardly can believe that all the configured memory is consumed by applications/processes running in the VM (except for a fully loaded terminal server).

Maybe an interesting documentation to read, if you not do already did is http://www.vmware.com/files/pdf/techpaper/vsp_41_perf_memory_mgmt.pdf

André

0 Kudos
Mang201110141
Contributor
Contributor

Thanks for the information..

Like i said, this is test environment and i am running 1500 VMs in cluster. Its difficult to set reservation on all VMs. Right now my cluster is running out of Memory.

What do you recommend to reduce the Memory load on the ESX?

I think Limiting the VM will reduce the Memory utilization, however there will be performance impact - which is fine for now - Any thoughts on this?

VMware tools is installed on these VMs (There are other VMs which doesn't have VMware tools installed, however those are in different cluster)

-M

0 Kudos
a_p_
Leadership
Leadership

Its difficult to set reservation on all VMs...

Don't get me wrong, I don't want you to set the reservation. IMO there's already a reservation set on these VM's which should be removed.

Are the VMware tools installed with default options, i.e. with the ballooning active?

André

0 Kudos
Mang201110141
Contributor
Contributor

There is no reservation set on any of the VMs in cluster, and VMware tools is installed with default options (NO Change)

-M

0 Kudos