CoreOS 資料一覧

etcdを使ってCoreOSのクラスタを構成する手順(VirtualBox + Vagrant)

2016/01/31更新

対応バージョン: 935.0.0

etcdを使い、VirtualBox + Vagrant上でCoreOSの3ノードクラスタを構成する手順を示す。

#1: core-01(172.17.8.101)
#2: core-02(172.17.8.102)
#3: core-03(172.17.8.103)

クラスタが構成できたらサンプルのサービスを稼動させ、ノードダウン時のフェールオーバ試験を行う。

尚、discovery用のetcdはローカルに構築する代わりにdiscovery.etcd.ioに肩代わりしてもらうことができるのでここではその設定を説明する。

準備

まずhttps://github.com/coreos/coreos-vagrant.gitからVagrant用のファイル一式を入手する。

% git clone https://github.com/coreos/coreos-vagrant.git
% cd coreos-vagrant

この中のconfig.rbを修正し、OSインスタンス数をデフォルトの1から3に変更する。

% sed "s/$num_instances=1/$num_instances=3/" config.rb.sample > config.rb

% diff -u config.rb.sample config.rb
--- config.rb.sample    2016-01-30 23:44:28.353415043 +0900
+++ config.rb   2016-01-30 23:46:04.015869487 +0900
@@ -1,5 +1,5 @@
 # Size of the CoreOS cluster created by Vagrant
-$num_instances=1
+$num_instances=3
 
 # Used to fetch a new discovery token for a cluster of size $num_instances
 $new_discovery_url="https://discovery.etcd.io/new?size=#{$num_instances}"

discovery用のetcdを設定するためにhttps://discovery.etcd.io/newにアクセスして当該クラスタ管理用のトークンを取得する。

% curl -w "\n" -s https://discovery.etcd.io/new
https://discovery.etcd.io/bce7180z8cas685a02812896ai5d608b

このトークンをuser-dataに記述するだけでdiscovery.etcd.ioが提供するdiscoveryサービスを利用できる。

% cp user-data.sample user-data
% vi user-data
:
  6  #discovery: https://discovery.etcd.io/<token>
  7  discovery: https://discovery.etcd.io/bce7180z8cas685a02812896ai5d608b
:

VM作成、クラスタ構成

上記の準備ができたらvagrant upを実行するだけでOSイメージの入手からVMの作成、クラスタの構成まで完了する。

% vagrant up

% vagrant box list
coreos-alpha (virtualbox, 935.0.0)

% vagrant status
Current machine states:

core-01                   running (virtualbox)
core-02                   running (virtualbox)
core-03                   running (virtualbox)
:

% vagrant ssh core-01 -c "cat /etc/os-release"
NAME=CoreOS
ID=coreos
VERSION=935.0.0
VERSION_ID=935.0.0
BUILD_ID=2016-01-22-1705
PRETTY_NAME="CoreOS 935.0.0"
ANSI_COLOR="1;32"
HOME_URL="https://coreos.com/"
BUG_REPORT_URL="https://github.com/coreos/bugs/issues"

% vagrant ssh core-01 -c "fleetctl list-machines -l"
MACHINE                           IP            METADATA
3f2f3dd01bc642c29d396c0eaa4a7139  172.17.8.103  -
fdb1b6522c0c4646ba9f4e72f24d7c17  172.17.8.102  -
ff737b5bf54e40b08d9583c77ac06e94  172.17.8.101  -

サービス登録

サンプルとしてサービスを一つ稼動させる。

ここではcore-01にログインして作業するが稼動するノードはクラスタ内のいずれかが自動的に選ばれる。

% vagrant ssh core-01
core-01$

まずはサービス定義のファイルを作成する。サービス名は「myhost」、処理内容は1秒おきに自ホスト名を出力する簡単なものとする。

core-01$ vi /tmp/myhost.service
[Unit]
Description=myhost

[Service]
ExecStart=/bin/bash -c "while true; do uname -n; sleep 1; done"

サービスの稼動はsubmit(登録)、load(systemdへの読み込み)、start(起動)の順で実施する。

submit(登録)

core-01$ fleetctl submit /tmp/myhost.service
Unit myhost.service inactive

core-01$ fleetctl cat myhost
[Unit]
Description=myhost

[Service]
ExecStart=/bin/bash -c "while true; do uname -n; sleep 1; done"

core-01$ fleetctl list-unit-files
UNIT            HASH     DSTATE    STATE     TARGET
myhost.service  febc229  inactive  inactive  -

load(systemdへの読み込み)

loadした時点でサービスがどのノードで稼動するかが決定する。ここでは172.17.8.103(core-03)が選ばれているのが確認できる。

core-01$ fleetctl load myhost
Unit myhost.service loaded on 3f2f3dd0.../172.17.8.103

core-01$ fleetctl list-unit-files
UNIT            HASH     DSTATE  STATE   TARGET
myhost.service  febc229  loaded  loaded  3f2f3dd0.../172.17.8.103

