Generating -bind parameter in Consul JSON files for use with Marathon

555 views Asked by At

I am working on launching Consul containers on docker with Marathon and I've run into a somewhat subjective issue regarding creating the JSON files.

Currently I plan to launch containers with JSON files of this format

server-1.json

{
      "id": "consul-server-2",
      "cmd": "consul agent -server -client=0.0.0.0 -ui -bind=100.10.30.40 -retry-join=server-1.local -data-dir=/tmp/consul",
      "cpus": 1,
      "mem": 512.0,
      "instances": 1,
      "container": {
        "type": "DOCKER",
        "docker": {
          "image": "consul:latest",
          "name": "dev-consul",
          "network": "HOST"
        }
      },
      "constraints": [
        [
          "hostname",
          "CLUSTER",
          "server-1.local"
        ]
      ]
}

I need to be able to change the -bind address for each JSON file and I was planning on using heredocs with BASH but I am not sure if there are better practices as far as ease of maintainability for creating these type of files.

Ideally I would have liked to have a field in Consul or Marathon which could automatically give me the IP address of a specific port to feed to -bind but because I have multiple private IPs it seems I need to configure it manually.

2

There are 2 answers

0
Sean On

Look at consul 0.7.2 or newer. There is a soon-to-be documented feature in Consul that allows for runtime configuration of IP addresses. I wouldn't recommend running Consul in a container unless running net=host, but using the configuration snippet above:

{
          "id": "consul-server-2",
          "cmd": "consul agent -server -client='{{ GetPrivateIP }}' -ui -bind=100.10.30.40 -retry-join=server-1.local -data-dir=/tmp/consul",
          "cpus": 1,
          "mem": 512.0,
          "instances": 1,
          "container": {
            "type": "DOCKER",
            "docker": {
              "image": "consul:latest",
              "name": "dev-consul",
              "network": "HOST"
            }
          },
          "constraints": [
            [
              "hostname",
              "CLUSTER",
              "server-1.local"
            ]
          ]
        }

Other options for what address to use can be explored based on the hashicorp/go-sockaddr package.

0
rkrzr On

It sounds like you have a configuration management issue. If I understand you correctly you have a number of servers in an internal network where each has an internal IP address and you now want to generate the right service files for each server.

Typically you would use a configuration management system like e.g. Ansible, Chef or Puppet to solve this.

Personally I can recommend Ansible since it is easy to get started with and low overhead.

To solve your problem you would then first create an inventory file with the IP addresses of your servers and then create a Jinja2 template for your service files. You can then use the correct IP address for each server in that template and finally deploy all the files with Ansible.