検証用に 1 台のサーバで consul を複数起動する
consul cluster の検証用にサーバを何台も立ち上げるのは面倒ですよね?
ということで、1台でなんとかできないかなと思い試してみました。
data_dir 作成
server 3 台、node 1 台を立ちあげられるように、4 つディレクトリを作成します。
$ mkdir -p /var/lib/consul/{1,2,3,4}
config-file 作成
consul01 から順に起動していく設定です。
ポイントは、1 台ずつ port をずらしているところです。
$ cat > consul01.conf
{
"datacenter": "dc01",
"data_dir": "/var/lib/consul/1",
"log_level": "INFO",
"node_name": "consul01",
"start_join": [],
"server": true,
"bootstrap_expect": 3
}
$ cat > consul02.conf
{
"datacenter": "dc01",
"data_dir": "/var/lib/consul/2",
"log_level": "INFO",
"ports": {
"dns": 8610,
"http": 8510,
"rpc": 8410,
"server": 8310,
"serf_lan": 8311,
"serf_wan": 8312
},
"node_name": "consul02",
"start_join": ["127.0.0.1:8301"],
"server": true
}
$ cat > consul03.conf
{
"datacenter": "dc01",
"data_dir": "/var/lib/consul/3",
"log_level": "INFO",
"ports": {
"dns": 8620,
"http": 8520,
"rpc": 8420,
"server": 8320,
"serf_lan": 8321,
"serf_wan": 8322
},
"node_name": "consul03",
"start_join": [
"127.0.0.1:8301",
"12y.0.0.1:8311"
],
"server": true
}
$ cat > consul04.conf
{
"datacenter": "dc01",
"data_dir": "/var/lib/consul/4",
"log_level": "INFO",
"ports": {
"dns": 8630,
"http": 8530,
"rpc": 8430,
"server": 8330,
"serf_lan": 8331,
"serf_wan": 8332
},
"node_name": "consul04",
"start_join": [
"127.0.0.1:8301",
"127.0.0.1:8311",
"127.0.0.1:8321"
]
}
consul 起動
実際に起動してきます。
先述の通り、01 から順に起動していきます。
実験中に間をおかずに次々と起動していったら、
leader がいつまでたっても決まらないことがありましたので、
数秒ずつ間をおいて起動するのが良さそうです。
$ consul agent -config-file consul01.conf
$ consul agent -config-file consul02.conf
$ consul agent -config-file consul03.conf
$ consul agent -config-file consul04.conf
全部起動できたら、consul members を実行してみましょう。
$ consul members Node Address Status Type Build Protocol DC consul01 10.0.2.15:8301 alive server 0.5.2 2 dc01 consul02 10.0.2.15:8311 alive server 0.5.2 2 dc01 consul03 10.0.2.15:8321 alive server 0.5.2 2 dc01 consul04 10.0.2.15:8331 alive client 0.5.2 2 dc01
consul cluster を組めていることがわかります。
実験のために consul01 を落とした場合に発生するのですが、
consul01 以外は、標準の port を使っていないので、
consul members を実行するときなどに port 指定する必要があるので注意してください。
具体的には、以下のようになります。
$ consul members Error connecting to Consul agent: dial tcp 127.0.0.1:8400: connection refused $ consul members -rpc-addr=127.0.0.1:8410 Node Address Status Type Build Protocol DC consul02 10.0.2.15:8311 alive server 0.5.2 2 dc01 consul01 10.0.2.15:8301 left server 0.5.2 2 dc01 consul03 10.0.2.15:8321 alive server 0.5.2 2 dc01 consul04 10.0.2.15:8331 alive client 0.5.2 2 dc01