1. 程式人生 > >如何使用RHEL/CentOS 7安裝建立和管理LXC Linux Containers

如何使用RHEL/CentOS 7安裝建立和管理LXC Linux Containers

LXC, 是Linux Containers的縮寫, 是一種基於linux核心的虛擬解決方案, 它一般運行於作業系統之上,允許你同時執行多個被隔離開的發行版本.

LXC和KVM虛擬化的區別是LXC不去模擬硬體,而是共享相同的核心名稱空間,這一點上非常類似於chroot應用.
這裡寫圖片描述

正是由於這一特點, 區別於其他諸如KVM,XEN或者VMWARE的虛擬化解決方案,LXC成為了一種更加快速的虛擬化解決方案.

這篇文章將會引導你如何在一個CentOS/RHEL的linux上安裝部署以及執行LXC container.

前提事項
下列的最小化安裝的linux作業系統
- CentOS 7 Linux
- RHEL 7
- Fedora 23 Server

Step 1: 在Linux上安裝LXC Virtualization

1 . LXC virtualization是在yum的擴充套件package的epel中提供的,為了使用Epel, 你需要開啟一個終端執行如下命令:

# yum install epel-release
  • 1

2 . 在安裝LXC之前,需要確保Perl語言直譯器和debootstrap包已經使用如下命令安裝完畢.

# yum install debootstrap perl libvirt
  • 1

3 . 最後,終於可以使用如下命令安裝LXC了.

# yum install lxc lxc-templates
  • 1

4 . 安裝完畢之後,確認LXC和libvirt的後臺程序是否在執行之中.

# systemctl status lxc.service
# systemctl start lxc.service
# systemctl start libvirtd 
# systemctl status lxc.service
  • 1
  • 2
  • 3
  • 4

示例輸出

Check LXC Daemon Status
[[email protected]
~]# systemctl status lxc.service lxc.service - LXC Container Initialization and Autoboot Code Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled) Active: inactive (dead) [[email protected] ~]# systemctl start lxc.service [[email protected] ~]# 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用如下命令檢查LXC核心虛擬化狀態.

# lxc-checkconfig
  • 1

示例輸出

Check LXC Kernel Virtualization Configuration
[[email protected] ~]# 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
  • 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

Step 2: 在Linux上建立和管理LXC容器

5 . 使用如下命令列出已經安裝到你的系統之中可用的LXC模板容器.

# ls -alh /usr/share/lxc/templates/
  • 1
List LXC Templates Containers
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

6 . 建立一個LXC容器的過程非常簡單. 下面這條語句就可用於建立一個新的容器.

# lxc-create -n container_name -t container_template
  • 1

就像下面的引用的摘要資訊中看到的那樣,我們將建立一個新的名為mydeb的容器,而這個容器基於從LXC倉庫中pull下來的模板而建立的debain的。

Creating LXC Container
[[email protected] ~]# 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...
...
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

7 . 當一系列基本的依賴組建和package都被下載和安裝到你的系統之後,容器也會被創建出來。當建立過程結束,你的預設的root的使用者密碼將會被顯示。為了安全考慮,一旦登入到容器控制檯之後請修改密碼。

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

8 . 現在你可以使用lxc-ls去列出你的容器,以及使用lxc-info去獲取啟動中和停止了的容器資訊了。

為了啟動一個新的容器在後臺 (指定-d選項執行) ,使用如下命令:

# lxc-start -n mydeb -d
  • 1

9 . 當容器被啟動之後,你可以使用lxc-ls –active 命令列出正在執行的容器以及取得詳細的正在執行的容器的資訊。

# lxc-ls --active
  • 1

10 . 為了能夠登陸到容器的控制檯,需要指定一個正在執行的容器名使用lxc-console命令。使用root和密碼進行登陸,密碼預設則是由lxc的超級使用者所建立的。

一旦登陸到容器之中,你可以執行幾條命令確認詳細資訊。通過察看/etc/issue.net檔案內容確認詳細資訊,使用passwd命令改變root的密碼,使用ifconfig來確認網路介面的詳細資訊。

# lxc-console -n mydeb
# cat /etc/issue.net
# ifconfig
# passwd
  • 1
  • 2
  • 3
  • 4

示例輸出

Connect to LXC Container
[[email protected]~]# lxc-console -n mydcb
Connected to tty 1
Type &ltCtrl+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 protected]:~# cat /etc/issue.net
Debian GNU/Linux 8
[email protected]:~# 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 protected]:~# passwd root
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
  • 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

11 . 敲擊Ctrl+a然後q,就可以從容器控制檯中退出而回到宿主機控制檯上。
而通過執行如下命令這可以停止一個正在執行中的容器。

# lxc-stop -n mydcb
  • 1

12 . 為了建立一個基於Ubuntu模板的LXC容器, 你需要進入到/usr/sbin/目錄下並建立如下debootstrap連結.

# cd /usr/sbin
# ln -s debootstrap qemu-debootstrap
  • 1
  • 2

13 . 使用vi編輯器編輯qemu-debootstrap檔案並按照如下方式替換兩行MIRROR的行的資訊:

DEF_MIRROR=”http://mirrors.kernel.org/ubuntu”
DEF_HTTPS_MIRROR=”https://mirrors.kernel.org/ubuntu”
  • 1
  • 2

詳細資訊可以參看如下:

....
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”
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

14 . 最後,使用相同的lxc-create命令建立一個新的基於Ubuntu模板LXC容器。

一旦建立Ubuntu容器的過程結束,顯示登陸資訊的信心將會如被顯示在螢幕上。

# lxc-create -n myubuntu -t ubuntu
  • 1

示例輸出

Create LXC Ubuntu Container
Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... 
Installing packages in template: ssh,vim,language-pack-en
Downloading ubuntu precise minimal ...
  • 1
  • 2
  • 3
  • 4

15 . 為了建立一個特定的基於本地模板的容器,需要按照如下語法格式進行建立:

# lxc-create -n container_name -t container_template -- -r distro_release -a distro_architercture 
  • 1

比如建立一個amd64系統的debian wheezy 容器可以使用如下命令:

# lxc-create -n mywheezy -t debian -- -r wheezy -a amd64
  • 1

示例輸出

Create LXC Container Based on Distro Architecture
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
...
  • 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

16 . 從LXC倉庫中下載通用模板,然後基於此模板建立容器。

# lxc-create -n mycentos6 -t download -- -d centos -r 6 -a i386
  • 1

lxc-create 命令列的其他選項:

-n = name 
-t = template
-d = distibution
-a = arch
-r = release
  • 1
  • 2
  • 3
  • 4
  • 5

17 . 指定容器名可以使用lxc-destroy命令刪除容器。

# lxc-destroy -n mywheezy
  • 1

18 . 使用 lxc-clone 命令可以clone一個已經存在的容器:

# lxc-clone mydeb mydeb-clone
  • 1

19 . 所有建立的容器都存放在 /var/lib/lxc/ 目錄. 如果因為某些特定原因,你需要手動地調整容器設定你需要編輯每個容器目錄的設定檔案。

# ls /var/lib/lxc
  • 1

這些就是你需要了解的關於LXC相關的最基礎的資訊。

原文: http://www.tecmint.com/install-create-run-lxc-linux-containers-on-centos/

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://www.cnblogs.com/captainbed