MicroServicesãã³ã³ãããªã¼ã±ã¹ãã¬ã¼ã·ã§ã³ãªã©ãè±çãã§ãããããã«æ¬æ ¼çã«è¸ã¿åºãã«ã¯ã¾ã è¥å¹²ã®æéãå¿ è¦ãªããã«æãã¾ãã ã¨ã¯ããããµã¼ãã®åçãªæ§æå¤æ´ã¸ã®å¯¾å¿ã¯ãããã§ãåºã¦ããã®ã§ããµã¼ãã¹ãã£ã¹ã«ããªã¨ããµã¼ãã®ã¹ãã¼ã¿ã¹ãããªã¬ã«ããã¢ã¯ã·ã§ã³ã«ã¯èå³ãããã¾ããã
ããããããã§ãã¾ã㯠Consul by HashiCorp ã®æ©è½ã試ãã¦ã¿ããã¨æãã¾ãã
- ã¾ã㯠Consul ã¨ã¯ä½ã
- ã¯ã©ã¹ã¿ãæ§æãã
- ãã¼ãã®æ»ã«å¯¾ããèæ§
- Service Discovery
- K/V Store
- æå¾ã«
ã¾ã㯠Consul ã¨ã¯ä½ã
Consul ã¯ã以ä¸ã®æ©è½ãæã£ããã¼ã«ã§ãã
- ãµã¼ãã¹ãã£ã¹ã«ããª
- ãã«ã¹ãã§ãã¯
- K/V ã¹ãã¢
ããã¼ã«ãã¨æ¸ããããã«ãå®è£ ã¨ãã¦åãè¾¼ããã©ã¤ãã©ãªãã§ã¯ãªããç¬ç«ãã¦åä½ããããã¤ããªãã¨ãã¦æä¾ããã¾ãã
ã¢ã¼ããã¯ãã£
ã¢ã¼ããã¯ãã£ã¨ãã¦ã¯ã¯ã©ã¤ã¢ã³ãã»ãµã¼ãæ§æã«ãªã£ã¦ãã¦ãã¯ã©ã¤ã¢ã³ãããµã¼ãã¨ãã«è¤æ°å°ããæ§æãããã¨ã§å¯ç¨æ§ãä¸ããèãã«ãªã£ã¦ãã¾ãã ã¯ã©ã¤ã¢ã³ãããµã¼ããã¾ã¨ãã¦ãConsul Agent ã¨å¼ã°ãã¾ãã
åºæ¬çã«ã¯ããã¼ã¿ã»ã³ã¿(ããã§ã®ããã¼ã¿ã»ã³ã¿ãã¯ãä¸è¬çãªæå³ãª DC ã§ã¯ãªããä½é 延ãé«å¸¯åã®ãããã¯ã¼ã¯ã§ç¹ãããç°å¢ã¨ãã¦å®ç¾©ããã¾ã) å ã§ã¯ã©ã¤ã¢ã³ãã¨ãµã¼ããã¯ã©ã¹ã¿ã¨ãã¦åä½ãã ãã®ãã¼ã¿ã»ã³ã¿å ã«éãã¦ãã¡ã³ãã·ããæ å ±ããµã¼ãã¹ã¹ãã¼ã¿ã¹ã¨ãã£ãæ å ±å ±æãè¡ãã¾ãã
ä¸æ¹ããã¼ã¿ã»ã³ã¿éã¯åã ã®ãã¼ã¿ãµã¼ãéã®äºãã®ãµã¼ãå士ããäºãã®æ å ±ã®å ±æãè¡ãã¨ããå¤å±¤æ§é ã«ãªã£ã¦ãã¾ãã
å ¬å¼ã®ã¬ã¤ãã§ããã¨ã以ä¸ã®å³ãåããããããã¨æãã¾ãã
ä¸è¨ã§ LAN/WAN GOSSIP ã¨æ¸ããã¦ããã¨ãã㯠SWIM ããã¼ã¹ã¨ããéä¿¡ãããµã¼ãéãããã¯ã¯ã©ã¤ã¢ã³ãéã§è¡ããã GOSSIP ã«ã¤ãã¦ã¯ããããã以ä¸ã®ã¨ã³ããªãåç §ãã ããã
åã ã® Consul Agent ã¯ãHTTP API 㨠DNS Interface ãæã£ã¦ãã¦ã
- ãã¼ãã®æ å ±
- ãµã¼ãã¹ã®æ å ±
ãªãããããããã®ã¤ã³ã¿ãã§ã¼ã¹ãå©ç¨ãããã¨ã§åç §ã§ãã¾ãã DNS Interface ãæã£ã¦ããã£ã¦ãããã¨ã¯ãã¢ããªã±ã¼ã·ã§ã³ããã¯ãã¹ãåãã¡ãã£ã¨å¤ããã ãã§ããã㨠transparent ã« Service Discovery ãå©ç¨ã§ããã£ã¦ãã¨ã§ããã
ã¯ã©ã¹ã¿ãæ§æãã
ããããã®å°æ°ã® consul agent ã§ã¯ã©ã¹ã¿ãå½¢æãããã£ãã®ã§ãDocker Composeã使ããã¨ã«ãã¾ããã
Dockerfile
consul ã«é¢ãã¦ã¯ãå ¬å¼ã alpine ãã¼ã¹ã®ã¤ã¡ã¼ã¸ãæä¾ãã¦ããã¦ãã¾ãã
ããã¤ãã³ãã³ãã使ãããã£ãã®ã§ã以ä¸ã®ãã㪠Dockerfile ãç¨æãã¾ããã
ä¸è¨ã® consul ã® image ããã¼ã¹ã«ãã¦ãapache2 ãã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã
httpd
ã¨ã³ãã³ããå©ãã° apache ãèµ·åããããã«ãªã£ã¦ãã¾ãã
FROM consul:1.1.0 ENV HTTPD_DIR /usr/local/apache2 ENV HTTPD_PID_DIR /run/apache2 # We install apache2 with some utility commands, but it is to be run manually. RUN set -ex \ && apk update \ && apk add --no-cache --virtual .deps \ curl \ bind-tools \ apache2 \ && adduser -D -S -G www-data www-data \ && mkdir -p "$HTTPD_DIR" "$HTTPD_PID_DIR" \ && chown www-data:www-data "$HTTPD_DIR" "$HTTPD_PID_DIR"
docker-compose.yaml
docker-compose.yaml ã«ã¯ããµã¼ãã¨ã¯ã©ã¤ã¢ã³ãããããããµã¼ãã¹ã¨ãã¦ç¨æãã¾ãã
ããã«ããããµã¼ããã¯ã©ã¤ã¢ã³ãã¨ããdocker-compose
ã® scale
ãªãã·ã§ã³ã§ä»»æã®å°æ°ãæå®ã§ããããã«ãªãã¾ãã
version: '3' services: server: &agent build: context: . dockerfile: Dockerfile image: kiririmode/consul:latest command: "agent -server -ui -bootstrap-expect ${SERVER_NUM} -retry-join consul_server_1 -client 0.0.0.0 -recursor=8.8.8.8" ports: - "8500-8549:8500" - "10000-10049:80" client: <<: *agent command: "agent -ui -retry-join consul_server_1 -client 0.0.0.0 -recursor=8.8.8.8" ports: - "8550-8599:8500" - "10050-10099:80"
ã¯ã©ã¹ã¿ãããã
ãµã¼ãå°æ°ã¯ãRaft ã使ç¨ãã¦ããé¢ä¿ã¨ãããã©ã¼ãã³ã¹ã®è¦å°ããã3 å°ã 5 å°ãæ¨å¥¨ããã¦ãã¾ãã ããã§ã¯ã¾ãã1 ã¤ã®ãã¼ã¿ã»ã³ã¿å ã«ãµã¼ã 3 å°ãã¯ã©ã¤ã¢ã³ã 5 å°ãããªãã¯ã©ã¹ã¿ãæ§ç¯ãã¦ã¿ã¾ãã
$ export SERVER_NUM=3 $ docker-compose -p consul up --scale server=3 --scale client=5
ã¾ãã¯ã¡ã³ãã¼ã確èªãã¾ãã 以ä¸ã®ããã«ãæå³ããã¨ããããµã¼ã 3 å°ãã¯ã©ã¤ã¢ã³ã 5 å°ã®ã¯ã©ã¹ã¿ãæ§æããã¦ãããã¨ãåããã¾ãã
$ docker-compose -p consul exec --index=1 server consul members Node Address Status Type Build Protocol DC Segment 37087184553e 172.18.0.4:8301 alive server 1.1.0 2 dc1 <all> 6a56ea9fa974 172.18.0.3:8301 alive server 1.1.0 2 dc1 <all> df2dc341f480 172.18.0.2:8301 alive server 1.1.0 2 dc1 <all> 4804c51b4d9f 172.18.0.9:8301 alive client 1.1.0 2 dc1 <default> 712f685e59ca 172.18.0.6:8301 alive client 1.1.0 2 dc1 <default> 9c182e4347bc 172.18.0.8:8301 alive client 1.1.0 2 dc1 <default> d455dbe41191 172.18.0.5:8301 alive client 1.1.0 2 dc1 <default> fbd8cbc2cea2 172.18.0.7:8301 alive client 1.1.0 2 dc1 <default>
Raft ã®ã¹ãã¼ã¿ã¹ã¯ã©ãã§ãããããRaft ã®ã¹ãã¼ã¿ã¹ã確èªããããã«ã¯ãconsul operator raft <sub-command>
ã使ãã¾ãã
以ä¸ã®ããã«ãNode 0a35
ããªã¼ãã¨ãã¦é¸åºããã¦ãããã¨ããããã¾ãã
$ docker-compose -p consul exec --index=1 server consul operator raft list-peers Node ID Address State Voter RaftProtocol 6a56ea9fa974 0a35ddd5-f851-e1dc-804e-1aad15a165b8 172.18.0.3:8300 leader true 3 df2dc341f480 bc14f2e3-988f-5177-973d-78ab02ada1ed 172.18.0.2:8300 follower true 3 37087184553e e7636e01-6fa2-0610-a5d9-8b7da9a107b2 172.18.0.4:8300 follower true 3
ã§ã¯ãDNS InterfaceãHTTP API ããããã§ã¡ã³ãã®ç¶æ ãè¦ã¦ã¿ã¾ãããã
DNS Interface
consul 㯠DNS ãµã¼ãããã¤ããªã«åãè¾¼ã¾ãã¦ãããããã©ã«ãã§ã¯ 8600 çªãã¼ãã§èµ·åãã¾ãã
åå空é㯠consul
ã§çµããããã«ãªã£ã¦ãã¾ãã
ä¾ãã°ãconsul ãµã¼ããåå解決ããå ´åã¯ãconsul.service.consul
ã«ååããè¡ãã¾ãã
çµæã¨ãã¦ã¯ã以ä¸ã®ããã« 3 å°ã® server ãåå解決ã§ãã¦ãã¾ãã
$ docker-compose -p consul exec --index=1 server \ dig +noall +answer @127.0.0.1 -p 8600 consul.service.consul consul.service.consul. 0 IN A 172.18.0.3 consul.service.consul. 0 IN A 172.18.0.2 consul.service.consul. 0 IN A 172.18.0.4
HTTP API
HTTP ãµã¼ãã«ã¤ãã¦ããã¤ããªã«åãè¾¼ã¾ãã¦ãããåç §ç³»ã»æ´æ°ç³»ã¨ãã£ã種ã ã®æ©è½ãå©ç¨ã§ãã¾ã (ããããæå³ã§ãDNS Interface ããé«æ©è½ã§ã)ã
以ä¸ã¯ãã¡ã³ãã®æ å ±ãåç §ãã API ã§ããããã®ããã« 8 å°ã®æ å ±ãè¿ã£ã¦ãããã¨ãåããã¾ãã
$ docker-compose -p consul exec --index=1 server \ curl http://127.0.0.1:8500/v1/catalog/nodes \ | jq -r '.[].Address' 172.18.0.4 172.18.0.9 172.18.0.3 172.18.0.6 172.18.0.8 172.18.0.5 172.18.0.2 172.18.0.7
ãã¼ãã®æ»ã«å¯¾ããèæ§
consul ã«ããã¦ããã¼ãã®ã¹ãã¼ã¿ã¹ã¯é常ç¶æ
ã®ä»ã«ãleft
㨠failed
ãããã¾ãã
è¡åããã¯ã©ã¹ã¿ãé¢è±ããå ´å㯠left
ããããªãã®æ
éãçºçããå ´å㯠failed
ã«é·ç§»ãã¾ãã
ããã§ã¯ã3 ã¤ç®ã®ãµã¼ãã« SIGKILL ãçºè¡ãã¦ã¿ã¾ãã
$ docker kill -s KILL consul_server_3
consul_server_3
ããããã¨ãã¯ã©ã¤ã¢ã³ã 5 ã¯ãã¯ãå¨å²ã®ãã¼ãããã® ping ã«å¿çããªããªããSWIM ã®ä¸ã§
failed
ã¨å¤å®ããã¾ããå®éãconsul members
ã®çµæã¯ä»¥ä¸ã®ã¨ããã§ã1 å°ã®ã¯ã©ã¤ã¢ã³ãã failed
ã®
ã¹ãã¼ã¿ã¹ã«ãªã£ã¦ãããã¨ããããã¾ãã
$ docker-compose -p consul exec --index=1 server \ consul members Node Address Status Type Build Protocol DC Segment 37087184553e 172.18.0.4:8301 alive server 1.1.0 2 dc1 <all> 6a56ea9fa974 172.18.0.3:8301 alive server 1.1.0 2 dc1 <all> df2dc341f480 172.18.0.2:8301 failed server 1.1.0 2 dc1 <all> 4804c51b4d9f 172.18.0.9:8301 alive client 1.1.0 2 dc1 <default> 712f685e59ca 172.18.0.6:8301 alive client 1.1.0 2 dc1 <default> 9c182e4347bc 172.18.0.8:8301 alive client 1.1.0 2 dc1 <default> d455dbe41191 172.18.0.5:8301 alive client 1.1.0 2 dc1 <default> fbd8cbc2cea2 172.18.0.7:8301 alive client 1.1.0 2 dc1 <default>
DNS Interface ã§åãåããã¦ã¿ã¦ãã1 å°æ¸ã£ã¦ãããã¨ã確èªã§ãã¾ãã
$ docker-compose -p consul exec --index=1 server \ dig +noall +answer @127.0.0.1 -p 8600 consul.service.consul consul.service.consul. 0 IN A 172.18.0.3 consul.service.consul. 0 IN A 172.18.0.4
Service Discovery
ãªãã¨ãã£ã¦ããconsul ã®ã¦ã¼ã¹ã±ã¼ã¹ã® 1 ã¤ã¯ Service Discovery ã§ãã
ä¸è¨ã® consul.service.consul
ã¸ã® DNS ã¯ã¨ãªããã® 1 ã¤ã§ã¯ãã£ãã®ã§ãããããã§ã¯åçã« Service ã®è¨å®ã追å ãã¦ã¿ã¾ãã
Docker ãã¹ãã«ã以ä¸ã®ãã㪠web.json
ãç¨æãã¾ãã
ããã¯ã"apache" ã¨ãããµã¼ãã¹ã«é¢ããè¨å®ã¨ãã¦å®ç¾©ãã¦ãã¦ã10 ç§æ¯ã« http://localhost
ã« GET ã§ãã«ã¹ãã§ãã¯ãå®æ½ãã¾ãã
{ "Name": "apache", "Port": 80, "Check": { "Name": "http on port 80", "HTTP": "http://localhost", "Method": "GET", "Interval": "10s", "Timeout": "1s" } }
ãããä»»æã® consul ã¨ã¼ã¸ã§ã³ãã« HTTP 㧠PUT ãã¾ããããã§ã¯ã1 çªç®ã®ã¯ã©ã¤ã¢ã³ãã« PUT ãã¦ã¿ã¾ãããã
# 1 çªç®ã®ã¯ã©ã¤ã¢ã³ãã® 8500 çªãã¼ãã«ãããã³ã°ããã¦ãããã¼ã $ docker-compose -p consul port --index=1 client 8500 0.0.0.0:8571 # Docker ãã¹ããããµã¼ãã¹è¨å®ãæå ¥ $ curl -XPUT [email protected] http://localhost:8571/v1/agent/service/register
ãã®æ®µéã§ã¯ã1 çªç®ã®ã¯ã©ã¤ã¢ã³ã㧠httpd ãç«ã¡ä¸ãã£ã¦ããªãã®ã§ã"apache" ã¨ãããµã¼ãã¹ããã£ã¹ã«ããªãããã¨ãã¦ã åå¨ãã¾ããã
# 1 çªç®ã®ãµã¼ãä¸ã§ DNS ã§ã® Service Discovery $ docker-compose -p consul exec --index=1 server \ dig +noall +answer @127.0.0.1 -p 8600 apache.service.consul # ä½ãè¿å´ããã¾ãã
ã§ã¯ãhttpd ãç«ã¡ä¸ãã¦ã¿ãå¾ã®çµæã以ä¸ã«ãªãã¾ãã ãã®ã¨ããããã¡ã㨠apache ãµã¼ãã¹ã Discovery ã§ãããã¨ããããã¾ãã
# 1 çªç®ã®ã¯ã©ã¤ã¢ã³ãä¸ã§ httpd ãç«ã¡ä¸ãã $ docker-compose -p consul exec --index=1 client httpd # 1 çªç®ã®ãµã¼ãä¸ã§ httpd ã® Service Discovery $ docker-compose -p consul exec --index=1 server \ dig +noall +answer @127.0.0.1 -p 8600 apache.service.consul apache.service.consul. 0 IN A 172.18.0.8
ãã¡ããããã®è¨å®ãä»ã®ãã¼ãã«ãæå ¥ããã°ãService Discovery ã®çµæã®æ°ãå¢ãã¦ããã¾ãã
ä»å㯠http ã§ãã«ã¹ãã§ãã¯ãè¡ãã¾ããããä»ã«ã TCP ããä»»æã®ã¹ã¯ãªãããç¨ãã¦ã®ãã«ã¹ãã§ãã¯ãå®ç¾ã§ãã¾ãã
K/V Store
consul 㯠Key Value Store ã®æ©è½ãæã£ã¦ãã¾ãã ããã§ã¯ã3 å°ç®ã®ã¯ã©ã¤ã¢ã³ã㧠Key-Value ãæå ¥ããããã 2 å°ç®ã®ãµã¼ãã§ç¢ºèªãã¦ã¿ã¾ãã
ã¾ãã¯æå
¥ãkey 㯠kiririmode
ãvalue 㯠dislikes onion
ã¨ãã¦ã¾ãã
$ curl -XPUT -d "dislikes onion" http://localhost:$(docker-compose -p consul port --index=3 client 8500 | cut -d: -f2)/v1/kv/kiririmode true
ããããµã¼ãã§ç¢ºèªãã¦ã¿ã¾ãã
$ curl http://localhost:$(docker-compose -p consul port --index=2 server 8500 | cut -d: -f2)/v1/kv/kiririmode\?pretty [ { "LockIndex": 0, "Key": "kiririmode", "Flags": 0, "Value": "ZGlzbGlrZXMgb25pb24=", "CreateIndex": 153, "ModifyIndex": 153 } ]
ãã®ããã«ãvalue 㯠base64 ã§ã¨ã³ã³ã¼ãããã¦ãã¾ãããã³ã¼ãããã¨ããããã«å¥ãµã¼ãã§åãå¤ãåå¾ã§ãããã¨ããããã¾ãã
$ curl -s http://localhost:$(docker-compose -p consul port --index=2 server 8500 | cut -d: -f2)/v1/kv/kiririmode \ | jq -r '.[0].Value' \ | base64 -d dislikes onion
æå¾ã«
ä»å㯠ãã¼ã«ã«ãã¹ãä¸ã«è¤æ°åã® Docker Container ã¨ãã¦æµ®ãã¹ãå½¢ã§ã¯ã©ã¹ã¿ãæ§æãã¾ããã ããã¯åã«ãã®æ¹ãæ軽ã«æ¤è¨¼ã§ããããã§ãã
consul ã Container ããå©ç¨ããå ´åã
- Docker Host ã«å¯¾ã㦠1 ã¤ã® Container ã¨ãã
- ã¾ããbridge ã§ã¯ãªã㦠Host Networking (
--net host
) ã使ç¨ãã
ã¨ããã®ãæ¬æ¥ãªã®ã§ã注æãã ããã
詳細ã¯ãconsul ããåç §ãã ããã