2014年8月11日 星期一

初探Docker - Docker 跟 LXC 以及一般Hypervisor有何差別?




聽到Docker 這個玩意大概是2014年年初,但是一直到最近才真的有時間開始研究....

話說一開始聽到Docker 是利用LXC的原理來實作,腦袋瓜馬上浮現的架構圖就如下圖所示,是個Lightweight 且只能跑Linux的環境東西,能應用的範圍應該有限?但是我錯了~大錯特錯~:P





關於 LXC


先撇除一般Hypervisor 不談(大家應該都很熟悉了),在了解Docker之前我們應該先來了解,什麼是 LXC (Linux Container)?我們現來看一下他的定義:

LXC (LinuX Containers) provide an isolated operating system environment with its own file system, network, process and block I/O space. A favorite way to describe containers is that they are like “chroot on steroids”, since they provide the file system isolation that chroot jails provide but they go beyond that by also providing an IP address, a separate process domain, user ids and dedicated access to the host’s physical resources (i.e. memory, CPU) which chroot jails do not provide


主要用意是提供比chroot更完整的 process 的isolation,但是範疇都還是寄生在同一個Host OS上面,所以他很輕量,也很快(因為不像Hypervisor 每開一個Guest OS都要跑完整的開機流程),參考LXC containers in Ubuntu Server 14.04 LTS 這篇文章的比較如下:



Virtualization (i.e. kvm, xen) LXC Containers
Footprint Requires a hypervisor and a full operating system image. Does not require a hypervisor or a separate operating system image.
OS supported Any OS supported by the hypervisor Most Linux distros, uses same kernel as host
Typical server deployment 10 – 100 VMs 100 - 1000 containers
Boot time Less than a minute Seconds
Physical resources use (i.e. memory, CPU) Each VM has resource reserved for its own use Shared by all containers


更多關於LXC 的操作,可以參考以下文章:

Why Docker more than LXC


沒錯,一開始Docker也是架設在LXC之上,不過從Docker 0.9版以後,LXC 已經不再是Docker 唯一且預設的執行環境 (請參考:docker drops LXC as default execution environment),Docker 現在支援更多種的"isolation tools"包含:
  • DOpenVZ
  • systemd-nspawn
  • libvirt-lxc
  • libvirt-sandbox
  • qemu/kvm
  • BSD Jails
  • Solaris Zones
  • chroot





不過這又是另外的故事了,我們還是回到最一開始的討論,到底Docker比LXC多提供了什麼?參考下面這張投影片:





Docker增加了AUFS以及Base image的概念:

AUFS 的概念可以參考 - Linux AuFS Examples: Another Union File System Tutorial ,以我的理解就是把Host的目錄當作Guest (Container)的Disk 來用。


但我覺得最有趣的就是base image的概念,透過base image,我們可以就可以在docker上執行各種的distribution Linux 而不受限於Host OS種類,Image的概念如下,其實就是一堆必要檔案的集合:

Docker images are essentially a collection of files which include everything needed to run that process. This is everything from the OS packages and up.
A docker image has a default process it runs when it is instantiated. This could be bash, to drop you into the terminal, or a web server so you can access it from the browser.



此外Docker 又有一種有趣的玩法,就是直接透過執行Docker run 這個指令,就可以在Host OS上對Container 內的OS下命令,光說沒有Fu,可以看一下以下的指令以及執行結果。


CoreOS (alpha)
core@core-01 ~ $ docker run centos:centos6 yum update
Loaded plugins: fastestmirror
Setting up Update Process
Resolving Dependencies
--> Running transaction check
---> Package ca-certificates.noarch 0:2013.1.95-65.1.el6_5 will be updated
---> Package ca-certificates.noarch 0:2014.1.98-65.0.el6_5 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch        Version                    Repository    Size
================================================================================
Updating:
 ca-certificates      noarch      2014.1.98-65.0.el6_5       updates      1.1 M

Transaction Summary
================================================================================
Upgrade       1 Package(s)

Total download size: 1.1 M

各位看出來了嗎?這個結果很有趣,我的Host OS 是CoreOS (這又是另一個故事),但是我可以透過 docker run centos:centos6 (指定image:指定instance)這個指令要求container執行CentOS專屬的指令 yum update,而且我從外部下的所有指令都可以被錄下來用來製作image,這樣我只要config好一個image就可以複製成很多份去deploy了!

當然我們也可以像傳統使用VM的方式,登入這個Container(模擬),指令如下:

docker run -i -t centos:centos6 /bin/bash
bash-4.1#

這就跟傳統Vagrant ssh 的感覺很像。


玩到這邊慢慢的對於Docker 有些fu了~不過還是有許多的問題,且讓我邊玩邊整理筆記~:P


張貼留言