Caravan.Cluster.DnsStrategy (caravan v1.0.1)
Implements a libcluster strategy for node distribution based on Consul DNS. By
default it uses :inet_res
to query the nameservers, though it can be configured to use any
module conforming to the Caravan.DnsClient
behaviour.
Prerequisites
First things first, is that you'll need to have Consul setup and configured as a nameserver either
in your hosts file or via an erl_inetrc file You'll
need to create a service that will return SRV records with the hostname and
distribution port. The Consul documentation
has directions on what needs to be setup and how to test with dig
.
Let's look at an example:
'likes-service-3434@prod.socialmedia.consul`
Above, likes-service
is an app name. It will correspond with the :node_sname
config option. The port is the last integer to the left of the '@'. You'll
need this because our nodes will be using Caravan.Epmd.Client
and
Caravan.Epmd.Dist_dist
to use the port number of the node name instead of
being assigned a port randomly by epmd
.
Also note that the hostname of cluster nodes returned by Consul must be the
same as that in the nodes -name
parameter
Configuration
Here's a sample configuration
config :libcluster,
topologies: [
caravan: [
# The selected clustering strategy. Required.
strategy: Caravan.Cluster.DnsStrategy,
config: [
#service name that returns the distribution port in a SRV record
query: "likes-service-dist.service.consul",
#forms the base of the node name. App name is a good one.
node_sname: "profile-service",
#The poll interval for the Consul service in milliseconds. Defaults to 5s
poll_interval: 5_000
#The module of the DNS client to use.
dns_client: Caravan.DnsClient
],
]
]
Summary
Functions
Returns a specification to start this module under a supervisor.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.