Prometheus+Ansible+Consul实现服务发现

news/2024/5/18 14:09:46 标签: prometheus, ansible, consul, consul服务发现

一、简介


1、Consul简介

  • Consul 是基于 GO 语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能。Consul 提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能。

  • 在没有使用 consul 服务自动发现的时候,我们需要频繁对 Prometheus 配置文件进行修改,无疑给运维人员带来很大的负担。引入consul之后,只需要在consul中维护监控组件配置,prometheus就能够动态发现配置

2、实验环境

IP操作系统安装服务
172.18.200.52ubuntu 22.04.1Docker、Prometheus、Grafana、Consul
172.18.200.53ubuntu 22.04.1node-exporter

二、安装Consul


1、配置docker-compose.yml

# cat docker-compose.yml
version : '3'
services:
  consul:
    image: consul:1.15
    container_name: consul
    hostname: consul
    volumes:
      - ./consul/config:/consul/config
      - ./consul/data:/consul/data/
    ports:
      - 8500:8500
    command: ["consul","agent","-dev","-bootstrap","-config-dir","/consul/config","-data-dir","/consul/data","-ui","-log-level","INFO","-bind","127.0.0.1","-client","0.0.0.0"]

consul_33">2、启动consul

# docker-compose up -d

3、浏览器访问

http://172.18.200.52:8500

在这里插入图片描述

三、配置Ansible


1、安装

# apt-get install ansible

2、修改配置

# cat /etc/ansible/ansible.cfg
[defaults]
#host_key_checking = False
#error_on_undefined_vars = True
#timeout = 60
#inventory = inventory.tmp
#roles_path = /conjurinc
#remote_tmp = /tmp
host_key_checking = False
log_path = /var/log/ansible.log

ansibleplaybook_65">四、ansible-playbook编写


1、查看目录结构

# tree ./
# tree ./
./
├── inventory
│   └── hosts
├── node_exporter_roles.yml
└── roles
    ├── node-exporter
    │   ├── files
    │   │   └── node_exporter-1.6.1.linux-amd64.tar.gz
    │   ├── handlers
    │   │   └── main.yml
    │   ├── tasks
    │   │   └── main.yml
    │   └── templates
    │       └── node_exporter.service.j2
    └── register
        ├── files
        │   └── consul_register.sh
        └── tasks
            ├── main.yml
            └── register.yml

2、配置hosts

# cat inventory/hosts
[linux]
172.18.200.53 name=linux-172.18.200.53

[linux:vars]
consul_ip=172.18.200.52
consul_port=8500
node_exporter_port=9100

3、配置node_exporter_roles.yml

# cat node_exporter_roles.yml
- hosts: linux
  gather_facts: no
  roles:
    - role: node-exporte

4、配置roles/node-exporter

(1)下载exporter

下载地址:https://github.com/prometheus/node_exporter/releases/tag/v1.6.1

在这里插入图片描述

(2)配置handlers
# cat roles/node-exporter/handlers/main.yml
- name: restart node exporter service
  systemd:
    name: node_exporter
    state: restarted
    daemon-reload: yes

- include: roles/register/tasks/register.ymlr
(3)配置tasks
# cat roles/node-exporter/tasks/main.yml
- name: push node_exporter
  unarchive:
    src: node_exporter-1.6.1.linux-amd64.tar.gz
    dest: /usr/local

- name: rename
  shell: |
    cd /usr/local
    if [ ! -d node_exporter ]
      then mv node_exporter-1.6.1.linux-amd64 node_exporter
    fi

- name: copy node_exporter systemd
  template:
    src: node_exporter.service.j2
    dest: /usr/lib/systemd/system/node_exporter.service
  notify: restart node exporter service

- name: start node_exporter
  systemd:
    name: node_exporter
    state: started
    enabled: yes
    daemon-reload: yes

- include: roles/register/tasks/main.yml
(4)配置templates

node_exporter_port:端口可以进行配置

# cat roles/node-exporter/templates/node_exporter.service.j2
[Unit]
Description=node_exporter

[Service]
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:{{ node_exporter_port }}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

5、配置roles/register

(1)配置files
# cat roles/register/files/consul_register.sh
#!/bin/bash

instance_id=$1
service_name=$2
ip=$3
port=$4
consul_ip=$5
consul_port=$6

curl -X PUT -d '{"id": "'"$instance_id"'","name": "'"$service_name"'","address": "'"$ip"'","port": '"$port"',"tags": ["'"$service_name"'"],"checks": [{"http": "http://'"$ip"':'"$port"'","interval": "5s"}]}' http://$consul_ip:$consul_port/v1/agent/service/register
(2)配置tasks

