LoginSignup
115
99

More than 5 years have passed since last update.

Consul by HashiCorp ~サービス・ディスカバリ入門~

Last updated at Posted at 2016-08-01
1 / 43

誰?


Topics

  • HashiCorp
  • Consul 概要
  • 開発背景
  • アーキテクチャ
  • Consul でサービス登録
  • Consul Template

HashiCorp

  • 会社設立は 2012 年
    • Mitchell Hashimoto 氏と Armon Dadger
  • データセンタ管理に革命をもたらすこと
  • 設計思想は The tao of HashiCorp(Workflows, not Technologies やインフラのコード化)
  • オープンソースの基本技術を元に、オープンな開発体制(2010年から)

hashicorp.png


Consul


Consul とは

  • https://www.consul.io/
  • インフラ上のサービス設定とサービスディスカバリのためのツール

    • Consul has multiple components, but as a whole, it is a tool for discovering and configuring services in your infrastructure.
  • オープンソースとして開発・公開


Consul の機能

  • サービス・ディスカバリとオーケストレーション
  • ヘルスチェック
  • キーバリュー・ストア
  • 耐障害性

サービス・ディスカバリ

  • Consul クライアントが「api」や「mysql」のようなサービスを定義
  • 他のクライアントは Consul で特定のサービスを発見(discover)できる。
  • サービスの情報を DNS 、HTTP、API 経由で把握できる
  • Serf のオーケストレーション機能群

ヘルスチェック

  • クライアントはヘルスチェック(シェルスクリプトや Nagios プラグイン)でサービスを監視
  • サービス・ディスカバリと連動しており、監視状態が直ちに(サービス・カタログに)反映する

ただし、一般的な監視システムとは異なる

  • ノード(エージェント)が状態をサーバに伝える
  • サーバはあくまでデータを保持するだけ

キーバリュー・ストア

  • Consul の内部データを保管するための、階層的なキーバリュー・ストア
  • 動的な設定変更や、リーダーエレクション機能を備える
  • HTTP や API を通して簡単にアクセス

耐障害性

  • Raft プロトコル(分散合意アルゴリズム)を使ったデータ可用性を高める仕組み
    • リーダー選出
    • ログのレプリケーション
  • Gossip プロトコルを使った仕組み
    • イベント送信
    • 障害検出
  • 簡単に複数のデータセンタ(ネットワーク)を横断して利用可能(データセンタという概念)

consul-service.png


アーキテクチャ

  • クライアント・サーバ型の分散・高可用性システム
  • 各ノードでは Consul エージェントを実行
    • Consul は単一のバイナリ(追加のソフトウェアは不要) エージェントは Consul サーバと通信
    • Consul サーバはデータを保管・複製する場所
    • サーバ自身でリーダーを選出
    • 通常はデータ損失を避けるため3、5台のサーバ推奨

consul-arch2.png


consul-arch1.png


他のツールとの比較

  • Go 言語で書かれている
    • バイナリ1つで動作
  • 設定や管理がシンプル
  • 機能はサービス・ディスカバリに特化

Consul を使う


ダウンロード

  • Consul のサイトから、アーキテクチャごとのバイナリを取得
  • ダウンロードした ZIP を展開
  • パーミッション確認
  • パスが通った場所に置く
  • consul エージェント実行

Linux の例

$ wget -O conzul.zip https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
$ unzip consul.zip
$ ./consul version
Consul v0.6.4
Consul Protocol: 3 (Understands back to: 1)
$ nohup consul agent -server \
    -data-dir="/tmp/consul" \
    -bootstrap-expect 1 \
    -node=consul1 \
    -config-dir=/etc/consul.d &
$ ./consul members
Node  Address          Status  Type    Build  Protocol  DC
web   172.17.0.1:8301  alive   server  0.6.4  2         dc1
$ consul join <ノード> <ノード>

API
$ consul members
Node            Address               Status  Type    Build  Protocol  DC
web.zem.jp      128.199.84.221:8301   alive   server  0.5.2  2         dc1
node1.zem.jp    128.199.130.243:8301  alive   client  0.5.2  2         dc1
node2.zem.jp    128.199.137.114:8301  alive   client  0.5.2  2         dc1

サービス登録例

web.json
{
  "service": {
    "name": "web",
    "tags": [ "httpd" ],
    "port": 80,
    "check": {
      "script": "curl localhost:80 >/dev/null 2>&1",
      "interval": "10s"
    }
  },
  "key": "web",
  "handler": "/opt/consul/consul-handler.sh"
}

