How-to install LXC and OpenQuake LXC on RHEL/CentOS 6
How-to install LXC and OpenQuake LXC on RHEL/CentOS 6As root user:
1. Add the EPEL repo to your RHEL/CentOS 6 server:
rpm -ivh http://mirror.1000mbps.com/fedora-epel/6/i386/epelrelease-6-8.noarch.rpm
2. Install LXC 0.9.0 from epel-testing and some other stuff needed:
yum install --enablerepo=epel-testing lxc lxc-libs lxc-templates
bridge-utils libcgroup
3. Enable cgroups:
service cgconfig start
service cgred start
chkconfig --level 345 cgconfig on
chkconfig --level 345 cgred on
4. Setup the network: the easiest way is to create an internal network, so you do not need to
expose the LXC to the bare-metal server network.
a. Create the bridge:
brctl addbr lxcbr0
b. Make the bridge persistent on reboot: create /etc/sysconfig/network-scripts/ifcfglxcbr0 and add
DEVICE="lxcbr0"
TYPE="Bridge"
BOOTPROTO="static"
IPADDR="10.0.3.1"
NETMASK="255.255.255.0"
c. Start the bridge interface
ifup lxcbr0
5. Configure the firewall to allow outgoing traffic from the container: edit
/etc/sysconfig/iptables and
a. Comment or remove
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
b. Add at the end of file:
*nat
:PREROUTING ACCEPT
:OUTPUT ACCEPT
:POSTROUTING ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT
c. Restart the firewall:
service iptables restart
6. Enable IPv4 forwarding editing /etc/sysctl.conf:a. Change net.ipv4.ip_forward = 0 to net.ipv4.ip_forward = 1
b. Then apply the new parameters
sysctl –p
7. Download OpenQuake LXC:
cd /tmp && wget ftp://ftp.openquake.org/oq-
1.0/lxc/Ubuntu_lxc_12.04.2_64_oq_1-0.tar.bz2
8. Extract the OpenQuake LXC:
tar --numeric-owner -C /var/lib/lxc \
-xpsjf /tmp/Ubuntu_lxc_12.04.2_64_oq_1-0.tar.bz2
9. Check if the LXC is installed and ready: with the command lxc-ls you should see
“openquake”
10.Setup the OpenQuake LXC ip address: open
/var/lib/lxc/openquake/rootfs/etc/network/interfaces and change iface eth0 inet
dhcp to
iface eth0 inet static
address 10.0.3.2
netmask 255.255.255.0
gateway 10.0.3.1
dns-nameservers 8.8.8.8
11.Start the OpenQuake LXC:
lxc-start –d –n openquake
12.Login into the running OpenQuake LXC:
lxc-console –n openquake
(to detach press ctrl-a + q)
You can also login using SSH from the host server:
ssh openquake@10.0.3.2
User: openquake
Password: openquake
Please note:
• This how-to is intended for a fresh, standard installation of RHEL/CentOS 6 (and is tested
on 6.4).It may need some adjustments for customized installations.
• On 5. the firewall could be already customized by the sysadmin, please be careful when
edit it. For more details please ask to your network and/or system administrator.
• On 5. section b. -A POSTROUTING -o eth0 -j MASQUERADE “eth0” is the name of the
host server main interface. It can differ in your configuration (see the used interface with
ifconfig).
• On 8. the --numeric-owner is mandatory.
• On 10. the 8.8.8.8 DNS is the one provided by Google. It’s better to use your internal DNS,
so change that IP address with the one associated to your DNS server. For more details
please ask to your network and/or system administrator.
CentOS 6 使用 LXC
原文: http://purplegrape.blog.51cto.com/1330104/1343766LXC 自kernel 2.6.27 加入linux 内核,依赖内核的cgroup和namespace功能而实现,非常轻量级,设计用于操作系统内部应用级别的隔离。
与传统的硬件虚拟化技术相比有以下优势:
a、更小的虚拟化开销。Linux内核本身是一个很好的硬件资源调度器,LXC的诸多特性基本由内核提供,而内核实现这些特性只有极少的花费,CPU,内存,硬盘都是直接使用。
b、更快速的部署。建好了模板,分分钟搞定一台虚拟机完全不是梦话。LXC虚拟机本质上只是宿主机上的一个目录,克隆一个LXC虚拟机只需要简单的copy就行,这也为备份提供了极大便利。
LXC 目前已经比较成熟,目前最新版为0.9,官方计划2014年2月推出1.0版本并进行长期维护,希望将LXC投入生产的同学建议观望,CentOS 从6.5 开始支持LXC技术。( LXC 1.0已于2014-02-20 发布)
不同于vmware,kvm等虚拟化技术,它是一种类似chroot的容器技术,非常的轻量级。LXC 还可以轻松解决单机上一个应用多实例的问题。
环境CentOS 6.5 x64
1、安装LXC
yum install libcgroup lxc lxc-templates --enablerepo=epel
/etc/init.d/cgconfig start
2、检查环境
lxc-checkconfig
输出如下即是OK
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-2.6.32-431.1.2.0.1.el6.x86_64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/ptsinstances: enabled
--- Control groups---
Cgroup: enabled
Cgroup namespace: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled
--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config/usr/bin/lxc-checkconfig
/usr/share/lxc/templates/ 下有好几个模板可供选择,
# ls /usr/share/lxc/templates/
lxc-alpinelxc-busybox lxc-debianlxc-fedoralxc-oraclelxc-sshdlxc-ubuntu
没有CentOS ?github上有一个现成的centos的模板 ,https://github.com/fajarnugraha/lxc/blob/centos-template/templates/lxc-centos.in
或者下载本文的附件lxc-centos(需解压),放入/usr/share/lxc/templates/,权限755
3、使用模板安装一个centos 虚拟机
lxc-create -n centos -t centos
安装后,虚拟机默认位于/var/lib/lxc/centos/
4、打开虚拟机并进入开机console,
lxc-start -n centos
在后台运行虚拟机,并通过console连接过去 (使用ctrl+a+q退出console)
lxc-start -n centos -d
lxc-console -n centos
5、配置虚拟机网络,
这里偷了个懒,直接利用libvirt创建出的virbr0,也可以自己手动创建桥接。
如果上面新创建的虚拟机启动失败,很可能是网络没有配置好。
编辑文件/var/lib/lxc/centos/config,确保文件包含一下内容
lxc.network.type= veth
lxc.network.link = virbr0
lxc.network.flags = up
lxc.network.name = eth0
lxc.network.ipv4 = 192.168.122.65/24
lxc.network.ipv4.gateway = 192.168.122.1
如果需要第二块网卡,则继续在/var/lib/lxc/centos/config添加一组配置
lxc.network.type = veth
lxc.network.link = virbr1
lxc.network.flags = up
lxc.network.name = eth1
lxc.network.ipv4 = 192.168.100.66/24
虚拟机网络默认由dnsmasq分配,如果没有在lxc中指定,则由虚拟机内部dhcp获得。
得益于新版linux内核 (v3.12之后),LXC 1.0 的安全性得到了大幅提升, 但 LXC 的原理决定它的缺点依然是隔离不够彻底,安全性不如kvm,vmware,xen等。
veth依赖网卡桥接,且可以与任何机器(宿主机,其他虚拟机,局域网其他机器)通讯。
在网络层,可以采取下面的方式加固安全:
如果要隔绝虚拟机与宿主机的通讯(虚拟机之间可以通信,与局域网其他机器也可以通信),网卡可选择macvlan中的bridge模式
lxc.network.type = macvlan
lxc.network.macvlan.mode = bridge
lxc.network.flags = up
lxc.network.link = eth0
如果要进一步隔离同一宿主机上不同虚拟机之间的通讯(仅可与局域网其他机器通信),网卡还要选择macvlan中的vepa模式
lxc.network.type = macvlan
lxc.network.macvlan.mode = vepa
lxc.network.flags = up
lxc.network.link = eth0
下面是两种特殊的网络
lxc.network.type = none
none表示停用网络空间的namespace,复用宿主机的网络。
如果容器里的操作系统真的与网络堆栈分离,这个可以有很大的想象空间,黑客入侵到一个没有网络的容器里会是什么样子?!另外据说关闭容器也会关闭宿主机,ubuntu phone通过lxc里的安卓容器,使用网络复用达到兼容安卓应用的目的。(貌似需要lxc1.0才能支持,个人没有测试通过。)
lxc.network.type = empty
empty表示容器没有网卡,仅有一个回环lo,无法通过网络层与外部通信。(unix socket 是题外话)
6、控制虚拟机的资源
虚拟机默认与宿主机共享硬件资源,CPU,内存,IO等,也可以用cgroup实现资源隔离。
#设置虚拟机只使用0,1两个CPU核心
lxc-cgroup -n centos cpuset.cpus 0,1
#设置虚拟机可用内存为512M
lxc-cgroup -n centos memory.limit_in_bytes 53687091
#设置虚拟机消耗的CPU时间
lxc-cgroup -n centos cpu.shares 256
#设置虚拟机消耗的IO权重
lxc-cgroup -n centos blkio.weight 500
另一种限制资源的方法是将具体的限制写入虚拟机的配置文件
#设置虚拟机只使用0,1两个CPU核心
lxc.cgroup.cpuset.cpus= 0,1
#设置虚拟机消耗的CPU时间
lxc.cgroup.cpu.shares= 256
#设置虚拟机可用内存为512M
lxc.cgroup.memory.limit_in_bytes = 512M
#设置虚拟机可用的swap空间为1G
lxc.cgroup.memory.memsw.limit_in_bytes = 1G
#设置虚拟机可使用的IO权重
lxc.cgroup.blkio.weight=500
7、安装ubuntu 12.04
LXC强大到有点变态,在centos上运行ubuntu?没错,因为内核对于LInux发行版来说是通用的。
lxc-create -n ubuntu -t ubuntu -- -r lucid
点到为止,不深入也不推荐这么干。
TIPS:如果将LXC虚拟机放在LVM或者btrfs文件系统上,还会有一些神奇的效果。
update 2014-0903
前面卖了点关子,解释一下
1、如果你系统里恰好有个lvm VG 名字叫做lxc,那么lxc会识别到,创建的虚拟机配置文件依然是/var/lib/lxc/xxx/config,但是lxc镜像会在/dev/lxc/xxx 这个LV 上 (默认10G大小);
2、如果你的/var 单独分区,恰好使用的是btrfs文件系统,lxc也会识别,创建lxc容器时自动创建子卷,并将容器镜像放在里面;
页:
[1]