OpenVZ: Setting Up OpenVZ
Introduction
OpenVZ is an operating system-level virtualization technology based on the Linux kernel. OpenVZ allows a physical server to run multiple isolated instances of operating systems, known as Virtual Private Servers (VPS) or Virtual Environments (VE).
Compared to virtual machines like VMware and paravirtualization technologies like Xen, OpenVZ offers less flexibility in the choice of operating system: both guest and host operating systems must be Linux (although different Linux distributions can be used in different VEs). However, OpenVZ’s OS-level virtualization offers better performance, better scalability, higher density, better dynamic resource management, and easier administration than its alternatives. According to the OpenVZ website, this virtualization method introduces a very low performance penalty: only 1 to 3% loss compared to a physical computer.
OpenVZ is the basis of Virtuozzo, a proprietary product provided by SWsoft, Inc. OpenVZ is distributed under the GNU General Public License version 2.
OpenVZ includes the Linux kernel and a set of user commands.
Installation
Installing OpenVZ on Debian is straightforward:
|
|
We also need to edit the sysctl file to add these settings:
|
|
And activate what we need:
|
|
Configuration
Preparation of Environments
You can create your environments in two ways:
- Using debootstrap
- Using templates
Both solutions are valid. The template method is faster as the template is already on your machine.
Personally, I used templates for a long time. More recently, I had to migrate my server to Squeeze (which was in testing at the time) and no templates existed. So I opted for the debootstrap version.
Debootstrap
Creating a VE with debootstrap is very simple. Here’s the list of available distributions:
|
|
For my part, I chose Squeeze:
|
|
- –arch: Choose the desired architecture, here amd64.
- squeeze: The name of the desired distribution
- /mnt/containers/private/101: The location where the VE should be placed (I’m using the VE ID here)
Template
Instead of creating our template, we can download pre-existing ones from: http://wiki.openvz.org/Download/template/precreated and place them in the right location. I modified the file /etc/vz/vz.conf
to change the default paths. It now points to /mnt/containers
.
|
|
Networking
NAT Mode
If you need to have multiple VEs available from a single IP and you can’t have other directly accessible IPs, you’ll need to use NAT mode. This is especially the case if you have a Dedibox (Illiad) or a Kimsufi server (OVH) where only one WAN IP is available (by default) for a given server and it’s impossible to use bridge mode.
Let’s start by configuring the network card:
|
|
So far, everything is standard. However, after creating a VE, we’ll need masquerading to provide internet access from the VEs and PREROUTING for port redirection. We’ll use IPtables to provide external access from VEs and port redirections from the outside to the VEs:
|
|
Here I have ports 80 and 443 redirected to one machine (Ed).
With a Bridged Interface
The major advantage of the venet interface is that it allows the administrator of the physical server to decide on the network configuration of the virtual machine. This is particularly appreciated for a hosting provider who wants to sell a virtual machine hosting service, as they can freely let their customers manage their OpenVZ virtual server while being certain that they cannot disrupt the network, allocate more IP addresses than planned, modify routing tables, etc. If you’re using OpenVZ to provide hosting services to your customers, the veth interface is not for you. Since I’m not in this situation, the potential security issues posed by the veth interface for a hosting provider don’t concern me.
I use both venet and veth on the servers I manage: veth when I need to have access to ethernet layers from virtual servers, and venet in all other cases. But, unlike the simplicity offered by venet interfaces, to be able to use a veth interface in a virtual server, you need to perform some additional configuration operations to prepare the physical server to receive virtual servers using veth. That’s what I’ll describe in the following sections.
First, let’s install the necessary tools:
|
|
Then create a typical configuration:
|
|
Now reboot the server.
For OpenVZ to dynamically add or remove a veth interface to the bridge when starting a virtual server, we need to create some files on the physical server. This operation needs to be done once for all on the physical server, regardless of the number of virtual servers that will be created later. It’s also possible to do it on a server that, in the end, hosts only virtual servers using venet interfaces - it doesn’t disturb them at all.
First, create the /etc/vz/vznet.conf
file with the following content:
|
|
Then create the file /usr/sbin/vznetaddbr
with the following content:
|
|
For information, I modified the line containing “bridge=” with my interface vmbr0.
You also need to set the execute permission on this file by typing:
|
|
Now let’s configure the VE (see also how to manage a VE below):
|
|
Finally edit your machine’s configuration and add the bridge interface for it:
|
|
The end of the configuration file should look like this:
|
|
Now you just need to configure the eth0 interface as you normally would in your VE.
With VLANs
You may need to create VLANs in your VEs. This works very well with a bridged interface. To do this, on the host machine, you must have a configured VLAN (use this documentation for setup). For those who still want an example:
|
|
This example is made with a bridged interface because I have KVM running on it, but there’s nothing forcing it to be bridged.
Then, when you create your VE, you don’t have to do anything special when creating the network interface for your VE. Launch the creation of your VE and don’t forget to install the “vlan” package to be able to create VLAN access within your VE. Here’s another example to give you an idea of the VE network configuration:
|
|
For the VE configuration, it’s almost identical to the host machine configuration - you need to create a VLAN interface on the main interface (again, there’s no need to have the main interface configured, just the VLAN is enough). For those who are still skeptical, here’s an example of configuration in a VE:
|
|
With Bonding
You may need to create bridged bonding in your VEs. To do this, on the host machine, you must have a configured bonding (use this documentation for setup). For those who still want an example:
|
|
NFS
Here I will only cover the client side in a VE, not a server in a VE.
Server
First on the server side, set up your NFS server by following this documentation.
Then we’ll install this, which will allow us to use not only the NFS v3 protocol, but also to abstract the kernel layer (useful in case of a crash):
|
|
Client
- On the host machine (HN), add these lines to sysctl.conf:
|
|
Then apply all this on the fly:
|
|
Now let’s activate NFS on the VEs that interest us:
|
|
I don’t know if this step is essential, but just in case, I’ll add it anyway:
|
|
Then you can mount your NFS mount points normally. However, you may encounter some permission issues. That’s why I recommend adding the ’no_root_squash’ option in the exports file on the server:
|
|
And on the client, add the nolock option to mount the NFS:
|
|
Mount Bind
Mount binds can sometimes be very useful. Here’s how to do them:
Mount script
Create a script in /etc/vz/conf/vps.mount
for all VEs or /etc/vz/conf/CTID.mount
for a specific VE (replace CTID with the VE number):
|
|
And adapt the last line to your needs.
Unmount script
And finally the same for the unmounting script, so /etc/vz/conf/vps.umount
or /etc/vz/conf/CTID.umount
:
|
|
Finally apply the necessary permissions to make it executable:
|
|
Management
Create a VE
Choose the method you want depending on the VM creation method you prefer (template or debootstrap).
Template
To create a container, use this command:
|
|
If you want to configure your interfaces in bridge mode, don’t forget this part.
Debootstrap
At the time of writing, Squeeze is not yet the stable version, but has just frozen to become stable (today itself). So there are a few small things that may vary, like this that we need to create:
|
|
We’re copying the default parameters of a VE to a new name that it will be able to take by default. Let’s configure it:
|
|
Then we add some additional lines needed in our configuration file:
|
|
Then we configure the network parameters:
|
|
Next we’ll remove udev, which can prevent the VM from booting:
|
|
Then we’ll start the VE and enter it:
|
|
Configure your source.list file:
|
|
Then run these commands to remove the superfluous and remove the gettys (VEs don’t use them):
|
|
We solve the mtab problem:
|
|
Start a VE
Now you can start your VE:
|
|
Change the root password of a VE
If you want to change the root password:
|
|
List VEs
To list your VEs, use the vzlist command:
|
|
Stop a VE
To stop your VE:
|
|
Restart a VE
To restart a VE:
|
|
Destroy a VE
To permanently delete a VE:
|
|
Enter a VE
|
|
VE Limits
By default there are limits that VEs cannot exceed. We’ll see how to manage them here. If you still want to go further, know that everything is explained on the official website http://wiki.openvz.org/User_beancounters.
List the limits
To list the limits, we’ll execute this command:
|
|
The last line should always be 0, otherwise you’ve clearly reached the limits.
Here’s some important information:
- held: Currently the value of the resource
- maxheld: the maximum value the resource has reached
- barrier: the soft limit (warning). It means that the held value has already been up to this value
- limit: the hard limit. The held value will never exceed this value.
To increase them, you have two solutions:
- You can do it on the fly.
- Go to
/etc/vz/conf/101.conf
and increase the values that are causing problems. Unfortunately, this requires a restart of the VE for these parameters to be taken into account.
It’s possible that the failcnt isn’t reset to 0… don’t worry, it will happen later.
Maximize the limits
If you have too many restrictions compared to your needs or you feel that the VE you’re going to create will be heavy, maximize this from the start:
|
|
- 2: change this number to the number of VMs you want to run on your machine and it will take care of calculating the limits for your VE in the best way
A file /etc/vz/conf/ve-max-limits.conf-sample is created. You can edit it at any time and make the modifications you want. If you want to apply it to a VE:
|
|
VE 101 now has a new configuration via the ‘max-limits’ config.
Apply limits on the fly
For example, if you want to change the RAM size on the fly:
|
|
- 786432: corresponding to the soft limit (barrier)
- 1048576: corresponding to the hard limit (limit)
Thanks to ‘setmod restart’, I can now apply limits on the fly.
Disk quotas
You may have set up disk quotas and you’re reaching 100% of your VE disk. No problem, we’ll increase the quota on the fly:
|
|
If you don’t want a quota, add this line to the VE configuration:
|
|
Limit the CPU
If you want to limit the CPU, there are several ways to do it. Either you play with the scheduler, or you apply a percentage. For basic uses, the second solution will suit you:
|
|
For example, here I limited my VE 102 to 80% of CPU. For more info: http://wiki.openvz.org/Resource_shortage
Disable the most restrictive limits
If you have a lot of load on a VE, you’ll regularly need to increase certain parameters, which can quickly become tiring. To be at ease with some of them, you can simply disable the limits by entering the maximum value:
|
|
Validation of limits
If you want to validate your limits to make sure everything is ok:
|
|
Automatic validation of limits
If, for example, you want to validate your machine’s resource usage in the best way and also follow OpenVZ best practices, you can use the vzcfgvalidate tool:
|
|
This will launch an interactive mode informing you of what’s wrong. It’s up to you to validate whether or not you want to make the changes. If you don’t have the necessary analysis capabilities for the problems or simply don’t want to bother with this, replace the ‘-i’ option with ‘-r’, which will automatically adjust to the correct values.
For those who don’t want to bother at all with limits, here’s a small script that will automatically adjust all limits of all your VEs. Just add it to crontab:
|
|
Automatic adjustment of limits
Whether for my work or for home, the constant modification of resource values is a bit of a headache for me. So I created a tool to manage all this on its own called vzautobean (OpenVZ Auto Management User Beancounters).
To install it is very simple, we’ll fetch it and place it in the folder with the other OpenVZ binaries:
|
|
Then either you launch it and it will run on all your VEs in use, or you can specify one or more specific VEs:
|
|
Here it will do VE 102 and 103. By default it increases all barriers by 10% and limits by 20% relative to maxheld.
If you wish, you can change these values by adding 2 arguments:
|
|
Here the barrier should be set to 30% above maxheld and the limit to 50% above. And this for all VEs that are launched!
Now all you have to do is put this in cron if you want this program to run regularly.
Backup and restore
Installation
You may need to backup an entire environment, and for that there’s the vzdump command:
|
|
If you’re on a Debian older than 6, you can find the command here:
|
|
Backup
To backup, it’s very simple:
|
|
Restore
If you want to restore:
|
|
The machine will be restored on the VE corresponding to $my_veid.
Services
Some services may have some problems working properly (always the same ones). That’s why I’ll give solutions for the ones I’ve encountered.
NTP
For the NTP service, simply configure the VE like this:
|
|
GlusterFS
If you want to use glusterfs in a VE, you may encounter permission problems:
fuse: failed to open /dev/fuse: Permission denied
To work around this, we’ll create the fuse device from the host on the VE in question and add admin rights to it (a bit poor in terms of security, but no choice):
|
|
Encfs
If you want to use encfs in a VE, you may encounter permission problems:
EncFS Password:
fuse: device not found, try 'modprobe fuse' first
fuse failed. Common problems:
- fuse kernel module not installed (modprobe fuse)
- invalid options -- see usage message
Be aware that you need to load the fuse module at the VZ level for VEs to inherit it. Add this to your VZ to avoid having to load the module at each boot:
|
|
Then load it dynamically to access it afterwards:
|
|
To work around these issues, we’ll create the fuse device from the host on the VE in question and add admin rights to it (a bit poor in terms of security, but no choice):
|
|
It’s possible that the second line doesn’t work when the VE is off. Run it once it’s turned on, then mount your encfs partition.
OpenVPN
If you want to run an OpenVPN server in a VE, add these kinds of permissions and create the necessary devices:
|
|
FAQ
bridge vmbr0 does not exist!
Damn!!! Oh I had a hard time with this sh*t! Indeed by default Debian assigns the name of the interface to vmbr0 for bridging (certainly a new nomenclature for Debian 6). I had found this problem when installing my server and then like an idiot I hadn’t noted it down! When you do an update, it may stop working (the bridge of your VMs) because the file /usr/sbin/vznetaddbr
may be replaced. When launching a VE, this gives you something like:
Adding interface veth101.0 to bridge vmbr0 on CT0 for CT101
bridge vmbr0 does not exist!
To fix the problem quickly, modify this file and replace the name of this interface with the correct one (here br0). For my part, I changed the name of the interface to avoid being bothered in the future.
I have freezes when booting/stopping my VZs
If you have freezes at boot or stop of VZs and you’re in bridge mode, you need to change the MAC addresses like this:
|
|
Resources
- Some Tips On OpenVZ Deployment
- Splitting Resources Evenly Between OpenVZ VMs With vzsplit
- Installing And Using OpenVZ On Debian Lenny AMD64
- http://www.libresys.fr/2008/10/14/les-differentes-formes-de-configuration-du-reseau-avec-openvz/
- http://www.famille-fontes.net/comments.php?y=10&m=02&entry=entry100208-094400
- http://wiki.openvz.org/Backup_of_a_running_container_with_vzdump
- http://www.kafe-in.net/Blog/OpenVZ-Les-ressources-syst-me-dans-un-VE
Last updated 07 Apr 2014, 15:52 CEST.