User Tools

Site Tools


hashicorp_consul

This is an old revision of the document!


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 сервисов

server$ curl --request PUT \
  --url http://localhost:8500/v1/agent/service/register \
  --header 'Content-Type: application/json' \
  --data '{
    "ID": "web-app-N",
    "Name": "web-service",
    "Address": "192.168.X.10",
    "Port": 8N,
    "Check": {
      "Name": "Web App HTTP Check",
      "HTTP": "http://192.168.X.10:8N/",
      "Interval": "10s",
      "Timeout": "1s"
    }
  }'
...

server$ curl -s "http://localhost:8500/v1/health/service/web-service"

server$ curl -s "http://localhost:8500/v1/health/service/web-service?passing=1"
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$ ###curl --request PUT http://localhost:8500/v1/agent/service/deregister/web-app-N
...

Пример с 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:~# cat register.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
hashicorp_consul.1793524372.txt.gz · Last modified: 2026/11/01 12:12 by val · Currently locked by: val