Hashicorp Consul
Запуск
server# docker run -d --name consul-dev -p 8500:8500 hashicorp/consul:2.1.0 agent -dev -client=0.0.0.0
Добавление и мониторинг web сервисов
$ curl --request PUT \
--url http://server:8500/v1/agent/service/register \
--header 'Content-Type: application/json' \
--data '{
"ID": "web-app-N",
"Name": "web-service",
"Address": "192.168.X.1",
"Port": 8N,
"Check": {
"Name": "Web App HTTP Check",
"HTTP": "http://192.168.X.1:8N/",
"Interval": "10s",
"Timeout": "1s"
}
}'
...
$ curl -s "http://server:8500/v1/health/service/web-service" | ...
или
$ curl -s "http://server:8500/v1/health/service/web-service?passing=1" | ...
... jq '.[] | .Node.Node as $node | .Service.ID as $sid | .Checks[] | {Node: $node, ServiceID: $sid, CheckName: .Name, Status: .Status}'
server$ curl -s "http://localhost:8500/v1/health/service/web-service?passing=1" | \
jq '[{ "targets": [ .[] | "\(.Service.Address):\(.Service.Port)" ], "labels": { "job": "web-service" } }]' \
| tee /etc/prometheus/targets.json
Удаление сервиса
server$ ID=web-app-N
server$ curl --request PUT http://localhost:8500/v1/agent/service/deregister/$ID
Пример с node-exporter
server$ curl --request PUT \
--data '{
"ID": "node-exporter-gate",
"Name": "node-exporter",
"Address": "192.168.X.1",
"Port": 9100
}' \
http://server:8500/v1/agent/service/register
clientN:~# crontab -l
@reboot sleep 10; /usr/local/bin/register-in-consul.sh
#!/bin/bash
IP_ADDRESS=$(ip route get 1 | awk '{print $7; exit}')
HOSTNAME=$(hostname)
SERVICE_ID="node-exporter-$HOSTNAME"
curl --request PUT \
--header "Content-Type: application/json" \
--data "{
\"ID\": \"$SERVICE_ID\",
\"Name\": \"node-exporter\",
\"Address\": \"$IP_ADDRESS\",
\"Port\": 9100,
\"Meta\": {
\"hostname\": \"$HOSTNAME\"
}
}" \
http://server:8500/v1/agent/service/register
Пример с windows-exporter
PS> mkdir C:\Scripts
PS> notepad C:\Scripts\register-in-consul.ps1
$Hostname = [System.Net.Dns]::GetHostName()
$ServiceId = "windows-exporter-$Hostname"
$IpAddress = (Get-NetIPAddress -AddressFamily IPv4 |
Where-Object { $_.InterfaceAlias -notlike "*Loopback*" -and $_.IPAddress -notlike "169.254.*" } |
Select-Object -First 1).IPAddress
$Body = @{
ID = $ServiceId
Name = "windows-exporter"
Address = $IpAddress
Port = 9182
Meta = @{
hostname = $Hostname
}
} | ConvertTo-Json -Compress
$Uri = "http://server:8500/v1/agent/service/register"
Invoke-RestMethod -Method Put -Uri $Uri -ContentType "application/json" -Body $Body
PS> PowerShell.exe -ExecutionPolicy Bypass -File C:\Scripts\register-in-consul.ps1
PS Admin>
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File C:\Scripts\register-in-consul.ps1"
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Trigger.Delay = "PT10S"
Register-ScheduledTask -TaskName "RegisterInConsul" -Action $Action -Trigger $Trigger -User "NT AUTHORITY\SYSTEM" -Force
PS Admin> Get-ScheduledTask -TaskName "RegisterInConsul"