webui.png


HTTP
$ curl http://127.0.0.1:8500/v1/catalog/nodes
[{"Node":"web.zem.jp","Address":"128.199.84.221"}]

DNS
$ dig @127.0.0.1 -p 8600 web.zem.jp.node.consul

;; QUESTION SECTION:
;web.zem.jp.node.consul.    IN      A

;; ANSWER SECTION:
web.zem.jp.node.consul. 0   IN      A       128.199.84.221
  • DNS の情報(サービス・ディスカバリ)はヘルスチェックと連動

DNS
$ dig @127.0.0.1 -p 8600 web.service.consul

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

;; ANSWER SECTION:
web.service.consul.     0       IN      A       128.199.137.114
web.service.consul.     0       IN      A       128.199.130.243

DNS
$ dig @127.0.0.1 -p 8600 web.service.consul

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

;; ANSWER SECTION:
web.service.consul.     0       IN      A       128.199.137.114
web.service.consul.     0       IN      A       128.199.130.243
  • でもこれ、見てるだけ・・・?

consul watch

  • イベントの status 変化をトリガとし、任意のコマンド実行
$ consul watch –http-addr=127.0.0.1:8500 \
    -type=service \
    –service=mysql \
    /opt/action.sh
  • タイプ service
  • サービス名 mysql
  • イベント発生時に /opt/action.sh を実行

consul watch

  • イベントの status 変化をトリガとし、任意のコマンド実行
$ consul watch –http-addr=127.0.0.1:8500 \
    -type=service \
    –service=mysql \
    /opt/action.sh
  • タイプ service
  • サービス名 mysql
  • イベント発生時に /opt/action.sh を実行
  • 面倒そう・・・

tmpl1.png


tmpl2.png


tmpl3.png


Consul Template

  • https://github.com/hashicorp/consul-template
  • consul のイベントを監視するデーモン
  • 状況変化が発生すると
    • テンプレートを読み込む
    • サービス・ディスカバリの状態をファイルに反映
    • 任意コマンドの実行(オプション)

テンプレートの例

host-node.tmpl
# consul nodes{{range nodes}}
{{.Address}}    {{.Node}}{{end}}

テンプレートの例

host-node.tmpl
# consul nodes{{range nodes}}
{{.Address}}    {{.Node}}{{end}}
$ consul-template -consul <Consulサーバ>:<HTTP port> \
    -template "テンプレートのファイル名:出力先:コマンド(オプション)" \
    -template "同上" (オプション) \
    -dry (オプション)

テンプレートの例

host-node.tmpl
# consul nodes{{range nodes}}
{{.Address}}    {{.Node}}{{end}}
$ consul-template -consul <Consulサーバ>:<HTTP port> \
    -template "テンプレートのファイル名:出力先:コマンド(オプション)" \
    -template "同上" (オプション) \
    -dry (オプション)
/etc/hosts
# consul nodes
192.168.39.3    sion.pocketstudio.net
192.168.39.11    web1
192.168.39.12    web2

nginx に活用する例

upstream frontend { {{range service "web"}}
    server {{.Address}}:{{.Port}};{{end}}
}

server {
    listen 80 default_server;
    location / {
        proxy_pass http://frontend;
    }
}

nginx に活用する例

upstream frontend { {{range service "web"}}
    server {{.Address}}:{{.Port}};{{end}}
}

server {
    listen 80 default_server;
    location / {
        proxy_pass http://frontend;
    }
}

consul-template を使うには

$ sudo yum -y install git golang
$ export GOPATH=/usr/local/src/go
$ git clone https://github.com/hashicorp/consul-template.git
$ cd consul-templae
$ make
$ sudo cp ./bin/consul-template /usr/bin/
$ consul-template --version
consul-template v0.7.1.dev

…と思っていたら、今はバイナリがあるので簡単に使えますよ!
https://releases.hashicorp.com/consul-template/0.15.0/
(会場からのご指摘ありがとうございました!)


ふりかえり


ふりかえり

  • Consul とは
    • インフラ上のサービス設定とサービスディスカバリのためのツール
  • 機能
    • サービス・ディスカバリとオーケストレーション
    • ヘルスチェック
    • キーバリュー・ストア
    • 耐障害性
  • Consul Template で定型処理の簡易化

何か気になるところはありますか?


references

115
99
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
115
99