实战-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
1.4.2 更改数据存储目录
prometheus的数据默认会存放在应用所在目录下,这里我们修改为/data/prometheus:
[root@prometheus local]# mkdir /data/prometheus -p
[root@prometheus local]# chown -R prometheus:prometheus /usr/local/prometheus
[root@prometheus local]# chown -R prometheus:prometheus /data/prometheus/
1.5 创建systemd启动脚本
prometheus的启动很简单,只需要直接启动解压目录的二进制文件prometheus,然后再加上一些参数即可。
[root@prometheus local]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure
User=prometheus
[Install]
WantedBy=multi-user.target
#不要忘记执行
[root@prometheus local]# systemctl daemon-reload
1.6 启动prometheus并设置为开机自启动
#启动prometheus并设置为开机自启动
[root@prometheus local]# systemctl enable --now prometheus
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.
#查看prometheus服务状态
[root@prometheus local]# systemctl status prometheus
● prometheus.service - Prometheus
Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-01-29 07:17:49 CST; 12s ago
Docs: https://prometheus.io/
Main PID: 1916 (prometheus)
CGroup: /system.slice/prometheus.service
└─1916 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.337Z caller=head.go:645 component=tsdb msg="Replaying on-disk memo...s if any"
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.337Z caller=head.go:659 component=tsdb msg="On-disk memory mappabl…on=12.223µs
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.337Z caller=head.go:665 component=tsdb msg="Replaying WAL, this ma... a while"
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.338Z caller=head.go:717 component=tsdb msg="WAL segment loaded" se...Segment=0
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.338Z caller=head.go:722 component=tsdb msg="WAL replay completed" …ion=431.8µs
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.340Z caller=main.go:742 fs_type=XFS_SUPER_MAGIC
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.340Z caller=main.go:745 msg="TSDB started"
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.340Z caller=main.go:871 msg="Loading configuration file" filename=...theus.yml
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.354Z caller=main.go:902 msg="Completed loading of configuration file" filen…µs
Jan 29 07:17:49 prometheus prometheus[1916]: level=info ts=2021-01-28T23:17:49.355Z caller=main.go:694 msg="Server is ready to receive web requests."
Hint: Some lines were ellipsized, use -l to show in full.
#查看prometheus服务所使用的的端口号
[root@prometheus local]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1226,fd=13))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1014,fd=3))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1226,fd=14))
LISTEN 0 128 [::]:9090 [::]:* users:(("prometheus",pid=1916,fd=9))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1014,fd=4))
[root@prometheus local]#
1.7 访问prometheus
我们看到有9090端口,然后直接以IP:Port的方式来访问:(在自己笔记本上进行测试) http://192.168.10.10:9090
还可以查看到当前正在被监控的主机,这里只有我们当前部署的这台。如下所示:
点击Status--->Targets可以看到
从UP状态可以看的出来节点是正常的。
2、安装node exporter
node exporter的作用:收集操作系统的基本系统, 例如cpu, 内存, 硬盘空间等基本信息, 并对外提供api接口,用于prometheus查询存储。
**备注:**以下配置在node1和node2都要执行。(我们这边先在node1上执行,后在node2执行一样的操作)
2.1 下载安装包
node exporter最新的版本是1.0.1。下载链接:https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
2.2 解压(node节点操作)
首先,将刚下载好的node_exporter-1.0.1.linux-amd64.tar软件包上传到node1和node2机器上,再执行如下命令:
[root@node1 ~]# mv node_exporter-1.0.1.linux-amd64.tar.gz /usr/local
[root@node1 ~]# cd /usr/local
[root@node1 local]# tar xf node_exporter-1.0.1.linux-amd64.tar.gz
[root@node1 local]# mv node_exporter-1.0.1.linux-amd64 node_exporter
#查看node_exporter相关文件
[root@node1 local]# pwd
/usr/local
[root@node1 local]# ls
bin etc games include lib lib64 libexec node_exporter node_exporter-1.0.1.linux-amd64.tar.gz sbin share src
[root@node1 local]# cd node_exporter
[root@node1 node_exporter]# ll -h
total 19M
-rw-r--r--. 1 3434 3434 12K Jun 16 2020 LICENSE
-rwxr-xr-x. 1 3434 3434 19M Jun 16 2020 node_exporter
-rw-r--r--. 1 3434 3434 463 Jun 16 2020 NOTICE
2.3 启动node exporter(node节点操作)
node exporter无需修改配置文件,安装上去 之后可以直接启动。 node exporter的二进制启动文件在/usr/local/node_exporter目录下面。这里我们直接以nohup的方式来启动这个服务。
[root@node1 local]# nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
[1] 2547
[root@node1 local]# ps -ef|grep 2547
root 2547 1876 0 07:36 pts/0 00:00:00 /usr/local/node_exporter/node_exporter
root 2552 1876 0 07:37 pts/0 00:00:00 grep --color=auto 2547
[root@node1 local]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1220,fd=13))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1007,fd=3))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1220,fd=14))
LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=2547,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1007,fd=4))
[root@node1 local]#
node exporter默认地端口是9100,只要能看到端口,说明服务启动是没有问题的。
接下来按照相同的方法把第二个节点完成配置:
[root@node2 local]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1223,fd=13))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1005,fd=3))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1223,fd=14))
LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=2534,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1005,fd=4))
[root@node2 local]#
2.4 加入开机启动(node节点操作)
node exporter直接写在Linux系统启动脚本/etc/rc.local文件里面即可。当然,最后赋予x执行权限
[root@node1 local]# vim /etc/rc.local
#!/bin/bash
touch /var/lock/subsys/local #默认就存在的
nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
[root@node1 local]# chmod +x /etc/rc.local
接下来按照相同的方法把第二个节点完成配置:
[root@node2 local]# vim /etc/rc.local
#!/bin/bash
touch /var/lock/subsys/local #默认就存在的
nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
[root@node2 local]# chmod +x /etc/rc.local