SlideShare a Scribd company logo
Service Discovery in a
 Architecture
using 
Microservice
Consul
Who Am I
Currently doing Devops, Scala stu
At Equeris, lean startup within Equens
Docker, Consul, Scala, Cloud and other buzzwords.
Email me at:
I write at:
Twitter:
jos.dirksen@gmail.com
http://www.smartjava.org
@josdirksen
Follow along
Sources, presentation etc:
Demo heavy session
https://github.com/josdirksen/next-build-consul
Microservices?
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
From three tier to Microservices
Running Microservices is hard
Where 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"
Service
Discovery
Basic approach
Hardcoded IP Address or LookupDNS
Lookup is nice!
Requires managing names (con g les), DNS Server
How to handle failover?
DNS
Now with failover
Point to a loadbalancerDNS
Works nicely with !
How to check health and register services?
Programmatic access to LB?
DNS
What would be nice
Does : Lightweight ( , REST) support failover
Has exible and manages
Lookups DNS
Health checking Con guration
Consul
"Consul [..] provides an opinionated framework for
service discovery and eliminates the guess-work
and development e ort. Clients simply register
services and then perform discovery using a DNS
or HTTP interface. Other systems require a home-
rolled solution." - consul.io
Main Features
Service discovery through REST and DNS
Simple registration using REST API
Distributed KV store for con guration
Provides extensive health checking
All in one package
Good to know
Multi DC-ready
API for distributed locks
Easy HA Setup
Event system
Consul = Zookeeper + Nagios + DNSMasq +
Scriptings + ...
Consul Architecture
Service Registration Flow
1. Servicecalls Consul Agentwith registration message:
.
2. Agentcommunicates registration with Consul Server
3. Agentchecks health of Service.
4. If check succeeds mark as Healthy, if not mark as Unhealthy,
communicate results with Server
5. When a lookup for Serviceoccurs, only return Healthyservices
http://agent_host/v1/agent/service/register
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 up
Check types: script, http, tcp, TTL, Docker
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 Server
Works great with Docker ( : will show in Demo)teaser
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
}]
DEMO
Closer look: DNS Lookup
In 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
Consul
Ecosystem
Consul template
Render template based on Consul state
Setup 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}}
Demo
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
Service discovery in a microservice architecture using consul
Prometheus & Grafana
More information
links:
This presentation, sources and docker stu :
https://www.consul.io/
https://github.com/hashicorp/consul-template
https://github.com/hashicorp/envconsul
https://github.com/josdirksen/next-build-consul
Thank You!

More Related Content

PPTX
Python/Flask Presentation
PPTX
Flask – Python
PDF
Clean code
PDF
Python Collections Tutorial | Edureka
PPT
Web Application Introduction
PDF
MongoDB and Node.js
PPTX
Clean Code I - Best Practices
PDF
Docker compose
Python/Flask Presentation
Flask – Python
Clean code
Python Collections Tutorial | Edureka
Web Application Introduction
MongoDB and Node.js
Clean Code I - Best Practices
Docker compose

What's hot (20)

PPTX
Asp.net web api
PDF
Nest.js Introduction
PDF
Clean coding-practices
PPT
Learn REST API with Python
PDF
A python web service
PDF
CKA Certified Kubernetes Administrator Notes
PDF
Php Tutorials for Beginners
PPTX
Introduction to Node.js
PDF
Bootstrap
PDF
Java Serialization Deep Dive
PDF
Icebreaker with DevOps
PDF
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
PPTX
Introduction to java 8 stream api
PPTX
MongoDB - Aggregation Pipeline
PPTX
Introduction to laravel framework
PPTX
Django - Python MVC Framework
PPTX
Docker Security workshop slides
PDF
Docker & kubernetes
PPT
Understanding REST
PPTX
ASP.NET MVC 3.0 Validation
Asp.net web api
Nest.js Introduction
Clean coding-practices
Learn REST API with Python
A python web service
CKA Certified Kubernetes Administrator Notes
Php Tutorials for Beginners
Introduction to Node.js
Bootstrap
Java Serialization Deep Dive
Icebreaker with DevOps
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
Introduction to java 8 stream api
MongoDB - Aggregation Pipeline
Introduction to laravel framework
Django - Python MVC Framework
Docker Security workshop slides
Docker & kubernetes
Understanding REST
ASP.NET MVC 3.0 Validation
Ad

Viewers also liked (6)

PPTX
Service Discovery and Registration in a Microservices Architecture
PDF
Consul: Microservice Enabling Microservices and Reactive Programming
PPTX
Service Discovery using etcd, Consul and Kubernetes
PDF
Service Discovery 101
PPTX
Building a Cloud Native Service - Docker Meetup Santa Clara (July 20, 2017)
PDF
Consul and Complex Networks
Service Discovery and Registration in a Microservices Architecture
Consul: Microservice Enabling Microservices and Reactive Programming
Service Discovery using etcd, Consul and Kubernetes
Service Discovery 101
Building a Cloud Native Service - Docker Meetup Santa Clara (July 20, 2017)
Consul and Complex Networks
Ad

