CoreOS fleetで分散デプロイ・フェイルオーバー
CoreOS + Vargrant でクラスタ構成を作る - DELAEMON BLOG
前回からの続き
ローカルPCにfleetをセットアップ
% go version
go version go1.4.2 darwin/amd64
% git clone git@github.com:coreos/fleet.git
% cd fleet
% ./build
クラスタを確認
% bin/fleetctl --endpoint=http://172.17.8.101:4001 list-machines -l MACHINE IP METADATA 7911e093c0fd4883891ab966aa7feec3 172.17.8.101 - f3ea4deb0bf2404db1bd6a9199daa511 172.17.8.102 - fb059b20960d4119995c23cc22ca178e 172.17.8.103 -
稼働中のunitを確認、この時点では何もない
% bin/fleetctl --endpoint=http://172.17.8.101:4001 list-units
UNIT MACHINE ACTIVE SUB
systemdに読み込まれたunitを確認、これも何もなし
% bin/fleetctl --endpoint=http://172.17.8.101:4001 list-unit-files
UNIT HASH DSTATE STATE TARGET
unitを登録、確認。systemdに読み込まれたけど、起動はしてない状態
% bin/fleetctl --endpoint=http://172.17.8.101:4001 submit examples/hello.service % bin/fleetctl --endpoint=http://172.17.8.101:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae inactive inactive - % bin/fleetctl --endpoint=http://172.17.8.101:4001 list-units UNIT MACHINE ACTIVE SUB
unitを起動、確認
% bin/fleetctl --endpoint=http://172.17.8.101:4001 start hello Unit hello.service launched on 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.101:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.101:4001 list-units UNIT MACHINE ACTIVE SUB hello.service 7911e093.../172.17.8.101 active running
現在稼働中のサーバが停止したら、他所に移動するか試すためにVM止める
% vagrant status Current machine states: core-01 running (virtualbox) core-02 running (virtualbox) core-03 running (virtualbox) % vagrant suspend core-01 ==> core-01: Saving VM state and suspending execution... % vagrant status Current machine states: core-01 saved (virtualbox) core-02 running (virtualbox) core-03 running (virtualbox)
unitが別のサーバ上で動いてるのを確認
% bin/fleetctl --endpoint=http://172.17.8.103:4001 list-units UNIT MACHINE ACTIVE SUB hello.service f3ea4deb.../172.17.8.102 active running % bin/fleetctl --endpoint=http://172.17.8.103:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102
core-01をvagrant resumeで復活させてもunitは移動しない
依存するunitを同一のサーバ上で動かすサンプルの設定
% cat examples/ping.service [Unit] Description=PING [Service] ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done" % cat examples/pong.service [Unit] Description=PONG [Service] ExecStart=/bin/bash -c "while true; do echo \"pong\"; sleep 1; done" [X-Fleet] MachineOf=ping.service
X-FleetのMachineOf=ping.serviceのように書く
unit起動、submitは端折れる
% bin/fleetctl --endpoint=http://172.17.8.103:4001 start examples/ping.service Unit ping.service launched on 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.103:4001 start examples/pong.service Unit pong.service launched on 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.103:4001 list-units UNIT MACHINE ACTIVE SUB hello.service f3ea4deb.../172.17.8.102 active running ping.service 7911e093.../172.17.8.101 active running pong.service 7911e093.../172.17.8.101 active running % bin/fleetctl --endpoint=http://172.17.8.103:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102 ping.service d9de011 launched launched 7911e093.../172.17.8.101 pong.service 667e8de launched launched 7911e093.../172.17.8.101
unitの稼働状態の詳細を確認
% bin/fleetctl --endpoint=http://172.17.8.103:4001 status ping ● ping.service - PING Loaded: loaded (/run/fleet/units/ping.service; linked-runtime; vendor preset: disabled) Active: active (running) since Sun 2015-08-16 13:09:21 UTC; 1min 38s ago Main PID: 2879 (bash) Memory: 328.0K CPU: 84ms CGroup: /system.slice/ping.service ├─2879 /bin/bash -c while true; do echo "ping"; sleep 1; done └─3101 sleep 1 Aug 16 13:10:51 core-01 bash[2879]: ping Aug 16 13:10:52 core-01 bash[2879]: ping Aug 16 13:10:53 core-01 bash[2879]: ping Aug 16 13:10:54 core-01 bash[2879]: ping Aug 16 13:10:55 core-01 bash[2879]: ping Aug 16 13:10:56 core-01 bash[2879]: ping Aug 16 13:10:57 core-01 bash[2879]: ping Aug 16 13:10:58 core-01 bash[2879]: ping Aug 16 13:10:59 core-01 bash[2879]: ping Aug 16 13:11:00 core-01 bash[2879]: ping % bin/fleetctl --endpoint=http://172.17.8.103:4001 status pong ● pong.service - PONG Loaded: loaded (/run/fleet/units/pong.service; linked-runtime; vendor preset: disabled) Active: active (running) since Sun 2015-08-16 13:09:31 UTC; 1min 38s ago Main PID: 2911 (bash) Memory: 324.0K CPU: 80ms CGroup: /system.slice/pong.service ├─2911 /bin/bash -c while true; do echo "pong"; sleep 1; done └─3125 sleep 1 Aug 16 13:11:00 core-01 bash[2911]: pong Aug 16 13:11:01 core-01 bash[2911]: pong Aug 16 13:11:02 core-01 bash[2911]: pong Aug 16 13:11:03 core-01 bash[2911]: pong Aug 16 13:11:04 core-01 bash[2911]: pong Aug 16 13:11:05 core-01 bash[2911]: pong Aug 16 13:11:06 core-01 bash[2911]: pong Aug 16 13:11:07 core-01 bash[2911]: pong Aug 16 13:11:08 core-01 bash[2911]: pong Aug 16 13:11:09 core-01 bash[2911]: pong
unitのログを確認
% bin/fleetctl --endpoint=http://172.17.8.102:4001 journal -f ping -- Logs begin at Wed 2015-08-12 01:08:16 UTC. -- Aug 16 13:15:49 core-01 bash[2879]: ping Aug 16 13:15:50 core-01 bash[2879]: ping Aug 16 13:15:51 core-01 bash[2879]: ping
unitを停止
% bin/fleetctl --endpoint=http://172.17.8.102:4001 stop ping Unit ping.service loaded on 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-units UNIT MACHINE ACTIVE SUB hello.service f3ea4deb.../172.17.8.102 active running ping.service 7911e093.../172.17.8.101 inactive dead pong.service 7911e093.../172.17.8.101 active running % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102 ping.service d9de011 loaded loaded 7911e093.../172.17.8.101 pong.service 667e8de launched launched 7911e093.../172.17.8.101
pingを止めてもpongは止まらない
unitを削除
% bin/fleetctl --endpoint=http://172.17.8.102:4001 destroy ping Destroyed ping.service % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-units UNIT MACHINE ACTIVE SUB hello.service f3ea4deb.../172.17.8.102 active running % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102 pong.service 667e8de launched inactive -
pingをsystemd上の登録から消すと、pongは止まる、systemdには登録されたまま
pingをunloadしてもpongは止まる
依存するunitのサーバが停止したらunitは別サーバに移動されない
% bin/fleetctl --endpoint=http://172.17.8.102:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102 ping.service d9de011 launched launched 7911e093.../172.17.8.101 pong.service 667e8de launched launched 7911e093.../172.17.8.101 % vagrant suspend core-01 ==> core-01: Saving VM state and suspending execution... % vagrant status Current machine states: core-01 saved (virtualbox) core-02 running (virtualbox) core-03 running (virtualbox) % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-unit-files UNIT HASH DSTATE STATE TARGET hello.service e55c0ae launched launched f3ea4deb.../172.17.8.102 ping.service d9de011 launched launched 7911e093.../172.17.8.101 pong.service 667e8de launched launched 7911e093.../172.17.8.101 % bin/fleetctl --endpoint=http://172.17.8.102:4001 list-units UNIT MACHINE ACTIVE SUB hello.service f3ea4deb.../172.17.8.102 active running ping.service 7911e093.../172.17.8.101 active running pong.service 7911e093.../172.17.8.101 active running % bin/fleetctl --endpoint=http://172.17.8.102:4001 journal -f ping Error running remote command: timed out while initiating SSH connection
CoreOS + Vargrant でクラスタ構成を作る
CoreOSのクラスタ構成ことはじめとしてローカルPC上にCoreOS + Vargrantでテスト環境を作った。
環境
OSX 10.10.4
VirtualBox 4.3.30
Vagrant 1.7.4
coreos-vargrant でクラスタ構築
% git clone https://github.com/coreos/coreos-vagrant.git % cd coreos-vagrant/ % cp config.rb.sample config.rb #config.rbを編集 => $num_instances=3 % vagrant up % vagrant ssh core-01 CoreOS alpha (766.0.0) core@core-01 ~ $ exit % vagrant status Current machine states: core-01 running (virtualbox) core-02 running (virtualbox) core-03 running (virtualbox)
https://coreos.com/os/docs/latest/booting-on-vagrant.html
ifconfigしてVirtualBoxのgatewayのipを把握しておく。vboxnet<%d>で表示されてる。今回は172.17.8.1
複数ある場合はVirtualBoxの設定 -> ネットワーク -> アダプタ2 で確認
CoreOSの設定更新
cd coreos-vagrant cp user-data.sample user-data #user-dataを編集 => discovery: http://172.17.8.1:4001/v2/keys/machines vagrant reload
CoreOS上からクラスタを確認
% vagrant ssh core-01 core@core-01 ~ $ fleetctl list-machines -l MACHINE IP METADATA 18a7945540b041f6a61327a4149fd233 172.17.8.102 - 2d963622db9d4cfc959b3e6b633a4a8a 172.17.8.101 - b9fb68c68dcf47218f83a3755d991fdd 172.17.8.103 -
etcdctl 使ってみる
core@core-01 ~ $ etcdctl set hello world world core@core-01 ~ $ etcdctl get hello world core@core-01 ~ $ exit logout % vagrant ssh core-02 core@core-02 ~ $ etcdctl get hello world
おわり。
Dockerコンテナ上のファイルをIntelliJから編集
共有ボリューム用コンテナ作って、samba用コンテナで共有、ローカルPCと開発サーバ用コンテナから参照、編集、実行する
対象バージョン
Mac OS X 10.10.3
IntelliJ 14.1.3
VirtualBox 4.3.28
Docker 1.6.2
boot2docker 1.6.2
手順
共有ボリューム用コンテナ作成
docker run -v /mnt/sda1/data:/data --name my-data busybox true
samba起動
docker run --rm -v /usr/local/bin/docker:/docker -v /var/run/docker.sock:/docker.sock svendowideit/samba my-data
ターミナルに接続方法が表示される
OS XはFinder->移動->サーバ接続->cifs://192.168.59.103
ゲストで接続できる
開発サーバのコンテナを起動して確認
docker run -it --rm --volumes-from my-data centos /bin/bash ls -la /data
プロジェクト用のディレクトリ作ってIntelliJからファイル作れるように権限あげとく
mkdir /data/projectDir chmod 777 /data/projectDir
あとはIntelliJでプロジェクトで使うSDKの登録や、リモートデバッグの設定をよしなにやればOK
ローカルPC上のファイルいじるよりは反映にタイムラグがあったり、
Runでフリーズ、クラッシュはたまにする
Golangならクロスコンパイルすればいいだけなので、わざわざDocker上で開発しなくていい
Docker using boot2docker on OS X
Docker: Image -> New Container | Now Container -> New Image
boot2docker: Dockerコンテナ稼働に特化した軽量Linuxディストリビューション
開発環境は手元のOS XにDockerで、本番と同じ環境をすぐ作ったり、いらなくなったらすぐ捨てて新しく綺麗な状態にしたい
対象バージョン
Mac OS X 10.10.3
VirtualBox 4.3.28
Docker 1.6.2
boot2docker 1.6.2
手順
0. VirtualBoxインストール
1. docker, boot2docker インストール
brew update
brew install docker boot2docker
※XCodeを最新にしてcommand line toolsも新しくしておく
最初古くて boot2docker-cli が not foundで失敗してた
すんなり行かないときはbrew doctor
2. VMの初期化、起動、ログイン
boot2docker init
boot2docker start
boot2docker ssh
いま動いてるの?
boot2docker status
3. dockerのイメージ取得、コンテナ起動、ログイン
docker pull centos:latest docker run -it --name go_server centos /bin/bash
落としてきたイメージ一覧
docker images
コンテナの停止、起動
docker stop docker start
動いてるコンテナの再起動
docker restart
コンテナの稼働状況一覧
docker ps --all
止まってるコンテナは全部削除
docker ps --all --quiet | xargs docker rm
コンテナからイメージ作成
docker commit -m 'COMMENT' CONTAINER-ID IMAGE-NAME:TAG
コマンドの使い方は?オプション何があるの?
docker help docker ps --help
AtomからIntelliJに移行してGolang
いったんVim辞めたら、すぐ移行できた
cocos2d-xはXCodeやUnityはMonoDevelopなど触ったのも障壁を下げたかも
Atomは起動が少し遅いのとプラグインをもりもり入れた以外は特に問題ない
でもIntelliJは言語のプラグインのみと少しの設定変更で段違いに快適だった
Golangは言語のプラグインが無い?と思ったけどレポジトリ追加すれば使える
手順
0. $GOPATHはよしなに設定してある想定
1.レポジトリ追加
Preference -> Plugins -> Browse Repositories -> Manage repositories -> Custom Plugin Repositories
で以下追加
https://plugins.jetbrains.com/plugins/alpha/list
2. Go というプラグインが検索結果に表示される、インストール
3.再起動
4.Project Structure で Go SDK追加
/usr/local/opt/go とかインストールしたパスを指定すればいいけど、
そもそもProject Structureってどこからいけるのと思ったら、
Help->Find Actionすればすぐたどり着ける。困ったらまずこれ
唯一残念なところ
ブレークポイントは効かない
IntelliJの設定はこちらを参考にさせてもらった
NeoVim Install On Ubuntu14.04
Linuxbrewが必要でrubyやpython-setuptoolsまで入れたら、
結局不要で、Ubuntuはパッケージ用意されてた
Installing · neovim/neovim Wiki · GitHub
Homebrew/linuxbrew · GitHub
sudo apt-get install curl ruby python-setuptools ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)" #/path/.linuxbrew/binにパスを通す brew tap neovim/homebrew-neovim brew install --HEAD neovim #省略 make[2]: *** [CMakeFiles/busted.dir/all] Error 2 make[2]: Leaving directory `/tmp/neovim-yzfKc0/.deps' make[1]: *** [all] Error 2 make[1]: Leaving directory `/tmp/neovim-yzfKc0/.deps' make: *** [deps] Error 2 READ THIS: https://github.com/Homebrew/linuxbrew/blob/master/share/doc/homebrew/Troubleshooting.md#troubleshooting If reporting this issue please do so at (not Homebrew/homebrew): https://github.com/neovim/homebrew-neovim/issues
Linuxbrewで入れるのかと思ったら時間かかるし、途中でコケる。
Ubuntuはパッケージ用意されてる。
sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt-get update
sudo apt-get install neovim
PHPで色付きdump
書きなぐり
<?php // echo -e '\e[VALUE[;VALUE..]mSTRINGS\e[m' /* Text attributes 0 All attributes off 1 Bold on 4 Underscore (on monochrome display adapter only) 5 Blink on 7 Reverse video on 8 Concealed on Foreground colors 30 Black 31 Red 32 Green 33 Yellow 34 Blue 35 Magenta 36 Cyan 37 White Background colors 40 Black 41 Red 42 Green 43 Yellow 44 Blue 45 Magenta 46 Cyan 47 White */ $output = null; $indent = null; function color($value = null) { global $output; global $indent; switch (true) { case is_int($value): $type = '(int)'; $color = 36; // Cyan break; case is_string($value): $type = '(str)'; $color = 31; // Red break; case is_bool($value): $type = '(bool)'; $color = 32; // Green $value = $value ? 'true' : 'false'; break; case is_array($value): $output .= "$indent(array) => [\n"; $indent .= " "; foreach ($value as $v) { color($v); } $indent = substr($indent , 0 , strlen($indent) - 2); $output .= "$indent]\n"; return; case is_object($value): break; default: if (is_null($value)) { $type = '(null)'; $value = 'null'; } $color = 33; // Yellow break; } $output .= sprintf("\e[%dm%s%s %s \e[m\n", $color, $indent, $type, $value); } color(123); color('abc'); color(true); color(); color([1,'1',false]); color([[1,2,3], ['a', 'b', 'c'], [true, false, null]]); echo $output . "\n";
こんな感じ