容器概念与Linux Container原理

一、容器与LxC

  在像KVM等众多主机虚拟化解决方案中,对每一个虚拟机实例提供的是从底层硬件开始一直到上层的环境,在硬件级进行资源划分。虚拟机的内核是运行在硬件内核之上的。由于每个虚拟实例都有自己的运行内核,所以各实例之间有非常好的隔离性。

  但在某些场景中使用KVM等虚拟机过于笨重,例如用户仅仅只是需要在用户空间运行某一个程序,没有必要管理用户空间内核再到系统内核,可以将用户空间内核剥离掉,直接由系统空间提供运行程序的权限即可,也就是说在一个内核空间上构建出相互隔离的空间,用一种可以将各用户空间在同一内核级中会互相产生干扰的方式隔离出来,也就是说在用户空间就做到各个空间互相隔离,这就是容器的概念。

  用户空间中可以只跑一个进程,也可以在一个用户空间中跑多个进程。跑多个进程时容器的功能就类似与主机虚拟化技术,LxC就是这一种在用户空间上就相互隔离的虚拟化容器技术。而另一种容器技术中,每个用户空间只负责运行一个进程且相互隔离。这样每一个进程所依赖的环境在各个容器中的相互独立。

  下面是Wiki对LXC的解释:

  LXC (Linux Containers) is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.
  The Linux kernel provides the cgroups functionality that allows limitation and prioritization of resources (CPU, memory, block I/O, network, etc.) without the need for starting any virtual machines, and also namespace isolation functionality that allows complete isolation of an applications' view of the operating environment, including process trees, networking, user IDs and mounted file systems.

二、安装LxC和简单使用

yum install -y lxc lxc-templates

  简单使用:
 lxc-checkconfig :检查系统环境是否满足容器使用要求;

 lxc-create :创建lxc容器;

  例: lxc-create -n NAME -t TEMPLATE_NAME 

 lxc-start :启动容器;

  例: lxc-start -n NAME -d 
  Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself

 lxc-info :查看容器相关的信息;

  例: lxc-info -n NAME 

 lxc-info -n NAME :附加至指定容器的控制台;

  例: lxc-console -n NAME -t NUMBER 

 lxc-stop :停止容器;

 lxc-destory :删除处于停机状态的容器;

 lxc-snapshot :创建和恢复快照;

posted @ 2018-11-18 15:45  readygood  阅读(9472)  评论(1编辑  收藏  举报