创建容器
lxc-create {-n name} {-t template} [-- template-options]
-t 指定模板,比如debian、ubuntu、centos等
-n 指定容器名称,这是以后几乎所有操作都要使用的
template-options: 根据不同的模板有不同的选项,可以指定发行版本、创建容器时使用的mirror url,架构等等
- 创建debian的testing容器:
sudo lxc-create -t debian -n debian -- --mirror=http://mirrors.163.com/debian/ -r testing
- 创建cnetos7容器
sudo lxc-create -n centos -t centos -- -R Centos_7 --repo=http://mirrors.163.com/centos/7/os/x86_64
- 创建ubuntu16.04容器
sudo lxc-create -n ubuntu -t ubuntu -- -r xenial --mirror=http://mirrors.aliyun.com/ubuntu/
--security-mirror=http://mirrors.aliyun.com/ubuntu/
容器创建完毕后,默认的文件系统路径为/var/lib/lxc/{name}
- 启动容器
sudo lxc-start {-n name}
- 查看容器信息
sudo lxc-info {-n name}
- 进入容器
sudo lxc-attatch {-n name}
此时就可以像操作正常系统一样执行命令了。
除此之外,还可以使用lxc-console来登录容器。
网络配置
使用网桥配置容器网络。
- 1)在主机中添加网桥
在主机的/etc/network/interfaces
中添加:
auto br0
iface br0 inet dhcp
bridge_ports enp2s0
bridge_fd 0
bridge_maxwait 0
其中enp2s0是我的主机的唯一网卡,执行/etc/init.d/networing restart
重启网络后会发现多了一个br0的网卡
- 2)配置容器的网络
容器的网络配置文件默认路径为/var/lib/lxc/{name}/config
,加入网络选项:
lxc.network.type = veth
lxc.network.flags = up
# that's the interface defined above in host's interfaces file
lxc.network.link = br0
# name of network device inside the container,
# defaults to eth0, you could choose a name freely
# lxc.network.name = lxcnet0
lxc.network.hwaddr = 00:FF:AA:00:00:01
然后在容器的/etc/network/interfaces/
文件中添加:
auto eth0
iface eth0 inet dhcp
如果容器没有启动dhclient服务,则启动。
- 3)重启容器
sudo lxc-stop -n ubuntu
sudo lxc-start -n ubuntu
sudo lxc-attatch -n ubuntu
此时发现容器多了一个名为eth0的网卡:
网络配置完毕。