Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Consul 入門

Consul 入門

チーム内LT大会

De2cd22cd6242773153ee76de1c9ecdb?s=128

AGAWA Koji

March 29, 2016
Tweet

Transcript

  1. Consul ೖ໳

  2. ͸͡Ίʹ Axion ͷΠϯϑϥͰҎԼͷ HashiCorp ੡πʔϧΛಋೖ༧ఆ • Packer • Terraform •

    Nomad • Consul ← ࠓ೔͸͜ΕΛ঺հ
  3. What's Consul ? ػೳ • αʔϏεͷݕग़ͱ؅ཧ • Key-Value Storage ඇػೳ

    • ߴ͍Մ༻ੑΛඋ͑ͨ෼ࢄγεςϜ • ෳ਺ Datacenter Λαϙʔτ
  4. ࣅͯΔ΋ͷ • ZooKeeper • etcd

  5. Service • api, mysql, ka-a ͳͲͳΜΒ͔ͷػೳΛఏڙ͢Δ΋ͷ • αʔϏεͷϝϯόʔ͸ IP ͱϙʔτͷϦετ

    • ϔϧενΣοΫʹΑΔϝϯόʔͷࢮ׆؂ࢹ • HTTP API ͱ DNS Ͱ໰͍߹Θ͕ͤͰ͖Δ
  6. DNS Ͱ A Ϩίʔυ໰͍߹Θͤ $ 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 agent Λ DNS αʔόʹࢦఆͯ͠໰͍߹Θͤ • *.service.consul Λղܾ͢ΔͱαʔϏεͷϝϯόʔ͕ฦͬͯ͘Δ • Ϧετͷฒͼ͸ϥϯμϜ (DNS ϥ΢ϯυϩϏϯ) • ϔϧενΣοΫΛ௨ͬͨੜ͖͍ͯΔϝϯόʔؚ͚͕ͩ·ΕΔ
  7. DNS Ͱ SRV Ϩίʔυ໰͍߹Θͤ $ dig @127.0.0.1 -p 8600 web.service.consul

    SRV ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 node2.zem.jp.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 node1.zem.jp.node.dc1.consul. ;; ADDITIONAL SECTION: node2.zem.jp.node.dc1.consul. 0 IN A 128.199.137.114 node1.zem.jp.node.dc1.consul. 0 IN A 128.199.130.243 • SRV ϨίʔυͰ͋Ε͹ϙʔτ൪߸·Ͱ෼͔Δ • Ψν Docker ͰαʔϏεߏ੒͢Δͱϙʔτ͕ϥϯμϜͳͷͰ༗༻ • ͨͩ͠ར༻ଆͰ SRV ͷϨκϧό͕ඞཁ • طʹ SRV ΛಡΉ࣮૷͕ͳ͚Ε͹ HTTP API ΛಡΉ࣮૷Λॻ͍ͨ΄͏ָ͕
  8. DNS Ͱ໰͍߹Θͤ ϩʔΧϧʹ dnsmasq ΛೖΕͯ .consul υϝΠϯͷ໊લղܾΛ Consul ʹ೚ ͤΔΑ͏ʹ͢ΔͱɺConsul

    ͷଘࡏΛҙࣝͤͣʹαʔϏεͷऔಘ͕Ͱ͖Δ ! [/etc/dnsmasq.conf] strict-order [/etc/dnsmasq.conf.d/consul.conf] server=/consul/127.0.0.1#8600 [/etc/resolv.conf] nameserver 127.0.0.1 nameserver 8.8.8.8
  9. DNS ໰͍߹Θͤͷར༻γʔϯ • طଘͷϓϩάϥϜʹखΛՃ͑Δ͜ͱͳ͘αʔϏεࢀর͕Ͱ͖Δ • Ϋϥελܥϛυϧ΢ΣΞ΁ͷ઀ଓઌࢦఆ val c = new

    AerospikeClient("aerospike.service.consul", 3000) • Ka$a, Aerospike, Cassandra, ZooKeeper ͳͲ͸ੜ͖͍ͯΔϊʔ υʹ઀৮Ͱ͖Ε͹Ϋϥελϝϯόʔ͕ల։͞ΕΔͷͰɺConsul service Ͱ͋Ε͹ෳ਺ͷॳظϗετΛࢦఆ͢Δඞཁ͸ͳ͍
  10. HTTP Ͱͷ໰͍߹Θͤ $ curl http://localhost:8500/v1/catalog/service/web [{"Node":"Armons-MacBook-Air","Address":"172.20.20.11","ServiceID":"web", \ "ServiceName":"web","ServiceTags":["rails"],"ServicePort":80}] • DNS

    ͱҧͬͯશϝϯόʔ͕ฦΔΤϯυϙΠϯτ͕͋Δ • ಛʹ໨৽͍͜͠ͱ͸ͳ͍…
  11. HTTP Blocking Query • HTTP API Ͱఏڙ͞Ε͍ͯΔσʔλͷมߋΛ؂ࢹͰ͖Δ • DNS ΍

    HTTP ΛϙʔϦϯάͤͣʹޮ཰తʹϝϯόʔมߋ΁௥ਵͰ͖Δ • ͍ΘΏΔ COMET ௨৴ (Server Push) • Non-Blocking ͳ HTTP Client Ͱ࢖͏͜ͱ • finagle ͕ΫϥΠΞϯταΠυͷϩʔυόϥϯαʔΛ͍࣋ͬͯΔͨΊɺͦͷϝϯόʔΛ Consul ͷαʔϏεͱಉظ͢ΔίʔυΛॻ͍ͨ 1 1 h$ps:/ /github.com/CyberAgent/acd-axion/blob/c35087433a652e66f2ef8716c6acc244c8b815aa/client/src/main/ scala/axion/finagle/consul/ConsulResolver.scala
  12. αʔϏεͷొ࿥ • ઃఆϑΝΠϧͰ੩తʹొ࿥ • ϗετʹΠϯετʔϧ͞Ε͍ͯΔϛυϧ΢ΣΞͳͲ • HTTP API Ͱಈతʹొ࿥ •

    ಈతʹσϓϩΠ͢ΔΞϓϦέʔγϣϯͳͲ • Nomad ͸σϓϩΠͨ͠ΞϓϦΛαʔϏεొ࿥͢Δػೳ͕͋ Δ
  13. ྫ͑͹ Ka$a Λొ࿥͢Δ { "service": { "name": "kafka-broker", "port": 9200,

    "checks": [ { "id": "kakfa-broker", "tcp": "localhost:9200", "interval": "5s", "timeout": "1s" } ] } } • Ka$a ͕ಈ͍͍ͯΔϗετͰ Consul ͷઃఆϑΝΠϧʹهड़ • tcp/9200 ΛϔϧενΣοΫ
  14. Key-Value Storage $ curl -X PUT -d 'test' http://localhost:8500/v1/kv/web/key1 true

    $ curl http://localhost:8500/v1/kv/web/key1 [{"CreateIndex":97,"ModifyIndex":97,"Key":"web/key1","Flags":0,"Value":"dGVzdA=="}] • ֊૚Խ͞ΕͨΩʔͷ GET/PUT/DELETE (γϯϓϧ!) • Blocking Query Ͱ watch Ͱ͖Δ͠ɺ CAS ΋͋Δ • εέʔϥϏϦςΟ͸ແ͍ͷͰ஫ҙ • ϗετ΍ΞϓϦͷઃఆͳͲࢀরස౓ͷ௿͍σʔλͷΈ֨ೲ͢Δ • ଞ͸ઐ༻ͷ KVS (Aerospike, Cassandra ͳͲ)΁!
  15. Consul ΞʔΩςΫνϟ

  16. ͔͍͍ͬ͜ UI ΋͋ΔΑ!

  17. ·ͱΊ • Ϋϥ΢υɾίϯςφʹΑΓಈతʹͳͬͨΠϯϑϥΛ؅ཧͰ͖Δ • ੩తͳΠϯϑϥͰ͋ͬͯ΋ʮαʔϏεʯͷߟ͑ํΛ౷ҰͰ͖Δ • Consul ͸ඞཁ࠷খݶͷ΋ͷΛηϯεྑ࣮͘૷͍ͯ͠Δ • ͢΂ͯͷϓϩδΣΫτͰجຊ૷උʹͯ͠΋ྑ͍ͷͰ͸?