跳至内容

第 1 章: 安装和配置

在本章中,您需要以 root 用户身份登录,或者能够以sudo身份执行 root 权限。

安装 EPEL 和 OpenZFS 仓库

LXD 需要 EPEL(企业版 Linux 额外软件包)仓库,可以使用以下方法轻松安装:

dnf install epel-release

安装完成后,验证没有更新

dnf upgrade

如果升级过程中存在内核更新,请重新启动服务器。

8 和 9 的 OpenZFS 仓库

使用以下命令安装 OpenZFS 仓库:

dnf install https://zfsonlinux.org/epel/zfs-release-2-2$(rpm --eval "%{dist}").noarch.rpm

安装 snapddkmsvimkernel-devel

LXD 安装需要在 Rocky Linux 上使用 snap 包。因此,您需要使用以下命令安装 snapd(以及一些其他有用的程序):

dnf install snapd dkms vim kernel-devel

现在启用并启动 snapd

systemctl enable snapd

然后运行

systemctl start snapd

在继续操作之前,重新启动服务器。

安装 LXD

安装 LXD 需要使用 snap 命令。此时,您只是在安装它,而不是进行设置

snap install lxd

安装 OpenZFS

dnf install zfs

环境设置

大多数服务器内核设置不足以运行大量容器。如果您从一开始就假设您将在生产中使用您的服务器,则需要提前进行这些更改以避免出现诸如“打开文件太多”之类的错误。

幸运的是,调整 LXD 的设置并不难,只需修改几个文件并重新启动即可。

修改 limits.conf

您需要更改的第一个文件是 limits.conf 文件。该文件是自文档的。查看文件中的注释中的解释以了解该文件的功能。要进行修改,请输入

vi /etc/security/limits.conf

整个文件都由注释组成,并在底部显示了当前默认设置。在文件结束标记 (#End of file) 上方空白处,您需要添加自定义设置。完成后的文件末尾将如下所示:

# Modifications made for LXD

*               soft    nofile           1048576
*               hard    nofile           1048576
root            soft    nofile           1048576
root            hard    nofile           1048576
*               soft    memlock          unlimited
*               hard    memlock          unlimited

保存您的更改并退出 (Shift+:+w+q+! 用于vi)。

使用 90-lxd.override.conf 修改 sysctl.conf

使用systemd,您可以更改系统整体配置和内核选项而不修改主配置文件。相反,将您的设置放在一个单独的文件中,该文件将覆盖您需要的特定设置。

要进行这些内核更改,您将创建一个名为 90-lxd-override.conf 的文件,位于 /etc/sysctl.d 中。要执行此操作,请键入

vi /etc/sysctl.d/90-lxd-override.conf

RL 9 和 net.core.bpf_jit_limit 的最大值

由于最近的内核安全更新,net.core.bpf_jit_limit 的最大值似乎为 1000000000。如果您运行的是 Rocky Linux 9.x,请在下面的自文档文件中调整此值。如果您将其设置为高于此限制根本没有设置,它将默认为系统的默认值 264241152,如果您运行大量容器,这可能不够。

将以下内容放置在该文件中。请注意,如果您想知道自己在做什么,文件内容是自文档的

## The following changes have been made for LXD ##

# fs.inotify.max_queued_events specifies an upper limit on the number of events that can be queued to the corresponding inotify instance
 - (default is 16384)

fs.inotify.max_queued_events = 1048576

# fs.inotify.max_user_instances This specifies an upper limit on the number of inotify instances that can be created per real user ID -
(default value is 128)

fs.inotify.max_user_instances = 1048576

# fs.inotify.max_user_watches specifies an upper limit on the number of watches that can be created per real user ID - (default is 8192)

fs.inotify.max_user_watches = 1048576

# vm.max_map_count contains the maximum number of memory map areas a process may have. Memory map areas are used as a side-effect of cal
ling malloc, directly by mmap and mprotect, and also when loading shared libraries - (default is 65530)

vm.max_map_count = 262144

# kernel.dmesg_restrict denies container access to the messages in the kernel ring buffer. Please note that this also will deny access t
o non-root users on the host system - (default is 0)

kernel.dmesg_restrict = 1

# This is the maximum number of entries in ARP table (IPv4). You should increase this if you create over 1024 containers.

net.ipv4.neigh.default.gc_thresh3 = 8192

# This is the maximum number of entries in ARP table (IPv6). You should increase this if you plan to create over 1024 containers.Not nee
ded if not using IPv6, but...

net.ipv6.neigh.default.gc_thresh3 = 8192

# This is a limit on the size of eBPF JIT allocations which is usually set to PAGE_SIZE * 40000. Set this to 1000000000 if you are running Rocky Linux 9.x

net.core.bpf_jit_limit = 3000000000

# This is the maximum number of keys a non-root user can use, should be higher than the number of containers

kernel.keys.maxkeys = 2000

# This is the maximum size of the keyring non-root users can use

kernel.keys.maxbytes = 2000000

# This is the maximum number of concurrent async I/O operations. You might need to increase it further if you have a lot of workloads th
at use the AIO subsystem (e.g. MySQL)

fs.aio-max-nr = 524288

保存您的更改并退出。

此时,重新启动服务器。

检查sysctl.conf

重新启动后,以 root 用户身份重新登录服务器。您需要检查我们的覆盖文件是否已完成工作。

这并不难。除非您想验证所有设置,否则无需验证所有设置,但检查几个设置将验证设置是否已更改。使用 sysctl 命令执行此操作

sysctl net.core.bpf_jit_limit

这将显示

net.core.bpf_jit_limit = 3000000000

对覆盖文件中的其他几个设置执行相同的操作以验证更改。

作者:史蒂文·斯宾塞

贡献者:埃塞奎尔·布鲁尼、安娜·吉尔诺娃