192.168.10.14 prometheus、consul
192.168.10.100 各類服務
一、基於Consul的服務發現
Consul 是由 HashiCorp 開發的一個支援多資料中心的分散式服務發現和鍵值對儲存服務的開源軟體,是一個通用的服務發現和註冊中心工具,被大量應用於基於微服務的軟體架構當中。
1.1 二進位制安裝consul(二選一)
在頁面 https://www.consul.io/downloads 下載符合自己系統的安裝檔案,比如我們這裡是 Linux 系統,使用下面命令下載安裝即可:
wget https://releases.hashicorp.com/consul/1.18.1/consul_1.18.1_linux_amd64.zip apt install unzip -y unzip consul_1.18.1_linux_amd64.zip mv consul /usr/local/bin consul version # 啟動consul 為了檢視更多的日誌資訊,我們可以在 dev 模式下執行 Consul,如下所示: consul agent -dev -client 0.0.0.0 啟動命令後面使用 -client 引數指定了客戶端繫結的 IP 地址,預設為 127.0.0.1
1.2 docker安裝Consul(二選一)
# docker 執行 docker run -d --name consul -p 8500:8500 consul:1.14.5 # consul http訪問地址 http://192.168.10.100:8500/ui/dc1/services
1.3 透過api註冊到Consul
# 第一種方式:使用命令註冊 curl -X PUT -d '{"id": "node1","name": "node_exporter","address": "node_exporter","port": 9100,"tags": ["exporter"],"meta": {"job": "node_exporter","instance": "Prometheus伺服器"},"checks": [{"http": "http://192.168.10.14:9100/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register # 第二種方式:把json資料放在檔案中,使用這個json檔案註冊 mkdir /data/consul cd /data/consul cat > node_exporter.json<<"EOF" { "id": "node2", "name": "node_exporter", "address": "192.168.10.100", "port": 9100, "tags": ["exporter"], "meta": { "job": "node_exporter", "instance": "test伺服器" }, "checks": [{ "http": "http://192.168.10.14:9100/metrics", "interval": "10s" }] } EOF curl --request PUT --data @node_exporter.json http://localhost:8500/v1/agent/service/register
consul
的服務,我們可以在瀏覽器中訪問 http://192.168.11.61:8500
檢視註冊的服務。在 Consul UI 頁面中可以看到有 consul
和 node_exporter
兩個 Service 服務。
1.4 配置Prometheus
在 Prometheus 的配置檔案 prometheus.yml
檔案中的 scrape_configs
部分新增如下所示的抓取配置:
備份原始檔:
cd /data/docker-prometheus cp -a prometheus/prometheus.yml prometheus/prometheus.yml.bak ls -l prometheus/prometheus.yml.bak
使用以下配置
cat > prometheus/prometheus.yml<<"EOF" # 全域性配置 global: scrape_interval: 15s # 將搜刮間隔設定為每15秒一次。預設是每1分鐘一次。 evaluation_interval: 15s # 每15秒評估一次規則。預設是每1分鐘一次。 # Alertmanager 配置 alerting: alertmanagers: - static_configs: - targets: ['alertmanager:9093'] # 報警(觸發器)配置 rule_files: - "alert.yml" - "rules/*.yml" # 搜刮配置 scrape_configs: - job_name: 'prometheus' # 覆蓋全域性預設值,每15秒從該作業中刮取一次目標 scrape_interval: 15s static_configs: - targets: ['localhost:9090'] - job_name: 'alertmanager' # 覆蓋全域性預設值,每15秒從該作業中刮取一次目標 scrape_interval: 15s static_configs: - targets: ['alertmanager:9093'] - job_name: 'consul_exporter' consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*exporter.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap # Spring Boot 2.x 應用資料採集配置 - job_name: 'consul_springboot_demo' metrics_path: '/actuator/prometheus' scrape_interval: 5s consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*springboot.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap #http配置 - job_name: "consul-blackbox_http" metrics_path: /probe params: module: [http_2xx] consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*blackbox_http.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [__meta_consul_service_address] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.10.100:9115 #tcp檢查配置 - job_name: "consul_blackbox_tcp" metrics_path: /probe params: module: [tcp_connect] consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*blackbox_tcp.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [__meta_consul_service_address] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.10.100:9115 #icmp檢查配置 - job_name: "consul_blackbox_icmp" metrics_path: /probe params: module: [icmp] consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*blackbox_icmp.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [__meta_consul_service_address] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.10.100:9115 #域名檢測 - job_name: consul_domain_exporter scrape_interval: 10s metrics_path: /probe consul_sd_configs: - server: '192.168.10.14:8500' services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*domain.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [__meta_consul_service_address] target_label: __param_target - target_label: __address__ replacement: 192.168.10.100:9222 EOF
透過 consul_sd_configs
配置用於自動發現的 Consul 服務地址,服務名為[],我們透過relabel_configs的過濾規則只接收指定的exporter
curl -X POST http://localhost:9090/-/reload
配置完成後重新啟動 Prometheus,然後重新檢視 Prometheus 頁面上的 targets
頁面,驗證上面的配置是否存在:
http://192.168.10.14:9090/targets?search=
正常情況下是可以看到會有一個 exporter
的任務,下面有 2 個自動發現的抓取目標。
1.5 建立新增指令碼
prometheus上使用預先準備好的指令碼,一次新增多個targets:
cat >/data/consul/api.sh <<"EOF" #nginx curl -X PUT -d '{"id": "nginx1","name": "nginx_exporter","address": "192.168.10.100","port": 9113,"tags": ["exporter"],"meta": {"job": "nginx_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9113/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #rabbitmq curl -X PUT -d '{"id": "rabbitmq1","name": "rabbitmq_exporter","address": "192.168.10.100","port": 9419,"tags": ["exporter"],"meta": {"job": "rabbitmq_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9419/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #redis curl -X PUT -d '{"id": "redis1","name": "redis_exporter","address": "192.168.10.100","port": 9121,"tags": ["exporter"],"meta": {"job": "redis_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9121/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #mongodb curl -X PUT -d '{"id": "mongodb1","name": "mongodb_exporter","address": "192.168.10.100","port": 9216,"tags": ["exporter"],"meta": {"job": "mongodb_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9216/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #mysql curl -X PUT -d '{"id": "mysql1","name": "mysqld_exporter","address": "192.168.10.100","port": 9104,"tags": ["exporter"],"meta": {"job": "mysqld_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9104/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #cadvisor curl -X PUT -d '{"id": "cadvisor1","name": "cadvisor","address": "cadvisor","port": 8080,"tags": ["exporter"],"meta": {"job": "cadvisor","instance": "Prometheus伺服器","env":"test"},"checks": [{"http": "http://192.168.11.61:8080/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id": "cadvisor2","name": "cadvisor","address": "192.168.10.100","port": 8080,"tags": ["exporter"],"meta": {"job": "cadvisor","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:8080/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #springboot curl -X PUT -d '{"id": "springboot1","name": "springboot","address": "192.168.10.100","port": 8081,"tags": ["springboot"],"meta": {"job": "springboot","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:8081/actuator/prometheus", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #process_exporter curl -X PUT -d '{"id": "process1","name": "process_exporter","address": "192.168.10.100","port": 9256,"tags": ["exporter"],"meta": {"job": "process_exporter","instance": "test伺服器","env":"test"},"checks": [{"http": "http://192.168.10.100:9256/metrics", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #http curl -X PUT -d '{"id": "http1","name": "blackbox_http","address": "https://www.jd.com","tags": ["blackbox_http"],"checks": [{"http": "http://192.168.10.100:9115", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #tcp curl -X PUT -d '{"id": "tcp1","name": "blackbox_tcp","address": "192.168.11.61:9090","tags": ["blackbox_tcp"],"checks": [{"http": "http://192.168.10.100:9115", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #icmp curl -X PUT -d '{"id": "icmp1","name": "blackbox_icmp","address": "192.168.10.100","tags": ["blackbox_icmp"],"checks": [{"http": "http://192.168.10.100:9115", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register #domin curl -X PUT -d '{"id": "domain1","name": "domain_exporter","address": "baidu.com","tags": ["domain"],"checks": [{"http": "http://192.168.10.100:9222", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register EOF
# 新增domin:id和adress需要修改
curl -X PUT -d '{"id": "domain2","name": "domain_exporter","address": "9you.com","tags": ["domain"],"checks": [{"http": "http://192.168.10.100:9222", "interval": "5s"}]}' http://localhost:8500/v1/agent/service/register
執行指令碼
sh /data/consul/api.sh # 檢查 http://192.168.10.14:9090/targets?search=
# 服務註冊
http://192.168.10.14:8500/ui/dc1/services
1.6 consul刪除服務
curl --request PUT http://127.0.0.1:8500/v1/agent/service/deregister/ID # 服務ID就是例如:"id": "domain2",id不能重複;微服務ID
二、ConsulManager
官方gitee地址
1、ConsulManager需要依賴Consul
,請先完成Consul的部署。(暫時最高支援Consul v1.14.5)(docs/Consul部署說明.md)
2、使用docker-compose
來部署ConsulManager
下載:wget https://starsl.cn/static/img/docker-compose.yml(倉庫根目錄下docker-compose.yml)
vim docker-compose.yml
version: "3.2"
services:
flask-consul:
image: swr.cn-south-1.myhuaweicloud.com/starsl.cn/flask-consul:latest
container_name: flask-consul
hostname: flask-consul
restart: always
volumes:
- /usr/share/zoneinfo/PRC:/etc/localtime
environment:
consul_token: xxxxx-xxxxx-xxxxx
consul_url: http://192.168.10.14:8500/v1
admin_passwd: password
log_level: INFO
nginx-consul:
image: swr.cn-south-1.myhuaweicloud.com/starsl.cn/nginx-consul:latest
container_name: nginx-consul
hostname: nginx-consul
restart: always
ports:
- "1026:1026"
volumes:
- /usr/share/zoneinfo/PRC:/etc/localtime
修改3個環境變數: consul_token:consul的登入token(如何獲取?),當然也可以不獲取token,這樣consul使用無密碼登入(不安全)。 consul_url:consul的URL(http開頭,/v1要保留) admin_passwd:登入ConsulManager Web的admin密碼 ● 啟動:docker-compose pull && docker-compose up -d ● 訪問:http://{IP}:1026,使用配置的變數 admin_passwd 登入 參考:https://github.com/starsliao/ConsulManager/blob/main/docs/FAQ.md
http://192.168.10.14:1026,賬號密碼:admin/password
可以透過管理服務,新增和刪除都行
新增