name:ansible hosts的name
group_names[0]:组名,如果属于children,那么就是group_names[1]
inventory_hostname:ansible hosts的ip
node_exporter_port:node_exporter的端口,默认9100
consul_ip:consule服务的ip
consul_port:consule服务的端口

# cat roles/register/tasks/main.yml
- name: push consul_register.sh
  copy:
    src: roles/register/files/consul_register.sh
    dest: /usr/local/bin

- include: roles/register/tasks/register.yml
# cat roles/register/tasks/register.yml
- name: register nodes into consul
  shell: /bin/bash /usr/local/bin/consul_register.sh {{ name }} {{ group_names[0] }} {{ inventory_hostname }} {{ node_exporter_port }} {{ consul_ip }} {{ consul_port }}

五、修改Prometheus配置


prometheusyml_215">1、配置prometheus.yml

services中的linux:ansible hosts文件中的group名字
这里的services为列表,所有可以添加多个不同组的服务器进来,也实现了分组

# cat prometheus/conf/prometheus.yml
...
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "linux"
    consul_sd_configs:
      - server: 172.18.200.52:8500
        services: ['linux']

2、重启

# docker restart prometheus

六、执行并添加Grafana


ansibleplaybook_236">1、执行ansible-playbook命令

# ansible-playbook -i inventory/hosts node_exporter_roles.yml

2、查看Consul

在这里插入图片描述

3、添加Grafana

模板id:9276

在这里插入图片描述


http://www.niftyadmin.cn/n/5177494.html

相关文章

HTML5学习系列之主结构

HTML5学习系列之主结构 前言HTML5主结构定义页眉定义导航定义主要区域定义文章块定义区块定义附栏定义页脚 具体使用总结 前言 学习记录 HTML5主结构 定义页眉 head表示页眉&#xff0c;用来表示标题栏&#xff0c;引导和导航作用的结构元素。 <header role"banner…

【PG】PostgreSQL 预写日志(WAL)、checkpoint、LSN

目录 预写式日志&#xff08;WAL&#xff09; WAL概念 WAL的作用 WAL日志存放路径 WAL日志文件数量 WAL日志文件存储形式 WAL日志文件命名 WAL内容 检查点&#xff08;checkpoint&#xff09; 1 检查点概念 2 检查点作用 触发检查点 触发检查点之后数据库操作 设置合…

STM32F4 GPIO端口二极管作用——二极管钳位作用

如上图所示&#xff0c;有两个保护二极管&#xff0c;用于保护内部电路&#xff0c;防止I\O引脚外部过高或者过低的电压输入时造成内部电路损坏。 具体来讲&#xff1a;当引脚输入电压高于VDD时&#xff0c;上面的二极管导通&#xff0c;输入点电压被钳位到约VDD0.7V&#xff…

Cesium 展示——改变点与线的关联关系后可实现对点或线的单独操作

文章目录 需求分析1. 实现区域选中状态(更改前)2. 循环遍历实体判断该区域内的实体(更改前)1. 将每一条线和线所对应的两个点进行关联(更改后的逻辑)2. 将每一个点和所对应的两条线进行关联(更改后的逻辑)3. 在新增点后修改【线、点】【点、线】间的关联关系(更改后的…

2023年【汽车驾驶员(高级)】证考试及汽车驾驶员(高级)实操考试视频

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 汽车驾驶员&#xff08;高级&#xff09;证考试考前必练&#xff01;安全生产模拟考试一点通每个月更新汽车驾驶员&#xff08;高级&#xff09;实操考试视频题目及答案&#xff01;多做几遍&#xff0c;其实通过汽车…

COCO数据集下载

文章目录 COCO官网貌似全部失效百度网盘提取码一直是1152 COCO官网 官网下载 train2017.zip annotations_trainval2017.zip val2017.zip stuff_annotations_trainval2017.zip test2017.zip image_info_test2017.zip 貌似全部失效 百度网盘提取码一直是1152 stuff_annotatio…

基于果蝇算法优化概率神经网络PNN的分类预测 - 附代码

基于果蝇算法优化概率神经网络PNN的分类预测 - 附代码 文章目录 基于果蝇算法优化概率神经网络PNN的分类预测 - 附代码1.PNN网络概述2.变压器故障诊街系统相关背景2.1 模型建立 3.基于果蝇优化的PNN网络5.测试结果6.参考文献7.Matlab代码 摘要&#xff1a;针对PNN神经网络的光滑…

IDEA 2023搭建 SpringMVC +FreeMarker+JDBC

1.IDEA的版本&#xff0c;目前最新是2023&#xff0c;要选择旗舰版。笔者曾选择社区版&#xff0c;发现少了很多功能。只能重新安装。 2.安装好以后的第1件事&#xff0c;是设置Maven&#xff0c;并将下载地址改为淘定站&#xff0c;参照这篇一次包会——最新IDEA配置Maven指南…