core-01$ fleetctl list-units
UNIT            MACHINE                   ACTIVE    SUB
myhost.service  3f2f3dd0.../172.17.8.103  inactive  dead

start(起動)

core-01$ fleetctl start myhost
Unit myhost.service launched on 3f2f3dd0.../172.17.8.103

core-01$ fleetctl list-units
UNIT            MACHINE                   ACTIVE    SUB
myhost.service  3f2f3dd0.../172.17.8.103  active    running

サービスの状態を見る。が、このサービスは172.17.8.103(core-03)で動いているため以下のようなエラーになる。

core-01$ fleetctl status myhost
Error running remote command: SSH_AUTH_SOCK environment variable
is not set. Verify ssh-agent is running.
See https://github.com/coreos/fleet/blob/master/Documentation/using-the-client.md for help.

この事象の対処については割愛するが、改めて172.17.8.103(core-03)でサービスの状態を見ると正しく動作しているのが確認できる。

core-03$ fleetctl status myhost
● myhost.service - myhost
   Loaded: loaded (/run/fleet/units/myhost.service; linked-runtime; vendor preset: disabled)
   Active: active (running) since Sat 2016-01-30 16:00:27 UTC; 2min 21s ago
 Main PID: 1439 (bash)
   Memory: 380.0K
      CPU: 130ms
   CGroup: /system.slice/myhost.service
           ├─1439 /bin/bash -c while true; do uname -n; sleep 1; done
           └─1735 sleep 1

Jan 30 16:02:39 core-03 bash[1439]: core-03
Jan 30 16:02:40 core-03 bash[1439]: core-03
Jan 30 16:02:41 core-03 bash[1439]: core-03
Jan 30 16:02:42 core-03 bash[1439]: core-03
Jan 30 16:02:43 core-03 bash[1439]: core-03

core-03$ fleetctl journal myhost
-- Logs begin at Sat 2016-01-30 14:49:24 UTC, end at Sat 2016-01-30 16:06:02 UTC. --
Jan 30 16:05:53 core-03 bash[1439]: core-03
Jan 30 16:05:54 core-03 bash[1439]: core-03
Jan 30 16:05:55 core-03 bash[1439]: core-03
Jan 30 16:05:56 core-03 bash[1439]: core-03
Jan 30 16:05:57 core-03 bash[1439]: core-03

サービスの定義は/run/fleet/units配下に格納される。

core-03$ cat /run/fleet/units/myhost.service  
[Unit]
Description=myhost

[Service]
ExecStart=/bin/bash -c "while true; do uname -n; sleep 1; done"

フェールオーバ試験

サービスが稼動しているcore-03を落とし上げしてみる。

core-03停止

% vagrant halt core-03

クラスタの稼動状態を見ると172.17.8.103(core-03)が外れているのが確認できる。

% vagrant ssh core-01 -c "fleetctl list-machines -l"
MACHINE                           IP            METADATA
fdb1b6522c0c4646ba9f4e72f24d7c17  172.17.8.102  -
ff737b5bf54e40b08d9583c77ac06e94  172.17.8.101  -

サービスは172.17.8.102(core-02)にフェールオーバしている。

% vagrant ssh core-01 -c "fleetctl list-units"
UNIT            MACHINE                   ACTIVE  SUB
myhost.service  fdb1b652.../172.17.8.102  active  running

core-03起動

% vagrant up core-03

クラスタの稼動状態を見ると172.17.8.103(core-03)がクラスタに組み込まれているのが確認できる。

% vagrant ssh core-01 -c "fleetctl list-machines -l"
MACHINE                           IP            METADATA
3f2f3dd01bc642c29d396c0eaa4a7139  172.17.8.103  -
fdb1b6522c0c4646ba9f4e72f24d7c17  172.17.8.102  -
ff737b5bf54e40b08d9583c77ac06e94  172.17.8.101  -

ただしサービスはフェールオーバ先で稼動したままである(仕様通り)。

% vagrant ssh core-01 -c "fleetctl list-units"
UNIT            MACHINE                   ACTIVE  SUB
myhost.service  fdb1b652.../172.17.8.102  active  running

サービス削除

サービスを削除するにはstop(停止)、unload(systemdからの除去)、destroy(破棄)の順で実施する。

stop(停止)

core-01$ fleetctl stop myhost
Unit myhost.service loaded on fdb1b652.../172.17.8.102

core-01$ fleetctl list-unit-files
UNIT            HASH     DSTATE  STATE   TARGET
myhost.service  febc229  loaded  loaded  fdb1b652.../172.17.8.102

unload(systemdからの除去)

core-01$ fleetctl unload myhost
Unit myhost.service inactive

core-01$ fleetctl list-unit-files
UNIT            HASH     DSTATE    STATE     TARGET
myhost.service  febc229  inactive  inactive  -

destroy(破棄)

core-01$ fleetctl destroy myhost
Destroyed myhost.service

core-01$ fleetctl list-unit-files
UNIT            HASH     DSTATE  STATE   TARGET

参考サイト