Search code examples
consul

Register multiple nodes with consul


The question is, how do I register multiple nodes with consul under the same ID. I'm running a consul server in docker, and in my machine localhost I run two processes of the same HelloWorld nodejs app on my mac.

Problem: the entry for the process running at 3000 gets replaced by the process running at 3001 hence I'm ending up with one node only.

Question 2 Where do I download this GUI Client (not Web UI) from for Mac as shown in the screenshot.


Payload for node 1 port 3000

{
  HTTP: 'http://My-Mac-Pro.local:3000/health',
  Interval: '15s',
  Name: 'My-Mac-Pro.local',
  ID: 'user1'
}

Payload for node 2 port 3001

{
  HTTP: 'http://My-Mac-Pro.local:3001/health',
  Interval: '15s',
  Name: 'My-Mac-Pro.local',
  ID: 'user2'
}

nodeJS code

let http = require("http");
http.request({
    method: "PUT",
    hostname: env.CONSUL_HOST,
    port: 8500,
    path: "/v1/agent/check/register",
    headers: {
        "content-type": "application/json; charset=utf-8"
    }
}, function(response){
    if (response.statusCode == 200) {
        resolve();
    }
}).on("error", reject).end(JSON.stringify(body));

Expectation: See the multiple nodes under web

enter image description here


Solution

  • When you register services, each of services should register with unique service's ID.

    It could be something as : ${serviceName}-${hostname}-{ip}-${port}-${process.pid()}-${uuid.v4()} or any combination of those to ensure that your service ID is unique. Different ID in registration payload is what sets consul to differ instances of same app/serviceIdentity running and they wont "override" one another.

    Example of registration payload:

      const id = `${ip}-${hostname}-${serviceIdentity}-${port}`;
      const registrationDetails ={
        Name: serviceIdentity,
        ID: id,
        Address: ip,
        Port: parseInt(port),
        Check: {
          CheckID: `http-${id}`,
          Name: `http-${id}`,
          TLSSkipVerify: true,
          HTTP: `http://${host}:${port}/health`,
          Interval: '10s',
          Notes: `Service http health`,
          DeregisterCriticalServiceAfter: '60s',
        },
      };