用 Vagrant 的技巧

在「Ten Things I Wish I’d Known Before Using Vagrant」這邊作者整理了使用 Vagrant 的十個技巧,有些以前就知道,有些是每次遇到都要查一次,有些則是新發現的工具...

其中第四點提到的工具「vagrant-landrush/landrush」之前沒看過:

A Vagrant plugin that provides a simple DNS server for Vagrant guests

Landrush is a simple cross-platform DNS for Vagrant VMs that is visible on both, the guest and the host.

It spins up a small DNS server and redirects DNS traffic from your VMs to use it, automatically registering/unregistering IP addresses of guests as they come up and go down.

也可以從範例知道實際用途:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"

  config.landrush.enabled = true

  config.vm.hostname = "myhost.vagrant.test"

  config.landrush.host 'static1.example.com', '1.2.3.4'
  config.landrush.host 'static2.example.com', '2.3.4.5'
end

以前有遇到這樣的需求時都還傻傻的去寫 /etc/hosts... (也是會動啦,而且每一台都寫一樣的東西是還好,但有種很 workaround 的感覺 XD)

之後有遇到時用看看...

Facebook 的「Augmented Traffic Control」模擬網路環境

Facebook 推出 Augmented Traffic Control,模擬網路環境:「Augmented Traffic Control: A tool to simulate network conditions」。

可以測這五種變數:

  • bandwidth
  • latency
  • packet loss
  • corrupted packets
  • packets ordering

Facebook 的成果是 Python + Django 寫的前端管理界面,實際運作還是透過 Linuxiptables

也有提供 Vagrant 的操作方式讓人「試用」,主要是讓人在本機上就可以用吧?

Vagrant 1.7 引入的 Push 機制

離上次改版也半年了,Vagrant 1.7 引入了 Push 機制:「Vagrant Push: One Command to Deploy Any Application」。

功能很簡單,就是「把整個目錄傳到指定的位置上」,可以看做是 code deploy 機制。目前支援的通訊協定 Atlas、FTP/SFTP、Heroku 以及自訂 script。

Vagrant 的這個功能有著 Unix philosophy 的想法。功能很簡單,但似乎是以後新功能的墊腳石。code deploy 這個功能現在已經有一堆 open source 產品可以用,如果之後不以此延伸,看起來沒有太大意義。

當然也是順便幫自家產品 Atlas 打廣告...

另外是 Vagrant 這個字的念法,也可以在這篇文章裡的影片看到示範:

Vagrant 1.6.3

Vagrant 官方沒公告,但在 GitHub 上可以看到 Changelog:「1.6.3 (May 29, 2014)」。

本來在 Docker provider 上有兩個 workaround 要設定 (docker.has_ssh 以及 override.ssh.port),現在都不需要了,預設值就有幫你自動偵測:

  • providers/docker: Better error messaging when SSH is not ready direct to container. [GH-3763]
  • providers/docker: Don't port map SSH port if container doesn't support SSH. [GH-3857]
  • providers/docker: Proper SSH info if using native driver. [GH-3799]
  • providers/docker: Verify host VM has SSH ready. [GH-3838]

這樣處理起來又方便一些...

總算是搞定 Vagrant + Docker...

Docker 最大的好處就是啟動速度,比 VirtualBox 快非常多,但 Vagrant 官方對於 Docker provider 的範例還是太少,踹了老半天才踹出來:

ENV["VAGRANT_DEFAULT_PROVIDER"] = "docker"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.provider "docker" do |docker, override|
        docker.image = "fgrehm/vagrant-ubuntu:precise"
        docker.has_ssh = true

        override.ssh.port = 22
    end
end

然後用 vagrant up 跑起來,接下來就可以用 vagrant ssh 連進去。

其中 override 是目前的 workaround,可以參考 GitHub 上的「Docker provider: cannot 'vagrant ssh' when not using a Docker host VM · Issue #3799 · mitchellh/vagrant」。

Docker 的 image 不透過 Vagrant 管理,而是 Docker 自己處理。可以用 docker images (列出) 與 docker rmi [repository] (刪除) 操作。

