Prometheus监控的搭建(ansible安装——超详细)

news2024/9/25 9:35:13

目录

1.各组件功能介绍

2.安装批量部署工具ansbile

3.执行服务器

4.各服务器间做免密

5.下载安装包

5.1Prometheus的下载的下载地址

5.2exporter的下载地址

5.3grafana的下载地址

6.编辑ansible需要的配置文件

7.编写ansible文件

8.验证执行结果


今天和大家分享一下搭建Prometheus的方法,搭建Prometheus实现监控一共需要三个组件,他们分别是Prometheus、grafana、exporter。如果需要实现报警功能,还需要装Alertmanager组件。目前测试了麒麟V10、Centos7、Ubuntu18、Ubuntu20版本,都可以跑通,即使跑不通稍微修改也可以正常跑通,跑不通的可以私信我。不是基于docker跑的,所以说大部分环境都可以跑通。按照我的步骤跑不通,你打我,哈哈哈哈哈。

1.各组件功能介绍

Prometheus:

作用:Prometheus 是一种开源的系统监控和警报工具包,最初由SoundCloud开发。它主要用于收集和存储系统和服务的时间序列数据(metrics),并提供强大的查询语言(PromQL)用于分析这些数据。Prometheus 支持多种数据模型,适用于动态的服务发现和标签化的时间序列数据。

Exporter:

作用:Exporter 是一种用于从现有系统和服务中获取指标数据并将其转换为 Prometheus 格式的工具。Exporter 可以是官方支持的,也可以是社区或第三方开发的,用于监控各种不同类型的系统(如数据库、Web 服务器、消息代理等)。Exporter 通过暴露 HTTP 端点或其他形式的接口,允许 Prometheus 定期抓取和存储这些系统的指标数据。

Grafana:

作用:Grafana 是一个开源的数据可视化和监控平台,用于展示和分析 Prometheus 或其他数据源中的指标数据。Grafana 提供了丰富的图表和仪表盘编辑功能,用户可以根据需要创建个性化的监控仪表盘,并支持多种数据源的数据整合和展示。除了图表展示外,Grafana 还支持警报功能,可以根据设定的阈值条件触发警报通知。

Alertmanager:

Alertmanager 可以根据配置的路由规则,将报警通知发送到指定的接收端,如电子邮件、Slack、PagerDuty 等。

2.安装批量部署工具ansbile

3.执行服务器

主机名主机ip部署服务
host1192.168.1.11exporter、prometheus
host2192.168.1.12exporter、grafana

4.各服务器间做免密

promethus与所有服务器做免密(包括自身也需要做)

[root@host1 ~]# ssh-keygen -t rsa -b 4096

[root@host1 ~]# ssh-copy-id 192.168.1.11

[root@host1 ~]# ssh-copy-id 192.168.1.12

5.下载安装包

可以去官网下载

也可以去清华园下载 

5.1Prometheus的下载的下载地址

wget https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/LatestRelease/prometheus-2.49.1.linux-amd64.tar.gz

5.2exporter的下载地址

wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz

5.3grafana的下载地址

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3.linux-amd64.tar.gz

##这些文件都需要拉到/tmp目录下并且修改文件名为:

grafana-enterprise-10.2.3.linux-amd64.tar.gz

node_exporter.tar.gz

prometheus.tar.gz

##如果不修改文件名修改yaml文件中对应的名字

6.编辑ansible需要的配置文件

[root@host1 ~]# vim host

[prometheus_node]

192.168.1.11

[all]

192.168.1.11

192.168.1.12

[prometheus_grafana]

192.168.1.12

[root@host1 ~]# vim prometheus.yml.j2

global:

  scrape_interval: 15s

  evaluation_interval: 15s

alerting:

  alertmanagers:

    - static_configs:

        - targets: []

scrape_configs:

  - job_name: "prometheus"

    static_configs:

      - targets:

          - "192.168.1.11:9090"

  - job_name: "node_exporter"

    static_configs:

      - targets:

          - "192.168.1.11:9100"

          - "192.168.1.12:9100"

###有其他exporter,写入其下面就可 

7.编写ansible文件

 [root@host1 ~]# vim prometheus.yaml

