聊聊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.

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