物理機OS:
Ubuntu 12.04 3.8.0-42-generic x86_64 VirtualBox: Oracle VM VirtualBox Manager 4.2.10 Vagrant: Vagrant 1.7.3 CoreOS: stable 717.3.0 coreos-vagrant source: commit b9ed7e2182ff08b72419ab3e89f4a5652bc75082
一、原理
如果沒有Wall,CoreOS的coreos-vagrant安裝將非常簡單:
1、git clone https://github.com/coreos/coreos-vagrant
2、編輯配置文件
3、vagrant up
4、vagrant ssh
但是現在有了Wall,步驟3:vagrant up會報錯:無法連接到http://stable.release.core-os.net/amd64-usr/717.3.0/xx這個url,導致安裝失敗。
我大致分析了一下vagrant up的執行過程:
1、設置配置默認值
$num_instances = 1 $instance_name_prefix = "core" $update_channel = "alpha" $image_version = "current" $enable_serial_logging = false $share_home = false $vm_gui = false $vm_memory = 1024 $vm_cpus = 1 $shared_folders = {} $forwarded_ports = {}
2、判斷是否存在config.rb這個配置,如果有,則加載。
3、設置config.vm.url,並獲取對應的json文件:
{ "name": "coreos-stable", "description": "CoreOS stable", "versions": [{ "version": "717.3.0", "providers": [{ "name": "virtualbox", "url": "http://stable.release.core-os.net/amd64-usr/717.3.0/coreos_production_vagrant.box", "checksum_type": "sha256", "checksum": "99dcd74c7cae8b1d90f108f8819f92b17bfbd34f4f141325bd0400fe4def55b6" }] }] }
4、根據config.vm.provider(是virtualbox還是vmvare等)來決定采用哪種虛擬機創建邏輯。
這裡我們看到,整個過程只需要從core-os.net下載兩個文件:coreos_production_vagrant.box和coreos_production_vagrant.json。如果我們提前將這兩個文件下載到本地,並放在一個臨時的http server下,修改Vagrantfile和coreos_production_vagrant.json這兩個文件,就應該可以通過coreos-vagrant安裝了。
二、coreos-vagrant安裝single instance CoreOS
好了,根據上述原理,我們首先要下載coreos_production_vagrant.box和coreos_production_vagrant.json這兩個文件,根據我們的channel和版本選擇,兩個文件的下載地址分別為:
http://stable.release.core-os.net/amd64-usr/717.3.0/coreos_production_vagrant.box http://stable.release.core-os.net/amd64-usr/717.3.0/coreos_production_vagrant.json
接下來就是不管你用什麼梯子,只要把這兩個文件下載到本地,並放到一個目錄下就好了。
我們需要修改一下coreos_production_vagrant.json,將其中的url改為:
"url": "http://localhost:8080/coreos_production_vagrant.box"
我們要將這兩個文件放到一個local file server中,後續供core-vagrant訪問。最簡單的方法就是使用:
python -m SimpleHTTPServer 8080
當然使用Go實現一個簡單的http file server也是非常簡單的:
//fileserver.go package main import "net/http" import "log" func main() { log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("./")))) }
接下來我們就可以按照正常步驟,下載coreos-vagrant並up了:
$git clone https://github.com/coreos/coreos-vagrant
修改Vagrantfile:
$ diff Vagrantfile Vagrantfile.bak 14,15c14,15 < $update_channel = "stable" < $image_version = "717.3.0" — > $update_channel = "alpha" > $image_version = "current" 55c55 < config.vm.box_url = "http://localhost:8080/coreos_production_vagrant.json" — > config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json" % [$update_channel, $image_version]
將user-data.sample改名為user-data,並編輯user-data,在etcd2下面增加一行:
etcd2: name: core-01
將units:下面對於etcd2的注釋去掉,以enable etcd2服務。(將etcd服務注釋掉)
萬事俱備,只需vagrant up。
$ vagrant up Bringing machine 'core-01' up with 'virtualbox' provider… ==> core-01: Box 'coreos-stable' could not be found. Attempting to find and install… core-01: Box Provider: virtualbox core-01: Box Version: 717.3.0 ==> core-01: Loading metadata for box 'http://localhost:8080/coreos_production_vagrant.json' core-01: URL: http://localhost:8080/coreos_production_vagrant.json ==> core-01: Adding box 'coreos-stable' (v717.3.0) for provider: virtualbox core-01: Downloading: http://localhost:8080/coreos_production_vagrant.box core-01: Calculating and comparing box checksum… ==> core-01: Successfully added box 'coreos-stable' (v717.3.0) for 'virtualbox'! ==> core-01: Importing base box 'coreos-stable'… ==> core-01: Matching MAC address for NAT networking… ==> core-01: Checking if box 'coreos-stable' is up to date… ==> core-01: Setting the name of the VM: coreos-vagrant_core-01_1437121834188_89503 ==> core-01: Clearing any previously set network interfaces… ==> core-01: Preparing network interfaces based on configuration… core-01: Adapter 1: nat core-01: Adapter 2: hostonly ==> core-01: Forwarding ports… core-01: 22 => 2222 (adapter 1) ==> core-01: Running 'pre-boot' VM customizations… ==> core-01: Booting VM… ==> core-01: Waiting for machine to boot. This may take a few minutes… core-01: SSH address: 127.0.0.1:2222 core-01: SSH username: core core-01: SSH auth method: private key core-01: Warning: Connection timeout. Retrying… ==> core-01: Machine booted and ready! ==> core-01: Setting hostname… ==> core-01: Configuring and enabling network interfaces… ==> core-01: Running provisioner: file… ==> core-01: Running provisioner: shell… core-01: Running: inline script
登入你的coreos實例:
$ vagrant ssh CoreOS stable (717.3.0) core@core-01 ~ $
在vagrant up時,你可能會遇到如下兩個錯誤:
錯誤1:
Progress state: VBOX_E_FILE_ERROR VBoxManage: error: Could not open the medium storage unit '/home1/tonybai/.vagrant.d/boxes/coreos-stable/717.3.0/virtualbox/coreos_production_vagrant_image.vmdk'. VBoxManage: error: VMDK: inconsistent references to grain directory in '/home1/tonybai/.vagrant.d/boxes/coreos-stable/717.3.0/virtualbox/coreos_production_vagrant_image.vmdk' (VERR_VD_VMDK_INVALID_HEADER).
這個問題的原因很可能是你的Virtualbox版本不對,比如版本太低,與coreos_production_vagrant.box格式不兼容。可嘗試安裝一下高版本virtualbox來解決。
錯誤2:
core-01: SSH address: 127.0.0.1:2222 core-01: SSH username: core core-01: SSH auth method: private key core-01: Warning: Connection timeout. Retrying… core-01: Warning: Connection timeout. Retrying… core-01: Warning: Connection timeout. Retrying…
coreos虛擬機創建後,似乎一直無法連接上。在coreos的github issue中,有人遇到了這個問題,目前給出的原因是因為cpu的支持虛擬化技術的vt開關沒有打開,需要在bios中將其開啟。這主要在安裝64bit box時才會發生。
到這裡,我們已經完成了一個single instance coreos虛擬機的安裝。vagrant halt可以幫助你將啟動的coreos虛擬機停下來。
$ vagrant halt ==> core-01: Attempting graceful shutdown of VM…
三、 CoreOS cluster
上面雖然成功的安裝了coreos,然並卵。在實際應用中,CoreOS多以Cluster形式呈現,也就是說我們要啟動多個CoreOS實例。
使用vagrant啟動多個coreos實例很簡單,只需將配置中的$num_instances從1改為n。
這裡我們啟用config.rb這個配置文件(將config.rb.sample改名為config.rb),並將其中的$num_instances修改為3:
# Size of the CoreOS cluster created by Vagrant $num_instances=3
該配置文件中的數據會覆蓋Vagrantfile中的默認配置。
三個instance中的etcd2要想組成集群還需要一個配置修改,那就是在etcd.io上申請一個token:
$curl https://discovery.etcd.io/new https://discovery.etcd.io/fe81755687323aae273dc5f111eb059a
將這個token配置到user-data中的etcd2下:
etcd2: #generate a new token for each unique cluster from https://discovery.etcd.io/new #discovery: https://discovery.etcd.io/<token> discovery: https://discovery.etcd.io/fe81755687323aae273dc5f111eb059a
我們再來up看看:
$ vagrant up Bringing machine 'core-01' up with 'virtualbox' provider… Bringing machine 'core-02' up with 'virtualbox' provider… Bringing machine 'core-03' up with 'virtualbox' provider… ==> core-01: Checking if box 'coreos-stable' is up to date… ==> core-01: VirtualBox VM is already running. ==> core-02: Importing base box 'coreos-stable'… ==> core-02: Matching MAC address for NAT networking… ==> core-02: Checking if box 'coreos-stable' is up to date… ==> core-02: Setting the name of the VM: coreos-vagrant_core-02_1437388468647_96550 ==> core-02: Fixed port collision for 22 => 2222. Now on port 2200. ==> core-02: Clearing any previously set network interfaces… ==> core-02: Preparing network interfaces based on configuration… core-02: Adapter 1: nat core-02: Adapter 2: hostonly ==> core-02: Forwarding ports… core-02: 22 => 2200 (adapter 1) ==> core-02: Running 'pre-boot' VM customizations… ==> core-02: Booting VM… ==> core-02: Waiting for machine to boot. This may take a few minutes… core-02: SSH address: 127.0.0.1:2200 core-02: SSH username: core core-02: SSH auth method: private key core-02: Warning: Connection timeout. Retrying… ==> core-02: Machine booted and ready! ==> core-02: Setting hostname… ==> core-02: Configuring and enabling network interfaces… ==> core-02: Running provisioner: file… ==> core-02: Running provisioner: shell… core-02: Running: inline script ==> core-03: Importing base box 'coreos-stable'… ==> core-03: Matching MAC address for NAT networking… ==> core-03: Checking if box 'coreos-stable' is up to date… ==> core-03: Setting the name of the VM: coreos-vagrant_core-03_1437388512743_68112 ==> core-03: Fixed port collision for 22 => 2222. Now on port 2201. ==> core-03: Clearing any previously set network interfaces… ==> core-03: Preparing network interfaces based on configuration… core-03: Adapter 1: nat core-03: Adapter 2: hostonly ==> core-03: Forwarding ports… core-03: 22 => 2201 (adapter 1) ==> core-03: Running 'pre-boot' VM customizations… ==> core-03: Booting VM… ==> core-03: Waiting for machine to boot. This may take a few minutes… core-03: SSH address: 127.0.0.1:2201 core-03: SSH username: core core-03: SSH auth method: private key core-03: Warning: Connection timeout. Retrying… ==> core-03: Machine booted and ready! ==> core-03: Setting hostname… ==> core-03: Configuring and enabling network interfaces… ==> core-03: Running provisioner: file… ==> core-03: Running provisioner: shell… core-03: Running: inline script $vagrant ssh core-02 CoreOS stable (717.3.0) core@core-02 ~ $
可以看到Vagrant啟動了三個coreos instance。關閉這些instance,同樣用halt:
$ vagrant halt ==> core-03: Attempting graceful shutdown of VM… ==> core-02: Attempting graceful shutdown of VM… ==> core-01: Attempting graceful shutdown of VM…
四、小結
以上僅僅是CoreOS最基本的入門,雖然現在安裝ok了,但CoreOS的各種服務組件的功用、配置;如何與Docker配合形成分布式服務系統;如何用Google Kubernetes管理容器集群等還需更進一步深入學習,這個後續會慢慢道來。
原文:http://tonybai.com/2015/07/20/install-coreos-by-coreos-vagrant/