LXD教程入门实践 配置独立ip 挂载gpu显卡驱动 制作镜像

机器环境

  • 以Ubuntu 16.04.6 LTS系统为例

安装lxd3.0

方式一、apt安装方式lxd

sudo apt install lxd
  • ubuntu16系统使用apt方式安装的lxd是2.0
  • ubuntu18系统使用apt方式安装的lxd是3.0
  • 实际工作需要lxd3.0,故使用以下snap方式安装

方式二、snap安装方式lxd

sudo snap install lxd --channel=3.0/stable

安装zfs和bridge-utils

  • zfs用于管理物理磁盘,支持lxd高级功能
  • bridge-utils 用于搭建网桥
sudo apt-get install zfsutils-linux bridge-utils

配置网桥

  • 一般教程,安装完lxd相关依赖,会进行sudo lxd init操作
  • 建议在初始化前,配置好网桥,可实现独立ip
    • lxd3.0版本才有此功能,会用到bridge-utils依赖包

查看机器网络情况

root@ubuntu:~# ifconfig
enp2s0    Link encap:Ethernet  HWaddr 44:8a:5b:a4:62:8d  
          inet addr:192.168.1.152  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::468a:5bff:fea4:628d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:308676 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23853 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:44473910 (44.4 MB)  TX bytes:1766587 (1.7 MB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:241 errors:0 dropped:0 overruns:0 frame:0
          TX packets:241 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:19272 (19.2 KB)  TX bytes:19272 (19.2 KB)
  • 注意记住上图的enp2s0lo

修改机器网络配置

编辑/etc/network/interfaces,参考以下

auto lo
iface lo inet loopback

auto br1
iface br1 inet dhcp
    bridge_ports enp2s0
iface enp2s0 inet dhcp

先关闭enp2s0,再打开br1

sudo ifdown enp2s0
sudo ifup br1
  • 此时若是外网ssh连接的机器,可能会失去连接,建议在本机操作

将br1与容器相连

  • 在容器已初始化的情况下,需执行命令进行br1网络与容器相连,参考文末链接文章
  • 此文配置完网络后,还会初始化lxd容器,后续在sudo lxd init时可配置使br1与容器相连

LXD 初始化

sudo lxd init

ZFS设置大小要尽量大,注意网络相关选项,参考如下:

Do you want to configure a new storage pool (yes/no) [default=yes]? 
Name of the storage backend to use (dir or zfs) [default=zfs]: ^C
root@ubuntu:~# sudo lxd init
Do you want to configure a new storage pool (yes/no) [default=yes]? yes
Name of the storage backend to use (dir or zfs) [default=zfs]: zfs
Create a new ZFS pool (yes/no) [default=yes]? yes
Name of the new ZFS pool or dataset [default=lxd]: lxd
Would you like to use an existing block device (yes/no) [default=no]? no
Size in GB of the new loop device (1GB minimum) [default=43]: 43
Would you like LXD to be available over the network (yes/no) [default=no]? no
Do you want to configure the LXD bridge (yes/no) [default=yes]? yes

此时会跳到图形界面,根据提示选择不创建新的网桥,其中关键提示如下:

Would you like to configure LXD to use an existing bridge or host interface? (yes/no) [default=no]: yes
Name of the existing bridge or host interface: br1

执行完毕后,lxd安装成功,终端会有如下显示

LXD has been successfully configured.

容器操作

  • lxd3.0安装完毕后,可安装不同操作系统的容器。
  • 此时系统镜像来源于网络(此文会配置为清华源)
  • 在安装了原生操作系统(此文以ubuntu16为例)的容器中,安装openssh-server,配置好公钥和ssh配置文件,再制作成本地镜像,后续可使用此镜像安装新容器,则新容器可直接ssh密钥登录

加速源

  • 备份原image源,并添加清华源
    sudo lxc remote rename images images_bak 
    sudo lxc remote add images https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public
    
  • 列出远端images源的可用镜像
    sudo lxc image list images: 
    

下载镜像和安装容器

方式一、下载镜像到本地,再用本地镜像安装容器

  • 将远端镜像下载到本地,暂不安装容器
    sudo lxc image copy images:ubuntu/16.04 local:
    
  • 列出本地镜像
    sudo lxc image list
    
    root@ubunt:~# sudo lxc image list
    +-------+--------------+--------+--------------------------------------+--------+---------+-----------------------------+
    | ALIAS | FINGERPRINT  | PUBLIC |             DESCRIPTION              |  ARCH  |  SIZE   |         UPLOAD DATE         |
    +-------+--------------+--------+--------------------------------------+--------+---------+-----------------------------+
    |       | 92e395dbcf45 | no     | Ubuntu xenial amd64 (20191202_07:42) | x86_64 | 80.64MB | Dec 3, 2019 at 3:51am (UTC) |
    +-------+--------------+--------+--------------------------------------+--------+---------+-----------------------------+
    
  • 根据本地镜像安装容器
    • 容器命名貌似不能带下划线
    sudo lxc launch local:92e395dbcf45 ubuntu16-clean
    

方式二、直接用远端镜像安装容器,此时远端镜像也会自动保存在本地

  • 根据远端镜像安装容器

    sudo lxc launch images:ubuntu/18.04 ubuntu18-clean
    
  • 列出本地镜像

    root@ubuntu:~# sudo lxc image list
    +-------+--------------+----
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
LXC(Linux Containers)和LXD(Linux Container Daemon)是一种轻量级虚拟化技术,常用于在Linux系统中创建和管理容器。以下是一些常用的LXC/LXD配置: 1. 安装LXD:使用适合你的Linux发行版的包管理工具进行安装。 2. 初始化LXD:运行`sudo lxd init`命令来初始化LXD。在初始化过程中,你可以设置默认的存储池、网络设置、镜像源等。 3. 创建容器:使用`lxc launch`命令创建一个新容器。例如,要创建一个名为"mycontainer"的容器,可以运行`lxc launch images:ubuntu/20.04 mycontainer`。 4. 启动和停止容器:使用`lxc start`命令启动容器,例如`lxc start mycontainer`。使用`lxc stop`命令停止容器,例如`lxc stop mycontainer`。 5. 进入容器:使用`lxc exec`命令可以在容器内执行命令。例如,要在"mycontainer"容器内执行`ls`命令,可以运行`lxc exec mycontainer -- ls`。 6. 管理容器配置:使用`lxc config`命令可以管理容器的配置。例如,要查看容器的IP地址,可以运行`lxc config show mycontainer | grep ipv4.address`。 7. 管理网络:使用`lxc network`命令可以管理LXD网络。例如,要列出所有网络,可以运行`lxc network list`。 8. 导出和导入容器:使用`lxc export`命令可以将容器导出到一个文件。例如,要将"mycontainer"导出到"mycontainer.tar.gz"文件,可以运行`lxc export mycontainer mycontainer.tar.gz`。使用`lxc import`命令可以从导出文件导入一个容器。 这些是LXC/LXD的一些常用配置,还有其他更高级的配置和功能可供探索。你可以使用`lxc --help`命令或参考LXD的官方文档来获取更多详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值