Docker Docker underlying cornerstone namespace and cgroup cgroup and underlying cornerstone namespace

Docker with the namespace underlying cornerstone cgroup

 

The container is essentially the same system for the processes of a business objective synthesis of a set of services, in a space called a namespace, the namespace in the same process can communicate with each other, but can not see the other namespace in the process. Each namespace can have its own independent host name, process ID system, IPC, network, file system, user, and so on resources. In a way, to achieve a simple virtual: You can run multiple mutually-aware system while allowing a host.

Alt text

In addition, in order to limit the use of namespace to physical resources, the process can be used for CPU, memory and other resources required to do certain restrictions. This is Cgroup technology, Cgroup is the Control group mean. For example, we often say that the container 4c4g, in fact, is to limit this vessel namespace used in the process of computing resources can be used up to 4 cores and 4GB of memory.
In short, Linux kernel provides namespace complete isolation, Cgroup complete resource constraints. namespace + Cgroup underlying technology constitutes the container (the container is the rootfs file system layer technology).

namespace

A global namespace some system resources into a package abstraction, the abstraction global resource instances have their own isolated for the present process is the namespace. These global resources to change the namespace of the process is visible, while the other process is not visible.
Linux provides about several namespaces:

  Namespace   Constant                           Isolates
  -  IPC            CLONE_NEWIPC            System V IPC, POSIX message queues
  -  Network     CLONE_NEWNET           Network devices, stacks, ports, etc.
  -  Mount        CLONE_NEWNS             Mount points
  -  PID            CLONE_NEWPID            Process IDs
  -  User          CLONE_NEWUSER User and group IDs - UTS CLONE_NEWUTS Hostname and NIS domain name 

为了在分布式的环境下进行通信和定位,容器必然需要一个独立的IP、端口、路由等等,自然就想到了网络的隔离。同时,你的容器还需要一个独立的主机名以便在网络中标识自己。想到网络,顺其自然就想到通信,也就想到了进程间通信的隔离。可能你也想到了权限的问题,对用户和用户组的隔离就实现了用户权限的隔离。最后,运行在容器中的应用需要有自己的PID,自然也需要与宿主机中的PID进行隔离。

cgroups

Cgroups是control groups的缩写,最初由google的工程师提出,后来被整合进Linux内核。Cgroups是Linux内核提供的一种可以限制、记录、隔离进程组(process groups)所使用的物理资源(如:CPU、内存、IO等)的机制。对开发者来说,cgroups 有如下四个有趣的特点:

  • cgroups 的 API 以一个伪文件系统的方式实现,即用户可以通过文件操作实现 cgroups 的组织管理。
  • cgroups 的组织管理操作单元可以细粒度到线程级别,用户态代码也可以针对系统分配的资源创建和销毁 cgroups,从而实现资源再分配和管理。
  • 所有资源管理的功能都以“subsystem(子系统)”的方式实现,接口统一。
  • 子进程创建之初与其父进程处于同一个 cgroups 的控制组。

本质上来说,cgroups 是内核附加在程序上的一系列钩子(hooks),通过程序运行时对资源的调度触发相应的钩子以达到资源追踪和限制的目的。实现 cgroups 的主要目的是为不同用户层面的资源管理,提供一个统一化的接口。从单个进程的资源控制到操作系统层面的虚拟化。Cgroups 提供了以下四大功能:

  • 资源限制(Resource Limitation):cgroups 可以对进程组使用的资源总额进行限制。如设定应用运行时使用内存的上限,一旦超过这个配额就发出 OOM(Out of Memory)。
  • 优先级分配(Prioritization):通过分配的 CPU 时间片数量及硬盘 IO 带宽大小,实际上就相当于控制了进程运行的优先级。
  • 资源统计(Accounting): cgroups 可以统计系统的资源使用量,如 CPU 使用时长、内存用量等等,这个功能非常适用于计费。
  • 进程控制(Control):cgroups 可以对进程组执行挂起、恢复等操作。
    Docker正是使用cgroup进行资源划分,每个容器都作为一个进程运行起来,每个业务容器都会有一个基础的pause容器也就是POD作为基础容器。pause容器提供了划分namespace的内容,并连通同一POD下的所有容器,共享网络资源。查看容器的PID,对应/proc/pid/下是该容器的运行资源。
 
 

