Skip to main content
Version: 2.7


service discovery via DNS#

Some service discovery system, like Consul, support exposing service information via DNS. Therefore we can use this way to discover service directly.

First of all, we need to configure the address of DNS servers:

# add this to config.yamldiscovery:   dns:     servers:       - ""          # use the real address of your dns server

Unlike configuring the domain in the Upstream's nodes field, service discovery via DNS will return all records. For example, with upstream configuration:

{    "id": 1,    "discovery_type": "dns",    "service_name": "test.consul.service",    "type": "roundrobin"}

and test.consul.service be resolved as and, this result will be the same as:

{    "id": 1,    "type": "roundrobin",    "nodes": [        {"host": "", "weight": 1},        {"host": "", "weight": 1}    ]}

Note that all the IPs from test.consul.service share the same weight.

If a service has both A and AAAA records, A record is preferred.

If you want to specify the port for the upstream server, you can add it to the service_name:

{    "id": 1,    "discovery_type": "dns",    "service_name": "test.consul.service:1980",    "type": "roundrobin"}

Another way to do it is via the SRV record, see below.

SRV record#

By using SRV record you can specify the port and the weight of a service.

Assumed you have the SRV record like this:

; under the section of blah.serviceA       300 IN      A       300 IN      A       300 IN      A
; name  TTL         type    priority    weight  portsrv     86400 IN    SRV     10          60      1980 Asrv     86400 IN    SRV     20          20      1981 B

Upstream configuration like:

{    "id": 1,    "discovery_type": "dns",    "service_name": "srv.blah.service",    "type": "roundrobin"}

is the same as:

{    "id": 1,    "type": "roundrobin",    "nodes": [        {"host": "", "port": 1980, "weight": 60, "priority": -10},        {"host": "", "port": 1981, "weight": 10, "priority": -20},        {"host": "", "port": 1981, "weight": 10, "priority": -20}    ]}

Note that two records of domain B split the weight evenly. For SRV record, nodes with lower priority are chosen first, so the final priority is negative.

As for 0 weight SRV record, the RFC 2782 says:

Domain administrators SHOULD use Weight 0 when there isn't any server selection to do, to make the RR easier to read for humans (less noisy). In the presence of records containing weights greater than 0, records with weight 0 should have a very small chance of being selected.

We treat weight 0 record has a weight of 1 so the node "have a very small chance of being selected", which is also the common way to treat this type of record.