zoukankan      html  css  js  c++  java
  • prometheus监控之自动发现

    prometheus监控之自动发现,这里采用服务端添加配置文件,具体操作如下,目前prometheus server只有如下节点:

     现在开始添加配置文件:

    1.首先创建存放配置文件的目录:

    # mkdir /usr/local/prometheus/target/node/ -p

    2.然后在prometheus server配置文件中定义file类型:

      - job_name: 'host_discovery'
        file_sd_configs:
        - files:
          - "/usr/local/prometheus/target/node/host_discovery.json"
          refresh_interval: 6s

    3.添加targets到json配置文件中:

    [root@master node]# cat host_discovery.json 
    [{
        "targets": ["172.16.23.121:9100"],
        "labels": {
            "instance": "172.16.23.121",
            "role": "node1"
        }
    }]

    上面双引号一定要注意不要使用单引号,不然日志会出现如下报错:

    [root@master node]# tail -f /var/log/messages
    Sep 22 23:06:50 master prometheus: level=error ts=2020-09-22T15:06:50.849Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"
    Sep 22 23:06:50 master prometheus: level=error ts=2020-09-22T15:06:50.849Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"
    Sep 22 23:06:50 master prometheus: level=error ts=2020-09-22T15:06:50.849Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"
    Sep 22 23:06:54 master prometheus: level=error ts=2020-09-22T15:06:54.378Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"
    Sep 22 23:06:54 master prometheus: level=error ts=2020-09-22T15:06:54.866Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"
    Sep 22 23:07:00 master prometheus: level=error ts=2020-09-22T15:07:00.378Z caller=file.go:344 component="discovery manager scrape" discovery=file msg="Error reading file" path=/usr/local/prometheus/target/node/host_discovery.json err="invalid character '\'' looking for beginning of value"

    4.配置完成后,最后校验prometheus server配置文件,校验没问题后就加载配置文件:

    # /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
    # curl -X POST http://172.16.23.120:9090/-/reload

    5.刷新控制台,查看targets节点:

     接下来通过向json文件host_discovery.json增加节点,然后不加载prometheus,刷新控制台看效果:

    [root@master node]# cat host_discovery.json 
    [{
        "targets": ["172.16.23.121:9100","172.16.23.122:9100"],
        "labels": {
            "instance": "172.16.23.121",
            "role": "node1"
        }
    }]

    刷新prometheus控制台:

     可以看见新增了122节点,但是label属性都是相同,这种明显不是我们希望看见的,所以继续修改:

    [root@master node]# cat host_discovery.json 
    [{
        "targets": ["172.16.23.121:9100"],
        "labels": {
            "instance": "172.16.23.121",
            "role": "node1"
        }
    },
    {
        "targets": ["172.16.23.122:9100"],
        "labels": {
            "instance": "172.16.23.122",
            "role": "node2"
        }
    }]

    上面配置就是一个targets对应自己私有的label,然后刷新控制台:

     当然基于配置文件发现的方式除了json文件,yaml文件也是可以,操作如下:

    [root@master node]# cat host_discovery.yml 
    - targets:
      - "172.16.23.121:9100"
      labels:
        instance: "172.16.23.121"

    然后重载prometheus server:

    # curl -X POST http://172.16.23.120:9090/-/reload

    刷新控制台:

     继续向yml配置文件添加节点:

    [root@master node]# cat host_discovery.yml 
    - targets:
      - "172.16.23.121:9100"
      labels:
        instance: "172.16.23.121"
    - targets:
      - "172.16.23.122:9100"
      labels:
        instance: "172.16.23.122"

    然后刷新控制台:

  • 相关阅读:
    Github 上 36 个最实用的 Vue 开源库
    C 语言快速入门,21 个小项目足矣!「不走弯路就是捷径」
    18个挑战项目带你快速入门深度学习
    Linux 运维入门到跑路书单推荐
    Python 网络爬虫的常用库汇总
    45 个常用Linux 命令,让你轻松玩转Linux!
    [新手必备]Python 基础入门必学知识点笔记
    快速入门 Python 数据分析实用指南
    18位不重复订单号
    相对路径转绝对路径
  • 原文地址:https://www.cnblogs.com/jsonhc/p/13715587.html
Copyright © 2011-2022 走看看