基于Ubuntu 16.04.5 LTS系统,首先安装lxd。
# sudo apt-get install lxd
将当前用加入到lxd用户组中:
# newgrp lxd
使用lxc list可查看当前系统的容器列表,目前还没有创建任何容器,列表为空。
# sudo lxc list
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+
在创建容器之前,需要先执行lxd init初始化全局配置。以下可见,lxd创建了一个名为lxdbr0的网桥,用户容器与宿主机通信,网桥IPv4地址设置为10.254.178.1,如果此地址与网络中其它子网地址相同,需要进行修正。另外依据lxd init提示创建了容器的IPv4网段(10.254.178.20 - 10.254.178.254)。IPv6网段暂不设置。
# sudo lxd init
...
Would you like to create a new network bridge (yes/no) [default=yes]?
What should the new bridge be called [default=lxdbr0]?
...
LXD has been successfully configured.
LXD以镜像images为基础来创建系统容器,支持远端镜像服务器或者本地镜像images仓库。目前LXD内置了三个默认的远端镜像服务器,和默认创建了一个本地镜像仓库,可使用如下命令查看,另外也可使用lxc remote add命令添加自定义的镜像服务器:
$ lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
| NAME | URL | PROTOCOL | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| images | https://images.linuxcontainers.org | simplestreams | YES | NO |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix:// | lxd | NO | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu | https://cloud-images.ubuntu.com/releases | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily | https://cloud-images.ubuntu.com/daily | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
如下命令从远端镜像服务器下载Ubuntu 18.04的image,别名设置为ubuntu-bionic,方便之后使用,由于是从互联网下载镜像,网络不佳时会比较慢:
$ lxc image copy ubuntu:18.04 local: --alias ubuntu-bionic
Image copied successfully!
显示本地image镜像可使用lxc image list local:命令,可看到刚刚下载的image。显示远端服务器镜像使用lxc image list ubuntu:,由于服务器上面images较多,或者网络不佳等原因,导致显示较慢。可以直接登录到服务器进行查看(https://cloud-images.ubuntu.com/releases)。以后在创建相同容器时,都可使用本地的基础镜像,不需再从网络下载。
$ lxc image list local:
+---------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+---------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| ubuntu-bionic | 8f9da4cd832b | no | ubuntu 18.04 LTS amd64 (release) (20181101) | x86_64 | 174.44MB | Nov 19, 2018 at 5:14am (UTC) |
+---------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
下载的镜像保存在目录/var/lib/lxd$/images目录下:
$ sudo ls /var/lib/lxd/images/ -l
total 178636
-rw-r--r-- 1 root root 784 11月 19 14:23 8f9da4cd832ba0235749caa2249c1ecfcee0cee052c4647fb502955fcec70072
-rw-r--r-- 1 root root 182915072 11月 19 14:45 8f9da4cd832ba0235749caa2249c1ecfcee0cee052c4647fb502955fcec70072.rootfs
使用lxc image show命令显示别名所指定的镜像的详细信息:
$ lxc image show local:ubuntu-bionic
auto_update: false
properties:
architecture: amd64
description: ubuntu 18.04 LTS amd64 (release) (20181101)
label: release
os: ubuntu
release: bionic
serial: "20181101"
version: "18.04"
public: false
确认本地image镜像不在使用后,可使用如下命令删除:
$ lxc image delete local:ubuntu-bionic
创建Ubuntu容器ubcontainer,之后使用lxc list可见容器已在运行状态,自动获得的IPv4地址为10.245.178.194:
$ lxc launch ubuntu-bionic ubcontainer
Creating ubcontainer
Starting ubcontainer
$ lxc list
+-------------+---------+-----------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------------+---------+-----------------------+------+------------+-----------+
| ubcontainer | RUNNING | 10.245.178.194 (eth0) | | PERSISTENT | 0 |
+-------------+---------+-----------------------+------+------------+-----------+
登录新创建容器的bash命令行:
$ lxc exec ubcontainer -- /bin/bash
root@ubcontainer:~#
使用exit退出容器,查看宿主机中网络设备变化:
$ ip link show type veth
13: vethBIQ3MT@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP mode DEFAULT group default qlen 1000
link/ether fe:eb:bd:81:fd:c5 brd ff:ff:ff:ff:ff:ff link-netnsid 0
以上可见,LXD新创建了一对veth设备vethBIQ3MT和if12,并且将vethBIQ3MT设备添加到了网桥lxdbr0之下,if12设备对于ubcontainer内部的网络接口eth0。
新创建的ubcontainer容器位于宿主机的目录/var/lib/lxd/containers/ubcontainers下:
$ sudo ls /var/lib/lxd/containers/ubcontainer
metadata.yaml rootfs templates
$
$ sudo ls /var/lib/lxd/containers/ubcontainer/rootfs
bin dev home lib64 mnt proc run snap sys usr boot etc lib media opt root sbin srv tmp var
$
$ sudo ls /var/lib/lxd/containers/ubcontainer/templates
cloud-init-meta.tpl cloud-init-user.tpl hostname.tpl cloud-init-network.tpl cloud-init-vendor.tpl
可直接在宿主机与容器直接拷贝文件。