User Tools

Site Tools


hashicorp_consul

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
hashicorp_consul [2026/11/01 10:58]
val created
hashicorp_consul [2026/11/01 15:14] (current)
val
Line 1: Line 1:
 ====== Hashicorp Consul ====== ====== Hashicorp Consul ======
  
 +==== Запуск ====
 +<​code>​
 +server# docker run -d --name consul-dev -p 8500:8500 hashicorp/​consul:​2.1.0 agent -dev -client=0.0.0.0
 +</​code>​
 +==== Добавление и мониторинг web сервисов ====
 +  * [[Язык программирования Python#​Доступ к каталогу по http]] на gate
 +<​code>​
 +$ 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}'​
 +</​code>​
 +  * [[Сервис Prometheus]]
 +<​code>​
 +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
 +</​code>​
 +
 +==== Удаление сервиса ====
 +<​code>​
 +server$ ID=web-app-N
 +
 +server$ curl --request PUT http://​localhost:​8500/​v1/​agent/​service/​deregister/​$ID
 +</​code>​
 +==== Пример с node-exporter ====
 +
 +  * Prometheus [[Сервис Prometheus#​Service Discovery]]
 +
 +<​code>​
 +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
 +</​code><​code>​
 +@reboot sleep 10; /​usr/​local/​bin/​register-in-consul.sh
 +</​code><​code>​
 +#!/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
 +</​code>​
 +
 +==== Пример с windows-exporter ====
 +
 +<​code>​
 +PS> mkdir C:\Scripts
 +
 +PS> notepad C:​\Scripts\register-in-consul.ps1
 +</​code><​code>​
 +$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
 +</​code><​code>​
 +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"​
 +</​code>​
hashicorp_consul.1793519911.txt.gz · Last modified: 2026/11/01 10:58 by val