LoginSignup
14
14

More than 5 years have passed since last update.

CoreOS を VPC で楽しんでみたい

Last updated at Posted at 2014-04-01

CoreOS だ!

もぉいじりましたか? とりあえず動かしたいですよね。@voluntas さんの CoreOS が提供してくれるもの - Qiita を写経してみることにしました。でも Vagrant とか面倒です。使ったことありません。なんで EC2 使ってみることにしました。

CoreOS 自体のことは @mopemope 先生の CoreOS 入門 - Qiita が全然入門じゃなくていいらしいぞ!

好き好き大嫌い! CloudFormation

CoreOS の CloudFront template はあるんですが、Classic EC2 を使ってるので VPC 用 template に改造してみました。S3 bucket 用意するのは面倒なので保存して使ってます。

構成としては

  • 2 AZ
  • AZ 毎に public/private subnet を 1 つずつ (全部で 4 つ)
  • でも片方の public subnet にだけ Amazon Linux を立ててここで etcd 起動
  • Amazon Linux に login して fleetctl で CoreOS クラスタをごにょごにょ!

という心積もりです。CoreOS instance はオフィシャルの template 同様 Auto Scaling Group で作ってみてます。

にしても、Amazon Linux は Docker は repository に入れるし、golang は repository に入れるし。見境ないっす。

以下は Amazon Linux 2014.03.0 と CoreOS 268.1.0 のときのログです。

VPC 内に CoreOS クラスタを作ってみる

保存したファイル読み込んで、AZ1, AZ2 に AZ 名入れて、ssh 用のKeyPair 入れれば動くはず。

specify_parameters.png

成功してれば Outputs に IP address が出てるので、ec2-user で login しましょう。

outputs.png

$ eval `ssh-agent`
Agent pid 32135
$ ssh-add ~/.ssh/mykey.pem
$ slogin -A -l ec2-user 54.199.246.68
The authenticity of host '54.199.246.68 (54.199.246.68)' can't be established.
ECDSA key fingerprint is c1:b5:71:15:e6:3b:f3:52:b6:ec:eb:2e:c6:a1:8c:b7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '54.199.246.68' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2014.03-release-notes/
No packages needed for security; 19 packages available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-10-0-0-81 ~]$

無理やり etcd 立ててるので、listen しています。

[ec2-user@ip-10-0-0-81 ~]$ netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 :::22                       :::*                        LISTEN
tcp        0      0 :::7001                     :::*                        LISTEN
tcp        0      0 :::4001                     :::*                        LISTEN

動いてるね! etcd の標準出力は /var/log/etcd.log に流してます。

写経開始

"etcd を使ってクラスタを構築する" 編

Private subnets に登録されている CoreOS インスタンスも確認できます。

[ec2-user@ip-10-0-0-81 ~]$ curl -s http://localhost:4001/v2/keys/machines
{"action":"get","node":{"key":"/machines","dir":true,"nodes":[{"key":"/machines/53a8da493aa94b469b8452e8b020ba3e","value":"http://10.0.3.52:7001","expiration":"2014-04-08T04:51:53.958060477Z","ttl":603881,"modifiedIndex":2,"createdIndex":2},{"key":"/machines/1a5d7b965f6745339d0c026e64e566bb","value":"http://10.0.3.51:7001","expiration":"2014-04-08T04:51:55.909665662Z","ttl":603883,"modifiedIndex":4,"createdIndex":4},{"key":"/machines/08a3aa7042314a80a97f3a938ac34e23","value":"http://10.0.2.89:7001","expiration":"2014-04-08T04:51:56.927863447Z","ttl":603884,"modifiedIndex":5,"createdIndex":5}],"modifiedIndex":2,"createdIndex":2}}

build したコマンドは /usr/local/bin に突っ込んでるので、上のリストから適当に IP address 選んで環境変数に突っ込むとクラスタが見えます。

[ec2-user@ip-10-0-0-81 ~]$ export FLEETCTL_ENDPOINT=http://10.0.2.89:4001
[ec2-user@ip-10-0-0-81 ~]$ fleetctl list-machines -l
MACHINE                                 IP              METADATA
3aec34c9-e4d6-4821-b19a-bcc281317d95    10.0.3.52       -
499b9828-8175-4045-9d1a-79e79497834b    10.0.3.51       -
d5c8cd66-042c-46a9-9faa-fad5fc3b43dd    10.0.2.89       -

"fleet で Cluster にデプロイする" 編

/root に etcdfleet を build したのがあるので、コピーしてみましょう。

