初めてのconsul


## 参考URL
http://qiita.com/zembutsu/items/3efb7ebc1d8dba521d3c
http://pocketstudio.jp/log3/2014/05/01/consul_with_dnsmasq_name_resolution/
http://pocketstudio.jp/log3/2014/04/18/what_is_consul/

## 構成

- consul01 (server)
	- 192.168.50.101
- consul02 (client)
	- 192.168.50.102
- consul03 (client)
	- 192.168.50.103


## Install

```
wget https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip
unzip 0.4.1_linux_amd64.zip
mv consul /usr/local/sbin/
consul -v
Consul v0.4.1
Consul Protocol: 2 (Understands back to: 1)
```

### web-ui(optional)

```
wget https://dl.bintray.com/mitchellh/consul/0.4.1_web_ui.zip
unzip 0.4.1_web_ui.zip
mkdir -p /opt/consul
mv dist/ /opt/consul/webui
```

## 起動

```
# 1台目[server]

consul agent -server -bootstrap -client=192.168.50.101 -dc=local -node=consul01 -data-dir=/tmp/consul -bind=192.168.50.101 [-ui-dir=/opt/consul/webui/]

# 2台目以降
consul agent -dc=local -node=consul02 -data-dir=/tmp/consul -bind=192.168.50.102 -join=192.168.50.101
```

こうなる

```
consul members -rpc-addr=192.168.50.101:8400
Node      Address              Status  Type    Build  Protocol
consul01  192.168.50.101:8301  alive   server  0.4.1  2
consul02  192.168.50.102:8301  alive   client  0.4.1  2
consul03  192.168.50.103:8301  alive   client  0.4.1  2
```

## catalog

### 登録

```
curl -X PUT -d '{"Node":"master-node", "Address":"192.168.50.101"}' http://192.168.50.101:8500/v1/catalog/register
```

### 参照

```
curl http://192.168.50.101:8500/v1/catalog/nodes | jq .
[
  {
    "Address": "192.168.50.101",
    "Node": "consul01"
  },
  {
    "Address": "192.168.50.102",
    "Node": "consul02"
  },
  {
    "Address": "192.168.50.103",
    "Node": "consul03"
  },
  {
    "Address": "192.168.50.101",
    "Node": "master-node"
  }
]
```

### DNSベースの参照

```
dig @192.168.50.101 -p 8600 master-node.node.local.consul

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

;; QUESTION SECTION:
;master-node.node.local.consul.	IN	A

;; ANSWER SECTION:
master-node.node.local.consul. 0 IN	A	192.168.50.101

;; Query time: 1 msec
;; SERVER: 192.168.50.101#8600(192.168.50.101)
;; WHEN: Tue Dec  9 23:33:08 2014
;; MSG SIZE  rcvd: 92
```

### 削除

```
curl -X PUT -d '{"Node":"master-node"}' http://192.168.50.101:8500/v1/catalog/deregister
```

## dnsmasqで名前解決できるようにする

### Install

```
yum install dnsmasq
```

### 設定

こういうことなので

``` /etc/dnsmasq.conf
# Add other name servers here, with domain specs if they are for
# non-public domains.
#server=/localnet/192.168.0.1

# Example of routing PTR queries to nameservers: this will send all
# address->name queries for 192.168.3/24 to nameserver 10.1.2.3
#server=/3.168.192.in-addr.arpa/10.1.2.3
```

こう設定する

``` /etc/dnsmasq.conf
server=/consul/192.168.50.101#8600
```

さらに`/etc/resolv.conf`の順序を強制するために下記設定もする

``` /etc/dnsmasq.conf
strict-order
```

`/etc/resolv.conf`でdnsmasq使うように

``` /etc/resolv.conf
nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4
```

起動

```
chkconfig dnsmasq on
service dnsmasq start
```

### 結果

名前解決できた

```
dig master-node.node.local.consul

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> master-node.node.local.consul
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61303
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;master-node.node.local.consul.	IN	A

;; ANSWER SECTION:
master-node.node.local.consul. 0 IN	A	192.168.50.101

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Dec 10 00:34:42 2014
;; MSG SIZE  rcvd: 92
```

### TTLも設定してみる

``` /etc/consul.d/dns_config.json
{
	"dns_config": {
		"node_ttl": "5s",
		"allow_stale": false ,
		"max_stale": "5s"
	}
}
```

```
consul agent -server -bootstrap -client=192.168.50.101 -dc=local -node=consul01 -data-dir=/tmp/consul -bind=192.168.50.101 -ui-dir=/opt/consul/webui/ -config-dir=/etc/consul.d/
```

## 実験

- こういうことしても低負荷で1msecで応答し続けた。

```
while true ; do dig @192.168.50.101 -p 8600 master-node.node.local.consul;done
```

TTL0でもいけるが、5秒にするだけでconsulの負荷がほぼなくなり、ほぼbash(ループ処理)の負荷。

- 同じkeyで上書き登録できた

```
curl -X PUT -d '{"Node":"master-node", "Address":"192.168.50.101"}' http://192.168.50.101:8500/v1/catalog/register
true

curl http://192.168.50.101:8500/v1/catalog/nodes | jq .[3]
{
  "Address": "192.168.50.101",
  "Node": "master-node"
}

curl -X PUT -d '{"Node":"master-node", "Address":"192.168.50.102"}' http://192.168.50.101:8500/v1/catalog/register
true

curl http://192.168.50.101:8500/v1/catalog/nodes | jq .[3]
{
  "Address": "192.168.50.102",
  "Node": "master-node"
}
```