Open psyhomb opened 4 years ago
@psyhomb Do you have a docker image built for your fork? This repo seems inactive, and I'm looking for something newer to pull from. This feature would be highly useful in our deployments as well.
Hey @issmirnov, yes docker image is available on dockerhub. 😉
Latest image version with features described above is v7.4.0:
docker pull psyhomb/registrator:v7.4.0
This is an example of how we are running it in the production environment on AWS ECS nodes:
#!/bin/bash
IP=$(ip a s dev eth0 | awk '/inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $2}' | awk -F'/' '{print $1}')
docker run -d \
--name registrator \
--network host \
--restart always \
-v /var/run/docker.sock:/tmp/docker.sock \
psyhomb/registrator:v7.4.0 -deregister=always -cleanup=true -explicit=true -ip=${IP} -tags='
{{- printf "%s" "cluster_name=" }}{{ mIndex "com.amazonaws.ecs.cluster" .Config.Labels | printf "%s," }}
{{- printf "%s" "environment=" }}{{ matchFirstElement "^ENVIRONMENT=" .Config.Env | splitIndex 1 "=" | printf "%s," }}
{{- printf "%s" "platform_unit_id=" }}{{ mIndex "com.amazonaws.ecs.task-arn" .Config.Labels | splitIndex -1 "/" | printf "%s," }}
{{- printf "%s" "container_id=" }}{{ strSlice .ID 0 12 | printf "%s," }}
{{- matchFirstElement "^SERVICE_NAME=" .Config.Env | toLower }}' \
consul://127.0.0.1:8500
Example of tags list for registered Consul service:
cluster_name=c1
environment=prod
platform_unit_id=5f0390aab96a4fcaa881ed92685a924d
container_id=82c083191a42
service_name=foo
v7.4.0
About
I have added support for Go template to
-tags
flag and with this feature it is now possible to dynamically evaluate all the container inspect object fields and to add their values to Consul tags list, also prebuilt docker image is available on dockerhub.Static tags can be used as before (non-breaking change):
Or you can utilize Go template and dynamically evaluate all the fields from docker container inspect object:
Go Template functions
List of supported custom template functions (to check predefined global template functions click here):
strSlice
Slice string from start to end (same as
s[start:end]
wheres
represents string).Usage:
strSlice s start end
Example:
strSlice .ID 0 12
Output:
"e20f9c1a7656"
sIndex
Return element from slice or array
s
by specifiying indexi
(same ass[i]
wheres
represents slice or array - indexi
can also take negative values to extract elements in reverse order).Usage:
sIndex i s
Example:
sIndex 0 .Config.Env
Output:
"ENVIRONMENT=test"
mIndex
Return value for key
k
stored in the mapm
(same asm["k"]
).Usage:
mIndex k m
Example:
mIndex "com.amazonaws.ecs.task-arn" .Config.Labels
Output:
"arn:aws:ecs:region:xxxxxxxxxxxx:task/368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
toUpper
Return string
s
with all letters mapped to their upper case.Usage:
toUpper s
Example:
toUpper "foo"
Output:FOO
toLower
Return string
s
with all letters mapped to their lower case.Usage:
toLower s
Example:
toLower "FoO"
Output:foo
replace
Replace all (-1) or first
n
occurrences ofold
withnew
found in the designated strings
.Usage:
replace n old new s
Example:
replace -1 "=" "" "=foo="
Output:foo
join
Create a single string from all the elements found in the slice
s
wheresep
will be used as separator.Usage:
join sep s
Example:
join "," .Config.Env
Output:
"ENVIRONMENT=test,SERVICE_8105_NAME=foo,HOME=/home/foobar,SERVICE_9404_NAME=bar"
split
Split string
s
into all substrings separated bysep
and return a slice of the substrings between those separators.Usage:
split sep s
Example:
split "," "/proc/bus,/proc/fs,/proc/irq"
Output:[/proc/bus /proc/fs /proc/irq]
splitIndex
split
andsIndex
function combined, indexi
can also take negative values to extract elements in reverse order. Same result can be achieved if using pipeline with both functions:{{ split sep s | sIndex i }}
Usage:
splitIndex i sep s
Example:
splitIndex -1 "/" "arn:aws:ecs:region:xxxxxxxxxxxx:task/368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
Output:"368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
matchFirstElement
Iterate through slice
s
and return first element that match regex expression.Usage:
matchFirstElement regex s
Example:
matchFirstElement "^SERVICE_" .Config.Env
Output:
"SERVICE_8105_NAME=foo"
matchAllElements
Iterate through slice
s
and return slice of all elements that match regex expression.Usage:
matchAllElements regex s
Example:
matchAllElements "^SERVICE_" .Config.Env
Output:
[SERVICE_8105_NAME=foo SERVICE_9404_NAME=bar]
httpGet
Fetch an object from URL.
Usage:
httpGet url
Example:
httpGet "https://ajpi.me/all"
Output:[]byte
(e.g. JSON object)jsonParse
Extract value from JSON object by specifying exact path (nested objects). Keys in path has to be separated with double colon sign.
Usage:
jsonParse b key1::key2::key3::keyN
Example:
jsonParse b "Additional::Country"
Output:
"United States"
Examples
E1
Use custom (
strSlice
) or predefined global (slice
) template function to slice values as needed:For multiple tags you'd just have to add comma separated values.
Output:
E2
In this example we're going to use
mIndex
template function to extract value from the Docker label that has dots in key name, then saved value will be passed tosplitIndex
template function which will be used to split string by/
character and to return last element from the list.Inspect container object snippet:
Output:
E3
It is also possible to set all env vars as lower case tags.
Output:
Or just one specific environment variable that match regex expression (first match only).
Output:
Or list of all regex matches.
Output:
E4
Use
httpGet
function to fetch data from any external HTTP source (must return a JSON object).Output:
Or if running on AWS EC2 instance you can use this function and EC2 metadata API to fetch EC2
instanceId
andinstanceType
.Output:
Or you can use more advanced example that will allow you to use different sources and tags per container.
Now you can start one or more containers with special environment variables.
container 1
Output:
container 2
Output:
Usage
To start using this feature you have to start
registrator
with-tags
flag:Afterwards you can start any arbitrary container with
SERVICE_NAME
env variable and with one or multiple published ports:Check if container is started successfully:
Output:
Check registrator log:
Output:
And finally check what's registered on Consul:
Output:
This is especially useful if you are using registrator to register Prometheus targets, because you can use these tags as labels.