[ec2-user@ip-10-0-0-81 ~]$ sudo cp -r /root/etcd /root/fleet .
[ec2-user@ip-10-0-0-81 ~]$ sudo chown -R ec2-user.ec2-user etcd fleet
[ec2-user@ip-10-0-0-81 ~]$ cd fleet/example
[ec2-user@ip-10-0-0-81 examples]$ ls
hello.service  ping.service  pong.service
[ec2-user@ip-10-0-0-81 examples]$ fleetctl list-units
UNIT    LOAD    ACTIVE  SUB     DESC    MACHINE
[ec2-user@ip-10-0-0-81 examples]$ fleetctl submit hello.service
[ec2-user@ip-10-0-0-81 examples]$ fleetctl start hello.service
Job hello.service started on 3aec34c9.../10.0.3.52
[ec2-user@ip-10-0-0-81 examples]$ fleetctl list-units
UNIT            LOAD    ACTIVE  SUB     DESC            MACHINE
hello.service   loaded  active  running Hello World     3aec34c9.../10.0.3.52

わーい、動いてる! key pair は同じのを設定しています。ssh-agent とか使ってればそのままアクセス可です。念のため、user name は core です。

[ec2-user@ip-10-0-0-81 examples]$ fleetctl status hello.service
The authenticity of host '10.0.3.52:22' can't be established.
RSA key fingerprint is de:b0:ec:0e:d3:84:41:d5:54:78:1d:cd:c2:0d:14:66.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.3.52:22' (RSA) to the list of known hosts.
hello.service - Hello World
   Loaded: loaded (/run/systemd/system/hello.service; enabled-runtime)
   Active: active (running) since Tue 2014-04-01 05:28:30 UTC; 2min 19s ago
 Main PID: 3105 (bash)
   CGroup: /system.slice/hello.service
           ├─3105 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
           └─3247 sleep 1

Apr 01 05:30:41 ip-10-0-3-52 bash[3105]: Hello, world
Apr 01 05:30:42 ip-10-0-3-52 bash[3105]: Hello, world
Apr 01 05:30:43 ip-10-0-3-52 bash[3105]: Hello, world
Apr 01 05:30:44 ip-10-0-3-52 bash[3105]: Hello, world

"fleet で Cluster でフェイルオーバーを確認する" 編

で、おもむろにここで 10.0.3.52 の子を terminate してみます。Management Console からでも何でも。すると???

[ec2-user@ip-10-0-0-81 examples]$ fleetctl list-machines -l
MACHINE                                 IP              METADATA
499b9828-8175-4045-9d1a-79e79497834b    10.0.3.51       -
d5c8cd66-042c-46a9-9faa-fad5fc3b43dd    10.0.2.89       -
[ec2-user@ip-10-0-0-81 examples]$ fleetctl status hello.service
The authenticity of host '10.0.2.89:22' can't be established.
RSA key fingerprint is f6:3d:f8:f7:fa:91:11:16:07:0c:22:4d:b8:c7:0f:a9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.2.89:22' (RSA) to the list of known hosts.
hello.service - Hello World
   Loaded: loaded (/run/systemd/system/hello.service; enabled-runtime)
   Active: active (running) since Tue 2014-04-01 05:37:48 UTC; 33s ago
 Main PID: 3105 (bash)
   CGroup: /system.slice/hello.service
           ├─3105 /bin/bash -c while true; do echo "Hello, world"; sleep 1; done
           └─3143 sleep 1

Apr 01 05:38:11 ip-10-0-2-89 bash[3105]: Hello, world
Apr 01 05:38:12 ip-10-0-2-89 bash[3105]: Hello, world
Apr 01 05:38:13 ip-10-0-2-89 bash[3105]: Hello, world
Apr 01 05:38:14 ip-10-0-2-89 bash[3105]: Hello, world
Apr 01 05:38:15 ip-10-0-2-89 bash[3105]: Hello, world

死んだこのサービスが乗り移っている!! で、Auto Scaling 使ってるので暫くするとちゃんと machines が 3 台に回復していました。怖い!!

[ec2-user@ip-10-0-0-81 examples]$ fleetctl list-machines -l
MACHINE                                 IP              METADATA
499b9828-8175-4045-9d1a-79e79497834b    10.0.3.51       -
d5c8cd66-042c-46a9-9faa-fad5fc3b43dd    10.0.2.89       -
f98380bc-c3b9-4db4-ab5b-776dd4920da8    10.0.3.242      -

まとめ

完全にこぴぺですごめんなさい

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14