GNU/Linux >> Linux Esercitazione >  >> Cent OS

Configura il contenitore Linux con LXC su CentOS 7 / RHEL 7

I container Linux (LXC) sono un metodo di virtualizzazione a livello di sistema operativo leggero che ci consente di eseguire più sistemi Linux isolati (container) su un singolo host. LXC non fornisce tutte le funzionalità dei software di virtualizzazione standard come VMware, VirtualBox e KVM, ma fornisce piuttosto un ambiente virtuale che dispone di CPU, memoria, blocchi I/O e rete propri. LXC crea un ambiente Linux simile a un'installazione Linux standard ma senza la necessità di un kernel separato.

LXC è un software gratuito ed è rilasciato con licenza GNU LGPLv2.1+. Il progetto LXC è sponsorizzato da Canonical Ltd, che sta dietro al sistema operativo Ubuntu.

In questa guida, ti mostrerò come installare LXC e come creare e gestire LXC utilizzando la riga di comando oltre che utilizzando LXC Web Portal .

Prerequisiti

LXC non è disponibile sul repository Base. Quindi dobbiamo aggiungere il repository EPEL al server usando il seguente comando.

# yum -y install epel-release

I container LXC utilizzano la rete bridge per avere accesso a/da una rete esterna, prima di avviare il container dobbiamo creare un bridge di rete su CentOS 7 / RHEL 7 . Il nome del bridge di rete dovrebbe essere "virbr0"

Installa LXC su CentOS 7

Una volta completati i prerequisiti, è il momento di installare LXC. Installa LXC e altri pacchetti importanti necessari per il corretto funzionamento dei container.

# yum -y install lxc lxc-templates libcap-devel libcgroup busybox wget bridge-utils lxc-extra

Esegui il comando seguente per verificare che tutto sia a posto per eseguire i container.

# lxc-checkconfig

 Kernel configuration not found at /proc/config.gz; searching...
 Kernel configuration found at /boot/config-3.10.0-327.el7.x86_64
 --- Namespaces ---
 Namespaces: enabled
 Utsname namespace: enabled
 Ipc namespace: enabled
 Pid namespace: enabled
 User namespace: enabled
 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

Creazione di container Linux

LXC viene fornito con modelli già pronti per una facile installazione dei contenitori e puoi elencare i modelli disponibili utilizzando il comando seguente.

# ls /usr/share/lxc/templates/

 lxc-alpine    lxc-archlinux  lxc-centos  lxc-debian    lxc-fedora  lxc-openmandriva  lxc-oracle  lxc-sshd    lxc-ubuntu-cloud
 lxc-altlinux  lxc-busybox    lxc-cirros  lxc-download  lxc-gentoo  lxc-opensuse      lxc-plamo   lxc-ubuntu

Per creare un container, immetti il ​​seguente comando.

# lxc-create -n centos_lxc -t centos

Dove,

-n

-t

Dopo aver emesso il comando precedente, LXC inizierà a creare il contenitore con il nome "centos_lxc “.

Risultato di esempio:

Host CPE ID from /etc/os-release: cpe:/o:centos:centos:7
 Checking cache download in /var/cache/lxc/centos/x86_64/7/rootfs ...
 Downloading centos minimal ...
 Loaded plugins: fastestmirror
 base                                                                                                                                             | 3.6 kB  00:00:00
 updates                                                                                                                                          | 3.4 kB  00:00:00
 Determining fastest mirrors
 .
 .
 .
 .
 Complete!
 Download complete.
 Copy /var/cache/lxc/centos/x86_64/7/rootfs to /var/lib/lxc/centos_lxc/rootfs ...
 Copying rootfs to /var/lib/lxc/centos_lxc/rootfs ...
 sed: can't read /var/lib/lxc/centos_lxc/rootfs/etc/init/tty.conf: No such file or directory
 Storing root password in '/var/lib/lxc/centos_lxc/tmp_root_pass'
 Expiring password for user root.
 passwd: Successsed: can't read /var/lib/lxc/centos_lxc/rootfs/etc/rc.sysinit: No such file or directory
sed: can't read /var/lib/lxc/centos_lxc/rootfs/etc/rc.d/rc.sysinit: No such file or directory

Container rootfs and config have been created.
Edit the config file to check/enable networking setup.

The temporary root password is stored in:

        '/var/lib/lxc/centos_lxc/tmp_root_pass'


The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible.  If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):

        chroot /var/lib/lxc/centos_lxc/rootfs passwd

Nota i dettagli di accesso sopra riportati e devi richiedere queste informazioni per accedere ai contenitori.

Credenziali contenitori

Per accedere al contenitore (centos_lxc ), utilizzare la password di root temporanea archiviata nella posizione seguente. Nel nostro caso, "Root-centos_lxc-KRzJLy ” è la password di root di centos_lxc .

# cat /var/lib/lxc/centos_lxc/tmp_root_pass

 Root-centos_lxc-KRzJLy

