Posted on Leave a comment

Setting instance CPU and memory limits

LXD instances can be be limited to the amount of CPU and Memory resources used by the host server. These limits can be set on each individual instance, or through the use of profiles. This how-to guide will create a profile establishing these limits and then will apply that profile to an instance. A single profile can be reused and applied to multiple instances.

We will call the new profile cpu2-memory4, naming it something that can easily identify it’s purpose. We will set the profile to limit the instance to 2 virtual CPUs and also 4 GB of memory. To create the new profile run the following command:

$ lxc profile create cpu2-memory4

We now have an empty profile without any configurations. To get a list of the profiles you have on your LXD server and see the newly created cpu2-memory4 profile, run the following command

$ lxc profile list
+---------------+---------+
|    NAME       | USED BY |
+---------------+---------+
| default       | 2       |
+---------------+---------+
| cpu2-memory4  | 0       |
+---------------+---------+

The new profile now can be modified to limit the CPU and memory usage. We can choose to edit the profile directly using an editor (lxc profile edit cpu2-memory4) or set limits using LXC based command-line options.

Choosing the later option, we can set both configuration limits at the same time with a single command. Let’s set the CPU limit to 2 virtual CPUs and the memory limit to 4GB of RAM by running the following command:

$ lxc profile set cpu2-memory4 limits.cpu=2 limits.memory=4GB

To view the contents of the profile use the following command:

$ lxc profile show cpu2-memory4
config:
  limits.cpu: "2"
  limits.memory: 4GB
description: ""
devices: {}
name: cpu2-memory4
used_by: []

If you are launching a new instance you can use the –-profile (or -p) option to add profiles to the instance as it is created. But if the instance already exists you will have to append the new profile to the instance. Let’s assume we have an instance named vm1. To append the cpu2-memory4 profile to this instance use the following command:

$ lxc profile add vm1 cpu2-memory4

To show the applied configuration to your instance use the following command:

$ lxc config show vm1 -e

The profile configuration can be removed from the instance. To remove the profile use the following command:

$ lxc profile remove vm1 cpu2-memory4

When you remove the profile from the instance, the profile still exists and can be used for other instances. If you want to permanently remove the profile it can be deleted with the following command:

$ lxc profile delete cpu2-memory4
Leave a Reply

Your email address will not be published. Required fields are marked *