TechnologyTips

HashiさんにConsulしてもらってみた

consul_webtop

 

 

 

 

 

 

 

 

Consul は Vagrant/Packer でおなじみ、 Mitchell Hashimoto 、 HashiCorp のクラスタ管理プロダクトです

Mitchell Hashimoto のリリースするプロダクトは私的にすさまじいものと思っていたんでちょっとかるーく検証してみました。(のメモ)

以下特徴

  • カテゴリとしてはOrchestrationToolらしい
  • バイナリ(1ファイルだけ)を解凍するだけですぐ使える
  • クラスタの情報を得るのに専用プロトコルが必要ない
  • クラスタの状態をHTTP/RPC/DNSなどをゴシッププロトコルで数百台のnodeに状態を高速に伝播させれる
  • 上記の事からHealthCheckを自分で書いて、Failしたら高速に迂回できる
  • KVSをもっている
  • マルチDataCenter対応
  • 軽いWebがある(本当に軽くて、900kb程度しかない)
  • Serfとの違いはよくわからない

consul検証メモ

 

Vagrantfile は これ で検証

 

sudo -i
curl -OL https://dl.bintray.com/mitchellh/consul/0.4.0_linux_amd64.zip
yum install -y unzip bind-utils
unzip 0.4.0_linux_amd64.zip

とりま起動

[root@consal1 ~]# ./consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul
==> 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: 'consal1.vagrant'
Datacenter: 'dc1'
Server: true (bootstrap: true)
Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
Cluster Addr: 172.16.236.132 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2014/10/07 14:14:29 [INFO] serf: EventMemberJoin: consal1.vagrant 172.16.236.132
2014/10/07 14:14:29 [INFO] serf: EventMemberJoin: consal1.vagrant.dc1 172.16.236.132
2014/10/07 14:14:29 [INFO] raft: Node at 172.16.236.132:8300 [Follower] entering Follower state
2014/10/07 14:14:29 [INFO] consul: adding server consal1.vagrant (Addr: 172.16.236.132:8300) (DC: dc1)
2014/10/07 14:14:29 [INFO] consul: adding server consal1.vagrant.dc1 (Addr: 172.16.236.132:8300) (DC: dc1)
2014/10/07 14:14:29 [ERR] agent: failed to sync remote state: No cluster leader

clusterに参加してるか確認

[root@consal1 ~]# ./consul members
Node Address Status Type Build Protocol
consal1.vagrant 172.16.236.132:8301 alive server 0.4.0 2

Cluster Member

Join