Similar to Service discovery in a microservice architecture using consul (20)

PPTX
Service Discovery Like a Pro
PPTX
Intro to Consul
PPTX
Discover/Register Everything in consul
PDF
Soa with consul
PDF
Service discovery like a pro (presented at reversimX)
PDF
Getting Started with Consul
PDF
20190727 HashiCorp Consul Workshop: 管管你們家 config 啦
PDF
Consul - service discovery and others
PDF
OCP with super tengen toppa
PDF
Infrastructure development using Consul
PDF
HashiStack. To the cloud and beyond...
PPTX
Docker Service Registration and Discovery
PPTX
Microservices with docker swarm and consul
PDF
Consul and docker swarm cluster
PDF
Consul tutorial
PDF
Introduction to Consul
PDF
Consul First Steps
PDF
Workshop Consul .- Service Discovery & Failure Detection
PDF
Consul and Consul Pusher
PDF
Service discovery in Docker environments
Service Discovery Like a Pro
Intro to Consul
Discover/Register Everything in consul
Soa with consul
Service discovery like a pro (presented at reversimX)
Getting Started with Consul
20190727 HashiCorp Consul Workshop: 管管你們家 config 啦
Consul - service discovery and others
OCP with super tengen toppa
Infrastructure development using Consul
HashiStack. To the cloud and beyond...
Docker Service Registration and Discovery
Microservices with docker swarm and consul
Consul and docker swarm cluster
Consul tutorial
Introduction to Consul
Consul First Steps
Workshop Consul .- Service Discovery & Failure Detection
Consul and Consul Pusher
Service discovery in Docker environments

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
August Patch Tuesday
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
Machine Learning_overview_presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Tartificialntelligence_presentation.pptx
PPTX
1. Introduction to Computer Programming.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
August Patch Tuesday
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Weekly Chronicles - August'25-Week II
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
Getting Started with Data Integration: FME Form 101
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
cloud_computing_Infrastucture_as_cloud_p
Machine Learning_overview_presentation.pptx
A Presentation on Artificial Intelligence
Tartificialntelligence_presentation.pptx
1. Introduction to Computer Programming.pptx

Service discovery in a microservice architecture using consul

  • 2. Who Am I Currently doing Devops, Scala stu At Equeris, lean startup within Equens Docker, Consul, Scala, Cloud and other buzzwords. Email me at: I write at: Twitter: [email protected] http://www.smartjava.org @josdirksen
  • 3. Follow along Sources, presentation etc: Demo heavy session https://github.com/josdirksen/next-build-consul
  • 5. 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
  • 7. Running Microservices is hard Where 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"
  • 9. Basic approach Hardcoded IP Address or LookupDNS Lookup is nice! Requires managing names (con g les), DNS Server How to handle failover? DNS
  • 10. Now with failover Point to a loadbalancerDNS Works nicely with ! How to check health and register services? Programmatic access to LB? DNS
  • 11. What would be nice Does : Lightweight ( , REST) support failover Has exible and manages Lookups DNS Health checking Con guration
  • 12. Consul "Consul [..] provides an opinionated framework for service discovery and eliminates the guess-work and development e ort. Clients simply register services and then perform discovery using a DNS or HTTP interface. Other systems require a home- rolled solution." - consul.io
  • 13. Main Features Service discovery through REST and DNS Simple registration using REST API Distributed KV store for con guration Provides extensive health checking All in one package
  • 14. Good to know Multi DC-ready API for distributed locks Easy HA Setup Event system Consul = Zookeeper + Nagios + DNSMasq + Scriptings + ...
  • 16. Service Registration Flow 1. Servicecalls Consul Agentwith registration message: . 2. Agentcommunicates registration with Consul Server 3. Agentchecks health of Service. 4. If check succeeds mark as Healthy, if not mark as Unhealthy, communicate results with Server 5. When a lookup for Serviceoccurs, only return Healthyservices http://agent_host/v1/agent/service/register
  • 17. 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 up Check types: script, http, tcp, TTL, Docker
  • 18. 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 Server Works great with Docker ( : will show in Demo)teaser
  • 19. 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 }]
  • 20. DEMO
  • 21. Closer look: DNS Lookup In 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
  • 23. Consul template Render template based on Consul state Setup 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}}
  • 24. Demo
  • 25. 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
  • 29. More information links: This presentation, sources and docker stu : https://www.consul.io/ https://github.com/hashicorp/consul-template https://github.com/hashicorp/envconsul https://github.com/josdirksen/next-build-consul