---
- name: Install and configure Prometheus and Node Exporter
  hosts: all
  become: yes
  tasks:
    - name: Create a user for Prometheus
      user:
        name: prometheus
        shell: /sbin/nologin

    - name: Create directories for Prometheus
      file:
        path: "{{ item }}"
        state: directory
        owner: prometheus
        group: prometheus
        mode: '0755'
      with_items:
        - /etc/prometheus
        - /var/lib/prometheus

- name: Install Prometheus on the Prometheus node
  hosts: prometheus_node
  become: yes
  tasks:
    - name: Extract Prometheus binary
      unarchive:
        src: /tmp/prometheus.tar.gz
        dest: /tmp
        remote_src: yes

    - name: Move Prometheus binaries to the proper location
      shell: |
        mv /tmp/prometheus-2.37.1.linux-amd64/prometheus /usr/local/bin/
        mv /tmp/prometheus-2.37.1.linux-amd64/promtool /usr/local/bin/
      become: yes

    - name: Move Prometheus configuration files
      ansible.builtin.copy:
        src: "{{ item }}"
        dest: "/etc/prometheus/"
        owner: prometheus
        group: prometheus
        remote_src: yes
      loop:
        - "/tmp/prometheus-2.37.1.linux-amd64/consoles"
        - "/tmp/prometheus-2.37.1.linux-amd64/console_libraries"
        - "/tmp/prometheus-2.37.1.linux-amd64/prometheus.yml"

    - name: Ensure Prometheus is running as a service
      copy:
        content: |
          [Unit]
          Description=Prometheus
          Wants=network-online.target
          After=network-online.target

          [Service]
          User=prometheus
          ExecStart=/usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus
          Restart=always

          [Install]
          WantedBy=multi-user.target
        dest: /etc/systemd/system/prometheus.service
        owner: root
        group: root
        mode: '0644'

    - name: Reload systemd to pick up Prometheus service
      command: systemctl daemon-reload

    - name: Enable Prometheus service
      systemd:
        name: prometheus
        enabled: yes

    - name: Start Prometheus service
      systemd:
        name: prometheus
        state: started

- name: Install Node Exporter on all nodes
  hosts: all
  become: yes
  tasks:

    - name: Copy node_exporter.tar.gz to target host
      ansible.builtin.copy:
        src: /tmp/node_exporter.tar.gz
        dest: /tmp/node_exporter.tar.gz

    - name: Extract Node Exporter binary
      unarchive:
        src: /tmp/node_exporter.tar.gz
        dest: /tmp
        remote_src: yes

    - name: Move Node Exporter binary to the proper location
      command: mv /tmp/node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin/

    - name: Ensure Node Exporter is running as a service
      copy:
        content: |
          [Unit]
          Description=Node Exporter
          Wants=network-online.target
          After=network-online.target

          [Service]
          User=root
          ExecStart=/usr/local/bin/node_exporter
          Restart=always

          [Install]
          WantedBy=multi-user.target
        dest: /etc/systemd/system/node_exporter.service
        owner: root
        group: root
        mode: '0644'

    - name: Reload systemd to pick up Node Exporter service
      command: systemctl daemon-reload

    - name: Enable Node Exporter service
      systemd:
        name: node_exporter
        enabled: yes

    - name: Start Node Exporter service
      systemd:
        name: node_exporter
        state: started

- name: Install and configure Grafana Enterprise
  hosts: prometheus_grafana
  become: yes
  tasks:
    - name: Copy grafana-enterprise-10.2.3.linux-amd64.tar.gz  to target host
      ansible.builtin.copy:
        src: /tmp/grafana-enterprise-10.2.3.linux-amd64.tar.gz
        dest: /tmp/grafana-enterprise-10.2.3.linux-amd64.tar.gz

    - name: Extract Grafana Enterprise tarball
      ansible.builtin.unarchive:
        src: /tmp/grafana-enterprise-10.2.3.linux-amd64.tar.gz
        dest: /usr/local/
        creates: /usr/local/grafana-v10.2.3

    - name: Rename Grafana directory
      ansible.builtin.command:
        argv:
          - mv
          - /usr/local/grafana-v10.2.3
          - /usr/local/grafana
        creates: /usr/local/grafana

    - name: Create Grafana systemd service file
      ansible.builtin.copy:
        content: |
          [Unit]
          Description=Grafana instance
          After=network.target

          [Service]
          Type=simple
          WorkingDirectory=/usr/local/grafana/
          ExecStart=/usr/local/grafana/bin/grafana-server
          Restart=always

          [Install]
          WantedBy=multi-user.target
        dest: /etc/systemd/system/grafana.service
      notify:
        - restart grafana

  handlers:
    - name: restart grafana
      ansible.builtin.systemd:
        name: grafana
        state: restarted

