service discovery in a microservice architecture using consul
TRANSCRIPT
![Page 1: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/1.jpg)
Service Discovery in a Architecture
using Microservice
Consul
![Page 2: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/2.jpg)
Who Am ICurrently doing Devops, Scala stu�
At Equeris, lean startup within EquensDocker, Consul, Scala, Cloud and other buzzwords.
Email me at: I write at: Twitter:
[email protected]://www.smartjava.org
@josdirksen
![Page 3: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/3.jpg)
Follow alongSources, presentation etc:
Demo heavy sessionhttps://github.com/josdirksen/next-build-consul
![Page 4: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/4.jpg)
Microservices?
![Page 5: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/5.jpg)
What are microservices?Small, �ne-grained easy to replace components.Organized around capabilities.Di�erent languages and backends (whatever �ts best).Fault tolerant, resiliant, automated deployements.
"Small Autonomous services that work together",Sam Newman
![Page 6: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/6.jpg)
From three tier to Microservices
![Page 7: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/7.jpg)
Running Microservices is hardWhere is my other service or database?Am I healthy, is the other one healthy?Where do I store con�guration?How do I handle redundancy and failover?...
, says everyone"Distributed systems are hard"
![Page 8: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/8.jpg)
ServiceDiscovery
![Page 9: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/9.jpg)
Basic approachHardcoded IP Address or LookupDNS
Lookup is nice!Requires managing names (con�g �les), DNS ServerHow to handle failover?
DNS
![Page 10: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/10.jpg)
Now with failoverPoint to a loadbalancerDNS
Works nicely with !How to check health and register services?Programmatic access to LB?
DNS
![Page 11: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/11.jpg)
What would be nice
Does : Lightweight ( , REST) support failoverHas �exible and manages
Lookups DNSHealth checking Con�guration
![Page 12: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/12.jpg)
Consul"Consul [..] provides an opinionated framework for
service discovery and eliminates the guess-workand development e�ort. Clients simply register
services and then perform discovery using a DNSor HTTP interface. Other systems require a home-
rolled solution." - consul.io
![Page 13: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/13.jpg)
Main FeaturesService discovery through REST and DNSSimple registration using REST APIDistributed KV store for con�gurationProvides extensive health checking
All in one package
![Page 14: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/14.jpg)
Good to knowMulti DC-readyAPI for distributed locksEasy HA SetupEvent system
Consul = Zookeeper + Nagios + DNSMasq +Scriptings + ...
![Page 15: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/15.jpg)
Consul Architecture
![Page 16: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/16.jpg)
Service Registration Flow1. Service calls Consul Agent with registration message:
.2. Agent communicates registration with Consul Server3. Agent checks health of Service.4. If check succeeds mark as Healthy, if not mark as Unhealthy,
communicate results with Server5. When a lookup for Service occurs, only return Healthy services
http://agent_host/v1/agent/service/register
![Page 17: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/17.jpg)
Sample: registration message{ "Name": "service1", "address": "10.0.0.12", "port": 8080, "Check": { "http": "http://10.0.0.12:8080/health", "interval": "5s" } }
Send when a new service starts upCheck types: script, http, tcp, TTL, Docker
![Page 18: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/18.jpg)
Sample: DNS Lookup$dig @nb-consul.local backend-service.service.consul
; <<>> DiG 9.8.3-P1 <<>> @nb-consul.local backend-service.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27716 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION: ;backend-service.service.consul. IN A
;; ANSWER SECTION: backend-service.service.consul. 0 IN A 10.0.9.3 backend-service.service.consul. 0 IN A 10.0.9.2 backend-service.service.consul. 0 IN A 10.0.9.4
Consul provides a DNS ServerWorks great with Docker ( : will show in Demo)teaser
![Page 19: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/19.jpg)
Sample: REST Lookup$ curl -s http://192.168.99.106:8500/v1/catalog/service/backend-service [{ "Node": "cf2f293e423c", "Address": "192.168.99.111", "ServiceID": "backend-service", "ServiceName": "backend-service", "ServiceAddress": "10.0.9.2", "ServicePort": 8080 },{ "Node": "072b4ea1abc1", "Address": "192.168.99.112", "ServiceID": "backend-service", "ServiceName": "backend-service", "ServiceAddress": "10.0.9.3", "ServicePort": 8080 }]
![Page 20: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/20.jpg)
DEMO
![Page 21: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/21.jpg)
Closer look: DNS LookupIn code:
resp, err := http.Get("http://backend-service:8081/") if err != nil { fmt.Println(err) } else { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) w.Header().Set("Content-Type",resp.Header.Get("Content-Type")) w.Write(body) }
For docker:
dns: 192.168.99.106 dns_search: service.consul
![Page 22: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/22.jpg)
ConsulEcosystem
![Page 23: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/23.jpg)
Consul templateRender template based on Consul stateSetup reverse proxy: Nginx, Apache, haproxy
global daemon maxconn {{key "service/haproxy/maxconn"}}
defaults mode {{key "service/haproxy/mode"}}{{range ls "service/haproxy/timeouts"}} timeout {{.Key}} {{.Value}}{{end}}
listen http-in bind *:8000{{range service "release.web"}} server {{.Node}} {{.Address}}:{{.Port}}{{end}}
![Page 24: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/24.jpg)
Demo
![Page 25: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/25.jpg)
Envconsul"III. Con�g Store con�g in the environment",
http://12factor.net/
More settings > More complexity
$ envconsul \ -consul demo.consul.io \ -prefix redis/config \ redis-server [opts...]
Vault: help in managing secrets
DEMO ENVCONSUL
![Page 26: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/26.jpg)
DEMO ENVCONSUL
![Page 27: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/27.jpg)
![Page 28: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/28.jpg)
Prometheus & Grafana
![Page 29: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/29.jpg)
More informationlinks:
This presentation, sources and docker stu�:
https://www.consul.io/https://github.com/hashicorp/consul-templatehttps://github.com/hashicorp/envconsul
https://github.com/josdirksen/next-build-consul
![Page 30: Service discovery in a microservice architecture using consul](https://reader031.vdocument.in/reader031/viewer/2022030317/586e8cd41a28aba0038b860f/html5/thumbnails/30.jpg)
Thank You!