OSS編~オーケストレーションツール Serf 02 イベントハンドラ編~

Serfではイベントが発生した際に任意のスクリプトを実行する事が出来ます。

Serfのイベントタイプ

Serfでは以下のイベントのタイプがあります。

イベントのタイプにあわせて実行するスクリプトを

・member-join

クラスターへの参加

・member-leave

クラスターからの離脱

・member-failed

メンバーの障害

・member-update

メンバーのアップデート

・member-reap

リストからの削除

・user

カスタムユーザーイベント

・query

クライアント側で実行結果が確認出来るカスタムユーザーイベント

イベントハンドラの実行

1.イベントハンドラ

先ずはSerfのイベントの環境変数を出力するスクリプトを作成します。

1$ vim handler.sh2#!/bin/bash34echo5echo "New event: ${SERF_EVENT}. Data follows..."6while read line; do7    printf "${line}\n"8done9$ chmod +x handler.sh

Serfエージェントを起動する際にイベントハンドラを指定して確認してみます。

1$ serf agent -log-level=debug  -node=foo -bind=127.0.0.1:5000 -rpc-addr=127.0.0.1:7373 -event-handler=./handler.sh2==> Starting Serf agent...3==> Starting Serf agent RPC...4==> Serf agent running!5         Node name: 'foo'6         Bind addr: '127.0.0.1:5000'7          RPC addr: '127.0.0.1:7373'8         Encrypted: false9          Snapshot: false10           Profile: lan1112==> Log data will now stream in as it occurs:1314    2015/03/06 17:00:49 [INFO] agent: Serf agent starting15    2015/03/06 17:00:49 [INFO] serf: EventMemberJoin: foo 127.0.0.116    2015/03/06 17:00:50 [INFO] agent: Received event: member-join17    2015/03/06 17:00:50 [DEBUG] agent: Event 'member-join' script output:18New event: member-join. Data follows...19foo     127.0.0.1

member-joinイベントでスクリプトが実施されたのが分かります。

2.カスタムユーザーイベント

user-eventを試してみます。

別ターミナルから以下のコマンドを実行します。

1$ serf event hello-there2Event 'hello-there' dispatched! Coalescing enabled: true

ユーザーイベントが実行されているのが分かります。

12015/03/06 17:33:21 [INFO] agent.ipc: Accepted client: 127.0.0.1:486342    2015/03/06 17:33:21 [DEBUG] agent: Requesting user event send: hello-there. Coalesced: true. Payload: ""3    2015/03/06 17:33:22 [INFO] agent: Received event: user-event: hello-there4    2015/03/06 17:33:22 [DEBUG] agent: Event 'user' script output:5New event: user. Data follows...

3.カスタムクエリー

カスタムクエリーを実行してみます。

イベントハンドラの結果が返ってきました。

1$ serf query test2Query 'test' dispatched3Ack from 'foo'4Response from 'foo':5New event: query. Data follows...6Total Acks: 17Total Responses: 189    2015/03/06 17:39:31 [INFO] agent.ipc: Accepted client: 127.0.0.1:4863810    2015/03/06 17:39:31 [DEBUG] agent: Requesting query send: test. Payload: ""11    2015/03/06 17:39:31 [INFO] agent: Received event: query: test12    2015/03/06 17:39:31 [DEBUG] serf: messageQueryResponseType: foo13    2015/03/06 17:39:32 [DEBUG] agent: Event 'query' script output:14New event: query. Data follows...15    2015/03/06 17:39:32 [DEBUG] serf: messageQueryResponseType: foo

4.イベントハンドラの複数設定

イベントハンドラを複数設置して試してみたいと思います。

以下のコンフィグではカスタムユーザーイベント「test」で実行されるhandler.shとカスタムクエリー「load」でのみ実行されるスクリプトになります。

1$ vim serf.conf2{3    "role" : "test",4    "event_handlers" : [5        "./handler.sh",6        "user:load=./load.sh"7    ]8}

load average を表示するスクリプトを作成します。

1$ vim load.sh2#!/bin/bash3echo `uptime | sed -e 's/.*average: //g' -e 's/,//g'`4$ chmod +x load.sh

エージェント起動時に「-config-file」を指定して実行します。

1$ serf agent -log-level=debug  -node=foo -bind=127.0.0.1:5000 -rpc-addr=127.0.0.1:7373 -config-file=./serf.conf2==> Deprecation warning: 'Role' has been replaced with 'Tags'3==> Starting Serf agent...4==> Starting Serf agent RPC...5==> Serf agent running!6         Node name: 'foo'7         Bind addr: '127.0.0.1:5000'8          RPC addr: '127.0.0.1:7373'9         Encrypted: false10          Snapshot: false11           Profile: lan1213==> Log data will now stream in as it occurs:1415    2015/03/06 18:13:06 [INFO] agent: Serf agent starting16    2015/03/06 18:13:07 [INFO] serf: EventMemberJoin: foo 127.0.0.117    2015/03/06 18:13:08 [INFO] agent: Received event: member-join

各イベントを実行してみます。

1$ serf query load2Query 'load' dispatched3Ack from 'foo'4Response from 'foo': 0.01 0.03 0.055Total Acks: 16Total Responses: 17vagrant@vagrant-ubuntu-trusty-64:~/tmp$ serf event test8Event 'test' dispatched! Coalescing enabled: true

それぞれのイベントにあわせてスクリプトが実行されているのが分かります。

12015/03/06 18:13:15 [DEBUG] agent: Requesting query send: load. Payload: ""2    2015/03/06 18:13:15 [INFO] agent: Received event: query: load3    2015/03/06 18:13:15 [DEBUG] serf: messageQueryResponseType: foo4    2015/03/06 18:13:15 [DEBUG] agent: Event 'query' script output: 0.01 0.03 0.055    2015/03/06 18:13:15 [DEBUG] serf: messageQueryResponseType: foo6    2015/03/06 18:13:28 [INFO] agent.ipc: Accepted client: 127.0.0.1:486497    2015/03/06 18:13:28 [DEBUG] agent: Requesting user event send: test. Coalesced: true. Payload: ""8    2015/03/06 18:13:29 [INFO] agent: Received event: user-event: test9    2015/03/06 18:13:29 [DEBUG] agent: Event 'user' script output:10New event: user. Data follows...

5.暗号化

最後に暗号化を試してみます。

キーを作成してエージェント起動時に「-encrypt」で指定して実行します。

1$ serf keygen2cgFnu5Jv4sShuwAt6Jej+g==34$ serf agent -encrypt cgFnu5Jv4sShuwAt6Jej+g==

いかがでしたでしょうか?

次回もお楽しみに!!!

この記事を書いた人

aws-recipe-user