- name: Backup and Modify Prometheus configuration
  hosts: prometheus_node
  become: yes
  tasks:
    - name: Backup original prometheus.yml
      ansible.builtin.copy:
        src: /etc/prometheus/prometheus.yml
        dest: /etc/prometheus/prometheus.yml.bak
      register: backup_result
      changed_when: backup_result.changed
      become: yes
      become_method: sudo

    - name: Ensure backup completed successfully
      assert:
        that:
          - backup_result.changed
        fail_msg: "Failed to backup prometheus.yml"
        success_msg: "Backup of prometheus.yml completed successfully"

    - name: Replace prometheus.yml configuration
      ansible.builtin.template:
        src: /root/prometheus.yml.j2
        dest: /etc/prometheus/prometheus.yml
      notify: restart prometheus

  handlers:
    - name: restart prometheus
      ansible.builtin.systemd:
        name: prometheus
        state: restarted

[root@host1 ~]# ansible-playbook -i host.txt  prometheus.yaml

8.验证执行结果

[root@host1 ~]# netstat -antup|grep 9100

[root@host1 ~]# netstat -antup|grep 9090

访问192.168.1.11:9090和192.168.1.12:3000

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1975893.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

网站在线查询工具箱源码分享

终极网络工具系统”(SAAS),是一款功能强大的PHP脚本在线查询工具。本版集合了超过470种快速且易用的Web工具,为日常任务处理和开发人员提供了极大的便利。作为一款综合性的网络工具系统,66toolkit不仅满足了用户的基本网络需求,更…

Java面试题 -- 为什么重写equals就一定要重写hashcode方法

在回答这个问题之前我们先要了解equals与hascode方法的本质是做什么的 1. equals方法 public boolean equals(Object obj) {return (this obj);}我们可以看到equals在不重写的情况下是使用判断地址值是否相同 所以默认的 equals 的逻辑就是判断的双方是否引用了一个对象&am…

【EI会议征稿】第四届高性能计算与通信工程国际学术会议(HPCCE 2024)

