CoreOS やってみる


CoreOS とは？

> CoreOS Meetup Tokyo #1 - 資料一覧 - connpass : http://coreos-meetup-tokyo.connpass.com/event/12596/presentation/

## Vagrant で動かすための準備

```
$ git clone https://github.com/coreos/coreos-vagrant/

# 設定
$ cp user-data.sample user-data
$ cp config.rb.sample config.rb
```

```
# インスタンス3つ用意する
diff -u config.rb.sample config.rb
--- config.rb.sample	2015-03-30 01:01:27.000000000 +0900
+++ config.rb	2015-03-30 01:04:24.000000000 +0900
@@ -25,7 +25,7 @@
 # after the equals sign..

 # Size of the CoreOS cluster created by Vagrant
-#$num_instances=1
+$num_instances=3

 # Change basename of the VM
 # The default value is "core", which results in VMs named starting with
```

```
# etcd は、フリー の discovery service を使う
$ curl https://discovery.etcd.io/new\?size\=3

$ diff -u user-data.sample user-data
--- user-data.sample	2015-03-30 01:01:27.000000000 +0900
+++ user-data	2015-04-12 20:28:35.000000000 +0900
@@ -5,6 +5,7 @@
     # generate a new token for each unique cluster from https://discovery.etcd.io/new
     # WARNING: replace each time you 'vagrant destroy'
     #discovery: https://discovery.etcd.io/<token>
+    discovery: https://discovery.etcd.io/98e509e4fe7433794f3a5864e02bba79
     addr: $public_ipv4:4001
     peer-addr: $public_ipv4:7001
   fleet: 

```

## 動かす

```
$ vagrant up
$ vagrant status
Current machine states:

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

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
```

## etcd, fleet 確認

```
$ vagrant ssh core-01
Last login: Mon Mar 30 14:08:49 2015 from 10.0.2.2
CoreOS alpha (633.1.0)
core@core-01 ~ $

# クラスタ確認 
core@core-01 ~ $ fleetctl list-machines
MACHINE		IP		METADATA
6dfa651c...	172.17.8.102	-
6eef3090...	172.17.8.103	-
e5b07b8a...	172.17.8.101	-

# etcd みてみる　
core@core-01 ~ $ etcdctl set first-etcd-key "Hello World"
Hello World
core@core-01 ~ $ etcdctl get first-etcd-key
Hello World

# core-02 から get してみる 
$ vagrant ssh core-02
CoreOS alpha (633.1.0)
core@core-02 ~ $ etcdctl get first-etcd-key
Hello World

# https://discovery.etcd.io/hogehogefugafugapiyopiyo でも確認できる
# chrome なら JSONView いれておくとよい
```

## fleet インスコ (Mac)

- bin/fleetctl は、/usr/lobal/bin パスとおしておくと楽かも
- brew でもインストールできるみたい

```
git clone git@github.com:coreos/fleet.git
cd fleet
./build
```

かくにん

- vagrant に接続できるようにする

```
$ export FLEETCTL_ENDPOINT=http://172.17.8.101:4001
$ bin/fleetctl list-machines -l
MACH]INE					IP		METADATA
0b07612e2955451e853e707627dde354	172.17.8.102	-
7cd5b9a706904e2fa169d500700accf9	172.17.8.101	-
cdeef8cb16e04286a8ccb3cf87b96516	172.17.8.103	-
```

## サービス（unit）を追加

サンプル（1秒おきに Hello World を返すもの）
> fleet/hello.service at master · coreos/fleet : https://github.com/coreos/fleet/blob/master/examples/hello.service


```
$ cat examples/hello.service
[Unit]
Description=Hello World

[Service]
ExecStart=/bin/bash -c "while true; do echo \"Hello, world\"; sleep 1; done"
```

サービス起動

```
$ bin/fleetctl start examples/hello.service
Unit hello.service launched on 0b07612e.../172.17.8.102
```

かくにん

```
$ bin/fleetctl list-units
UNIT		MACHINE				ACTIVE	SUB
hello.service	0b07612e.../172.17.8.102	active	running
```

詳細かくにん

```
$ bin/fleetctl status hello.service
The authenticity of host '172.17.8.102' can't be established.
RSA key fingerprint is 2f:ea:73:22:b0:09:65:a7:53:0c:b3:e2:c1:e5:be:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.8.102' (RSA) to the list of known hosts.
● hello.service - Hello World
   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime; vendor preset: disabled)
   Active: active (running) since Sun 2015-04-12 12:56:57 UTC; 3min 34s ago
 Main PID: 1024 (bash)
   CGroup: /system.slice/hello.service
           ├─1024 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
           └─1273 sleep 1

Apr 12 13:00:21 core-02 bash[1024]: Hello, world
Apr 12 13:00:22 core-02 bash[1024]: Hello, world
Apr 12 13:00:23 core-02 bash[1024]: Hello, world
Apr 12 13:00:24 core-02 bash[1024]: Hello, world
Apr 12 13:00:25 core-02 bash[1024]: Hello, world
Apr 12 13:00:26 core-02 bash[1024]: Hello, world
Apr 12 13:00:27 core-02 bash[1024]: Hello, world
Apr 12 13:00:28 core-02 bash[1024]: Hello, world
Apr 12 13:00:29 core-02 bash[1024]: Hello, world
Apr 12 13:00:30 core-02 bash[1024]: Hello, world
```

