Consulを使ってみる(インストール編)

概要

Consulを使ってみたので、そのまとめ。
まずは第一弾なので、インストール〜Consulの起動までやります。
Installing Consul - Consul
Run the Agent - Consul

http://labs.gree.jp/blog/wp-content/uploads/2014/12/consul_logo.png


Consul

前提

検証環境:Dockerのコンテナ
OS:CentOS6

インストールしてみる

まずは、
Dockerでコンテナを立ち上げます。

$ docker run -t -i -d \
--name consul_1 \
-h consul_1 \
-p 8300:8300 \
-p 8301:8301/tcp \
-p 8301:8301/udp \
-p 8302:8302/tcp \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53/tcp \
-p 8600:53/udp \
centos:centos6 /bin/bash
2158edd62ff537fa75596ed367544b8e411d0cee7b702999712b663c3b207755

$ docker attach consul_1

次に、Consulをダウンロードしてパスに配置します。

# wget https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip
--2015-02-02 20:16:54--  https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip
Resolving dl.bintray.com... 108.168.194.92, 108.168.194.91
Connecting to dl.bintray.com|108.168.194.92|:443... connected.
HTTP request sent, awaiting response... 302 
Location: https://d29vzk4ow07wi7.cloudfront.net/a3764e06bb49dbb78e5f27c8a576ff3c17ba7d8a?response-content-disposition=attachment%3Bfilename%3D%220.4.1_linux_amd64.zip%22&Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHAqOi8vZDI5dnprNG93MDd3aTcuY2xvdWRmcm9udC5uZXQvYTM3NjRlMDZiYjQ5ZGJiNzhlNWYyN2M4YTU3NmZmM2MxN2JhN2Q4YT9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRCUyMjAuNC4xX2xpbnV4X2FtZDY0LnppcCUyMiIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTQyMjkwODI3NX0sIklwQWRkcmVzcyI6eyJBV1M6U291cmNlSXAiOiIwLjAuMC4wLzAifX19XX0_&Signature=E23oQL3RpSJ-e8vJElsE6ce5nQkgtm-6bRav1~8O5tuvPmE5X~TZmN4t3WaVbXC1KPyXbLjCRkaJ6ezC4m6MhICfeB-QTkafnBdmS4E1C66dOMBGpWXrWf0OkilMaHmNn2~kma8~O3UhbA-70avz0LeKxZGHjbdCushxz5I1tJAFlIvYxJkKY5s8JQKuv21CCzhEupLNsjuiJtYLlylKeJiY8ZD2UIZsSLFkN4R2T1ApaXIzJdqEgK1t4W7s1NMBVVN3hFqD0EHPYo6KQ2NU6C1~GlYXN5Ys1Co0ZKx54Ab9EOMmndwVchAE7f~jln7wDj1N43IIdB433xhc9~5HEw__&Key-Pair-Id=APKAIQIOJCQ5764M5VTQ [following]
--2015-02-02 20:16:55--  https://d29vzk4ow07wi7.cloudfront.net/a3764e06bb49dbb78e5f27c8a576ff3c17ba7d8a?response-content-disposition=attachment%3Bfilename%3D%220.4.1_linux_amd64.zip%22&Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHAqOi8vZDI5dnprNG93MDd3aTcuY2xvdWRmcm9udC5uZXQvYTM3NjRlMDZiYjQ5ZGJiNzhlNWYyN2M4YTU3NmZmM2MxN2JhN2Q4YT9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRCUyMjAuNC4xX2xpbnV4X2FtZDY0LnppcCUyMiIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTQyMjkwODI3NX0sIklwQWRkcmVzcyI6eyJBV1M6U291cmNlSXAiOiIwLjAuMC4wLzAifX19XX0_&Signature=E23oQL3RpSJ-e8vJElsE6ce5nQkgtm-6bRav1~8O5tuvPmE5X~TZmN4t3WaVbXC1KPyXbLjCRkaJ6ezC4m6MhICfeB-QTkafnBdmS4E1C66dOMBGpWXrWf0OkilMaHmNn2~kma8~O3UhbA-70avz0LeKxZGHjbdCushxz5I1tJAFlIvYxJkKY5s8JQKuv21CCzhEupLNsjuiJtYLlylKeJiY8ZD2UIZsSLFkN4R2T1ApaXIzJdqEgK1t4W7s1NMBVVN3hFqD0EHPYo6KQ2NU6C1~GlYXN5Ys1Co0ZKx54Ab9EOMmndwVchAE7f~jln7wDj1N43IIdB433xhc9~5HEw__&Key-Pair-Id=APKAIQIOJCQ5764M5VTQ
Resolving d29vzk4ow07wi7.cloudfront.net... 54.230.142.31, 54.230.140.33, 54.192.141.193, ...
Connecting to d29vzk4ow07wi7.cloudfront.net|54.230.142.31|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4073679 (3.9M) [application/unknown]
Saving to: “0.4.1_linux_amd64.zip”

