LXC(Linux Containers, Linux 容器)
是一种基于 Linux 内核的轻量级虚拟化技术,允许在单一主机上运行多个相互隔离的 Linux 系统环境(容器)。它通过 Linux 内核的 cgroups(控制组)和 namespaces(命名空间)技术实现资源隔离与管理,提供类似虚拟机的体验,但无需完整操作系统,因此更高效。
介绍
LXD
:LXC 的增强管理工具,提供更友好的 CLI 和 API,支持快照、跨主机迁移等功能
lxd(Linux Container Daemon, Linux容器守护程序)
提供了 REST API 的 lxc 容器管理器,可以通过 API 进行容器的管理和控制
- lxc 提供了容器技术的基础,而 lxd 则是对 lxc 进行了更高级别的管理和控制,提供更多的功能和工具
与 Incus 的关系
- 曾经 LXD 是 linuxcontainers 社区维护的项目,LXC 是命令行工具,LXD 是后台守护进程。
- 后来 Canonical 公司接管了 LXD 项目进行开发维护,linuxcontainers 社区分叉并独立维护了 Incus 项目作为 LXC 和 LXD 的替代。
- 经过 LXD/Incus 项目分叉后,目前差异如下:
- Incus 有自己独立的命令行工具和守护进程(incus/incusd),并且只使用 incus 命令交互,不再暴露 incusd 命令给使用者。本镜像库(linuxcontainers 镜像)只能用于 Incus 项目。
- LXC/LXD 继续沿用之前体系,并且只能使用 Canonical 公司的镜像库。LXC/LXD 不能使用本镜像库。如果强行让 LXC/LXD 使用本镜像库,有些镜像可能可以启动,但是会出现各种各样的兼容性问题,例如虚拟机镜像无法启动 agent,无法进行 “exec”。
参考来
核心特性
轻量级
:共享主机内核,资源占用极低(相比传统虚拟机)。
快速启动
:容器可在秒级内启动或停止。
隔离性
:每个容器拥有独立的进程、网络、文件系统等。
资源限制
:通过 cgroups 限制 CPU、内存、磁盘 I/O 等资源。
灵活性
:支持运行多种 Linux 发行版(Ubuntu、CentOS 等)。
LXC vs 其他技术
技术 |
特点 |
传统虚拟机 |
完全虚拟化硬件,运行独立内核,资源占用高,启动慢 |
Docker |
基于容器,专注于应用级封装(单个进程),依赖镜像分层和标准化工具 |
LXC |
系统级容器,模拟完整 Linux 环境,适合需要多进程或完整 OS 功能的场景 |
lxc 安装&使用
# 安装 LXC
sudo apt-get install lxc # Debian/Ubuntu
# 创建容器(以 Ubuntu 为例)
sudo lxc-create -t download -n my_container -- -d ubuntu -r jammy -a amd64
# 启动容器
sudo lxc-start -n my_container
# 进入容器终端
sudo lxc-attach -n my_container
# 查看运行中的容器
sudo lxc-ls --active
# 停止容器
sudo lxc-stop -n my_container
lxd 安装&使用
sudo apt update
sudo apt install snapd
sudo snap install lxd
# 重新安装
sudo snap refresh lxd
# 初始化lxd
lxd init
lxd init --minimal
lxc help
$ lxc --help
Description:
Command line client for LXD
All of LXD's features can be driven through the various commands below.
For help with any of those, simply call them with --help.
Usage:
lxc [command]
Available Commands:
alias Manage command aliases
auth Manage user authorization
cluster Manage cluster members
completion Generate the autocompletion script for the specified shell
config Manage instance and server configuration options
console Attach to instance consoles
copy Copy instances within or in between LXD servers
delete Delete instances and snapshots
exec Execute commands in instances
export Export instance backups
file Manage files in instances
help Help about any command
image Manage images
import Import instance backups
info Show instance or server information
init Create instances from images
launch Create and start instances from images
list List instances
monitor Monitor a local or remote LXD server
move Move instances within or in between LXD servers
network Manage and attach instances to networks
operation List, show and delete background operations
pause Pause instances
profile Manage profiles
project Manage projects
publish Publish instances as images
query Send a raw query to LXD
rebuild Rebuild instances
remote Manage the list of remote servers
rename Rename instances and snapshots
restart Restart instances
restore Restore instances from snapshots
snapshot Create instance snapshots
start Start instances
stop Stop instances
storage Manage storage pools and volumes
version Show local and remote versions
warning Manage warnings
Flags:
--all Show less common commands
--debug Show all debug messages
--force-local Force using the local unix socket
-h, --help Print help
--project Override the source project
-q, --quiet Don't show progress information
--sub-commands Use with help or --help to view sub-commands
-v, --verbose Show all information messages
--version Print version number
Use "lxc [command] --help" for more information about a command.
镜像管理
# 查看镜像
lxc image list
# 删除镜像
lxc image delete <fingerprint-or-alias>
# 导出镜像,生成两个文件
lxc image export <image-alias> /home/exported-image.tar.xz
# 导入镜像
lxc image import 文件1.xz 文件2.root --alias <new-image-alias>
# 制作镜像
lxc publish <container-name> --alias <image-name?>
# 配置镜像源
lxc remote add mirror-images https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public
lxc remote list
# 获取镜像列表,注意后面有个 `:`
lxc image list mirror-images:
lxc image list mirror-images:ubuntu
lxc image list
启动&配置容器
# 启动第一个容器
lxc launch <image-name> <container-name>
lxc launch <fingerprint> <container-name>
lxc launch ubuntu:24.10 firstlxc
lxc launch ubuntu:22.04 firstlxc2 --config limits.cpu=1 --config limits.memory=192MiB
# 获取容器列表
$ lxc list
+----------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| firstlxc | RUNNING | 10.73.236.193 (eth0) | fd42:1f60:809d:34a2:216:3eff:feda:4770 (eth0) | CONTAINER | 0 |
+----------+---------+----------------------+-----------------------------------------------+-----------+-----------+
# 查看容器信息
lxc info firstlxc
# 查看配置
lxc config show firstlxc
# 修改配置
lxc config set firstlxc limits.memory=128MiB
# 查容器内存
lxc exec firstlxc -- free -m
# 查容器cpu
lxc exec firstlxc -- nproc
# 启动容器
lxc start <container-name>
# 停止容器
lxc stop <container-name>
# 删除容器
lxc delete <container-name>
# 强制删除
lxc delete <container-name> --force
容器交互
lxc exec first -- <bash或者lxc shell 容器名>
# 交互式
# 执行命令:进入交互界面
lxc exec firstlxc -- bash
# 执行命令
cat /etc/*release
# 退出交互shell
exit
# 非交互式
lxc exec firstlxc -- apt-get update
lxc exec firstlxc -- apt-get install sl -y
lxc exec firstlxc -- /usr/games/sl
文件导入和导出
# 从 lxd 容器中拉取文件或目录到本地系统
lxc file pull <container_name>/<remote_path> [<local_path>]
# 用于将本地系统中的文件或目录推送到 lxd 容器中
lxc file push [<local_path>] <container_name>/<remote_path>
快照管理
# 创建容器first的快照clean
lxc snapshot firstlxc clean
# 查看快照
lsx list firstlxc
lsx info firstlxc
# 恢复快照
lxc restore firstlxc clean
# 删除快照
lxc delete first/clean
配置
# 设置端口映射
# proxy: 设备类型,表示端口代理
# listen=tcp:0.0.0.0:8080: 定义在主机上监听的地址和端口
# connect=tcp:127.0.0.1:80: 定义连接到容器内部的地址和端口
lxc config device add <container-name> <device-name> proxy listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:80
# 设置磁盘目录/挂载目录
lxc config device add <container-name> <device-name>t disk source=/host/path/path/on/host path=/container/path
# 查看所有设备
lxc config device list
其他
Proxmox VE
:集成 LXC 的虚拟化管理平台