systemd-nspawn容器的简单使用

一、准备网络

1.1 基础网络配置

禁用系统默认网络管理器,启用systemd-networkd

systemctl disable network
systemctl disable networking
systemctl disable NetworkManager
systemctl enable systemd-networkd

1.2 创建网桥

创建bridge网桥规则文件

cat > /etc/systemd/network/br0.netdev <<EOF
[NetDev]
Name=br0
Kind=bridge
EOF
cat > /etc/systemd/network/br0.network <<EOF
[Match]
Name=br0

[Network]
DHCP=ipv4
EOF

重启启动systemd-networkd网络管理服务

systemctl restart systemd-networkd

1.3 启用域名解析

启用systemd-resolved本地DNS缓存服务

systemctl enable systemd-resolved
systemctl start systemd-resolved
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

测试网络是否正常

ping www.163.com

二、配置环境

2.1 启动machines.target

systemctl start machines.target

2.2 配置镜像分区

  1. 创建LV卷来存放镜像

    lvcreate -n machines -L 50G
    
  2. 格式化为BTRFS分区

    mkfs.btrfs /dev/${VGNAME}/machines
    
  3. 编写分区挂载文件

    system.mount方式挂载文件系统可防止因分区故障导致系统无法启动(系统分区无法挂载会进入紧急模式)。

    cat > /etc/systemd/system/var-lib-machines.mount <<EOF
    [Unit]
    Documentation=man:systemd-nspawn(1)
    Description=Virtual Machine and ContainerStorage
    ConditionPathExists=/dev/sys/machines
    
    [Mount]
    What=/dev/disk/by-uuid/2fbd6637-5615-4c75-9fa9-d7e814ed966f
    Where=/var/lib/machines
    Type=btrfs
    
    [Install]
    WantedBy=machines.target
    EOF
    

    UUID根据你自己创建的LV卷来确定,此处示例的分区UUD为2fbd6637-5615-4c75-9fa9-d7e814ed966f,可以使用命令blkid来查看所有分区的UUID

    更多文档语法请查看手册man(5):system.mount

  4. 挂载分区

    systemctl enable var-lib-machines.mount
    systemctl start var-lib-machines.mount
    

三、创建镜像

3.1 构建镜像

可以通过多种途径构建镜像,包括下面几种:

machinectl --verify=no pull-tar https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-root.tar.gz xenial-base

3.2 配置镜像

启动镜像

systemd-nspawn --boot --directory=/var/lib/machines/xenial-base --network-bridge=br0

以下操作在镜像内配置

修改镜像密码

passwd

卸载镜像Cloud-init工具

apt remove cloud-init

配置镜像使用DHCP获取IP地址

cat > /etc/systemd/network/host.network <<EOF
[Match]
Name=host0

[Network]
DHCP=ipv4
EOF

启动镜像systemd-networkd服务

systemctl enable systemd-networkd

配置镜像systemd-resolve获取DNS

systemctl enable systemd-resolve
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

配置结束后输入exit退出镜像

只读镜像,将镜像设置为只读可防止修改。

machinectl read-only xenial-base true

四、创建容器

4.1 构建容器

将刚创建好的镜像克隆成容器

machinectl clone xenial-base plex

4.2 配置容器

将容器参数以文件保存,这样就不用每次启动镜像都指定参数

如需创建配置文件,确保容器配置目录已创建,默认该目录不存在,需要手动创建。

mkdir -p /etc/systemd/nspawn
cat > /etc/systemd/nspawn/plex.nspawn <<EOF
[Exec]
Boot=on

[Files]
Bind=/mnt/media:/mnt

[Network]
VirtualEthernet=yes
Bridge=br0
EOF

更多规则请查看man(5):system.nspawn

4.3 启动容器

machinectl start plex

4.4 查看容器运行情况

machinectl list

五、容器登陆方式

可以通过两种方式登陆到容器

5.1 shell访问

通过machinectl的来获取容器shell

machinectl shell root@plex -- /bin/bash

5.2 ssh访问

想使用ssh方式的话必须按照ssh服务

machinectl shell root@plex -- /bin/bash -c "dpkg-reconfigure openssh-server"

添加key

export KEY=$(cat ~/.ssh/id_rsa.pub)
machinectl shell root@plex -- /bin/bash -c "ssh-keygen -f /root/.ssh/id_rsa -t rsa -N ''"
machinectl shell root@plex -- /bin/bash -c "echo ${KEY} | tee -a /root/.ssh/authorized_keys"

完成上面操作的话就可以通过ssh来登陆到容器中

六、测试应用

本次演示的应用是plex

6.1 登陆到容器

machinectl shell root@plex -- /bin/bash

6.2 部署应用

安装应用plex

echo plex > /etc/hostname
echo deb https://downloads.plex.tv/repo/deb/ public main | sudo tee /etc/apt/sources.list.d/plexmediaserver.list
curl https://downloads.plex.tv/plex-keys/PlexSign.key | sudo apt-key add -
apt-get update
apt-get install -y plexmediaserver

查看容器的IP地址信息,即可通过IP地址来访问容器内的plex应用。

ip route get 1
1.0.0.0 via 172.16.24.2 dev br0  src 172.16.24.211
    cache

plex

七、其他新的容器

通过上面的几个步骤的操作,我们完成了容器的基本创建和配置。如果想创建新的容器,可按照下面步骤进行操作。

  • 创建容器的 根文件系统(rootfs),然后将镜像克隆成容器: machinectl clone xenial-base new-container

  • 构建容器的unit配置文件。

cat > /etc/systemd/nspawn/new-container.nspawn <<EOF
[Exec]
Boot=on

[Network]
VirtualEthernet=yes
Bridge=br0
EOF
  • 重新加载systemd服务配置:systemctl daemon-reload

  • 如果想让容器在系统启动时自动启动,则使用命令: systemctl enable systemd-nspawn@new-container

  • 手动启动则使用命令: machinectl start new-container


帮助文档

更多文档信息请查看官网文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值