1. Service Discovery in
Docker Using NGINX
and NGINX Plus with
Consul
- Kunal Pariani (Pre-Sales Engineer @ NGINX)
2. #nginx #nginxconf2
Agenda
• What is service discovery & why it matters
• How open source NGINX integrates with Consul & Consul Template
• How NGINX Plus makes the integration easier
1. Using HTTP-based Dynamic Reconfiguration API **
2. DNS Resolution using SRV records **
3. Comparison between the two methods
• Q & A
** Live demo to show that stuff works !!
4. #nginx #nginxconf4
What It Is & Why It Matters
• Microservices-based applications are dynamic in nature
• Good way to stay on top of the current state of all the
services is to use service discovery
• Its the process of discovering the service locations
• Integration with NGINX enables automatic addition or
removal of the service instances from the load-
balancing pool
• No direct modification to NGINX configuration,
achieving automation
8. #nginx #nginxconf
• NGINX reloads every time there is a change in Consul
• Can lead to increased memory usage & eventually
overload the system
Limitations
9. #nginx #nginxconf9
NGINX Plus Improves the
Integration
• Method #1
Using HTTP-based dynamic reconfiguration API with Consul
watches
• Method #2
DNS Resolution using SRV records from Consul
11. #nginx #nginxconf11
Dynamic Reconfiguration API in NGINX Plus
• Configuration
upstream backend {
zone backend 64k;
server app-server;
}
server {
listen 8080; # Listen on a local port
location /upstream_conf {
allow 10.0.0.0/8; # Allow access only from LAN
deny all; # Deny everyone else
upstream_conf;
}
}
15. #nginx #nginxconf
DNS for Service Discovery
• DNS resolver in NGINX can point to the DNS interface of any
service discovery tool
• Open source NGINX can query for DNS A records to get all
the IP addresses associated with a domain name
• NGINX supports DNS queries over both UDP & TCP
• Honors the TTL in the DNS response, or you can override it
using the valid parameter to the resolver directive
16. #nginx #nginxconf
• No support for DNS SRV records, A records only
- NGINX Plus only
• Port numbers have to be statically defined
• Not suitable for microservices environments
Limitations with Open
Source NGINX
17. #nginx #nginxconf17
DNS SRV Records with
NGINX Plus
• SRV record includes extra information like port number, weight, and priority
• service parameter: enables resolving of SRV records and sets the service
name & protocol
• resolve parameter: Monitors changes in SRV records and automatically
modifies upstream configuration without the need for reload
http {
resolver <consul-dns-ip> valid=2s;
upstream backends {
zone backends 64k;
server service.consul service=http resolve;
}
}
20. #nginx #nginxconf20
Comparison of the HTTP API & DNS
Methods
HTTP-based API method DNS-based method
Push v/s pull method Push method
No polling
Consul watches triggers NGINX
Plus to reconfigure
Pull method
Keep polling DNS periodically
Additional DNS traffic
Reaction time Immediate since it’s a push method Depends on how often DNS gets
queried—NGINX Plus can query DNS
as often as every 1 second
Integration work Yes
Create and manage an external
script which gets triggered every time
there is a change in Consul
No
No separate script or integration
needed
Caveats API requests should be made
against a healthy NGINX Plus server
DNS server needs to be highly
available
Specify multiple name servers using
the resolver directive
21. #nginx #nginxconf21
How to Try Out NGINX Plus?
• Sign up for the 30-day free trial
https://www.nginx.com/#free-trial
• Developer License
https://www.nginx.com/developer-license/
Password: nginxconf16dev
22. #nginx #nginxconf22
Additional Resources
• Service Discovery in a Microservices Architecture
• Using DNS for Service Discovery with NGINX and NGINX Plus
• Service Discovery for NGINX Plus Using Consul APIs
• Service Discovery for NGINX Plus Using DNS SRV Records from Consul
• Service Discovery for NGINX Plus with etcd
• Service Discovery for NGINX Plus with ZooKeeper