歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

使用core-vagrant方式安裝CoreOS

CoreOS是一種專門為運行類docker容器而生的linux發行版。與其他通用linux發行版(ubuntu、debian、redhat)相 比,它具有體型最小,消耗最小,支持滾動更新等特點。除此之外CoreOS內置的分布式系統服務組件也給開發者和運維者組建分布式集群、部署分布式服務應 用帶來了極大便利。

CoreOS與知名容器Docker腳前腳後誕生,到目前為止已經較為成熟,國外主流雲平台提供商如Amazon EC2、Google Compute Engine、Microsoft Azure、Digtial Ocean等均提供了CoreOS image,通過這些服務,你可以一鍵建立一個CoreOS實例,這似乎也是CoreOS官方推薦的主流install方式(最Easy)。

CoreOS當然支持其他方式的安裝,比如支持虛擬機安裝(vagrant+virtualbox)、 PXE(preboot execute environment)安裝以及iso install to 物理disk方式。如果僅僅是做一些實驗,虛擬機安裝是最簡單也是最安全的方式。不過由於CoreOS的官方下載站在大陸無法直接訪問(大陸程序員們好悲 催啊),因此這一最簡單的虛擬機安裝CoreOS的過程也就不那麼簡單了。

通過core-vagrant安裝的直接結果是CoreOS被安裝到一個VirtualBox虛擬機中,之後我們利用Vagrant命令來進行 CoreOS虛擬機的啟停。CoreOS以及Vagrant都在持續演進,尤其是CoreOS目前在active dev中,版本號變化很快,這也是CoreOS滾動升級的必然結果。因此在安裝操作演示前,我們有必要明確一下這個安裝過程使用的軟件版本:

物理機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.boxcoreos_production_vagrant.json。如果我們提前將這兩個文件下載到本地,並放在一個臨時的http server下,修改Vagrantfile和coreos_production_vagrant.json這兩個文件,就應該可以通過coreos-vagrant安裝了。

二、coreos-vagrant安裝single instance CoreOS

好了,根據上述原理,我們首先要下載coreos_production_vagrant.boxcoreos_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/

Copyright © Linux教程網 All Rights Reserved