Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

advertise specific address for a service #570

Merged

Conversation

MerlinDMC
Copy link
Contributor

Enable setting a specific address in a service definition for advertise.
If no specific address is given it will fallback to the node address and reassemble the old behaviour.

This does provide functionality to have consul running on a separate private interface and advertise services for a different public/private one during service discovery.

example for two services on the same node that advertise different addresses:

{
  "service": {
    "name": "default-service",
    "port": 9200
  }
}

{
  "service": {
    "name": "external-service",
    "address": "10.0.1.4",
    "port": 9200
  }
}

consul bound to localhost via configuration file:

{
  "data_dir": "./datadir-consul",
  "log_level": "INFO",
  "node_name": "0b4fe5be-1ad6-c1f2-9f9f-86caca9a67b3",
  "advertise_addr": "127.0.0.1",
  "bind_addr": "127.0.0.1",
  "server": true,
  "bootstrap": true
}

The result of a service discovery via DNS:

$ dig @localhost -p 8600 external-service.service.consul default-service.service.consul +answer

; <<>> DiG 9.8.3-P1 <<>> @localhost -p 8600 external-service.service.consul default-service.service.consul +answer
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27792
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;external-service.service.consul. IN    A

;; ANSWER SECTION:
external-service.service.consul. 0 IN   A   10.0.1.4

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Fri Jan  2 21:39:50 2015
;; MSG SIZE  rcvd: 96

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30860
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;default-service.service.consul.    IN  A

;; ANSWER SECTION:
default-service.service.consul. 0 IN    A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Fri Jan  2 21:39:51 2015
;; MSG SIZE  rcvd: 94

external-service results in 10.0.1.4 because of the specific address in the service definition.
default-service results in 127.0.0.1 where the service defaults to the node address which is 127.0.0.1.

Enable setting a specific address in a service definition for advertise. If no specific address is given it will fallback to the node address and reassemble the old behaviour.
@sean-
Copy link
Contributor

sean- commented Jan 2, 2015

+1

This is fantastic functionality.

@armon
Copy link
Member

armon commented Jan 5, 2015

Awesome! I was planning to tackle this for 0.5 but you beat me to it :) Reviewing now.

@@ -625,7 +629,7 @@ func (d *DNSServer) serviceSRVRecords(dc string, nodes structs.CheckServiceNodes
resp.Answer = append(resp.Answer, srvRec)

// Add the extra record
records := d.formatNodeRecord(&node.Node, srvRec.Target, dns.TypeANY, ttl)
records := d.formatNodeRecord(&node.Node, node.Node.Address, srvRec.Target, dns.TypeANY, ttl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the SRV also needs to handle the case of the override address

@armon
Copy link
Member

armon commented Jan 5, 2015

Overall looks really good! I think we need to add some more tests to the DNS stuff to test the behavior works there as expected, as well as for the changes in the state_store.

@MerlinDMC
Copy link
Contributor Author

I did add the SRV change.
If you don't add tests matching your needs I'll see to add some when I get more spare time.

@armon
Copy link
Member

armon commented Jan 5, 2015

Awesome!

@armon armon merged commit bd65cbb into hashicorp:master Jan 8, 2015
@armon
Copy link
Member

armon commented Jan 8, 2015

Added some tests, and made a minor change to preserve API compatibility.

@MerlinDMC MerlinDMC deleted the feature/configurable_service_address branch January 8, 2015 20:55
sttts added a commit to sttts/consul that referenced this pull request Jan 22, 2015
The Address field was introduced in hashicorp#570. This patch extends this to the api.
sttts added a commit to sttts/consul that referenced this pull request Jan 22, 2015
The Address field was introduced in hashicorp#570. This patch extends this to the api.
duckhan pushed a commit to duckhan/consul that referenced this pull request Oct 24, 2021
* make sync-catalog work with clients disable

Currently the sync-catalog won't work when the clients are disable
because of the use of `HOST_IP` to connect to the service. I propose
that when clients are disable, the synccatalog falls back to the consul
server service.

When `client.enabled=true` the syncCatalog should make use of the
HOST_IP to connect with consul. When `client.enabled=false`
the syncCatalog should fall back to the consul server service.
duckhan pushed a commit to duckhan/consul that referenced this pull request Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants