聊聊Docker和虚拟机

本文是译文,比对下虚拟机和Docker,话不多说,进入正题。

附上链接:https://nickjanetakis.com/blog/comparing-virtual-machines-vs-docker-containers

中文链接:

作者:正则云科技
链接:https://www.zhihu.com/question/48174633/answer/180743885
来源:知乎

看看我们在虚拟机上怎么跑应用的:

从下到上理解上图:

  • 基础设施(Infrastructure)。它可以是你的个人电脑,数据中心的服务器,或者是云主机
  • 虚拟机管理系统(Hypervisor)。利用Hypervisor,可以在主操作系统之上运行多个不同的从操作系统。类型1的Hypervisor有支持MacOS的HyperKit,支持Windows的Hyper-V、Xen以及KVM。类型2的Hypervisor有VirtualBox和VMWare workstation。
  • 客户机操作系统(Guest Operating System)。假设你需要运行3个相互隔离的应用,则需要使用Hypervisor启动3个客户机操作系统,也就是3个虚拟机。这些虚拟机都非常大,也许有700MB,这就意味着它们将占用2.1GB的磁盘空间。更糟糕的是,它们还会消耗很多CPU和内存。
  • 各种依赖。每一个客户机操作系统都需要安装许多依赖。如果你的应用需要连接PostgreSQL的话,则需要安装libpq-dev;如果你使用Ruby的话,应该需要安装gems;如果使用其他编程语言,比如Python或者Node.js,都会需要安装对应的依赖库。
  • 应用。安装依赖之后,就可以在各个客户机操作系统分别运行应用了,这样各个应用就是相互隔离的。

我们再看看Docker:

不难发现,相比于虚拟机Docker要简洁很多。因为我们不需要运行一个臃肿的客户机操作系统了。

从下到上理解上图:

  • 基础设施(Infrastructure)
  • 主操作系统(Host Operating System)。所有主流的Linux发行版都可以运行Docker。对于MacOS和Windows,也有一些办法”运行”Docker。
  • Docker守护进程(Docker Daemon)。Docker守护进程取代了Hypervisor,它是运行在操作系统之上的后台进程,负责管理Docker容器。
  • 各种依赖。对于Docker,应用的所有依赖都打包在Docker镜像中,Docker容器是基于Docker镜像创建的。
  • 应用。应用的源代码与它的依赖都打包在Docker镜像中,不同的应用需要不同的Docker镜像。不同的应用运行在不同的Docker容器中,它们是相互隔离的。

对比虚拟机与Docker

Docker守护进程可以直接与主操作系统进行通信,为各个Docker容器分配资源;它还可以将容器与主操作系统隔离,并将各个容器互相隔离。虚拟机启动需要数分钟,而Docker容器可以在数毫秒内启动。由于没有臃肿的从操作系统,Docker可以节省大量的磁盘空间以及其他系统资源。

说了这么多Docker的优势,大家也没有必要完全否定虚拟机技术,因为两者有不同的使用场景。虚拟机更擅长于彻底隔离整个运行环境。例如,云服务提供商通常采用虚拟机技术隔离不同的用户。而Docker通常用于隔离不同的应用,例如前端后端以及数据库

服务器虚拟化 vs Docker

服务器好比运输码头:拥有场地和各种设备(服务器硬件资源)

服务器虚拟化好比作码头上的仓库:拥有独立的空间堆放各种货物或集装箱

(仓库之间完全独立,独立的应用系统和操作系统)

Docker比作集装箱:各种货物的打包

(将各种应用程序和他们所依赖的运行环境打包成标准的容器,容器之间隔离)

Docker有着小巧、迁移部署快速、运行高效等特点,但隔离性比服务器虚拟化差:不同的集装箱属于不同的运单(Docker上运行不同的应用实例),相互独立(隔离)。但由同一个库管人员管理(主机操作系统内核),因此通过库管人员可以看到所有集装箱的相关信息(因为共享操作系统内核,因此相关信息会共享)。

服务器虚拟化就好比在码头上(物理主机及虚拟化层),建立了多个独立的“小码头”—仓库(虚拟机)。其拥有完全独立(隔离)的空间,属于不同的客户(虚拟机所有者)。每个仓库有各自的库管人员(当前虚拟机的操作系统内核),无法管理其它仓库。不存在信息共享的情况

因此,我们需要根据不同的应用场景和需求采用不同的方式使用Docker技术或使用服务器虚拟化技术。例如一个典型的Docker应用场景是当主机上的Docker实例属於单一用户的情况下,在保证安全的同时可以充分发挥Docker的技术优势。对于隔离要求较高的环境如混合用户环境,就可以使用服务器虚拟化技术。

---------------正文分割线-----------

链接:https://devopscon.io/blog/docker/docker-vs-virtual-machine-where-are-the-differences/

这里有一篇文章,对于docker和虚拟机的区别,部分专业人士发表了他们的看法,可供参考:

1.Docker is container based technology and containers are just user space of the operating system. At the low level, a container is just a set of processes that are isolated from the rest of the system, running from a distinct image that provides all files necessary to support the processes. It is built for running applications. In Docker, the containers running share the host OS kernel.

A Virtual Machine, on the other hand, is not based on container technology. They are made up of user space plus kernel space of an operating system. Under VMs, server hardware is virtualized. Each VM has Operating system (OS) & apps. It shares hardware resource from the host.

VMs & Docker – each comes with benefits and demerits. Under a VM environment, each workload needs a complete OS. But with a container environment, multiple workloads can run with 1 OS. The bigger the OS footprint, the more environment benefits from containers. With this, it brings further benefits like Reduced IT management resources, reduced size of snapshots, quicker spinning up apps, reduced & simplified security updates, less code to transfer, migrate and upload workloads.

简介:Docker是基于容器,而容器本身只是从操作系统里隔离出来的一系列进程,运行在一个提供了支持文件的独立镜像上中,其目的是为了运行应用的,多个docker共享主机的操作系统内核。

2.A Dockerized application is just a process that runs on your system. It doesn’t require running a Hypervisor (such as VMWare or VirtualBox), which means there’s no guest operating system to lug around. I do think there are reasons to use Virtual Machines nowadays, but they solve a different set of problems than Docker. You can use Docker to isolate individual applications, and use Virtual Machines to isolate entire systems. They are operating at different levels of abstraction.

3.They differ because of technical reasons, but that’s not the point. One could implement Docker using virtual machines. (Actually, Intel’s clearcontainer and hyper.sh do!) But for most users, VMs are created and managed as plain machines that never get replaced. One has to upgrade them, fix them, etc. But a VM is still a full system, when something goes wrong, it’s hard to tell who’s guilty.

Containers and Docker are not in conflict with virtual machines; they are complementary technologies for distinct usages. VMs allow users to manage hosts by APIs and offer infrastructure elasticity. Docker allows users to define software as small lego blocks to assemble, so they embrace modern architectures: immutable infrastructures, microservices, distributed software, and more.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章