[root@consal2 ~]# ./consul agent -data-dir /tmp/consul -join 10.10.10.10
==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Joining cluster...
Join completed. Synced with 1 initial agents
==> Consul agent running!
Node name: 'consal2.vagrant'
Datacenter: 'dc1'
Server: false (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
Cluster Addr: 172.16.236.133 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2014/10/07 14:17:59 [INFO] serf: EventMemberJoin: consal2.vagrant 172.16.236.133
2014/10/07 14:17:59 [INFO] agent: (LAN) joining: [10.10.10.10]
2014/10/07 14:17:59 [INFO] serf: EventMemberJoin: consal1.vagrant 172.16.236.132
2014/10/07 14:17:59 [INFO] agent: (LAN) joined: 1 Err: <nil>
2014/10/07 14:17:59 [ERR] agent: failed to sync remote state: No known Consul servers
2014/10/07 14:17:59 [INFO] consul: adding server consal1.vagrant (Addr: 172.16.236.132:8300) (DC: dc1)

確認

[root@consal1 ~]# ./consul members
Node Address Status Type Build Protocol
consal1.vagrant 172.16.236.132:8301 alive server 0.4.0 2
consal2.vagrant 172.16.236.133:8301 alive client 0.4.0 2

3をJoin

[root@consal3 ~]# ./consul agent -data-dir /tmp/consul -join 10.10.10.10
==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Joining cluster...
Join completed. Synced with 1 initial agents
==> Consul agent running!
Node name: 'consal3.vagrant'
Datacenter: 'dc1'
Server: false (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
Cluster Addr: 172.16.236.134 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2014/10/07 14:18:57 [INFO] serf: EventMemberJoin: consal3.vagrant 172.16.236.134
2014/10/07 14:18:57 [INFO] agent: (LAN) joining: [10.10.10.10]
2014/10/07 14:18:57 [INFO] serf: EventMemberJoin: consal2.vagrant 172.16.236.133
2014/10/07 14:18:57 [INFO] serf: EventMemberJoin: consal1.vagrant 172.16.236.132
2014/10/07 14:18:57 [INFO] agent: (LAN) joined: 1 Err: <nil>
2014/10/07 14:18:57 [ERR] agent: failed to sync remote state: No known Consul servers
2014/10/07 14:18:57 [INFO] consul: adding server consal1.vagrant (Addr: 172.16.236.132:8300) (DC: dc1)

全部のCluster

[root@consal1 ~]# ./consul members
Node Address Status Type Build Protocol
consal1.vagrant 172.16.236.132:8301 alive server 0.4.0 2
consal2.vagrant 172.16.236.133:8301 alive client 0.4.0 2
consal3.vagrant 172.16.236.134:8301 alive client 0.4.0 2
[root@consal2 ~]# ./consul members
Node Address Status Type Build Protocol
consal2.vagrant 172.16.236.133:8301 alive client 0.4.0 2
consal1.vagrant 172.16.236.132:8301 alive server 0.4.0 2
consal3.vagrant 172.16.236.134:8301 alive client 0.4.0 2
[root@consal3 ~]# ./consul members
Node Address Status Type Build Protocol
consal1.vagrant 172.16.236.132:8301 alive server 0.4.0 2
consal3.vagrant 172.16.236.134:8301 alive client 0.4.0 2
consal2.vagrant 172.16.236.133:8301 alive client 0.4.0 2
[root@consal1 ~]# ./consul members -detailed
Node Address Status Tags
consal1.vagrant 172.16.236.132:8301 alive role=consul,dc=dc1,vsn=2,vsn_min=1,vsn_max=2,build=0.4.0:,port=8300,bootstrap=1
consal2.vagrant 172.16.236.133:8301 alive build=0.4.0:,role=node,dc=dc1,vsn=2,vsn_min=1,vsn_max=2
consal3.vagrant 172.16.236.134:8301 alive dc=dc1,vsn=2,vsn_min=1,vsn_max=2,build=0.4.0:,role=node

Querying Nodes

さがす

[root@consal1 ~]# dig @127.0.0.1 -p 8600 consal1.vagrant.node.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 -p 8600 consal1.vagrant.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11445
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;consal1.vagrant.node.consul. IN A

;; ANSWER SECTION:
consal1.vagrant.node.consul. 0 IN A 172.16.236.132

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 14:23:10 2014
;; MSG SIZE rcvd: 88

[root@consal1 ~]#

Halth Check

JSON作ってconfig-dirで指定

[root@consal1 ~]# mkdir /etc/consul.d/
[root@consal1 ~]# echo '{"check": {"name": "ping", "script": "ping -c1 google.com >/dev/null", "interval": "30s"}}' >/etc/consul.d/ping.json
[root@consal1 ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, "check": {"script": "curl localhost:80 >/dev/null 2>&amp;1", "interval": "10s"}}}' >/etc/consul.d/web.json
[root@consal1 ~]# ./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: 'consal1.vagrant'
Datacenter: 'dc1'
Server: true (bootstrap: true)
Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
Cluster Addr: 172.16.236.132 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2014/10/07 14:30:29 [INFO] serf: EventMemberJoin: consal1.vagrant 172.16.236.132
2014/10/07 14:30:29 [INFO] serf: EventMemberJoin: consal1.vagrant.dc1 172.16.236.132
2014/10/07 14:30:29 [INFO] raft: Node at 172.16.236.132:8300 [Follower] entering Follower state
2014/10/07 14:30:29 [INFO] consul: adding server consal1.vagrant (Addr: 172.16.236.132:8300) (DC: dc1)
2014/10/07 14:30:29 [INFO] consul: adding server consal1.vagrant.dc1 (Addr: 172.16.236.132:8300) (DC: dc1)
2014/10/07 14:30:29 [ERR] agent: failed to sync remote state: No cluster leader
2014/10/07 14:30:29 [WARN] Check 'service:web' is now critical
2014/10/07 14:30:30 [WARN] raft: Heartbeat timeout reached, starting election
2014/10/07 14:30:30 [INFO] raft: Node at 172.16.236.132:8300 [Candidate] entering Candidate state
2014/10/07 14:30:30 [INFO] raft: Election won. Tally: 1
2014/10/07 14:30:30 [INFO] raft: Node at 172.16.236.132:8300 [Leader] entering Leader state
2014/10/07 14:30:30 [INFO] consul: cluster leadership acquired
2014/10/07 14:30:30 [INFO] consul: New leader elected: consal1.vagrant
2014/10/07 14:30:39 [WARN] Check 'service:web' is now critical
2014/10/07 14:30:49 [WARN] Check 'service:web' is now critical
2014/10/07 14:30:59 [WARN] Check 'service:web' is now critical

httpを使って障害の確認

[root@consal2 ~]# /etc/init.d/httpd stop
httpd を停止中: [ OK ]
[root@consal2 ~]# curl http://localhost:8500/v1/health/state/critical
[{"Node":"consal2.vagrant","CheckID":"service:web","Name":"Service 'web' check","Status":"critical","Notes":"","Output":"","ServiceID":"web","ServiceName":"web"}]

webサービスから外れてる

[root@consal1 ~]# dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25077
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;web.service.consul. IN A

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 14:32:46 2014
;; MSG SIZE rcvd: 36

httpd install -> start 後


[root@consal1 ~]# dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15441
;; 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 172.16.236.132

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 14:33:56 2014
;; MSG SIZE rcvd: 70

出てきた

SRVを問い合わるとポート番号も出てくる

[root@consal2 ~]# dig SRV @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> SRV @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53503
;; 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 consal2.vagrant.node.dc1.consul.

;; ADDITIONAL SECTION:
consal2.vagrant.node.dc1.consul. 0 IN A 172.16.236.133

;; Query time: 2 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 16:10:14 2014
;; MSG SIZE rcvd: 152

ついでにnode2でもwebを作成

[root@consal2 ~]# yum -y insyall httpd &amp;&amp; service httpd start
---- 略 ---
[root@consal2 ~]# mkdir /etc/consul.d/
[root@consal2 ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, "check": {"script": "curl localhost:80 >/dev/null 2>&amp;1", "interval": "10s"}}}' >/etc/consul.d/web.json
[root@consal2 ~]# ./consul agent -data-dir /tmp/consul -join 10.10.10.10 -config-dir /etc/consul.d/
==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Joining cluster...
Join completed. Synced with 1 initial agents
==> Consul agent running!
Node name: 'consal2.vagrant'
Datacenter: 'dc1'
Server: false (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
Cluster Addr: 172.16.236.133 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2014/10/07 14:35:10 [INFO] serf: EventMemberJoin: consal2.vagrant 172.16.236.133
2014/10/07 14:35:10 [INFO] agent: (LAN) joining: [10.10.10.10]
2014/10/07 14:35:10 [INFO] serf: EventMemberJoin: consal1.vagrant 172.16.236.132
2014/10/07 14:35:10 [INFO] serf: EventMemberJoin: consal3.vagrant 172.16.236.134
2014/10/07 14:35:10 [INFO] agent: (LAN) joined: 1 Err: <nil>
2014/10/07 14:35:10 [ERR] agent: failed to sync remote state: No known Consul servers
2014/10/07 14:35:10 [INFO] consul: adding server consal1.vagrant (Addr: 172.16.236.132:8300) (DC: dc1)
2014/10/07 14:35:10 [WARN] Check 'service:web' is now critical
2014/10/07 14:35:10 [INFO] agent: Synced service 'web'
2014/10/07 14:35:10 [INFO] agent: Synced check 'service:web'

QUERYしてみる

[root@consal1 ~]# dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20336
;; flags: qr aa rd; QUERY: 1, ANSWER: 2, 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 172.16.236.133
web.service.consul. 0 IN A 172.16.236.132

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 14:36:09 2014
;; MSG SIZE rcvd: 104

落としてみる

[root@consal2 ~]# killall httpd
[root@consal3 ~]# dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46682
;; 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 172.16.236.132

;; Query time: 1 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue Oct 7 14:38:14 2014
;; MSG SIZE rcvd: 70

ちゃんと消えてる(・∀・)イイ!!

WebUI

WebUI

[root@consal3 ~]# curl -OL https://dl.bintray.com/mitchellh/consul/0.4.0_web_ui.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 242k 100 242k 0 0 100k 0 0:00:02 0:00:02 --:--:-- 319k
[root@consal3 ~]# unzip 0.4.0_web_ui.zip
Archive: 0.4.0_web_ui.zip
inflating: dist/index.html
creating: dist/static/
inflating: dist/static/application.min.js
inflating: dist/static/base.css
inflating: dist/static/bootstrap.min.css
inflating: dist/static/consul-logo.png
inflating: dist/static/favicon.png
inflating: dist/static/loading-cylon-purple.svg
[root@consal3 ~]# ./consul agent -data-dir /tmp/consul -join 10.10.10.10 -config-dir /etc/consul.d/ -ui-dir=dist/ -client=172.16.236.134

WebUI でHealthCheckやNodeの一覧が(‘(゚∀゚∩見れるよ!

コメントを残す