100%[========================================================================>] 4,073,679    871K/s   in 5.1s    

2015-02-02 20:17:01 (775 KB/s) - “0.4.1_linux_amd64.zip” saved [4073679/4073679]

[root@consul_1 /]# unzip 0.4.1_linux_amd64.zip 
Archive:  0.4.1_linux_amd64.zip
  inflating: consul                 
 
# mv consul /usr/local/bin/.

では、Consulを起動させます。

# consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -bind=172.17.0.2 &
==> 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: 'consul_1'
        Datacenter: 'dc1'
            Server: true (bootstrap: true)
       Client Addr: 127.0.0.1 (HTTP: 8500, DNS: 8600, RPC: 8400)
      Cluster Addr: 172.17.0.2 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

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

    2015/02/02 20:43:55 [INFO] raft: Node at 172.17.0.2:8300 [Follower] entering Follower state
    2015/02/02 20:43:55 [INFO] serf: EventMemberJoin: consul_1 172.17.0.2
    2015/02/02 20:43:55 [INFO] serf: EventMemberJoin: consul_1.dc1 172.17.0.2
    2015/02/02 20:43:55 [INFO] consul: adding server consul_1 (Addr: 172.17.0.2:8300) (DC: dc1)
    2015/02/02 20:43:55 [INFO] consul: adding server consul_1.dc1 (Addr: 172.17.0.2:8300) (DC: dc1)
    2015/02/02 20:43:55 [ERR] agent: failed to sync remote state: No cluster leader
    2015/02/02 20:43:57 [WARN] raft: Heartbeat timeout reached, starting election
    2015/02/02 20:43:57 [INFO] raft: Node at 172.17.0.2:8300 [Candidate] entering Candidate state
    2015/02/02 20:43:57 [INFO] raft: Election won. Tally: 1
    2015/02/02 20:43:57 [INFO] raft: Node at 172.17.0.2:8300 [Leader] entering Leader state
    2015/02/02 20:43:57 [INFO] consul: cluster leadership acquired
    2015/02/02 20:43:57 [INFO] consul: New leader elected: consul_1
    2015/02/02 20:43:57 [INFO] raft: Disabling EnableSingleNode (bootstrap)

無事、起動しました。
ちゃんと追加されているか確認します。

// メンバーに追加されている
# consul members
    2015/02/02 20:47:34 [INFO] agent.rpc: Accepted client: 127.0.0.1:43570
Node      Address          Status  Type    Build  Protocol
consul_1  172.17.0.2:8301  alive   server  0.4.1  2

// API経由でもnodeの確認が出来る
#  curl localhost:8500/v1/catalog/nodes
[{"Node":"consul_1","Address":"172.17.0.2"}]

// DNSでも引ける
# dig @127.0.0.1 -p 8600 consul_1.node.consul

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

;; QUESTION SECTION:
;consul_1.node.consul.		IN	A

;; ANSWER SECTION:
consul_1.node.consul.	0	IN	A	172.17.0.2

;; Query time: 5 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Mon Feb  2 20:45:15 2015
;; MSG SIZE  rcvd: 74

問題なさそうです。