服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Centos - CentOS7搭建Prometheus 监控Linux主机

CentOS7搭建Prometheus 监控Linux主机

2021-01-08 23:19linux运维菜 Centos

prometheus可以拆分成多个节点进行指标收集。本文主要介绍CentOS7搭建Prometheus 监控Linux主机。

 简介

prometheus可以拆分成多个节点进行指标收集。

安装环境:CentOS7

CentOS7搭建Prometheus 监控Linux主机

安装prometheus

wget -c https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz 

tar zxvf prometheus-2.23.0.linux-amd64.tar.gz  -C /opt/ 

cd /opt/ 

ln -s prometheus-2.23.0.linux-amd64 prometheus 

cat > /etc/systemd/system/prometheus.service <<EOF 

[Unit] 

Description=prometheus 

After=network.target 

 

[Service] 

Type=simple 

WorkingDirectory=/opt/prometheus 

ExecStart=/opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml" 

LimitNOFILE=65536 

PrivateTmp=true 

RestartSec=2 

StartLimitInterval=0 

Restart=always 

 

[Install] 

WantedBy=multi-user.target 

EOF 

systemctl daemon-reload  

systemctl enable prometheus 

systemctl start prometheus 

 CentOS7搭建Prometheus 监控Linux主机

配置Prometheus

这里配置的是监听/opt/prometheus/servers/目录下的json文件

cat > /opt/prometheus/prometheus.yml <<EOF 

# my global config 

global

  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute

  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute

  # scrape_timeout is set to the global default (10s). 

# Alertmanager configuration 

alerting: 

  alertmanagers: 

  - static_configs: 

    - targets: 

      # - alertmanager:9093 

 

Load rules once and periodically evaluate them according to the global 'evaluation_interval'

rule_files: 

  # - "first_rules.yml" 

  # - "second_rules.yml" 

 

# A scrape configuration containing exactly one endpoint to scrape: 

# Here it's Prometheus itself. 

scrape_configs: 

  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. 

  - job_name: 'prometheus' 

 

    # metrics_path defaults to '/metrics' 

    # scheme defaults to 'http'

 

    static_configs: 

    - targets: ['localhost:9090'

     

  - job_name: 'servers' 

    file_sd_configs: 

    - refresh_interval: 61s 

      files: 

        - /opt/prometheus/servers/*.json 

EOF 

systemctl restart prometheus 

json格式

CentOS7搭建Prometheus 监控Linux主机

每个json文件需要是一个数组对象,如果不需要自定义标签,可以直接写到targets里面去也可以,可以有多个文件

[     

    { 

        "targets": [ 

            "192.168.1.164:9100" 

        ], 

        "labels": { 

            "instance""192.168.1.164"

            "job""node_exporter" 

        } 

    }, 

    { 

        "targets": [ 

            "192.168.1.167:9100" 

        ], 

        "labels": { 

            "instance""192.168.1.167"

            "job""node_exporter" 

        } 

    } 

安装node_exporter

安装到/opt/node_exporter路径下,保持默认的端口

https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz 

tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/ 

cd /opt/ 

ln -s  node_exporter-1.0.1.linux-amd64 node_exporter 

cat > /etc/systemd/system/node_exporter.service <<EOF 

[Unit] 

Description=node_exporter 

After=network.target 

 

[Service] 

Type=simple 

WorkingDirectory=/opt/node_exporter 

ExecStart=/opt/node_exporter/node_exporter 

LimitNOFILE=65536 

PrivateTmp=true 

RestartSec=2 

StartLimitInterval=0 

Restart=always 

 

[Install] 

WantedBy=multi-user.target 

EOF 

systemctl daemon-reload 

systemctl enable node_exporter 

systemctl start node_exporter 

图形展示

直接安装grafana进行展示

yum -y install   https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm 

systemctl enable grafana-server 

systemctl start grafana-server 

启动之后,grafana默认监听的是3000端口,直接使用浏览器进行访问就可以了,默认用户名密码是admin/admin,第一次登陆之后会提示修改。

CentOS7搭建Prometheus 监控Linux主机

配置数据源:鼠标左边的菜单 Configuration -> Data Source -> Add data source -> 选择prometheus -> url那栏填入prometheus的地址就可以了 -> 最后 Save & test 就可以了。

grafana.com/grafana/dashboards 官网已经有人做好的模板,我们直接import进来就可以了。

导入面板:鼠标左边的菜单 Dashboards -> Import -> 填入id -> Load -> 选择数据源就可以了。

我经常用的是:1860 、8919 这两个来查看node_exporter监控

CentOS7搭建Prometheus 监控Linux主机

总结

安装这些服务都是使用systemd进行管理的,操作起来比较方便的。

这里没有设置告警,可以根据自己的需要设置对应的告警规则,使用alertmanager进行告警。

原文地址:https://www.toutiao.com/i6914917162675192324/

延伸 · 阅读

精彩推荐
  • Centos使用Xshell连接Centos 6.6服务器操作图文教程

    使用Xshell连接Centos 6.6服务器操作图文教程

    这篇文章主要介绍了使用Xshell连接Centos 6.6服务器操作图文教程,本文用详细的操作步骤讲解了如何使用Xshell连接Centos服务器,需要的朋友可以参考下 ...

    脚本之家4032019-09-18
  • CentosCentOS6.2网卡设置

    CentOS6.2网卡设置

    如果你想让服务器可以更新、让网友访问你的LINUX服务器,那一定要设置LINUX网上给网卡绑定一个IP,下面我们就介绍CentOS6.2网卡设置IP的方法。...

    CentOS教程网10092021-10-03
  • CentosCentOS 最新版本git的安装教程

    CentOS 最新版本git的安装教程

    本文主要给大家介绍了CentOS 最新版本git的安装教程,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧...

    CentOS教程网9252021-11-29
  • CentosCentOS7 阿里云的yum源使用详解

    CentOS7 阿里云的yum源使用详解

    这篇文章主要介绍了CentOS7 阿里云的yum源使用详解的相关资料,这里对备份yum源,添加EPEL源,和缓存清理,进行了介绍,需要的朋友可以参考下...

    天王9312020-12-28
  • Centos在CentOS系统上安装Docker的教程

    在CentOS系统上安装Docker的教程

    这篇文章主要介绍了在CentOS系统上安装Docker的教程,Docker是当下人气最为火热的容器类虚拟软件,需要的朋友可以参考下 ...

    开源中文社区3952019-09-17
  • CentosCentOS系统下软件包的制作方法和过程详解

    CentOS系统下软件包的制作方法和过程详解

    今天小编将为大家带来的是CentOS系统下软件包的制作方法和过程详解;希望对大家会有帮助,有需要的朋友一起去看看吧...

    CentOS之家4802019-05-30
  • CentosCentOS上SVN服务器端程序的安装与使用教程

    CentOS上SVN服务器端程序的安装与使用教程

    SVN是一款高人气的软件项目版本控制系统,由于其在Windows的客户端的简易操作,在Git的浪潮中仍然保有很多的用户数量,这里我们就来看一下CentOS上SVN服务器端...

    cnblogs3902019-07-16
  • Centoscentos 7中添加一个新用户并授权的步骤详解

    centos 7中添加一个新用户并授权的步骤详解

    这篇文章主要给大家介绍了关于在centos 7中添加一个新用户并授权的步骤,文中将实现的步骤介绍的非常详细,通过文中介绍的步骤可以轻松的创建一个新...

    Ryan.Miao9832022-02-12