在openwrt 下使用lxc容器环境

lxc容器比虚拟机更轻量,所有容器都是使用主机的内核,性能更好。适合不需要异构系统的情况下使用,例如在openwrt下面使用debian 的容器.

openwrt 21.02 开始支持lxc,虽然官方所有构建都支持lxc ,但为了性能考虑还是建议只在amd64,跟aarch64 主机上安装使用。

首先安装 lxc-checkconfig

opkg update
opkg install lxc-checkconfig

运行 lxc-checkconfig

提示:

Kernel configuration not found at /proc/config.gz; searching…
lxc-checkconfig: unable to retrieve kernel configuration

Try recompiling with IKCONFIG_PROC, installing the kernel headers,

这个是没有打开IKCONFIG_PROC,这个并不需要象网上说的那些重新编译内核,

opkg install kmod-ikconfig 安装模块就行

现在运行 lxc-checkconfig 提示 lxc-start: not found,除Cgroup 其它大部分已经通过了,

opkg install lxc-cgroup lxc-auto
service lxc-auto enable
service lxc-auto boot

现在运行 lxc-checkconfig 除 Cgroup v1 freezer controller: missing 其它都可以了,这个关系不大可以用v2.最后安装web管理

opkg install luci-i18n-lxc-zh-cn

打开web 管理界面 服务->lXC容器,这个时候模板下面默认是空的,这个模板每次打开页面都要从下面容器url跟新,看网络状态,一般需要30秒左右。

名称 是容器的名称,模板选择你要的系统。依照系统的大小,创建时间需要几分钟到几十种分钟
为了快一点我就建个openwrt.创建好 后点容器后面的more-> configure

注释原来的lxc.net.0.type = empty 改成下面的,br-lan是要使用的主机网络,hwaddr 为容器里面的mac地址,可以随意写,只要不冲突就行。
lxc.net.0.type = veth
lxc.net.0.link = br-lan
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:FF:AE:01:02:04

然后按照上面的提示需要kmod-veth,我们在安装一下 opkg install kmod-veth

然后启动容器。从主机的dhcp里面看到分配给容器的ip地址,然后跟操作虚拟机一样,ssh 或web 上去就行。

总结一下需要安的包

opkg update
opkg install kmod-veth kmod-ikconfig lxc-checkconfig lxc-cgroup lxc-auto luci-i18n-lxc-zh-cn

用命令从镜像下载容器,这个就是秒下载了.

lxc-create -n r1 -t download -- -d openwrt -a amd64 -r 21.02 --server mirrors.tuna.tsinghua.edu.cn/lxc-images --flush-cache

lxc-create -n debian -t download -- -l -d debian -a amd64 --server mirrors.tuna.tsinghua.edu.cn/lxc-images - --flush-cache

留下评论