VMware
Xen
Hyper-V
Qemu
KVM
OS-level virtualization
containerization takes place at the kernel level. Most modern operating system kernels now support the primitives necessary for containerization, including Linux with openvz, vserver and more recently lxc, Solaris with zones and FreeBSD with Jails.
FreeBSD
http://www.freebsd.org/doc/handbook/jails.html
Solaris
http://docs.oracle.com/cd/E26502_01/html/E29024/preface-1.html#scrolltoc
Linux
openvz http://openvz.org/Main_Page
vserver http://linux-vserver.org/Welcome_to_Linux-VServer.org
lxc https://linuxcontainers.org/
这里主要介绍lxc容器的使用方法
环境:
CentOS6.5 x64
lxc-0.7.5
官方网站:
https://linuxcontainers.org
http://libvirt.org/drvlxc.html
参考文档:
http://wiki.1tux.org/wiki/Centos6/Installation/Minimal_installation_using_yum
http://wiki.1tux.org/wiki/Lxc/Installation/Guest/Centos/6
http://17173ops.com/2013/11/14/linux-lxc-install-guide.shtml
http://www.ibm.com/developerworks/cn/linux/l-lxc-containers/
http://blog.csdn.net/quqi99/article/details/9532105
http://blog.sina.com.cn/s/blog_999d1f4c0101dxad.html
简介:
LXC是Linux containers的简称,是一种基于容器的操作系统层级的虚拟化技术。
LXC可以在操作系统层次上为进程提供虚拟的执行环境,一个虚拟的执行环境就是一个容器,同时可以为容器绑定特定的cpu和memory节点,分配特定比例的cpu时间、IO时间,限制可以使用的内存大小(包括内存和是swap空间),提供device访问控制,提供独立的namespace(网络、pid、ipc、mnt、uts)。
LXC主要包含三个方面:cgroup、network和rootfs。
Cgroups是control groups的缩写,是Linux内核提供的一种可以限制、记录、隔离进程组(process groups)所使用的物理资源(如:cpu,memory,IO等等)的机制。LXC主要通过cgroup实现对于资源的管理和控制。
Network是为容器提供的网络环境设置。
rootfs用于指定容器的虚拟根目录,设定此项以后,容器内所有进程将会把此目录根目录,不能访问此目录之外的路径,相当于chroot的效果。
生命周期和状态
CONTAINER LIFE CYCLE
When the container is created, it contains the configuration information. When a process is launched, the container will be starting and running. When the last process running inside the container exits,
the container is stopped.
In case of failure when the container is initialized, it will pass through the aborting state.
另外:从lxc-wait帮助里可以看到有7种状态:STOPPED, STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN。
已知的实现lxc的解决方案有2个: liblxc与libvirt
************
libvirt
************
libvirt是Linux上的虚拟化库,是长期稳定的C语言API,支持QEMU/KVM、Xen、LXC等主流虚拟化方案。
操作LXC容器和操作KVM虚拟机一样。
一.应用容器
1.定义lxc XM L
root@jun-live:~# cat lxc-test.xml
这里只定义了可用容器的最小化配置,在导入的时候系统会自动生成一份详细的配置文件,默认存放在/etc/libvirt/lxc目录下。
2.导入lxc容器
root@jun-live:~# virsh -c lxc:/// define lxc-test.xml
Domain lxc-test defined from lxc-test.xml
root@jun-live:~# virsh -c lxc:/// list --all
Id Name State
----------------------------------------------------
- lxc-test shut off
root@jun-live:~# ls /etc/libvirt/lxc/
lxc-test.xml
修改配置文件要通过以下命令调用VIM来编辑
root@jun-live:~# virsh -c lxc:/// edit lxc-test
3.开启应用实例
root@jun-live:~# virsh -c lxc:/// start lxc-test
Domain lxc-test started
root@jun-live:~# virsh -c lxc:/// list --all
Id Name State
----------------------------------------------------
22157 lxc-test running
4.连接应用实例
可以通过console从文本界面连接
root@jun-live:~# virsh -c lxc:/// console lxc-test
Connected to domain lxc-test
Escape character is ^]
sh-4.1# ls
bin dev lib media net root selinux sys var
boot etc lib64 misc opt run smb tmp
cgroup home lost+found mnt proc sbin srv usr
sh-4.1# pwd
/
可以看到,LXC的隔离性做的不大好,它对cpu, memory, network的隔离还好一点,但对存储的隔离不够,尤其对于应用容器,甚至可以从lxc虚机可以访问宿主机的根目录。当然,可以自己做一个根文件系统然后使用操作系统容器来隔离。但lxc在这块支持不够,全要自己实现,所以又催生了两个项目docker和dockerlite,似乎弥补了这些不足。
dockerlite利用LXC实现运行时资源隔离,并利用Btrfs文件系统的快照功能完成状态保持和虚拟环境克隆。所谓轻量级虚拟化,也指代操作系统级别的虚拟化,通过内核和用户态进程组的支持,实现的独立网络IP、进程树等类似虚拟机的隔离运行环境,但是和宿主机运行同样的内核。dockerlite 和另一款用 Go 语言实现的docker的区别有:
dockerlite 使用Shell脚本实现,而docker用Go。
dockerlite 使用BTRFS文件系统,而docker使用AUFS。
docker以后台进程方式运行并通过命令行客户端实现操作交互,dockerlite则无法以后台进程运行。
二.OS容器
自定义最小化chroot环境
A.将特定的软件包安装到指定的chroot目录
root@jun-live:~# mkdir /lxc-root
root@jun-live:~# setarch x86_64 bash
root@jun-live:~# yum -y install --installroot=/lxc-root/ dhclient openssh-server passwd rsyslog vim-minimal vixie-cron wget which yum
或者
yum -y --installroot=/lxc-root groupinstall "base"
yum -y --installroot=/lxc-root install dhclient
进入到chroot环境
root@jun-live:~# chroot /lxc-root/
bash-4.1# pwd
/
bash-4.1# ls
bin dev home lib64 mnt proc sbin srv tmp var
boot etc lib media opt root selinux sys usr
1.修改root密码
bash-4.1# echo redhat|passwd --stdin root
2.调试必须的设备文件及相应权限
bash-4.1# rm -rf /dev/null
bash-4.1# mknod -m 666 /dev/null c 1 3
bash-4.1# mknod -m 666 /dev/zero c 1 5
bash-4.1# mknod -m 666 /dev/urandom c 1 9
bash-4.1# mknod -m 600 /dev/console c 5 1
bash-4.1# mknod -m 600 /dev/tty1 c 4 1
bash-4.1# mknod -m 600 /dev/tty1 c 4 2
bash-4.1# mknod -m 600 /dev/tty1 c 4 3
bash-4.1# mknod -m 600 /dev/tty1 c 4 4
bash-4.1# mknod -m 600 /dev/tty1 c 4 5
bash-4.1# mknod -m 600 /dev/tty1 c 4 6
bash-4.1# mknod -m 600 /dev/tty1 c 4 7
bash-4.1# ln -s /dev/urandom /dev/random
bash-4.1# chown root:tty /dev/tty*
bash-4.1# mkdir -p /dev/shm
bash-4.1# chmod 1777 /dev/shm
bash-4.1# mkdir -p /dev/pts
bash-4.1# chmod 755 /dev/pts
3.初始化root登录shell
bash-4.1# cp -a /etc/skel/. /root/
4.设置主机名及网络相关配置
bash-4.1# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
bash-4.1# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=lxc-centos6
bash-4.1# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTOTO=dhcp
5.生成最小化/etc/fstab
bash-4.1# cat /etc/fstab
/dev/root / rootfs defaults 0 0
none /dev/shm