Instalación Linux Containers (LXC) en Centos 7
Hoy vamos a enseñaros más tecnologías de Containers, esta vez sobre Centos 7. Vamos a intentar explicaros qué es LXC, qué diferencias hay con, por ejemplo Docker, componentes, cómo se instala y otros apuntes interesantes.
Por cierto, está desarrollado por Canonical, creo que os sonará de un tal Ubuntu 🙂
La página oficial es: https://linuxcontainers.org
DOCKER vs LXC (Linux Containers)
Vamos a decir que son dos tipos de virtualización de containers diferentes, pero que pueden convivir ya que tienen misiones diferentes.
¿Qué diferencia hay entre Docker y LXC? Con Docker se generan containers de aplicación, y con LXC generamos containers de sistema.
LXC es una tecnología de virtualización de Containers sobre Linux. Se basa en una API y herramientas sencillas, utilizando Cgroups y Namespaces de Linux para proporcionar el aislamiento necesario.
Cgroups, desarrollado por Google e integrado en el kernel de linux versión 2.6.24, permite priorizar recursos o controlar grupo de procesos, por ejemplo.
Namespaces permite que varios procesos de sistema pueden coexistir en un espacio, y separados de otros.
Volviendo a LXC, comparte el kernel con el host, y existe menos aislamiento con respecto al sistema principal que con Docker. Aunque con Cgroups y Namespace, como hemos hablado, se palia esto.
Docker, como ya hemos hablado en otras entradas, se trata de containers de aplicaciones y es multiplataforma, donde sólo tenemos un proceso en el sistema que lo identifica.
Normalmente, con Docker necesitaremos varios containers para que una aplicación como, por ejemplo, un wordpress, funcione.
Instalando Linux Containers en Centos 7
Después de dar una pequeña pincelada sobre LXC, vamos a ver como los instalamos sobre Centos 7.
Para instalar LXC sobre Centos 7 utilizaremos el repo EPEL:
|
1 |
[root@centos ~]# yum install epel-release |
Paquetes necesarios para la instalación:
|
1 |
[root@centos ~]# yum install debootstrap perl libvirt |
Para instalar LXC:
|
1 |
[root@centos ~]# yum install lxc lxc-templates |
Verificamos el estado del servicio:
|
1 2 3 4 |
[root@centos ~]# systemctl status lxc.service ● lxc.service - LXC Container Initialization and Autoboot Code Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled; vendor preset: disabled) Active: inactive (dead) |
Arrancamos los servicios necesarios:
|
1 2 |
[root@centos ~]# systemctl start lxc.service [root@centos ~]# systemctl start libvirtd |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Y volvemos a comprobar: [root@centos ~]# systemctl status lxc.service ● lxc.service - LXC Container Initialization and Autoboot Code Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled; vendor preset: disabled) Active: active (exited) since vie 2019-04-26 22:23:15 CEST; 1min 6s ago Process: 11469 ExecStart=/usr/libexec/lxc/lxc-autostart-helper start (code=exited, status=0/SUCCESS) Process: 11444 ExecStartPre=/usr/libexec/lxc/lxc-devsetup (code=exited, status=0/SUCCESS) Main PID: 11469 (code=exited, status=0/SUCCESS) Tasks: 0 Memory: 0B CGroup: /system.slice/lxc.service abr 26 22:23:14 centos.negu.local systemd[1]: Starting LXC Container Initialization and Autoboot Code... abr 26 22:23:14 centos.negu.local lxc-devsetup[11444]: Creating /dev/.lxc abr 26 22:23:14 centos.negu.local lxc-devsetup[11444]: /dev is devtmpfs abr 26 22:23:14 centos.negu.local lxc-devsetup[11444]: Creating /dev/.lxc/user abr 26 22:23:15 centos.negu.local lxc-autostart-helper[11469]: Starting LXC autoboot containers: [ OK ] abr 26 22:23:15 centos.negu.local systemd[1]: Started LXC Container Initialization and Autoboot Code. |
Verificamos el estado en el sistema de la virtualización:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
[root@centos ~]# lxc-checkconfig Kernel configuration not found at /proc/config.gz; searching... Kernel configuration found at /boot/config-3.10.0-957.10.1.el7.x86_64 --- Namespaces --- Namespaces: enabled Utsname namespace: enabled Ipc namespace: enabled Pid namespace: enabled User namespace: enabled newuidmap is not installed newgidmap is not installed Network namespace: enabled Multiple /dev/pts instances: enabled --- Control groups --- Cgroup: enabled Cgroup clone_children flag: enabled Cgroup device: enabled Cgroup sched: enabled Cgroup cpu account: enabled Cgroup memory controller: enabled Cgroup cpuset: enabled --- Misc --- Veth pair device: enabled Macvlan: enabled Vlan: enabled Bridges: enabled Advanced netfilter: enabled CONFIG_NF_NAT_IPV4: enabled CONFIG_NF_NAT_IPV6: enabled CONFIG_IP_NF_TARGET_MASQUERADE: enabled CONFIG_IP6_NF_TARGET_MASQUERADE: enabled CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled --- Checkpoint/Restore --- checkpoint restore: enabled CONFIG_FHANDLE: enabled CONFIG_EVENTFD: enabled CONFIG_EPOLL: enabled CONFIG_UNIX_DIAG: enabled CONFIG_INET_DIAG: enabled CONFIG_PACKET_DIAG: enabled CONFIG_NETLINK_DIAG: enabled File capabilities: enabled Note : Before booting a new kernel, you can check its configuration usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig |
Con esto ya tenemos instalado LXC en Centos 7.
Administración de Containers sobre LXC
La ruta de las imágenes de containers disponibles en el sistema podéis encontrarla en:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@centos ~]# ls -alh /usr/share/lxc/templates/ total 340K drwxr-xr-x 2 root root 331 abr 26 22:21 . drwxr-xr-x 6 root root 106 abr 26 22:21 .. -rwxr-xr-x 1 root root 11K mar 7 21:30 lxc-alpine -rwxr-xr-x 1 root root 14K mar 7 21:30 lxc-altlinux -rwxr-xr-x 1 root root 11K mar 7 21:30 lxc-archlinux -rwxr-xr-x 1 root root 9,5K mar 7 21:30 lxc-busybox -rwxr-xr-x 1 root root 30K mar 7 21:30 lxc-centos -rwxr-xr-x 1 root root 11K mar 7 21:30 lxc-cirros -rwxr-xr-x 1 root root 18K mar 7 21:30 lxc-debian -rwxr-xr-x 1 root root 18K mar 7 21:30 lxc-download -rwxr-xr-x 1 root root 49K mar 7 21:30 lxc-fedora -rwxr-xr-x 1 root root 28K mar 7 21:30 lxc-gentoo -rwxr-xr-x 1 root root 14K mar 7 21:30 lxc-openmandriva -rwxr-xr-x 1 root root 14K mar 7 21:30 lxc-opensuse -rwxr-xr-x 1 root root 35K mar 7 21:30 lxc-oracle -rwxr-xr-x 1 root root 12K mar 7 21:30 lxc-plamo -rwxr-xr-x 1 root root 6,7K mar 7 21:30 lxc-sshd -rwxr-xr-x 1 root root 24K mar 7 21:30 lxc-ubuntu -rwxr-xr-x 1 root root 12K mar 7 21:30 lxc-ubuntu-cloud |
Podéis crear un container de la siguiente forma. El primer parámetro es el nombre del container y el segundo el nombre de la template. Veréis que el comportamiento no es como con Docker, sino que es algo intermedio entre una máquina virtual y generar un container con docker:
|
1 |
Otros parámetros para crear containers LXC:
|
1 2 3 4 5 |
-n = nombre -t = plantilla -d = distribución -a = arquitectura -r = versión |
Ya disponemos del container creado, y como veis al final, nos dice la contraseña de root. Que normalmente, cambiaremos cuando arranquemos una sesión en ese container. Ahora lo arrancamos:
|
1 |
[root@centos ~]# lxc-start -n container01 -d |
Y comprobamos:
|
1 2 3 4 5 |
[root@centos ~]# lxc-top Container CPU CPU CPU BlkIO Mem Name Used Sys User Total Used container01 0.06 0.04 0.02 0.00 688.00 KB TOTAL (1 ) 0.06 0.04 0.02 0.00 688.00 KB |
Para entrar en él para gestionarlo podemos utilizar:
|
1 2 3 4 5 6 7 8 9 |
[root@centos ~]# lxc-console -n container01 Connected to tty 1 Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself Debian GNU/Linux 9 container01 tty1 container01 login: root Password: |
Y una vez dentro trabajar con él:
|
1 2 3 4 5 6 7 8 9 10 11 |
Linux container01 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC 2019 x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@container01:~# cd / bin/ dev/ home/ lib64/ mnt/ proc/ run/ selinux/ sys/ usr/ boot/ etc/ lib/ media/ opt/ root/ sbin/ srv/ tmp/ var/ |
Pararlo:
[root@centos ~]# lxc-stop -n container01O clonarlo:
|
1 2 |
[root@centos ~]# lxc-clone container01 container02 Created container container02 as copy of container01 |
O destruirlo:
|
1 |
[root@centos ~]# lxc-destroy -n container02 |
Los containers LXC se guardan por defecto en:
|
1 2 |
[root@centos ~]# ls /var/lib/lxc/ container01 |
¿Te ha gustado la entrada SÍGUENOS EN TWITTER?
¿Te ha gustado la entrada SÍGUENOS EN TWITTER O INVITANOS A UN CAFE?
Máquinas Virtuales | Virtualización, AIOps y Ciberseguridad en Español · Raul Unzue (vExpert) Blog de Raúl Unzué (vExpert). Guías de virtualización, Proxmox, Docker, K8s y ciberseguridad en español. +1.700 artículos para Sysadmins y profesionales IT.


Que tal, tengo este error:
Downloading debian minimal …
I: Retrieving InRelease
I: Checking Release signature
E: Release signed by unknown key (key id DCC9EFBF77E11517)
The specified keyring /var/cache/lxc/debian/archive-key.gpg may be incorrect or out of date.
You can find the latest Debian release key at https://ftp-master.debian.org/keys.html
Failed to download the rootfs, aborting.
Failed to download ‘debian base’
failed to install debian
lxc_container: lxccontainer.c: create_run_template: 1092 container creation template for container01 failed