Vagrant 1.6

前幾天提到的 Vagrant 1.6 正式發行了:「Vagrant 1.6」,然後下載的地方已經迅速跳版號出 1.6.1 了...

Ubuntu 的人可以直接抓 deb 檔後用 dpkg -i 安裝...

官方列出三個大進展:

Global Status and Control

以往是分散管理,現在可以統一管理。對於有開很多 Vagrant 環境的人會變得方便,不然以往都要自己開 VirtualBox 確認。

Windows Guests

標題說明一切 XDDD

Docker-Based Development Environments

開一個 Docker container 的速度比開一個 VirtualBox 快很多。當然,開出來是 Linux 環境。但這對於跑測試之類的應用非常好用,速度會是重要的因素之一...

完整的 Changelog 可以看 GitHub 上 1.6.0 版的「CHANGELOG.md」。

Vagrant 1.6 將會支援 Docker

Vagrant 1.6 將會支援 Docker:「Feature Preview: Docker-Based Development Environments」。

對於 Linux 原生環境沒有問題,而對於非 Linux 環境,Vagrant 會建立一台跑 Linux 的虛擬機,然後在裡面跑 Docker,很有趣的作法 :p (反正想辦法支援)

Docker 與虛擬機的速度當然是沒辦法比較 (Docker 快太多),如果 1.6 穩定的話,應該要花不少時間來研究整合了 :p

Vagrant 1.5

Vagrant 1.5 出了:「Vagrant 1.5 and Vagrant Cloud」。

最大的亮點是前陣子就有寫「Feature Preview: Vagrant Share」的 Vagrant Share,可以讓其他人很方便的連到自己的 Vagrant 機器上。

Vagrant Cloud 則是產品面上的整合,算是社群功能?

最近拿 Vagrant 與 Docker 測試的次數愈來愈頻繁了,之前拿 Amazon EC2,但開機速度太慢,而 DigitalOcean 開機速度雖然快很多,但不是很容易透過 cli 操作...

而這兩套各有優缺點。Docker 速度快,但環境相容性沒有 Vagrant 好,另外 Vagrant 畢竟是包著 VirtualBox 這種全虛擬化環境,除了 Linux 以外也可以測其他的作業系統。

反正都是熱門的軟體,之後看社群發展出什麼功能再看要怎麼用 :p

測試 GitLab 的方式...

除了在「doc/install/installation.md」有手動安裝的方式外,在「GitLab CE Downloads」有提供 Ubuntu 12.04 LTS 64bits 的 deb 檔可以安裝。

官方在「GitLab Omnibus project」提供的安裝方式是:

apt-get install openssh-server postfix
dpkg -i ooxx.deb
gitlab-ctl reconfigure

不過在 Docker 裡面會卡在設定 redis 的部份而無法成功,卡在:

Recipe: gitlab::redis
  * user[gitlab-redis] action create (up to date)
  * directory[/var/log/gitlab/redis] action create (up to date)
  * directory[/var/opt/gitlab/redis] action create (up to date)
  * template[/var/opt/gitlab/redis/redis.conf] action create (up to date)
  * directory[/opt/gitlab/sv/redis] action create (up to date)
  * directory[/opt/gitlab/sv/redis/log] action create (up to date)
  * directory[/opt/gitlab/sv/redis/log/main] action create (up to date)
  * template[/opt/gitlab/sv/redis/run] action create (up to date)
  * template[/opt/gitlab/sv/redis/log/run] action create (up to date)
  * file[/opt/gitlab/sv/redis/down] action delete (up to date)
  * link[/opt/gitlab/init/redis] action create (up to date)
  * link[/opt/gitlab/service/redis] action create (up to date)
  * ruby_block[supervise_redis_sleep] action run

後來是用 Vagrant (跑 VirtualBox) 才測試成功。就可以看到 PostgreSQLRedis 整包都被放到 /opt 下了...

提供給要玩 GitLab 的人參考看看...