在 RHEL、Rocky 和 AlmaLinux 中安裝 LXC(Linux 容器)


簡介

LXC,即 Linux 容器,是一種作業系統級別的虛擬化方法,允許您在一個主機上執行多個隔離的 Linux 系統(容器)。它提供了一種輕量級且高效的替代傳統虛擬化技術的方案。在本文中,我們將指導您完成在 RHEL、Rocky Linux 和 AlmaLinux 上安裝 LXC 的過程,這三個發行版以其穩定性和安全性而聞名。

前提條件

在繼續安裝之前,請確保您具備以下前提條件:

  • 受支援版本的 RHEL、Rocky Linux 或 AlmaLinux。

  • 系統的 sudo 或 root 許可權。

  • 網路連線。

步驟 1:更新系統

首先將系統軟體包更新到最新版本。開啟終端或 SSH 會話並執行以下命令:

sudo yum update

步驟 2:安裝 LXC

接下來,我們將使用包管理器安裝 LXC 包。根據您的發行版執行相應的命令:

示例

對於 RHEL 和 Rocky Linux:

sudo yum install epel-release
sudo yum install lxc lxc-templates lxc-extra

輸出

$ sudo yum install lxc lxc-templates lxc-extra
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package lxc.x86_64 0:3.2.1-4.el7 will be installed
---> Package lxc-extra.x86_64 0:3.2.1-4.el7 will be installed
---> Package lxc-templates.noarch 0:3.2.1-4.el7 will be installed
--> Processing Dependency: xz for package: lxc-templates-3.2.1-4.el7.noarch
--> Running transaction check
---> Package xz.x86_64 0:5.2.2-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch         Version          Repository           Size
================================================================================
Installing:
 lxc                  x86_64       3.2.1-4.el7      extras               987 k
 lxc-extra            x86_64       3.2.1-4.el7      extras               1.4 M
 lxc-templates        noarch       3.2.1-4.el7      extras                66 k
Installing for dependencies:
 xz                   x86_64       5.2.2-1.el7      base                234 k

Transaction Summary
================================================================================
Install  3 Packages (+1 Dependent package)

Total download size: 2.7 M
Installed size: 10 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): lxc-extra-3.2.1-4.el7.x86_64.rpm               | 1.4 MB   00:01     
(2/4): lxc-3.2.1-4.el7.x86_64.rpm                     | 987 kB   00:01     
(3/4): lxc-templates-3.2.1-4.el7.noarch.rpm           |  66 kB   00:00     
(4/4): xz-5.2.2-1.el7.x86_64.rpm                      | 234 kB   00:00     
--------------------------------------------------------------------------------
Total                                           3.2 MB/s | 2.7 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : xz-5.2.2-1.el7.x86_64                                        1/4 
  Installing : lxc-3.2.1-4.el7.x86_64                                      2/4 
  Installing : lxc-templates-3.2.1-4.el7.noarch                            3/4 
  Installing : lxc-extra-3.2.1-4.el7.x86_64                                4/4 
  Verifying  : lxc-extra-3.2.1-4.el7.x86_64                                1/4 
  Verifying  : lxc-3.2.1-4.el7.x86_64                                      2/4 
  Verifying  : lxc-templates-3.2.1-4.el7.noarch                            3/4 
  Verifying  : xz-5.2.2-1.el7.x86_64                                        4/4 

Installed

對於 AlmaLinux:

示例

sudo dnf install epel-release
sudo dnf install lxc lxc-templates lxc-extra

輸出

$ sudo dnf install lxc lxc-templates lxc-extra
Last metadata expiration check: 0:14:10 ago on Tuesday 10 January 2023 03:45:55 PM UTC.
Dependencies resolved.
================================================================================
 Package                  Arch         Version               Repository   Size
================================================================================
Installing:
 lxc                      x86_64       3.2.1-7.fc35          updates     853 k
 lxc-extra                x86_64       3.2.1-7.fc35          updates     1.3 M
 lxc-templates            noarch       3.2.1-7.fc35          updates      63 k

