瘦了就帅了 / Podman的安装

Created Tue, 22 Jul 2025 18:49:39 +0800 Modified Sat, 26 Sep 2026 16:52:53 +0000

1. 在LXC容器下安装

修改特权容器

LXC容器必须是特权容器,但创建CT时,默认为非特权容器,要特别注意!!!

nano /etc/pve/lxc/<CTID>.conf
  • 删除或注释掉 unprivileged: 1 这一行。
  • 确保没有 lxc.idmap 相关的UID/GID映射配置(如有则删除)。
  • Web UI -> LXC 容器 -> 选项 (Options) -> 功能 (Features) -> 编辑 (Edit)
    • 勾选 FUSE
    • 勾选 嵌套

2. 安装

Debian

apt update \
&& apt install -y curl gpg gnupg2

# 查看 Debian版本
apt install -y lsb-release \
&& lsb_release -a

# if Debian Release == 13
# 添加 Kubic 项目的 Debian_Testing 软件源
echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_Testing/ /' | sudo tee /etc/apt/sources.list.d/kubic.list
# 下载并添加对应的 GPG 密钥
curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/Debian_Testing/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/kubic.gpg > /dev/null

# 更新软件包列表并安装 Podman
apt update \
    && apt install -y podman \
    && podman version 


# 安装 podman-compose
apt install -y pipx \
&& pipx ensurepath \
&& . ~/.bashrc \
&& pipx install podman-compose \
&& podman-compose version
    
# iptables必须安装,否则netavark无法运行
apt install -y iptables 
# 防火墙一定要加这条,否则容器之间的名称解析无法工作
iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT

AlmaLinux

dnf install -y podman python3 python3-pip \
    && podman --version ; podman info \
    && python3 -m pip install --upgrade pip \
    && python3 -m pip install podman-compose \
    && podman-compose -v \
    && systemctl enable podman-restart.service \
    && systemctl start podman-restart.service

3. 配置国内镜像源

# nano /etc/containers/registries.conf
unqualified-search-registries = ["docker.io"]

[[registry]]
prefix = "docker.io"
location = "docker.io"
[[registry.mirror]]
location = "docker.1ms.run"
[[registry.mirror]]
location = "hub.rat.dev"
[[registry.mirror]]
location = "docker.xuanyuan.me"

[[registry]]
prefix = "ghcr.io"
location = "ghcr.io"
[[registry.mirror]]
location = "ghcr.nju.edu.cn"

4. 配置日志

Rootful模式下使用

cat >> /etc/systemd/journald.conf << 'EOF'
# 限制磁盘上日志总大小(最关键的参数)
SystemMaxUse=200M
# 限制单个日志文件大小
SystemMaxFileSize=50M
# 限制保留的日志文件数量
SystemMaxFiles=5
# 运行时(内存中)日志大小,你已设置
RuntimeMaxUse=32M
EOF

systemctl restart systemd-journald
# 当 messages 超过 100M 时轮转,保留 3 个备份,并压缩旧文件。copytruncate 确保不需要重启 rsyslog
cat > /etc/logrotate.d/messages << 'EOF'
/var/log/messages {
    size 100M
    rotate 3
    compress
    missingok
    notifempty
    copytruncate
}
EOF

Rootless模式下使用

# /etc/containers/containers.conf
[containers]
# 将默认日志驱动从 journald 改为文件驱动
log_driver = "k8s-file"
# 设置日志文件大小上限 99 MB(例如 10 MiB = 10485760 字节)
log_size_max = 103809024

5. 修改 ping_group_range 行为

[containers]
default_sysctls = []

6. 测试podman是否安装成功

podman run --rm hello-world

其实命令与Docker一致,非常方便好用。