Kā instalēt, izveidot un pārvaldīt LXC (Linux konteinerus) RHEL/CentOS 7
LXC, saīsinājums no Linux Containers, ir viegls virtualizācijas risinājums, kura pamatā ir Linux kodols, un tas praktiski darbojas operētājsistēmas augšpusē, ļaujot vienlaikus darbināt vairākus izolētus izplatījumus.
Atšķirība starp LXC un KVM virtualizāciju ir tāda, ka LXC nemitē aparatūru, bet koplieto to pašu kodola nosaukumvietu, līdzīgi kā chroot lietojumprogrammas.
Tas padara LXC par ļoti ātru virtualizācijas risinājumu salīdzinājumā ar citiem virtualizācijas risinājumiem, piemēram, VMware.
Šis raksts palīdzēs jums uzzināt, kā jūs varat instalēt, izvietot un palaist LXC konteinerus CentOS/RHEL un Fedora izplatījumos.
Strādājoša Linux operētājsistēma ar minimālu instalēšanu:
- CentOS 7 Linux instalēšana
- RHEL 7 instalēšana
- Fedora 23 servera instalēšana
1. darbība: LXC virtualizācijas instalēšana Linux
1. LXC virtualizācija tiek nodrošināta, izmantojot Epel krātuves. Lai izmantotu šo repo, atveriet termināli un instalējiet savā sistēmā Epel krātuves, izsniedzot šādu komandu:
# yum install epel-release
2. Pirms turpināt LXC instalēšanas procesu, pārliecinieties, ka Perl valodas tulka un debootstrap pakotnes ir instalētas, izsniedzot tālāk norādītās komandas.
# yum install debootstrap perl libvirt
3. Visbeidzot instalējiet LXC virtualizācijas risinājumu ar šādu komandu.
# yum install lxc lxc-templates
4. Pēc LXC pakalpojuma instalēšanas pārbaudiet, vai darbojas LXC un libvirt dēmons.
# systemctl status lxc.service # systemctl start lxc.service # systemctl start libvirtd # systemctl status lxc.service
systemctl status lxc.service lxc.service - LXC Container Initialization and Autoboot Code Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled) Active: inactive (dead) systemctl start lxc.service systemctl status lxc.service lxc.service - LXC Container Initialization and Autoboot Code Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled) Active: active (exited) since Fri 2016-04-01 02:33:36 EDT; 1min 37s ago Process: 2250 ExecStart=/usr/libexec/lxc/lxc-autostart-helper start (code=exited, status=0/SUCCESS) Process: 2244 ExecStartPre=/usr/libexec/lxc/lxc-devsetup (code=exited, status=0/SUCCESS) Main PID: 2250 (code=exited, status=0/SUCCESS) Apr 01 02:33:06 mail systemd[1]: Starting LXC Container Initialization and Autoboot Code... Apr 01 02:33:06 mail lxc-devsetup[2244]: Creating /dev/.lxc Apr 01 02:33:06 mail lxc-devsetup[2244]: /dev is devtmpfs Apr 01 02:33:06 mail lxc-devsetup[2244]: Creating /dev/.lxc/user Apr 01 02:33:36 mail lxc-autostart-helper[2250]: Starting LXC autoboot containers: [ OK ] Apr 01 02:33:36 mail systemd[1]: Started LXC Container Initialization and Autoboot Code.
un pārbaudiet LXC kodola virtualizācijas statusu, izsniedzot zemāk esošo komandu.
# lxc-checkconfig
lxc-checkconfig Kernel configuration not found at /proc/config.gz; searching... Kernel configuration found at /boot/config-3.10.0-229.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: missing 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
2. darbība: izveidojiet un pārvaldiet LXC konteinerus Linux
5. Lai uzskaitītu pieejamos LXC veidņu konteinerus, kas jau ir instalēti jūsu sistēmā, izsniedziet šo komandu.
# ls -alh /usr/share/lxc/templates/
total 344K drwxr-xr-x. 2 root root 4.0K Apr 1 02:32 . drwxr-xr-x. 6 root root 100 Apr 1 02:32 .. -rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-alpine -rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-altlinux -rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-archlinux -rwxr-xr-x. 1 root root 9.7K Nov 15 10:19 lxc-busybox -rwxr-xr-x. 1 root root 29K Nov 15 10:19 lxc-centos -rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-cirros -rwxr-xr-x. 1 root root 17K Nov 15 10:19 lxc-debian -rwxr-xr-x. 1 root root 18K Nov 15 10:19 lxc-download -rwxr-xr-x. 1 root root 49K Nov 15 10:19 lxc-fedora -rwxr-xr-x. 1 root root 28K Nov 15 10:19 lxc-gentoo -rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-openmandriva -rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-opensuse -rwxr-xr-x. 1 root root 35K Nov 15 10:19 lxc-oracle -rwxr-xr-x. 1 root root 12K Nov 15 10:19 lxc-plamo -rwxr-xr-x. 1 root root 6.7K Nov 15 10:19 lxc-sshd -rwxr-xr-x. 1 root root 23K Nov 15 10:19 lxc-ubuntu -rwxr-xr-x. 1 root root 12K Nov 15 10:19 lxc-ubuntu-cloud
6. LXC konteinera izveides process ir ļoti vienkāršs. Komandu sintakse jauna konteinera izveidei ir paskaidrota zemāk.
# lxc-create -n container_name -t container_template
Zemāk esošajā fragmentā mēs izveidosim jaunu konteineru ar nosaukumu mydeb
, pamatojoties uz veidni debian
, kas tiks izvilkts no LXC krātuvēm.
lxc-create -n mydcb -t debian debootstrap is /usr/sbin/debootstrap Checking cache download in /var/cache/lxc/debian/rootfs-jessie-amd64 ... Downloading debian minimal ... W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg I: Retrieving Release I: Retrieving Packages I: Validating Packages I: Resolving dependencies of required packages... I: Resolving dependencies of base packages... ... ...
7. Pēc virknes bāzes atkarību un pakotņu, kas tiks lejupielādētas un instalētas jūsu sistēmā, tiks izveidots konteiners. Kad process būs pabeigts, ziņojumā tiks parādīta jūsu noklusējuma root konta parole. Mainiet šo paroli, tiklīdz sākat un piesakāties konteinera konsolē, lai būtu drošībā.
are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Failed to read /proc/cmdline. Ignoring: No such file or directory invoke-rc.d: policy-rc.d denied execution of start. Timezone in container is not configured. Adjust it manually. Root password is 'root', please change ! Generating locales (this might take a while)... en_IN.en_IN...character map file `en_IN' not found: No such file or directory /usr/share/i18n/locales/en_IN:55: LC_MONETARY: unknown character in field `currency_symbol' done Generation complete.
8. Tagad varat izmantot lxc-ls
, lai uzskaitītu savus konteinerus, un lxc-info
, lai iegūtu informāciju par darbojošos/apturēto konteineru.
Lai sāktu jaunizveidoto konteineru fonā (darbosies kā dēmons, norādot opciju -d
), izsniedziet šādu komandu:
# lxc-start -n mydeb -d
9. Pēc konteinera palaišanas jūs varat uzskaitīt darbojošos konteinerus, izmantojot komandu lxc-ls --active
un iegūt detalizētu informāciju par darbojošos konteineru.
# lxc-ls --active
10. Lai pieteiktos konteinera konsolē, izsniedziet komandu lxc-console
pret palaistā konteinera nosaukumu. Piesakieties, izmantojot lietotāja sakni un paroli, ko pēc noklusējuma ģenerējis lxc vadītājs.
Kad esat pieteicies konteinerā, varat palaist vairākas komandas, lai pārbaudītu izplatīšanu, parādot faila /etc/issue.net
saturu, mainiet saknes paroli, izsniedzot komandu passwd
vai skatiet informāciju par tīkla saskarnēm ar ifconfig
.
# lxc-console -n mydeb # cat /etc/issue.net # ifconfig # passwd
lxc-console -n mydcb Connected to tty 1 Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself Debian GNU/Linux 8 mydcb tty1 mydcb login: root Password: Last login: Fri Apr 1 07:39:08 UTC 2016 on console Linux mydcb 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 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. [email :~# cat /etc/issue.net Debian GNU/Linux 8 [email :~# ifconfig eth0 Link encap:Ethernet HWaddr 00:16:3e:d9:21:d7 inet6 addr: fe80::216:3eff:fed9:21d7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:107 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5796 (5.6 KiB) TX bytes:648 (648.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) [email :~# passwd root Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
11. Lai atvienotos no konteinera konsoles un atgrieztos resursdatora konsolē, atstājot konteineru aktīvā stāvoklī, tastatūrā nospiediet Ctrl + a
un pēc tam q
.
Lai apturētu darbojošos konteineru, izsniedziet šādu komandu.
# lxc-stop -n mydcb
12. Lai izveidotu LXC konteineru, kura pamatā ir Ubuntu veidne, ievadiet direktoriju /usr/sbin/
un izveidojiet šādu debootstrap simbolisko saiti.
# cd /usr/sbin # ln -s debootstrap qemu-debootstrap
13. Rediģējiet failu qemu-debootstrap
ar Vi redaktoru un aizstājiet šādas divas MIRROR rindas šādi:
DEF_MIRROR=”http://mirrors.kernel.org/ubuntu” DEF_HTTPS_MIRROR=”https://mirrors.kernel.org/ubuntu”
Lai uzzinātu, skatiet šo saturu un ievietojiet iepriekš minētās divas rindas, kā norādīts:
.... MAKE_TARBALL="" EXTRACTOR_OVERRIDE="" UNPACK_TARBALL="" ADDITIONAL="" EXCLUDE="" VERBOSE="" CERTIFICATE="" CHECKCERTIF="" PRIVATEKEY="" DEF_MIRROR=”http://mirrors.kernel.org/ubuntu” DEF_HTTPS_MIRROR=”https://mirrors.kernel.org/ubuntu”
14. Visbeidzot izveidojiet jaunu LXC konteineru, pamatojoties uz Ubuntu veidni, kas izsniedz to pašu komandu lxc-create
.
Kad Ubuntu konteinera ģenerēšanas process būs pabeigts, ziņojumā tiks parādīti jūsu konteinera noklusējuma pieteikšanās akreditācijas dati, kā parādīts zemāk esošajā ekrānuzņēmumā.
# lxc-create -n myubuntu -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 ...
15. Lai izveidotu noteiktu konteineru, pamatojoties uz vietējo veidni, izmantojiet šādu sintaksi:
# lxc-create -n container_name -t container_template -- -r distro_release -a distro_architercture
Šeit ir izvilkums, kā izveidot debian wheezy konteineru ar amd64 sistēmas arhitektūru.
# lxc-create -n mywheezy -t debian -- -r wheezy -a amd64
debootstrap is /sbin/debootstrap Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-amd64 ... Downloading debian minimal ... W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg I: Retrieving Release I: Retrieving Packages I: Validating Packages I: Resolving dependencies of required packages... I: Resolving dependencies of base packages... I: Found additional required dependencies: insserv libbz2-1.0 libdb5.1 libsemanage-common libsemanage1 libslang2 libustr-1.0-1 I: Found additional base dependencies: adduser debian-archive-keyring gnupg gpgv isc-dhcp-common libapt-pkg4.12 libbsd0 libclass-isa-perl libedit2 libgdbm3 libgssapi-krb5-2 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libncursesw5 libprocps0 libreadline6 libssl1.0.0 libstdc++6 libswitch-perl libusb-0.1-4 libwrap0 openssh-client perl perl-modules procps readline-common I: Checking component main on http://http.debian.net/debian... I: Retrieving libacl1 2.2.51-8 I: Validating libacl1 2.2.51-8 I: Retrieving adduser 3.113+nmu3 I: Validating adduser 3.113+nmu3 I: Retrieving apt 0.9.7.9+deb7u7 I: Validating apt 0.9.7.9+deb7u7 I: Retrieving libapt-pkg4.12 0.9.7.9+deb7u7 I: Validating libapt-pkg4.12 0.9.7.9+deb7u7 I: Retrieving libattr1 1:2.4.46-8 I: Validating libattr1 1:2.4.46-8 I: Retrieving base-files 7.1wheezy9 I: Validating base-files 7.1wheezy9 I: Retrieving base-passwd 3.5.26 ...
16. Piemēram, īpašus konteinerus dažādiem distro izlaidumiem un arhitektūrām var izveidot arī no vispārīgas veidnes, kas tiks lejupielādēta no LXC krātuvēm, kā parādīts šajā piemērā.
# lxc-create -n mycentos6 -t download -- -d centos -r 6 -a i386
Šeit ir saraksts ar komandrindas slēdžiem lxc-create
:
-n = name -t = template -d = distibution -a = arch -r = release
17. Konteinerus var izdzēst no resursdatora ar komandu lxc-iznīcināt
, kas izsniegta pret konteinera nosaukumu.
# lxc-destroy -n mywheezy
18. Konteineru var klonēt no esoša konteinera, izsniedzot komandu lxc-clone
:
# lxc-clone mydeb mydeb-clone
19. Visbeidzot, visi izveidotie konteineri atrodas direktorijā /var/lib/lxc/
. Ja kāda iemesla dēļ jums ir manuāli jāpielāgo konteinera iestatījumi, jums ir jārediģē konfigurācijas fails no katra konteinera direktorija.
# ls /var/lib/lxc
Šīs ir tikai pamatlietas, kas jums jāzina, lai apietu LXC.