0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Consul のタグ書き換えでハマったお話

Posted at

初投稿!

株式会社ミラティブでインフラエンジニアを担当している@Mirrativ_MasayaShimizuです。
はじめての投稿なもので、お見苦しい文があるかもしれませんがご了承ください。

今回はConsulでServiceTagsを設定した際に、API経由でのタグ書き換えに失敗してハマったお話をしたいと思います。

何にハマったの?

ConsulのAPI経由でServiceTagsの書き換えを行った際に、実行結果としてtrueが返ってくるのにも関わらず、
実際にはServiceTagsが書き換わらない謎現象に遭遇しました。

たとえば↓こんな感じで Service が定義されている場合・・・・

$ curl -s http://127.0.0.1:8500/v1/catalog/service/db | jq .[0]
{
  "ModifyIndex": yyyyyy,
  "CreateIndex": zzzzzz,
  "ServiceEnableTagOverride": true,
  "ServicePort": 3306,
  "ServiceMeta": {},
  "ServiceAddress": "",
  "ServiceTags": [
    "master" <=現在はこれ!!
  ],
  "ID": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "Node": "test-node01",
  "Address": "10.0.0.5",
  "Datacenter": "dc01",
  "TaggedAddresses": {
    "wan": "10.0.0.5",
    "lan": "10.0.0.5"
  },
  "NodeMeta": {
    "consul-network-segment": ""
  },
  "ServiceID": "db",
  "ServiceName": "db"
}

下記リクエストを送ると、実行結果としてtrueが返ってくるのにも関わらず、ServiceTagsの書き換えに失敗します(汗

$ curl -X PUT -d '{"Node":"test-node01", "Address":"10.0.0.5", "ServiceTags": ["slave"] }' http://127.0.0.1:8500/v1/catalog/register
true% <=成功してんじゃん

{
  "ModifyIndex": yyyyyy,
  "CreateIndex": zzzzzz,
  "ServiceEnableTagOverride": true,
  "ServicePort": 3306,
  "ServiceMeta": {},
  "ServiceAddress": "",
  "ServiceTags": [
    "master" <=あれ?書き換わってない・・・・
  ],
  "ID": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "Node": "test-node01",
  "Address": "10.0.0.5",
  "Datacenter": "dc01",
  "TaggedAddresses": {
    "wan": "10.0.0.5",
    "lan": "10.0.0.5"
  },
  "NodeMeta": {
    "consul-network-segment": ""
  },
  "ServiceID": "db",
  "ServiceName": "db"
}

じゃあどうすりゃいいの?

下記のようにServiceの詳細を定義してリクエストを送ると成功します。

$ curl -X PUT -d '{"Node":"test-node01", "Address":"10.0.0.5", "Service": {"ID":"db", "Service":"db", "Tags": ["slave"]} }' http://127.0.0.1:8500/v1/catalog/register
true%

$ curl -s http://127.0.0.1:8500/v1/catalog/service/db | jq .[0]
{
  "ModifyIndex": yyyyyy,
  "CreateIndex": zzzzzz,
  "ServiceEnableTagOverride": true,
  "ServicePort": 3306,
  "ServiceMeta": {},
  "ServiceAddress": "",
  "ServiceTags": [
    "slave" <=書き換わった!!
  ],
  "ID": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "Node": "test-node01",
  "Address": "10.0.0.5",
  "Datacenter": "dc01",
  "TaggedAddresses": {
    "wan": "10.0.0.5",
    "lan": "10.0.0.5"
  },
  "NodeMeta": {
    "consul-network-segment": ""
  },
  "ServiceID": "db",
  "ServiceName": "db"
}

他にもハマった点

Serviceの定義に含まれる「ServiceEnableTagOverride」のデフォルトは「false」になっています。
この設定を「true」に変更しないと、Consul API経由でServiceTagsの書き換えを行った際に、
書き換え前のTagに戻されてしまうのでご注意ください。

0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?