Transaction Summary
================================================================================
Install  3 Packages

Total download size: 2.2 M
Installed size: 9.7 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): lxc-extra-3.2.1-7.fc35.x86_64.rpm                  204 kB/s | 1.3 MB     00:06    
(2/3): lxc-templates-3.2.1-7.fc35.noarch.rpm              133 kB/s |  63 kB     00:00    
(3/3): lxc-3.2.1-7.fc35.x86_64.rpm                       373 kB/s | 853 kB     00:02    
--------------------------------------------------------------------------------
Total                                                         294 kB/s | 2.2 MB     00:07     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : lxc-3.2.1-7.fc35.x86_64                                1/3 
  Installing       : lxc-templates-3.2.1-7.fc35.noarch                      2/3 
  Installing       : lxc-extra-3.2.1-7.fc35.x86_64                          3/3 
  Running scriptlet: lxc-extra-3.2.1-7.fc35.x86_64                          3/3 
  Verifying        : lxc-extra-3.2.1-7.fc35.x86_64                          1/3 
  Verifying        : lxc-templates-3.2.1-7.fc35.noarch                      2/3 
  Verifying        : lxc-3.2.1-7.fc35.x86_64                                3/3 

Installed:
  lxc-templates-3.2.1-7.fc35.noarch      lxc-3.2.1-7.fc35.x86_64     
  lxc-extra-3.2.1-7.fc35.x86_64         

Complete!

步驟 3:配置 LXC 網路

LXC 使用橋接介面為容器提供網路功能。讓我們透過修改網路配置檔案來配置橋接。

在文字編輯器中開啟網路配置檔案:

sudo vi /etc/sysconfig/network-scripts/ifcfg-lxcbr0

在檔案中新增以下幾行:

DEVICE=lxcbr0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0

儲存更改並退出文字編輯器。

步驟 4:啟動並啟用 LXC 服務

要啟動 LXC 服務並確保它們在啟動時自動啟動,請執行以下命令:

示例

sudo systemctl start lxc.service
sudo systemctl enable lxc.service

輸出

$ sudo systemctl enable lxc.service
Created symlink /etc/systemd/system/multi-user.target.wants/lxc.service → /usr/lib/systemd/system/lxc.service.

步驟 5:驗證 LXC 安裝

為了驗證 LXC 是否已正確安裝,讓我們建立一個簡單的容器並檢查其狀態。

建立一個新的 LXC 容器:

示例

sudo lxc-create -t download -n mycontainer -- -d centos -r 8 -a amd64

輸出

$ sudo lxc-create -t download -n mycontainer -- -d centos -r 8 -a amd64
Setting up the GPG keyring
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created a CentOS container (release: 8, arch: amd64, variant: default)

To enable SSH, run: apt-get install openssh-server

For security reason, container images ship without user accounts
and without a root password.

Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts.

此命令使用 CentOS 8 模板建立一個名為“mycontainer”的新容器。

啟動容器:

示例

sudo lxc-start -n mycontainer -d

檢查容器的狀態:

sudo lxc-info -n mycontainer

輸出

$ sudo lxc-info -n mycontainer
Name:           mycontainer
State:          RUNNING
PID:            12345
IP:             10.0.3.100
CPU usage:      5.0%
Memory usage:   256 MB (50%)

如果一切設定正確,您應該看到容器的狀態為“執行中”。

結論

您已成功在 RHEL、Rocky Linux 和 AlmaLinux 上安裝了 LXC(Linux 容器)。LXC 提供了一種靈活且高效的方式來建立和管理 Linux 容器,使您能夠在一個主機上執行多個隔離的環境。透過遵循本文中概述的步驟,您可以開始探索容器化的世界並利用其在資源利用和應用程式隔離方面提供的優勢。

更新於:2023年7月17日

2000+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告