TagIt is a tool that updates Consul service registration tags with outputs of a script. It copies the current service registration and appends the output of the script line by line as tags, while keeping the original tags.
TagIt addresses a feature that's currently missing from Consul. You can read more about the need for this functionality in this Consul issue.
Here are some scenarios where TagIt can be useful:
- Database Leader Tagging: Ensure all writes go to the leader by tagging it appropriately.
- Non-Consul-Aware Service Discovery: Use Consul for service discovery with services that aren't Consul-aware.
- Web Server VHost Tagging: Tag all vhosts served by a web server or load balancer.
- Generic Service Tagging: Tag any services for Consul-based service discovery.
To install TagIt, use the following commands:
$ git clone https://github.com/ncode/tagit
$ cd tagit
$ go build
TagIt provides three main commands: run
, cleanup
, and systemd
.
The run
command starts TagIt and continuously updates the tags based on the script output:
$ ./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit
The cleanup
command removes all tags with the specified prefix from the service:
$ ./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit
The systemd
command generates a systemd service file for TagIt:
./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit
This command will output a systemd service file that you can use to run TagIt as a system service.
TagIt interacts with Consul as follows:
sequenceDiagram
participant tagit
participant consul
loop execute script on interval
tagit->>consul: Query service with ID
consul->>tagit: Return current service registration
tagit->>tagit: Execute script and process output
tagit->>consul: Update service registration with new tags
end
Here's an example of how to test TagIt:
-
Start a Consul agent in development mode:
consul agent -dev &
-
Register a service with Consul:
curl --request PUT --data @examples/consul/my-service1.json http://127.0.0.1:8500/v1/agent/service/register
-
Run TagIt:
./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit
-
Generate a systemd service file:
./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit > /etc/systemd/system/tagit-my-service1.service
-
Clean up the tags:
./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit
Contributions to TagIt are welcome! Please feel free to submit a Pull Request.
TagIt is licensed under the Apache License, Version 2.0. See the LICENSE file for details.