机器环境
- 以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)
- 注意记住上图的
enp2s0
和lo
修改机器网络配置
编辑
/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 +-------+--------------+----