lxd,lxc,nova-lxd

分析一下lxc, lxd, nova-lxd的关系以及源码

  • nova-lxd,An OpenStack Compute driver for LXD
  • lxd,lxd daemon和lxd client
  • lxc,liblxc和lxc-tools

关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
                              +---------------+
+----------+ | |
| lxc | | nova-lxd |
| lxd-client+---+ +------+ |
+----------+ | | | |
| | +---------------+
| |
| |
+--v------v---------+
| lxd |
| |
|Daemon based on |
|liblxc offering |
|a REST API | +-------------------------+
|to manage containers | |
+--------+----------+ | lxc/lxc |
| | tools to |
| | manage containers |
| | |
+--------v----------+ | |
| | | |
| go-lxc./v2 | +--------+----------------+
| | |
| | |
| | |
+----------+--------+ |
| |
| |
| |
| |
+---------v------------------------------------v---------+
| |
| |
| liblxc.so |
| |
+-------------------------+------------------------------+
|
|
|
+-------------------------v------------------------------+
| |
| |
| kernel |
| namespace, cgroups |
| |
| |
| |
+--------------------------------------------------------+

lxd分析

包含两部分

  • lxd, 类似docker daemon,对外提供restful api
  • lxc, lxd daemon的客户端

lxd daemon通过go lxc v2来调用liblxc.so,其中用到了cgo以及lxc的头文件,所以要先编译liblxc

lxc-tools

lxc-tools)只是lxc/lxc的一部分,提供可执行文件用于管理lxc容器。
lxc/lxc最重要的部分还是liblxc.so

nova-lxd

nova-compute的一个driver,类似以前看过的nova-docker

  • 相比docker,lxc的行为上更像虚拟机,所以更适合用来和openstack一起玩http://blog.decbug.com/2017/02/11/lxc_docker/
  • 北向: 提供spawn, plug-network等接口给nova-compute
  • 南向: 调用lxd daemon管理容器

基本功能及流程

创容器

1
2
3
4
5
6
7
8
9
10
11
12
13
# Check to see if LXD already has a copy of the image. If not,
# fetch it.
_sync_glance_image_to_lxd

# Plug in the network
plug_vifs(instance, network_info)

# Create the profile(including devices, flavor, )
container = self.client.containers.create(
container_config, wait=True)

# start
container.start(wait=True)

镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
image = IMAGE_API.get(context, image_ref)
IMAGE_API.download(context, image_ref, dest_path=image_file)

# generate metadata.yaml
metadata = {
'architecture': image.get(
'hw_architecture', obj_fields.Architecture.from_host()),
'creation_date': int(os.stat(image_file).st_ctime)}
metadata_yaml = json.dumps(
metadata, sort_keys=True, indent=4,
separators=(',', ': '),

# add metadata.yaml to tar.gz
tarball = tarfile.open(manifest_file, "w:gz")
tarinfo = tarfile.TarInfo(name='metadata.yaml')
tarinfo.size = len(metadata_yaml)
tarball.addfile(tarinfo, io.BytesIO(metadata_yaml))
tarball.close()

# upload tar.gz to local lxd image registry
image = client.images.create(
image.read(), metadata=manifest.read(),
wait=True)
image.add_alias(image_ref, '')

网络

https://github.com/openstack/nova-lxd/blob/master/nova/virt/lxd/vif.py

  • plug
  • unplug
  • brctl创建linux桥,以及addif
  • ovs-vsctl创建ovs桥,以及add port(pvo pvi)

本博客欢迎转发,但请保留原作者信息
github:codejuan
博客地址:http://blog.decbug.com/