实战-Centos安装prometheus-grafana-2020.11.27(测试成功)
实战-Centos安装prometheus-grafana-2020.11.27(测试成功)
这节课我们来学习二进制安装prometheus,然后使用Node Exporter来采集主机信息,由prometheus server来定时向node exporter拉取数据并使用Grafana进行图形化的展示。
目录
[toc]
实验环境
- 本次使用3台centos7.7虚机来搭建实验;(2个target,1个prometheus server);因为是实验环境,虚机计算规格可随便选择;
- 本次涉及软件包如下:
grafana-7.2.1.linux-amd64.tar
node_exporter-1.0.1.linux-amd64.tar
prometheus-2.23.0.linux-amd64.tar
模板文件:`主 机基础监控(cpu,内存,磁盘,网络).json`,将其导入即可;
- 本次3台机器ip规划如下:
#本次次prometheus server主机名设置为:prometheus,其余2个target节点主机名如下:
hostnamectl --static set-hostname prometheus
exec bash
hostnamectl --static set-hostname node1
exec bash
hostnamectl --static set-hostname node2
exec bash
#配置ip过程省略:
192.168.10.10 prometheus
192.168.10.11 node1
192.168.10.12 node2
#本次虚机网卡选择为nat模式,仅主机模式也行,只要保证他们之间能互访即可;
- 初始化配置
#另建议:关闭防火墙/NetwokManager/selinux(3台都需要配置)
systemctl stop firewalld#如果未关闭防火墙的话,其9090端口将无法访问;
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
setenforce 0
sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config
实验软件
1、安装prometheus server(以下操作均在prometheus节点操作)
Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖。用户只需要下载对应平台的二进制包,解压并且添加基本的配置即可正常使用Prometheus Server。
1.1 下载安装包
prometheus的官网https://prometheus.io/ 安装包下载链接https://prometheus.io/download/
截止目前(2020年11月30日)最新的二进制包版本是[prometheus-2.23.0.linux-amd64.tar.gz],链接如下:
1.2 解压
首先,将刚下载好的prometheus-2.23.0.linux-amd64.tar.gz软件包上传到192.168.10.10-prometheus机器上,再执行如下命令:
[root@prometheus ~]# mv prometheus-2.23.0.linux-amd64.tar.gz /usr/local/
[root@prometheus ~]# cd /usr/local/
[root@prometheus local]# tar xf prometheus-2.23.0.linux-amd64.tar.gz
[root@prometheus local]# ls
bin games lib libexec prometheus-2.22.0.linux-amd64.tar.gz share
etc include lib64 prometheus-2.22.0.linux-amd64 sbin src
[root@prometheus local]# mv prometheus-2.23.0.linux-amd64 prometheus
1.3 配置说明
解压后在prometheus的安装目录/usr/local/prometheus里面,包含默认地prometheus.yml配置文件。
[root@prometheus local]# pwd
/usr/local
[root@prometheus local]# ls
bin etc games include lib lib64 libexec prometheus prometheus-2.23.0.linux-amd64.tar.gz sbin share src
[root@prometheus local]# ls prometheus
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
[root@prometheus local]#
1.4配置prometheus
查看prometheus.yml配置文件内容:
[root@prometheus local]# pwd
/usr/local
[root@prometheus local]# cat /usr/local/prometheus/prometheus.yml
# 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']
[root@prometheus local]#
1.4.1 创建prometheus的用户
为了安全,尽量使用普通用户来启动prometheus服务。
[root@prometheus local]# useradd -s /sbin/nologin -M prometheus