In this post, I will describe how to use Memory Overcommitment Manager (MOM) to automatically manage memory ballooning on a KVM host.
Memory ballooning is a virtualization feature that allows youto apply a “soft” memory limit to your virtual machines. For example, you can start a virtual machine with 2GB of assigned memory. If, at a later time, you want to limit this virtual machine to 1GB you could use libvirt (
virsh setmem <domain>1048576) or the qemu monitor (
qemu> balloon 1024) to activate the memory balloon.
Memory ballooning can also be used if you want to over-commit memory. This means that you assign more memory to virtual machines than you have available. Memory overcommitment depends on an assumption that all virtual machines do not need 100% of their memory at all times. Unused memory can be shifted around based on the actual memory requirements of each virtual machine by dynamically adjusting the memory balloons of each guest. Obviously we need some automation here. One emerging solution for KVM/libvirt hosts is MOM (Memory Overcommitment Manager). MOM is a Python program that can (among other things) dynamically manage memory ballooning according to a user-defined policy. Many more details about MOM can be found in the wiki.
How to set up MOM
At the time of this writing, the recommended configuration requires the MOM daemon running on the host to connect to a small daemon running in each guest. As you will see, this is a bit tricky to set up. Rest assured that we are working diligently to make it easier in the future!
Install MOM on the host:
- Change to the directory containing the MOM distribution and install it
python setup.py install.
- Copy the sample configuration file
- Copy the ballooning policy file
Perform the following steps to install the mom guest daemon in each virtual machine:
- Install MOM in the same manner as for the host:
python setup.py install
- Arrange for the daemon to be started at boot:
Configure MOM network access: The MOM host daemon talks to each guest daemon over the network to gather statistics about guest memory usage. These statistics help MOM to make better memory ballooning decisions. In order to connect to the guests, MOM relies on a helper script to determine each guest’s IP address. A sample
name-to-ip helper script is provided in the
doc/ directory of the MOM source code. Follow these steps to configure MOM guest network connectivity:
- Edit the
name-to-iphelper script to suit your environment. MOM will pass the script a libvirt domain name and expect the script to print an IP address.
Putting it all together
Now that you have MOM and the guests set up you should be ready to start.
- Start MOM:
momd -c /etc/mom.conf -r /etc/mom.rules
- Start guests
Pay attention to the MOM output. If everything is working you should see messages like ‘Guest <name> is ready’. This means that MOM is communicating with the guests and is ready to manage memory ballooning. If you do not see the ‘ready’ messages, double-check your mom.conf and make sure you added GuestNetworkDaemon to the guest collectors and that name-to-ip points to an executable script that returns the proper IP addresses of your guests. Also make sure that mom-guestd is configured to start automatically when your guests boot.
The MOM Ballooning Policy
If you’ve gotten this far, you’re probably wondering about the memory ballooning policy that MOM uses by default. The principle goal of the default policy is to allow the guests as much memory as possible without adversely affecting the host. MOM watches the host and guests for signs of memory pressure. As long as host memory pressure is low, the guests will not be ballooned. When the amount of claimable memory on the host drops below 20% of total RAM, then MOM will start proactively ballooning guests. The amount of pressure applied to the guests is proportional to the amount of memory pressure seen in the host. Guests that are not using as much memory are ballooned more aggressively. When host memory pressure subsides, MOM restores the guests to their full memory allocation.
MOM is designed to be flexible. If you would prefer a different auto-ballooning algorithm on your host, feel free to edit the balloon.rules file to suit your needs. For a start, try modifying the variables at the top. For example, to start ballooning when there is less host memory pressure, increase
pressure_threshold, To alter the balloon change rate, modify
Hopefully this tutorial has helped you to set up an auto-balloon environment with KVM and libvirt virtualization. I look forward to hearing how it works out for you. We are working hard to better integrate this technology into the core tools and make it easier to use in the future.