前回は『OSS編~オーケストレーションツール Consul 01 インストール編~』と題して、オーケストレーションのConsulでインストールから起動までを試してみました。
今回は『OSS編~オーケストレーションツール Consul 02 サービス検出編~』と題して、オーケストレーションのConsulでサービス検出を試してみたいと思います。
サービス検出
1.サービスの登録
先ずは設定ファイルの置き場を作成して定義ファイル(80ポートをチェックしているフリ)を作成します。
1 2 |
# mkdir /etc/consul.d # echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' > /etc/consul.d/web.json |
2.エージェント起動
ConsulのServerエージェントを起動します。
「agent: Synced service ‘web」とサービスの検出が行われているのがわかります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -config-dir /etc/consul.d ==> WARNING: BootstrapExpect Mode is specified as 1; this is the same as Bootstrap mode. ==> WARNING: Bootstrap mode enabled! Do not enable unless necessary ==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1 ==> Starting Consul agent... ==> Starting Consul agent RPC... ==> Consul agent running! Node name: 'vagrant-ubuntu-trusty-64' Datacenter: 'dc1' Server: true (bootstrap: true) Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400) Cluster Addr: 10.0.2.15 (LAN: 8301, WAN: 8302) Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false Atlas: <disabled> ==> Log data will now stream in as it occurs: 2015/03/11 17:37:29 [INFO] serf: EventMemberJoin: vagrant-ubuntu-trusty-64 10.0.2.15 2015/03/11 17:37:29 [INFO] serf: EventMemberJoin: vagrant-ubuntu-trusty-64.dc1 10.0.2.15 2015/03/11 17:37:29 [INFO] raft: Node at 10.0.2.15:8300 [Follower] entering Follower state 2015/03/11 17:37:29 [INFO] consul: adding server vagrant-ubuntu-trusty-64 (Addr: 10.0.2.15:8300) (DC: dc1) 2015/03/11 17:37:29 [INFO] consul: adding server vagrant-ubuntu-trusty-64.dc1 (Addr: 10.0.2.15:8300) (DC: dc1) 2015/03/11 17:37:29 [ERR] agent: failed to sync remote state: No cluster leader 2015/03/11 17:37:30 [WARN] raft: Heartbeat timeout reached, starting election 2015/03/11 17:37:30 [INFO] raft: Node at 10.0.2.15:8300 [Candidate] entering Candidate state 2015/03/11 17:37:30 [INFO] raft: Election won. Tally: 1 2015/03/11 17:37:30 [INFO] raft: Node at 10.0.2.15:8300 [Leader] entering Leader state 2015/03/11 17:37:30 [INFO] consul: cluster leadership acquired 2015/03/11 17:37:30 [INFO] consul: New leader elected: vagrant-ubuntu-trusty-64 2015/03/11 17:37:30 [INFO] raft: Disabling EnableSingleNode (bootstrap) 2015/03/11 17:37:31 [INFO] agent: Synced service 'web' |
3.クエリーでの確認
DNSとHTTPのAPIで確認してみます。
DNSでは「NAME.service.consul」のような形になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ dig @127.0.0.1 -p 8600 web.service.consul ; <<>> DiG 9.9.5-3-Ubuntu <<>> @127.0.0.1 -p 8600 web.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21786 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;web.service.consul. IN A ;; ANSWER SECTION: web.service.consul. 0 IN A 10.0.2.15 ;; Query time: 4 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Mar 11 17:42:01 JST 2015 ;; MSG SIZE rcvd: 70 |
SRVオプションを利用する事でノードのレコードを取得出来ます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ dig @127.0.0.1 -p 8600 web.service.consul SRV ; <<>> DiG 9.9.5-3-Ubuntu <<>> @127.0.0.1 -p 8600 web.service.consul SRV ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64104 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 vagrant-ubuntu-trusty-64.node.dc1.consul. ;; ADDITIONAL SECTION: vagrant-ubuntu-trusty-64.node.dc1.consul. 0 IN A 10.0.2.15 ;; Query time: 10 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Mar 11 17:44:42 JST 2015 ;; MSG SIZE rcvd: 170 |
次の形にする事でタグによるフィルターも可能です。
「TAG.NAME.service.consul」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ dig @127.0.0.1 -p 8600 rails.web.service.consul ; <<>> DiG 9.9.5-3-Ubuntu <<>> @127.0.0.1 -p 8600 rails.web.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39355 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;rails.web.service.consul. IN A ;; ANSWER SECTION: rails.web.service.consul. 0 IN A 10.0.2.15 ;; Query time: 4 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Mar 11 17:47:57 JST 2015 ;; MSG SIZE rcvd: 82 |
最後にHTTP APIでも試してみます。
1 2 |
$ curl http://localhost:8500/v1/catalog/service/web [{"Node":"vagrant-ubuntu-trusty-64","Address":"10.0.2.15","ServiceID":"web","ServiceName":"web","ServiceTags":["rails"],"ServiceAddress":"","ServicePort":80}] |
いかがでしたでしょうか?
次回もお楽しみに!!!