o

Reimposta la password di root usando il comando seguente.

# chroot /var/lib/lxc/centos_lxc/rootfs passwd

PS:devi ancora avviare i container.

Avvio di container Linux

Dopo aver creato i contenitori, avvialo utilizzando il comando seguente, viene eseguito in background.

# lxc-start -n centos_lxc -d

Ora prendi la console del container usando il seguente comando.

Nota:utilizzo "-t" con "0" per collegare il contenitore con tty0, solo perché tty1 non mi rispondeva.

# lxc-console -n centos_lxc -t 0

Inserisci username e password per accedere. Puoi trovare la credenziale alla fine dell'output durante la creazione di un container. Devi cambiare la password di root al primo accesso.

Risultato di esempio:

Connected to tty 0
 Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
 CentOS Linux 7 (Core)
 Kernel 3.10.0-327.el7.x86_64 on an x86_64
 centos_lxc login: root
 Password:
 You are required to change your password immediately (root enforced)
 Changing password for root.
 (current) UNIX password:
 New password:
 Retype new password:
 [root@centos_lxc ~]#

Una volta effettuato l'accesso, puoi eseguire tutto il lavoro in questo contenitore come faresti su un normale server Linux.

Per uscire dalla console del contenitore, premi "Ctrl+a" seguito da "q" . Ora verrai riportato al terminale del computer host.

Se vuoi connetterti di nuovo al container (il container è ancora in esecuzione), esegui il comando seguente.

# lxc-console -n centos_lxc -t 0

Lavorare con i container Linux

Per elencare i contenitori sul computer host, utilizzare il comando seguente.

# lxc-ls

 centos_lxc

Inoltre, elenca i container attualmente attivi e in esecuzione sul computer host.

# lxc-ls --active

 centos_lxc

Dato che ho un solo container attualmente in esecuzione, ecco perché potresti vedere lo stesso output per entrambi i comandi.

Se vuoi ottenere le informazioni complete del contenitore in esecuzione, emetti il ​​seguente comando.

# lxc-info -n centos_lxc

 Name:           centos_lxc
 State:          RUNNING
 PID:            4047
 IP:             192.168.12.16
 CPU use:        0.47 seconds
 BlkIO use:      6.32 MiB
 Memory use:     4.19 MiB
 KMem use:       0 bytes
 Link:           vethM3N48G
 TX bytes:      1.53 KiB
 RX bytes:      1.94 KiB
 Total bytes:   3.47 KiB

Il comando precedente fornisce informazioni dettagliate (nome, stato, indirizzo IP, CPU, memoria, I/O e utilizzo della rete) del contenitore "centos_lxc".

Puoi anche utilizzare l'indirizzo IP per connetterti ai container invece della console LXC.

Puoi fermare un container in esecuzione usando il comando “lxc-stop”, usa il comando seguente per fermare un container “centos_lxc”.

# lxc-stop -n centos_lxc

Clone di container Linux

LXC ha il futuro di clonare un contenitore dal contenitore esistente, esegui il comando seguente per clonare un contenitore "centos_lxc" esistente in un nuovo contenitore "centos_lxc_clone".

Nota:devi interrompere un container in esecuzione prima di avviare il clone.

# lxc-clone centos_lxc centos_lxc_clone
 Created container centos_lxc_clone as copy of centos_lxc

Verifica se un container è stato creato correttamente.

# lxc-ls
 centos_lxc  centos_lxc_clone

Puoi iniziare a lavorare con un nuovo contenitore come di consueto.

# lxc_start -n centos_lxc_clone -d
# lxc_console -n centos_lxc_clone -t 0
 Connected to tty 0
 Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
 CentOS Linux 7 (Core)
 Kernel 3.10.0-327.el7.x86_64 on an x86_64
 centos_lxc_clone login: root
 Password:
 Last login: Sat Mar 26 14:11:17 from 192.168.12.1
 [root@centos_lxc_clone ~]#

Spegni il container dall'interno della console.

[root@centos_lxc_clone ~]# poweroff

Scattare un'istantanea

LXC ha anche un altro futuro chiamato snapshot, usa i seguenti comandi.

Nota:devi fermare un container prima di scattare un'istantanea.

# lxc-stop -n centos_lxc_clone

Per una demo, sto prendendo l'istantanea di centos_lxc_clone.

# lxc-snapshot -n centos_lxc_clone

Esempio di output:

lxc_container: lxccontainer.c: lxcapi_snapshot: 2879 Snapshot of directory-backed container requested.
lxc_container: lxccontainer.c: lxcapi_snapshot: 2880 Making a copy-clone.  If you do want snapshots, then
lxc_container: lxccontainer.c: lxcapi_snapshot: 2881 please create an aufs or overlayfs clone first, snapshot that
lxc_container: lxccontainer.c: lxcapi_snapshot: 2882 and keep the original container pristine.

