LXC is a lightweight “virtualization” method to run multiple virtual units (containers, akin to “chroot”) simultaneously on a single control host. Containers are isolated with Kernel Control Groups (cgroups) and Kernel Namespaces.
LXC provides an operating system-level virtualization where the Kernel controls the isolated containers. With other full virtualization solutions like Xen, KVM, or
Conceptually, LXC can be seen as an improved chroot technique. The difference is that a chroot environment separates only the file system, whereas LXC goes further and provides resource management and control via cgroups.
The LXC host provides the cgroups and controls all containers.
LXC starts the cgroup service automatically. The LXC host is now prepared for setting up containers.
A container is a “virtual machine” that can be started, stopped, connected, or disconnected in YaST. The two last actions are only available in the GUI version, not when YaST running in text mode. If you use YaST in a text console, use the lxc-console command as described in Procedure 5, “Starting, Accessing, and Stopping Your Container Manually”.
To set up an LXC container with YaST, proceed as follows:
A container is a “virtual machine” that can be started, stopped, frozen, or cloned (to name but a few tasks). To set up an LXC container, proceed as follows:
LXC containers can be started at boot time. However, you need to follow certain conventions. Every container has a subdirectory with its name in
To set up the automatic start of LXC containers, proceed as follows:
After this procedure, your LXC containers are correctly configured. To start it automatically next time you boot your computer, use insserv lxc.
LXC provides an operating system-level virtualization where the Kernel controls the isolated containers. With other full virtualization solutions like Xen, KVM, or
libvirt
the processor simulates a complete hardware environment and controls its virtual machines. - chroot
- A change root (chroot, or change root jail) is a section in the file system which is isolated from the rest of the file system. For this purpose, the chroot command is used to change the root of the file system. A program which is executed in such a “chroot jail” cannot access files outside the designated directory tree.
- cgroups
- Kernel Control Groups (commonly referred to as just “cgroups”) are a Kernel feature that allows aggregating or partitioning tasks (processes) and all their children into hierarchical organized groups to isolate resources.
- Container
- A “virtual machine” on the host server that can run any Linux system, for example openSUSE, SUSE Linux Enterprise Desktop, or SUSE Linux Enterprise Server.
- Container Name
- A name that refers to a container. The name is used by the
lxc
commands. - Kernel Namespaces
- A Kernel feature to isolate some resources like network, users, and others for a group of processes.
- LXC Host Server
- The system that contains the LXC system and provides the containers and management control capabilities through cgroups.
2. Overview¶
Benefits of LXC
- Isolating applications and operating systems through containers.
- Providing nearly native performance as LXC manages allocation of resources in real-time.
- Controlling network interfaces and applying resources inside containers through cgroups.
Limitations of LXC
- All LXC containers are running inside the host system's Kernel and not with a different Kernel.
- Only allows Linux “guest” operating systems.
- LXC is not a full virtualization stack like Xen, KVM, or
libvirt
. - Security depends on the host system. LXC is not secure. If you need a secure system, use KVM.
3. Setting up an LXC Host¶
Procedure 1. Preparing an LXC Host¶
- Install the following packages:
- lxc
- bridge-utils
- Check if everything is prepared for LXC:
lxc-checkconfig
You should see the wordsenabled
on each checked item. - If you want to access the virtual container's ethernet interface, create a network bridge. A network bridge allows to share the network link on the physical interface of the host (
eth0
):
- Open YaST and go to + .
- Click .
- Select as device type. Proceed with .
- Activate and select .
- Choose your bridged device(s), usually
eth0
. Proceed with . Optionally check your devices with the ifconfig command. Close the module.
- If you have created a network bridge, assign its interface zone:
- Start YaST and go to + .
- Open the tab.
- Select your bridge device (usually
br0
). - Click and select . Proceed with .
- Finish with .
4. Setting up LXC Containers with YaST¶
To set up an LXC container with YaST, proceed as follows:
- Open YaST and go to the LXC module. Is on Miscellaneous.
- Click .
- Enter a name of your container in the field.
- Select a Linux distribution (only SLES is supported) from the pop-up menu.
- Enter the bridge for your LXC container. If you do not have a bridge, click to configure a bridge.
- If needed, enter a password to log in to a LXC container. If you leave the password field empty, the standard password “root” is used for this container.
- Finish with and YaST tries to prepare the container. This action takes some time.
- After YaST has finished the preparation, click to launch the LXC container.
Procedure 3. Starting, Accessing, and Stopping Your Container with YaST¶
- Select the container and click
- Click the button. A new terminal window opens.
- Log in with user
root
and your password from Step 6 of Procedure 2, “Creating a Container with YaST”. If you did not set a password, use “root”. - Make your changes in your container.
- When you are finished, save all your work and log out.
- Click the button to close the terminal. It is still possible to reconnect to your container by clicking .
- To shutdown the container entirely, click the button.
5. Setting up LXC Containers Manually¶
Procedure 4. Creating a Container Manually¶
- Create a configuration file (name
lxc_vps0.conf
in this example) with the container name in it and edit it according to the following example:
lxc.utsname = vps0 lxc.network.type = veth lxc.network.flags = up lxc.network.link = br0 lxc.network.hwaddr = 00:30:6E:08:EC:80 lxc.network.ipv4 = 192.168.1.10 lxc.network.name = eth0
/usr/share/doc/packages/lxc/examples/
. Find details about all options in the lxc.conf man page. - Create a container by using the configuration file from Step 1. A list of available templates is located in
/usr/share/lxc/templates/
.
lxc-create -t
TEMPLATE
-f lxc.conf -nCONTAINER
CONTAINER
needs to be replaced by the value you specified forlxc.utsname
in the config file,vps0
in this example. Replace the placeholderTEMPLATE
with your preferred template name.
Downloading and installing the base packages for openSUSE or SUSE Linux Enterprise Server will take some time. The container will be created in/var/lib/lxc/
, and their configuration files will be stored underCONTAINER
/etc/lxc/
. - Finalize the configuration of the container:
- Change the root path to the installed LXC container with the chroot command:
chroot /var/lib/lxc/
CONTAINER_NAME
/rootfs/ - Change the password for user
root
with passwd root. - Create an
operator
user withoutroot
privileges:
useradd -m operator
- Change the operator's password:
passwd operator
- Leave the chroot environment with exit.
- Change the root path to the installed LXC container with the chroot command:
Procedure 5. Starting, Accessing, and Stopping Your Container Manually¶
- Start the container:
lxc-start -d -n
CONTAINER_NAME
- Connect to the container and log in:
lxc-console -n
CONTAINER_NAME
- Stop and remove your container always with the two steps:
lxc-stop -n
CONTAINER_NAME
lxc-destroy -nCONTAINER_NAME
6. Starting Containers at Boot Time¶
/etc/lxc/
, for example, /etc/lxc/my-sles
. This directory needs to be created once. There you place your configuration file (named config
). To set up the automatic start of LXC containers, proceed as follows:
- Activate the cgroup service with insserv boot.cgroup. This has to be done only once to enable this service at boot time. The command will populate the
/sys/fs/cgroup
directory. - Create a directory
/etc/lxc/
.CONTAINER
- Copy your configuration file to
/etc/lxc/
.CONTAINER
/config - Run /etc/init.d/boot.cgroup
start
to set up cgroups properly. - Run /etc/init.d/lxc
start
to start your containers. - Wait a few seconds and run /etc/init.d/lxc
list
to print the state of all your containers.
7. For More Information¶
- LXC Home Page
- http://lxc.sourceforge.net
- Kernel Control Groups (cgroups)
- http://www.suse.com/doc/sles11/book_sle_tuning/data/cha_tuning_cgroups.html
- Managing Virtual Machines with libvirt
- http://www.suse.com/doc/sles11/book_sles_kvm/data/part_managing_virtual.html
- LXC Container Driver
- http://libvirt.org/drvlxc.html
Comentarios
Publicar un comentario