在 RHEL、Rocky 和 AlmaLinux 中安装 LXC(Linux 容器)
介绍
LXC是Linux Containers的缩写,是一种操作系统级虚拟化方法,允许您在单个主机上运行多个隔离的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 容器,使您能够在单个主机上运行多个隔离的环境。通过执行本文中概述的步骤,您可以开始探索容器化的世界,并利用它在资源利用率和应用程序隔离方面提供的优势。