tail -f ライクに確認

```
$ bin/fleetctl journal -f hello.service
```

fleetctl で、動いているとこに ssh できる (今回は、core-02)

```
$ bin/fleetctl ssh hello.service
Last login: Sun Apr 12 13:03:12 2015 from 172.17.8.101
CoreOS alpha (633.1.0)

core@core-02 ~ $ ps aux | grep bash
root      1024  0.0  0.2  14052  3032 ?        Ss   12:56   0:00 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
core      1735  0.0  0.3  14188  3220 pts/0    Ss   13:06   0:00 -bash
core      1768  0.0  0.0   4448   780 pts/0    S+   13:07   0:00 grep --colour=auto bash
```

## fail over

core-02 をおとしてみる

```
$ vagrant status
Current machine states:

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

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

$ vagrant halt core-02
==> core-02: Attempting graceful shutdown of VM...

$ vagrant status
Current machine states:

core-01                   running (virtualbox)
core-02                   poweroff (virtualbox)
core-03                   running (virtualbox)
```


core-01 で動いてる！！

```
bin/fleetctl status hello.service
● hello.service - Hello World
   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime; vendor preset: disabled)
   Active: active (running) since Sun 2015-04-12 13:17:40 UTC; 1min 45s ago
 Main PID: 1344 (bash)
   CGroup: /system.slice/hello.service
           ├─1344 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
           └─1483 sleep 1

Apr 12 13:19:16 core-01 bash[1344]: Hello, world
Apr 12 13:19:17 core-01 bash[1344]: Hello, world
Apr 12 13:19:18 core-01 bash[1344]: Hello, world
Apr 12 13:19:19 core-01 bash[1344]: Hello, world
Apr 12 13:19:20 core-01 bash[1344]: Hello, world
Apr 12 13:19:21 core-01 bash[1344]: Hello, world
Apr 12 13:19:22 core-01 bash[1344]: Hello, world
Apr 12 13:19:23 core-01 bash[1344]: Hello, world
Apr 12 13:19:24 core-01 bash[1344]: Hello, world
Apr 12 13:19:25 core-01 bash[1344]: Hello, world
☁  fleet [master] bin/fleetctl ssh hello.service
Last login: Sun Apr 12 13:19:26 2015 from 172.17.8.101
CoreOS alpha (633.1.0)
core@core-01 ~ $ fleetctl status hello.service
● hello.service - Hello World
   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime; vendor preset: disabled)
   Active: active (running) since Sun 2015-04-12 13:17:40 UTC; 2min 10s ago
 Main PID: 1344 (bash)
   CGroup: /system.slice/hello.service
           ├─1344 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
           └─1543 sleep 1

Apr 12 13:19:40 core-01 bash[1344]: Hello, world
Apr 12 13:19:41 core-01 bash[1344]: Hello, world
Apr 12 13:19:42 core-01 bash[1344]: Hello, world
Apr 12 13:19:43 core-01 bash[1344]: Hello, world
Apr 12 13:19:44 core-01 bash[1344]: Hello, world
Apr 12 13:19:45 core-01 bash[1344]: Hello, world
Apr 12 13:19:46 core-01 bash[1344]: Hello, world
Apr 12 13:19:47 core-01 bash[1344]: Hello, world
Apr 12 13:19:48 core-01 bash[1344]: Hello, world
Apr 12 13:19:49 core-01 bash[1344]: Hello, world
```

## ちな

### Raft
複数のマシン間で共有できるようにする仕組みでは、etcd（KVS）が使われていて、Raftというアルゴリズムおよびそのプロトコルの考えをベースにしている。CoreOSを調べていくと必ずでてくる。
下記の記事がわかりやすい。

>
- Etcd - r7km/s : http://r7kamura.github.io/2014/02/26/etcd.html
- Raft : http://thesecretlivesofdata.com/raft/
- Raft : http://www.slideshare.net/pfi/raft-36155398
- etcd総選挙を眺めてみる - Qiita : http://qiita.com/ksato9700/items/9b44a95ce27ac23a94e1

### fleet (Unitファイル)
今回は、サンプルを動かしただけなので、次回は自作でやってみる。
サービスファイルを書くためにも、systemd もついて理解してた方がよさそう。

以下の記事、雑誌が詳しい。

> 
- Fleetの使い方，Unitファイルの書き方 | SOTA : http://deeeet.com/writing/2014/11/20/fleet/
- Software Design 2015年2月号｜技術評論社 : http://gihyo.jp/magazine/SD/archive/2015/201502
- Understanding fleet // Speaker Deck : https://speakerdeck.com/spesnova/understanding-fleet

## REF
> 
- Clustering CoreOS with Vagrant : https://coreos.com/blog/coreos-clustering-with-vagrant/
- Clustering Machines : https://coreos.com/docs/cluster-management/setup/cluster-discovery/
- Launching Containers with fleet : https://coreos.com/docs/launching-containers/launching/launching-containers-fleet/
- Dockerによる開発環境から本番環境までの一貫した管理 | 株式会社サイバーエージェント : https://www.cyberagent.co.jp/techinfo/techreport/report/id=9914
- CoreOSをfleetctlから操作する - Part1: --endpointと--tunnellオプション // masato's blog : http://masato.github.io/2014/07/25/coreos-fleetctl-endpoint-tunnell-options/