DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Service Discovery From Within the Kubernetes Cluster
  • Microservices With JHipster
  • From Monolith to Containers: Real-World Migration Blueprint
  • Exploring the Horizon of Microservices With KubeMQ's New Control Center

Trending

  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • AI Paradigm Shift: Analytics Without SQL
  • Smart Deployment Strategies for Modern Applications
  • Java Backend Development in the Era of Kubernetes and Docker
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Service Discovery With NGINX Plus and Consul

Service Discovery With NGINX Plus and Consul

Service discovery is crucial to service-oriented architecture, and distributed systems. Here's a look at service discovery with NGINX Plus and Consul, a service discovery tool.

By 
Patrick Nommensen user avatar
Patrick Nommensen
·
Jan. 29, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

Service discovery is a key component of most distributed systems and service-oriented or microservices architectures. Because service instances have dynamically assigned network locations which can change over time due to autoscaling, failures, or upgrades, clients need a sophisticated service discovery mechanism for tracking the current network location of a service instance.

This blog post describes how to dynamically add or remove load-balanced servers that are registered with Consul, a popular tool for service discovery. The solution uses NGINX Plus’ on-the-fly reconfiguration API, which eliminates the need to reload NGINX Plus configuration file when changing the set of load-balanced servers. With NGINX Plus Release 8 (R8), changes you make with the API now persist across restarts and configuration reloads.

Consul, developed by HashiCorp, is most widely used for discovering and configuring services in your infrastructure. It also provides failure detection (health checking), a key/value store, and support for multiple data centers out of the box.

To make it easier to combine the NGINX Plus on-the-fly reconfiguration API with Consul, we’ve created a GitHub project called consul-demo, with step-by-step instructions for creating the configuration described this blog post. In this post, we will walk you through the proof of concept. Using tools like Docker, Docker Compose, Homebrew, and jq, you can spin up a Docker–based environment in which NGINX Plus load balances HTTP traffic to a couple of hello-world applications, with all components running in separate Docker containers.

How the Demo Works

First we spin up a separate Docker container for each of the following apps:

  • Consul – Performs service discovery
  • Registrator – Registers services with Consul. Registrator monitors for containers being started and stopped and updates Consul when a container changes state.
  • tutum/hello-world – Simulates a service (a simple hello-world app)
  • google/golang-hello – Simulates a second service (another simple hello-world app)
  • NGINX Plus R8 – Load balances the other listed services

The NGINX Plus container listens on the public port 80, and the built-in NGINX Plus live activity monitoring dashboard on port 8080. The Consul container listens on ports 8300, 8400, 8500, and 53. It advertises a fixed IP address of 10.0.2.15 for intercontainer communication.

Registrator monitors Docker for new containers that are launched with exposed ports and registers the associated service with Consul. By setting environment variables within the containers, we can be more explicit about how to register the services with Consul. For each hello-world container, we set theSERVICE_TAGS environment variable to production to identify the container as an upstream server for load balancing by NGINX Plus. When a container quits or is removed, Registrator removes it from the Consul service catalog automatically.

Finally, we configure Consul watches in a JSON file included in consul-demo, so that every time there is an update to the list of registered services, an external handler (script.sh) is invoked. This bash script gets the list of all current NGINX Plus upstream servers, uses the Consul services API to loop through all the containers registered with Consul that are tagged production and uses NGINX Plus’ on-the-fly reconfiguration API (upstream_conf) to add them to the NGINX Plus upstream group if they’re not listed already. It also then removes from the NGINX Plus upstream group any production-tagged containers that are not registered with Consul.

Summary

Using a script like the one provided in our demo automates the process of upstream reconfiguration in NGINX Plus by dynamically reconfiguring your upstream groups based on the services registered with Consul. This automation also frees you from having to figure out how to issue the API calls correctly and reduces the amount of time between the state change of a service in Consul and its addition or removal within the NGINX Plus upstream group.

microservice Plus (programming language) Service discovery Docker (software) Discovery (law)

Published at DZone with permission of Patrick Nommensen. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Service Discovery From Within the Kubernetes Cluster
  • Microservices With JHipster
  • From Monolith to Containers: Real-World Migration Blueprint
  • Exploring the Horizon of Microservices With KubeMQ's New Control Center

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook