debian下初试lxc

现在容器型的虚拟化也很流行了,主要是相对XEN,KVM之类的能节省资源一些,这两天事情不是太多就简单了一下。
主要参考了debian的wiki(http://wiki.debian.org/LXC).我不是太喜欢把自己用的电脑搞一堆实验性的配置,所以就在virtualbox里面装了一个debian做宿主机再进行的,virtualbox使用的是桥接模式。
1.宿主机里面配置了一个网桥
/etc/network/interfaces
#allow-hotplug eth0
#iface eth0 inet dhcp

auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_fd 0
bridge_maxwait 0

然后重启网络
root@linux:~# ifconfig
br0 Link encap:Ethernet HWaddr 08:00:27:d2:35:1d
inet addr:192.168.2.207 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fed2:351d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:58 errors:0 dropped:0 overruns:0 frame:0
TX packets:59 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7430 (7.2 KiB) TX bytes:7406 (7.2 KiB)

eth0 Link encap:Ethernet HWaddr 08:00:27:d2:35:1d
inet6 addr: fe80::a00:27ff:fed2:351d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:65 errors:0 dropped:0 overruns:0 frame:0
TX packets:65 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8662 (8.4 KiB) TX bytes:7874 (7.6 KiB)

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:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:560 (560.0 B) TX bytes:560 (560.0 B)

root@linux:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.080027d2351d no eth0

2.宿主机安装基础的软件和配置
2.1 安装 lxc bridge-utils libvirt-bin debootstrap
aptitude install lxc bridge-utils libvirt-bin debootstrap
2.2 /etc/fstab 添加
cgroup /sys/fs/cgroup cgroup defaults 0 0
2.3 拷贝一份模板
cp -a /usr/lib/lxc/templates/lxc-debian /usr/lib/lxc/templates/lxc-squeeze
并把/usr/lib/lxc/templates/lxc-squeeze里面的 lenny替换为squeeze,把dhcp-client替换为isc-dhcp-client

新建/etc/lxc/lxc1.conf 加入自己自定义一些配置
lxc.network.type=veth
lxc.network.link=br0 #这个要和自己建立的网桥的名字一致
lxc.network.flags=up
lxc.network.name = eth0
lxc.cgroup.cpuset.cpus = 0
#lxc.network.ipv4=192.168.2.150/24
3. 创建容器并启动
3.1 创建容器
lxc-create -n lxc1 /etc/lxc/lxc1.conf -t squeeze
3.2 启动容器
lxc-start -n lxc1

启动之后我们可以brctl show看看

root@linux:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.080027d2351d no eth0
vethyXMqY8
root@linux:~# ifconfig
br0 Link encap:Ethernet HWaddr 08:00:27:d2:35:1d
inet addr:192.168.2.207 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fed2:351d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:525 errors:0 dropped:0 overruns:0 frame:0
TX packets:314 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:45074 (44.0 KiB) TX bytes:50956 (49.7 KiB)

eth0 Link encap:Ethernet HWaddr 08:00:27:d2:35:1d
inet6 addr: fe80::a00:27ff:fed2:351d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:531 errors:0 dropped:0 overruns:0 frame:0
TX packets:331 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:51830 (50.6 KiB) TX bytes:53050 (51.8 KiB)

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:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:560 (560.0 B) TX bytes:560 (560.0 B)

vethyXMqY8 Link encap:Ethernet HWaddr 42:65:bf:83:0a:f8
inet6 addr: fe80::4065:bfff:fe83:af8/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1368 (1.3 KiB) TX bytes:1508 (1.4 KiB)

是br0下新增了一个网卡。

此条目发表在OS分类目录。将固定链接加入收藏夹。

发表评论