容器虚拟技术演变之路 -- 初识Docker

398 阅读6分钟

​ 容器虚拟技术随着我们在实际开发实践中不断的更新迭代,由最初的虚拟机到如今的Docker。虽然本文介绍的内容我们已经有了答案,但是我们还是要追根溯源来探索其通过哪些技术做到去繁从简

虚拟机与Docker

虚拟机

​ 所谓虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统 。这个听起来有些绕口,其实就是通过软件的隔离,让你的电脑能够同步安装多个完整的计算机系统,这里说的软件比较有名的就是VMWare和OpenStack

​ 通过VMWare或OpenStack,我们在原来的电脑中模拟出一台甚至多台“子电脑”出来。这些“子电脑”可以是Mac、Windows、Linux中的任意系统、任意版本,只你软件支持且你能找到镜像。在“子电脑”里,你可以和正常电脑一样运行程序,例如打开word。各个“子电脑”之间的软件硬件都是相互隔离的,互不影响的

​ 虚拟机能够让一台机器像多台机器一样运行,这意味着您需要的服务器更少,并且还可以最大限度地利用您拥有的服务器。这种效率的提高能够节省硬件、冷却和维护成本。借助虚拟化,我们可以在同一台机器上运行多种类型的应用、桌面和操作系统

Docker

​ Linux容器虚拟技术(Linux container)它是Docker容器技术的基础。虚拟机虽然可以隔离出很多“子电脑”,但占用空间更大,启动慢。而容器技术恰好没有这些缺点,它不需要虚拟出整个操作系统,只需要虚拟一个小规模的环境(类似“沙箱”)

​ Docker容器本质上是宿主机上的一个进程。Docker 通过命名空间实现了资源隔离,通过 cgroups实现了资源的限制,通过写时复制机制(copy-on-write)实现了高效的文件操作。能够实现更快速的交付和部署、更高效的虚拟化、更轻松的迁移和扩展、更简单的管理

对比结论

docker -- Linux容器虚拟技术-1.jpg

Docker虚拟机(VM)
操作系统与宿主共享OS宿主OS上运行虚拟机OS
存储大小镜像小便于存储与传输镜像庞大(vmdk、vdi等)
运行性能几乎无额外性能损失操作系统额外的CPU、内存消耗
移植性轻便、灵活、适应于Linux笨重、与虚拟化技术耦合度高
硬件亲和性面向软件开发人员面向硬件运维人员
部署速度快速较慢

Docker 所用技术实现

Docker is written in the Go programming language and takes advantage of several features of the Linux kernel to deliver its functionality.

Docker是用Go编程语言编写的,它利用Linux内核的一些特性来提供其功能。

Namespaces

Docker uses a technology called namespaces to provide the isolated workspace called the container. When you run a container, Docker creates a set of namespaces for that container.

These namespaces provide a layer of isolation. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.

Docker使用名为namespace的技术来提供名为container的隔离工作区。当您运行一个容器时,Docker会为该容器创建一组名称空间。

这些名称空间提供了一层隔离。容器的每个方面都在一个单独的命名空间中运行,其访问权限仅限于该命名空间。

Docker运用了哪些Namespaces

The pid namespace:Used for process numbering (PID: Process ID)

进程隔离,各个容器之内的进程相互隔离。比如:/proc/ 文件内具有相同进程编号为1的进程,这表示在不同的容器内进程之间做到了相互隔离

The net namespace:Used for managing network interfaces (NET: Networking)

网络隔离,当运行docker成功之后,使用ifconfig命令,你会发现出现一个为docker0的网桥(默认网络模式),容器启动之后通过veth-pair技术,将自己namespace内的网卡成对映射到网桥上,使得namespace同过docker0(网桥)完成通讯,docker0可以理解为充当交换机的角色,而各个容器会在同一个网段内(可以理解为局网)每个容器拥有自己的IP

docker -- Linux容器虚拟技术-3.png

The ipc namespace:Used for managing access to IPC resources (IPC: InterProcess Communication)

进程之间隔离,容器之间使用的内存空间将被隔离

The mnt namespace:Used for managing mount-points (MNT: Mount)

挂载点隔离

The uts namespace:Used for isolating kernel / version identifiers. (UTS: Unix Timesharing System)

内核与版本进行隔离

Namespaces总结

/var/lib/docker/containers文件夹下存放的是启动的容器container,实际每个container就是一个进程,之所以称之为容器其实其内部就是一个小型的操作系统(如下图所示),之所称小型是因为许多系统命令在其容器内运行不了(个人观点一),不在局限于进程那么简单。而namespace(命名空间)为运行的容器提供了资源隔离,可以保证其运行安全减少冲突

docker -- Linux容器虚拟技术-2.png

Control groups

Docker also makes use of another technology called cgroups or control groups. A key to running applications in isolation is to have them only use the resources you want.

This ensures containers are good multi-tenant citizens on a host. Control groups allow Docker to share available hardware resources to containers and, if required, set up limits and constraints. For example, limiting the memory available to a specific container.

Docker 采用cgroups技术,隔离应用程序使用的资源,为了确保多个容器良好运行,Docker可以做到对多容器共享硬件资源,当然你也可以完成对资源的设置,比如为某个特定的容器设置对应的内存空间。

Control groups 总结

cgroups 其目的是通过为每个容器进行资源控制(设置),将主机资源发挥到极致。

Union file systems

Union file systems, or UnionFS, are file systems that operate by creating layers, making them very lightweight and fast. Docker uses union file systems to provide the building blocks for containers. Docker can make use of several union file system variants including: AUFS, btrfs, vfs, and DeviceMapper.

联合文件系统(UnionFS)是通过创建层来操作的文件系统,使它们非常轻量级和快速。Docker使用联合文件系统为容器提供构建块。Docker可以使用几个联合文件系统变体,包括:AUFS、btrfs、vfs和DeviceMapper。

Union file systems 总结

UnionFS 实质通过文件系统完成对容器的隔离和高效操作

Container format

Docker combines these components into a wrapper we call a container format. The default container format is called libcontainer.

Docker also supports traditional Linux containers using LXC. In the future, Docker may support other container formats, for example, by integrating with BSD Jails or Solaris Zones.

Docker将namespace、control groups、UnionFS 组合成我们称之为容器格式的包装器。默认的容器格式称为“libcontainer”(容器"依赖"库)。

Docker将来还支持使用[LXC]的传统Linux容器(linuxcontainers.org/).,Docker可能… Jails或Solaris Zones集成。