Featured image of post Debian 网关 [Episode 07]: 安装 LXD

Debian 网关 [Episode 07]: 安装 LXD

安装 Linux Containers (Linux 容器)

LXD is a next generation system container and virtual machine manager. It offers a unified user experience around full Linux systems running inside containers or virtual machines.

不是说安装 LXC 吗,怎么变成 LXD 了:

  • LXD 可以理解为第二代 LXC,本质上没什么区别,只是指令和管理方式变了
  • LXC 依赖 dnsmasq 且默认启用,和我们的网络配置有点小冲突
  • LXD 只需要设置一次镜像,不需要每次下载都指定镜像了

安装 LXD

通过 apt 安装

1sudo apt update && sudo apt install -y lxd

初始化配置

 1$ sudo lxd init
 2
 3Would you like to use LXD clustering? (yes/no) [default=no]: no
 4Do you want to configure a new storage pool? (yes/no) [default=yes]: yes
 5Name of the new storage pool [default=default]: default
 6Name of the storage backend to use (dir, zfs) [default=zfs]: dir
 7Would you like to connect to a MAAS server? (yes/no) [default=no]: no
 8Would you like to create a new local network bridge? (yes/no) [default=yes]: no
 9Would you like to configure LXD to use an existing bridge or host interface? (yes/no) [default=no]: yes
10Name of the existing bridge or host interface: br-lan
11Would you like the LXD server to be available over the network? (yes/no) [default=no]: no
12Would you like stale cached images to be updated automatically? (yes/no) [default=yes]: yes
13Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: no
  • 其中:
    • line 6 询问是否使用 zfs,由于我们只在数据盘上创建了 zfs,在这里启用会干扰数据盘的速度
    • line 8 询问是否创建新的网桥
    • line 9 询问是加入已有网桥
    • line 10 询问是已有网桥名称
  • 默认数据存储位置: /var/lib/lxd/storage-pools/default

设置镜像

 1# 任选一个
 2# 北外镜像
 3export MIRROR_URL="https://mirrors.bfsu.edu.cn/lxc-images/"
 4# 南京大学
 5export MIRROR_URL="https://mirror.nju.edu.cn/lxc-images/"
 6
 7# 设置镜像
 8# 当前用户
 9lxc remote add mirror ${MIRROR_URL} --protocol=simplestreams --public
10# root 用户
11sudo lxc remote add mirror ${MIRROR_URL} --protocol=simplestreams --public

授权使用 lxc 命令

本质上和 Docker 一样,将用户加入到对应组内,使用户拥有 sock 的读写权限

1sudo usermod -aG lxd ${USER}

重新登陆生效

基本使用方法

查看可用镜像

1lxc image list mirror:
2# with filter
3lxc image list mirror: architecture=x86_64 type=container

创建并启动容器

1# lxc launch <remote>:<image_name> <name> <args>
2lxc launch mirror:debian/12/cloud test
  • 默认均为非特权容器
  • 与 lxc 不一样,用户间容器不隔离,统一管理
  • 可通过指定参数创建特权容器
  • 也可后续修改为特权容器

列出所有容器

1lxc list

附加 | Attach

LXD 并没有提供类似 lxc-attach 的功能,但是可用这样实现:

1lxc exec <name> -- /bin/bash
2# 对于 Openwrt/Alpine
3lxc exec <name> -- /bin/ash

停止并销毁容器

1lxc stop <name>
2lxc delete <name>

在 LXD 中运行 podman

绑定静态IP

通过 dnsmasq 实现给指定主机名设置静态IP

编辑 /etc/dnsmasq.conf

1# dhcp-host=<IP>,<hostname>,infinite
2dhcp-host=192.168.64.2,podman,infinite

创建容器

  • 若要使用特权容器:在 config: 中追加 security.privileged: true
 1sudo lxc launch mirror:debian/12/cloud podman <<EOF
 2config:
 3  security.nesting: true
 4  linux.kernel_modules: ip_tables,ip6_tables,netlink_diag,nf_nat,overlay
 5  raw.lxc: |-
 6    lxc.apparmor.profile=unconfined
 7    lxc.mount.auto=proc:rw sys:rw cgroup:rw
 8    lxc.cgroup.devices.allow=a
 9    lxc.cap.drop=
10EOF

设置容器内系统的镜像

 1sudo lxc exec podman -- /bin/bash
 2
 3export MIRROR_URL="https://mirrors.bfsu.edu.cn"
 4export BRANCH="bookworm"
 5export COMPONENT="main contrib non-free non-free-firmware"
 6
 7echo "deb ${MIRROR_URL}/debian/ ${BRANCH} ${COMPONENT}
 8deb ${MIRROR_URL}/debian/ ${BRANCH}-updates ${COMPONENT}
 9deb ${MIRROR_URL}/debian-security/ ${BRANCH}-security ${COMPONENT}" | \
10tee /etc/apt/sources.list && apt update

安装 podman

1apt install -y podman podman-compose

测试

1podman run --rm -it alpine

[笔记] k3s

映射 /dev/kmsg

1echo 'L /dev/kmsg - - - - /dev/console' > /etc/tmpfiles.d/kmsg.conf
2systemctl reboot

安装及运行 k3s

1sudo apt install -y curl
2curl -fLO "https://github.com/k3s-io/k3s/releases/latest/download/k3s"
3chmod 755 k3s
4sudo mv k3s /usr/local/sbin
5
6k3s server --write-kubeconfig-mode 644
CC BY-NC-SA 4.0 转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!
最后更新于 Apr 28, 2023 03:00 +0800
人最重要的特点是会思考,写博客的初衷是保存思维的电子快照。 当然各种文档工具亦或是简单的文本编辑器也能随时记录点滴,选择建个人网站无非是想满足技术爱好者的偏执。
Built with Hugo
主题 StackJimmy 设计