# Config for Service Discovery using DNS and SRV records?

**URL:** <https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964>\
**Category:** Help!\
**Created:** [January 9, 2018, 11:27pm UTC](https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964 "2018-01-09T23:27:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bploetz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/bploetz/32/376_2.png) [@bploetz](https://discourse.haproxy.org/u/bploetz)\
**Post date:** [January 9, 2018, 11:27pm UTC](https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964/1 "2018-01-09T23:27:30Z")

</div>

I’m trying to use the new Service Discovery feature available in HAProxy 1.8 as described here:

> **[DNS for Service Discovery in HAProxy - HAProxy Technologies](https://www.haproxy.com/blog/dns-service-discovery-haproxy/)**
>
> HAProxy allows you to use DNS service discovery to detect server changes and automatically apply them to your configuration.

I have HAProxy sitting in front of a collection of backend servers (which are Docker containers running on ECS) that are auto-scaled in and out during the day. I’ve got a Route53 private hosted zone with an SRV record and associated A records for the live instances to facilitate discovery.

```
$ dig -t srv _api._tcp.test-service.internal +additional

;; ANSWER SECTION:
_api._tcp.test-service.internal. 60 IN SRV	1 1 8080 i-XXXXXXXXXXXXXXXXX.test-service.internal.

```

What I’m not sure about is how to configure HAProxy for this. It seems like I need a combination of server-template, and resolvers, but I can’t get it to work. Here’s what I have so far:

```
resolvers awsdns
		nameserver dns0 "${SERVICE_DISCOVERY_DNS_SERVER0}"
		nameserver dns1 "${SERVICE_DISCOVERY_DNS_SERVER1}"
		nameserver dns2 "${SERVICE_DISCOVERY_DNS_SERVER2}"
		nameserver dns3 "${SERVICE_DISCOVERY_DNS_SERVER3}"

backend api
		option tcp-check
		server-template api 5 "_api._tcp.${SERVICE_DISCOVERY_DOMAIN_NAME}" 8080 resolvers awsdns resolve-prefer ipv4

```

But this doesn’t work. I suspect the server-template I have above is wrong somehow. Anyone have any working examples of DNS Service Discovery that can share their configs?

Thanks in advance!  
BP

---

<div class="post-metadata">

**Author:** ![bploetz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/bploetz/32/376_2.png) [@bploetz](https://discourse.haproxy.org/u/bploetz)\
**Post date:** [January 10, 2018, 3:31am UTC](https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964/2 "2018-01-10T03:31:27Z")

</div>

I got this working. The problem was I was trying to use the nameservers for the private hosted zone as my resolvers, when instead I needed to use the VPC internal DNS resolver. This is always the “+2” of configured CIDR range ([http://blog.thestateofme.com/2015/09/01/forwarding-dns-queries-to-aws-vpc-resolvers/](http://blog.thestateofme.com/2015/09/01/forwarding-dns-queries-to-aws-vpc-resolvers/))

---

<div class="post-metadata">

**Author:** ![abarrios](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/abarrios/32/702_2.png) [@abarrios](https://discourse.haproxy.org/u/abarrios)\
**Post date:** [December 3, 2018, 5:14pm UTC](https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964/3 "2018-12-03T17:14:11Z")

</div>

Hi! Where you obtained the record prefix “\_api.\_tcp.”.  
Im trying to use with the AWS service discovery and the records that AWS created doesnt have the service and protocol prefix.

---

<div class="post-metadata">

**Author:** ![bploetz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/bploetz/32/376_2.png) [@bploetz](https://discourse.haproxy.org/u/bploetz)\
**Post date:** [December 3, 2018, 5:37pm UTC](https://discourse.haproxy.org/t/config-for-service-discovery-using-dns-and-srv-records/1964/4 "2018-12-03T17:37:07Z")

</div>

I didn’t use AWS’ Service Discovery for this (though I tried to). Gory details here: [https://medium.com/ground-signal-engineering/ecs-service-discovery-with-lambda-dns-and-haproxy-1126ab381688](https://medium.com/ground-signal-engineering/ecs-service-discovery-with-lambda-dns-and-haproxy-1126ab381688)