Per sapere dove viene salvata l'istantanea, esegui il comando seguente.

# lxc-snapshot -L -n centos_lxc_clone
 snap0 (/var/lib/lxcsnaps/centos_lxc_clone) 2016:03:26 10:59:10

In Centos 7, gli snapshot LXC sono archiviati in "/var/lib/lxcsnaps/

Ripristino dell'istantanea

Per ripristinare un contenitore dallo snapshot, utilizza il comando seguente.

# lxc-snapshot -r snap0 -n centos_lxc_clone

Rimozione dei contenitori

Per rimuovere completamente un contenitore, usa il seguente comando.

# lxc-destroy -n centos_lxc_clone

Eseguire un contenitore Ubuntu su CentOS 7

Ho riscontrato diversi problemi quando stavo cercando di eseguire un contenitore Ubuntu su CentOS, sono stato in grado di eseguire Ubuntu con l'aiuto di alcune modifiche condivise su altri siti Web.

Installa i seguenti pacchetti per i contenitori basati su Debian.

# yum -y install debootstrap perl
# cd /usr/sbin ; ln -sf debootstrap qemu-debootstrap

Esegui il comando seguente per sostituire Debian mirror in Ubuntu mirror.

# sed -i 's/DEF_HTTPS_MIRROR="https:\/\/mirrors.kernel.org\/debian"/DEF_HTTPS_MIRROR="https:\/\/mirrors.kernel.org\/ubuntu"/g' /usr/sbin/debootstrap

Grazie a unix.stackexchange.com

Ottieni il portachiavi preciso di Ubuntu e inseriscilo nella directory dei portachiavi.

# cd /tmp
# wget "http://archive.ubuntu.com/ubuntu/pool/main/u/ubuntu-keyring/ubuntu-keyring_2011.11.21.tar.gz"
# tar xzf ubuntu-keyring_2011.11.21.tar.gz
# mkdir /usr/share/keyrings/
# cp /tmp/ubuntu-keyring-2011.11.21/keyrings/ubuntu-archive-keyring.gpg /usr/share/keyrings/

Grazie a blog.toxa.de

Crea un contenitore Ubuntu usando il seguente comando.

# lxc-create -n ubuntu_lxc -t ubuntu

Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... 
Installing packages in template: ssh,vim,language-pack-en
Downloading ubuntu precise minimal ...
I: Retrieving Release 
I: Retrieving Release.gpg 
I: Checking Release signature
I: Valid Release signature (key id 630239CC130E1A7FD81A27B140976EAF437D05B5)
I: Retrieving Packages 
I: Validating Packages 
I: Retrieving Packages 

Generation complete.
Setting up libdevmapper1.02.1 (2:1.02.48-4ubuntu7.4) ...
Setting up dmsetup (2:1.02.48-4ubuntu7.4) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
Processing triggers for initramfs-tools ...
Processing triggers for resolvconf ...
invoke-rc.d: policy-rc.d denied execution of start.
Download complete
Copy /var/cache/lxc/precise/rootfs-amd64 to /var/lib/lxc/ubuntu_lxc/rootfs ... 
Copying rootfs to /var/lib/lxc/ubuntu_lxc/rootfs ...
Generating locales...
  en_US.UTF-8... up-to-date
Generation complete.
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Timezone in container is not configured. Adjust it manually.
##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##

Dall'output sopra, puoi vedere che l'utente predefinito è "ubuntu" e la password per l'utente è "ubuntu".

Prendi una console del contenitore Ubuntu con il seguente comando.

# lxc_console -n ubuntu_lxc

Ubuntu 12.04.5 LTS ubuntu_lxc console
ubuntu_lxc login: ubuntu
Password: 
Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.10.0-327.10.1.el7.x86_64 x86_64)
 * Documentation:  https://help.ubuntu.com/
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Would you like to enter a security context? [N]  
ubuntu@ubuntu_lxc:~$

PS:non ho verificato la funzionalità del server tranne l'accesso, per favore posta qui per qualsiasi problema.

È tutto. Sii social, condividi questo con i tuoi amici.


Cent OS
  1. Configura il server SysLog su CentOS 6 / RHEL 6

  2. Configura il contenitore Linux con LXC su Ubuntu 16.04

  3. Imposta Rsyslog con MySQL e LogAnalyzer su CentOS/RHEL 6/5

  4. CentOS / RHEL 7:come modificare gli argomenti di GRUB2 con grubby

  5. Impossibile avviare il sistema operativo Linux con Auditd (CentOS/RHEL)

Come installare Java su RHEL 8 / CentOS 8 Linux

Come installare WordPress su RHEL 8 / CentOS 8 Linux

Come installare GIMP su CentOS 8 / RHEL 8 Linux

Come configurare il server NFS su CentOS 8 / RHEL 8

Come configurare Jenkins su CentOS 8 / RHEL 8

Come installare e configurare i contenitori Linux LXC su CentOS / RHEL / Ubuntu