容器本质上是把系统中为同一个业务目标服务的相关进程合成一组,放在一个叫做namespace的空间中,同一个namespace中的进程能够互相通信,但看不见其他namespace中的进程。每个namespace可以拥有自己独立的主机名、进程ID系统、IPC、网络、文件系统、用户等等资源。在某种程度上,实现了一个简单的虚拟:让一个主机上可以同时运行多个互不感知的系统。

Alt text

此外,为了限制namespace对物理资源的使用,对进程能使用的CPU、内存等资源需要做一定的限制。这就是Cgroup技术,Cgroup是Control group的意思。比如我们常说的4c4g的容器,实际上是限制这个容器namespace中所用的进程,最多能够使用4核的计算资源和4GB的内存。
简而言之,Linux内核提供namespace完成隔离,Cgroup完成资源限制。namespace+Cgroup构成了容器的底层技术(rootfs是容器文件系统层技术)。

namespace

一个namespace把一些全局系统资源封装成一个抽象体,该抽象体对于本namespace中的进程来说有它们自己的隔离的全局资源实例。改变这些全局资源对于该namespace中的进程是可见的,而对其他进程来说是不可见的。
Linux 提供一下几种 namespaces:

  Namespace   Constant                           Isolates
  -  IPC            CLONE_NEWIPC            System V IPC, POSIX message queues
  -  Network     CLONE_NEWNET           Network devices, stacks, ports, etc.
  -  Mount        CLONE_NEWNS             Mount points
  -  PID            CLONE_NEWPID            Process IDs
  -  User          CLONE_NEWUSER User and group IDs - UTS CLONE_NEWUTS Hostname and NIS domain name 

为了在分布式的环境下进行通信和定位,容器必然需要一个独立的IP、端口、路由等等,自然就想到了网络的隔离。同时,你的容器还需要一个独立的主机名以便在网络中标识自己。想到网络,顺其自然就想到通信,也就想到了进程间通信的隔离。可能你也想到了权限的问题,对用户和用户组的隔离就实现了用户权限的隔离。最后,运行在容器中的应用需要有自己的PID,自然也需要与宿主机中的PID进行隔离。

cgroups

Cgroups是control groups的缩写,最初由google的工程师提出,后来被整合进Linux内核。Cgroups是Linux内核提供的一种可以限制、记录、隔离进程组(process groups)所使用的物理资源(如:CPU、内存、IO等)的机制。对开发者来说,cgroups 有如下四个有趣的特点:

  • cgroups 的 API 以一个伪文件系统的方式实现,即用户可以通过文件操作实现 cgroups 的组织管理。
  • cgroups 的组织管理操作单元可以细粒度到线程级别,用户态代码也可以针对系统分配的资源创建和销毁 cgroups,从而实现资源再分配和管理。
  • 所有资源管理的功能都以“subsystem(子系统)”的方式实现,接口统一。
  • 子进程创建之初与其父进程处于同一个 cgroups 的控制组。

本质上来说,cgroups 是内核附加在程序上的一系列钩子(hooks),通过程序运行时对资源的调度触发相应的钩子以达到资源追踪和限制的目的。实现 cgroups 的主要目的是为不同用户层面的资源管理,提供一个统一化的接口。从单个进程的资源控制到操作系统层面的虚拟化。Cgroups 提供了以下四大功能:

  • Resource constraints (Resource Limitation): cgroups can limit the total amount of resources used by process group. As used application runtime memory set limit, beyond which the quota is issued OOM (Out of Memory).
  • Priority assignment (Prioritization): by allocating CPU time slice number and size of the hard disk IO bandwidth, in fact, is equivalent to control the priority process is running.
  • Resource Statistics (Accounting): cgroups can resource usage statistics system, such as the use of long CPU, memory usage, and so on, this feature is ideal for charging.
  • Process Control (Control): cgroups process group can perform suspend, resume and other operations.
    Docker is the use cgroup resource partitioning, each container are up and running as a process, each business will have a container-based pause POD container that is used as the base container. pause container provides content into namespace and communicates all containers under the same POD, shared network resources. View the vessel PID, corresponding to / proc / pid / down run of the container resource.

Guess you like

Origin www.cnblogs.com/ExMan/p/12053681.html