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 13:42] (current)
val [Пример с node-exporter]
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]]
 +<​code>​
 +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"​
 +</​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>​
hashicorp_consul.1793519911.txt.gz · Last modified: 2026/11/01 10:58 by val · Currently locked by: val