出版出版 【SPIE出版 | 往届会后3个月内完成EI检索】 第四届高性能计算与通信工程国际学术会议(HPCCE 2024) 2024 4th International Conference on High Performance Computing and Communication 第四届高性能计算与通信工程国际学术会议(HPCCE 2024&#xf…

使用Chainlit接入通义千问快速实现一个自然语言转sql语言的智能体

文本到 SQL 让我们构建一个简单的应用程序,帮助用户使用自然语言创建 SQL 查询。 最终结果预览 ​ 先决条件 此示例有额外的依赖项。你可以使用以下命令安装它们: pip install chainlit openai​ 导入 应用程序 from openai import AsyncOpenAI…

扩展------零拷贝技术(Mmap,SendFile)

什么是零拷贝 零拷贝(Zero-Copy)是一种计算机操作技术,旨在减少数据在内存之间的拷贝次数,以提高数据传输的效率和性能。 传统的IO模式: 模拟网络传输数据运行过程: 用户态read()发起系统调用&#xff0c…

Flink中上游DataStream到下游DataStream的内置分区策略及自定义分区策略

目录 全局分区器GlobalPartitioner 广播分区器BroadcastPartitioner 哈希分区器BinaryHashPartitioner 轮询分区器RebalancePartitioner 重缩放分区器RescalePartitioner 随机分区器ShufflePartitioner 转发分区器ForwardPartitioner 键组分区器KeyGroupStreamPartitio…

力扣SQL50 第二高的薪水 ifnull() 分页

Problem: 176. 第二高的薪水 👨‍🏫 参考题解 Code select ifNull((select distinct salaryfrom employeeorder by salary desclimit 1,1),null) as SecondHighestSalary

【Python数据结构与算法】分治----汉诺塔问题

题目:汉诺塔问题 描述 古代有一个梵塔,塔内有三个座A、B、C,A座上有n个盘子,盘子大小不等,大的在下,小的在上。三个座都可以用来放盘子。有一个和尚想把这n个盘子从A座移到C座,但每次只能允许移…

AWS SES 认证策略设置全攻略:轻松掌握简单步骤!

最近,我有机会设置 Amazon Simple Email Service(以下简称:SES)的认证策略,所以这次写下来作为备忘。 前言 Amazon Simple Email Service(SES)是一项通过 API 端点或 SMTP 接口进行邮件发送的服…

MySQL:VIEW视图

概述 MySQL 视图(View)是一种虚拟存在的表,同真实表一样,视图也由列和行构成,但视图并不实际存在于数据库中。行和列的数据来自于定义视图的查询中所使用的表,并且是在使用视图时动态生成的。 数据库中只…

从Notion Sites的推出,分析SaaS服务发展浪潮

引言 前段时间,Notion发布了新功能“Notion Sites”,允许用户直接在Notion中编辑页面并将其作为网站发布。其实在此之前,一些SaaS(软件即服务)软件也具有该功能,比如HelpLook AI知识库、Squarespace、Wix等…

buu做题(13)

[BSidesCF 2019]Kookie 给了一个账户: cookie / monster 根据提示, 我们需要以 admin 的身份登录 抓个包 , 可以发现一个奇怪的地方, Set-Cookie: usernamecookie; 以这样的方式确定登录的用户, 尝试伪造一下 直接 加上一个请求头: Cookie:usernameadmin 就可以得到flag 也…

卡码网--数组篇(二分法)

系列文章目录 文章目录 系列文章目录前言数组二分查找 前言 详情看:https://programmercarl.com/ 总结知识点用于复习 数组 概念: 数组是存放在连续内存空间上的相同类型数据的集合。 数组可以方便的通过下标索引的方式获取到下标对应的数据。 特点:…

图欧资源站与AI站23年5月~24年5月一年更新日志大汇总!

Hello,大家好,我是图欧君,很久没上CSDN啦,来跟大家一口气盘点一下我和我们团队从2023年5月到2024年5月以来都干了些什么大事吧~本文超长!流量预警!建议在WIFI环境下观看! 别眨眼,三&…

python实现图像分割算法4

python实现流域变换算法 算法原理基本步骤数学模型Python实现详细解释优缺点应用领域流域变换(Watershed Transform)算法是一种用于图像分割的技术,特别适用于分割重叠和相邻的对象。它的基本思想是将图像视为拓扑表面,通过模拟水的流动来分割区域。流域变换广泛应用于医学…

Hadoop学习(三)

一、MapReduce框架原理 1.1InputFormat数据输入 MapTask并行度决定机制 1)数据块(HDFS存储数据单位),物理上把数据分成一块一块 2)数据切片(MapReduce程序计算输入数据的单位):只是在逻辑上…

2.MySQL库的操作

创建数据库 创建数据库的代码: CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,create_specification] ...];​create_specification:[DEFAULT] CHARACTER SET charset_name[DEFAULT] COLLATE collation_name 说明: 大写的表示关键…

21天学通C++:理解函数对象、Lambda表达式

第二十一章:理解函数对象 函数对象(也叫 functor)。 函数对象与谓词的概念 从概念上说,函数对象是用作函数的对象; 但从实现上说,函数对象是实现了 operator() 的类的对象。 虽然函数和函数指针也可归…

数据结构之八大排序(下)

找往期文章包括但不限于本期文章中不懂的知识点: 个人主页:我要学编程(ಥ_ಥ)-CSDN博客 所属专栏:数据结构(Java版) 数据结构之八大排序(上)-CSDN博客 上面博客讲述了另外六中排序算法。 目…

仓颉 -- 标识符 , 变量以及数据类型详解

仓颉 – 标识符 , 变量以及数据类型 一. 标识符 1. 普通标识符 由数字 , 字母 , 下划线构成 – cangjie , cangjie_2024由英文字母开头,后接零至多个英文字母、数字或下划线。由一至多个下划线开头,后接一个英文字母,最后可接零至多个英文…