Consul: how to register service with multi address?

Created on 28 Jan 2016  路  12Comments  路  Source: hashicorp/consul

Sorry for opening a new PR because I searched the issues and found nothing. I have a service, which have multi instances, and how i register the service with multi ip? Thanks.

Most helpful comment

Just put my findings here, in case someone searched the same issue.

To add multiple ips to service, you need to add several service id with the same service name:

curl -X PUT -d '{"ID": "app1", "Name": "app", "Address": "1.2.3.4", "Port": 3000}' http://127.0.0.1:8500/v1/agent/service/register

curl -X PUT -d '{"ID": "app2", "Name": "app", "Address": "5.6.7.8", "Port": 3000}' http://127.0.0.1:8500/v1/agent/service/register

dig @127.0.0.1 -p 8600 app.service.consul # will return 2 ip addresses

All 12 comments

Hi @AI-PaaS can you explain a little more about what you are trying to do - do you have multiple instances of the service on the same host with different ports, or multiple instances of the service across different hosts?

I have multiple instances of the service across different hosts

In that case you typically run the Consul agent on each of your hosts. You register the services available on that host via configuration files (https://www.consul.io/docs/agent/services.html) or via HTTP calls to the agent (https://www.consul.io/docs/agent/http/agent.html#agent_service_register). If you don't supply the address when you configure the service, it will get the address of the host running the agent.

The above steps will register all the different _instances_ of your service, each with its associated address. Then, clients can use DNS to grab a healthy instance of the service using the logical service name (foo.service.consul) or via the HTTP APIs.

I don't agree this way. In normal DNS, i can add multi addresses for one dns meta record, for example, round robin DNS. If i choose the method which install one agent in one host and i have hundreds of hosts, so there will too more agent and it's diffcult for managing.

Or you mean i should proxy the service instances using HAProxy?

Consul does quite a bit of the managing for you. If a host goes down, for example, Consul won't return any of the services on that host to requestors over DNS. You can also register service-level health checks that will take an instance out if they fail. Consul also does a random shuffle of addresses returned in DNS responses so that clients will load your services instances evenly across your cluster.

With Consul usually you don't have to proxy your services because it takes care of health checking and distributing traffic across your available instances. If you want to configure a proxy, though, you can use consul-template to manage the proxy's configuration and keep it in sync with the service catalog.

I'm going to close this out. Please let me know if you have any more questions, also the mailing list is a good place to get feedback as well.

Just put my findings here, in case someone searched the same issue.

To add multiple ips to service, you need to add several service id with the same service name:

curl -X PUT -d '{"ID": "app1", "Name": "app", "Address": "1.2.3.4", "Port": 3000}' http://127.0.0.1:8500/v1/agent/service/register

curl -X PUT -d '{"ID": "app2", "Name": "app", "Address": "5.6.7.8", "Port": 3000}' http://127.0.0.1:8500/v1/agent/service/register

dig @127.0.0.1 -p 8600 app.service.consul # will return 2 ip addresses

Thank you @xuqingfeng
That helps me for my solution:
https://groups.google.com/forum/#!topic/consul-tool/vM0wfYowyIU

Now i dont know why my first try was ending in ACL errors, but this works for me.

I have the same request,

I would like to use unix socket address to allow communication between consul and vault (running as two containers sharing a volume with socket file in the same Kubernetes POD). I also want to be able to expose the consul API for external UI access (:8500). So being able to bind both on the UNIX socket and TCP socket would be what I am looking for...

You can use this service definition as well:

{
  "services": [{
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6061,
    "id": "id-1"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6062,
    "id": "id-2"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6063,
    "id": "id-3"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6064,
    "id": "id-4"
  }]
}

You can use this service definition as well:

{
  "services": [{
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6061,
    "id": "id-1"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6062,
    "id": "id-2"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6063,
    "id": "id-3"
  },
  {
    "address": "10.6.8.102",
    "name": "id.102.co",
    "tags": ["envoy"],
    "port": 6064,
    "id": "id-4"
  }]
}

I run it and get Missing service name.
How to fix it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

runswithd6s picture runswithd6s  路  3Comments

aravind picture aravind  路  3Comments

matteoturra picture matteoturra  路  4Comments

nschoe picture nschoe  路  4Comments

philsttr picture philsttr  路  3Comments