CentOS 7.9系统安装部署prometheus+grafana

Prometheus和Grafana简单介绍

Prometheus 是一款开源的系统监控和告警工具,专注于时序数据的采集、存储和查询。它通过主动拉取目标服务暴露的指标接口(如 HTTP 端点)收集数据,支持自定义监控指标和灵活的查询语言(PromQL),能实时分析系统性能、资源使用等状态,并基于预定义规则触发告警。其典型应用场景包括服务器资源监控(CPU、内存、磁盘)、数据库性能追踪(查询延迟、连接数)及容器集群状态监控(如 Kubernetes 节点与 Pod 指标),常与容器化技术结合构建云原生监控体系。

Grafana 是一款开源的数据可视化和监控仪表盘工具,支持对接 Prometheus、Elasticsearch 等多种数据源,通过丰富的图表类型(折线图、柱状图、热力图等)将复杂数据转化为直观的可视化面板。它允许用户自定义仪表盘布局,设置数据刷新频率和阈值告警,还支持多数据源聚合展示,帮助运维人员快速定位系统瓶颈、追踪业务指标变化。在实际运维中,Grafana 常与 Prometheus 搭配使用,前者负责数据可视化与告警展示,后者负责数据采集与存储,共同构成完整的监控解决方案。

1. 安装prometheus

Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三⽅依赖。⽤户只需要下载对应

平台的⼆进制包,解压并且添加基本的配置即可正常启动Prometheus Server。

1.1 下载

下载地址:Download | Prometheus

github地址:https://github.com/prometheus/prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-386.tar.gz

 

1.1.1 解压
# 解压到指定⽬录中
tar xf prometheus-2.35.0.linux-386.tar.gz -C /usr/local/

 

1.1.2 解压后最好做软连接,后期升级⽅便
# 进⼊/usr/local/⽬录
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ll
总⽤量 0
...
drwxr-xr-x. 4 3434 3434 132 4⽉ 21 2022 prometheus-2.35.0.linux-386
...
# 改变属主属组
[root@localhost local]# chown -R root:root prometheus-2.35.0.linux-386
# 做软连接
[root@localhost local]# ln -s prometheus-2.35.0.linux-386 prometheus
[root@localhost local]# ll

 

1.1.3 ⽬录⽂件解读
# 进⼊到prometheus⽬录
[root@localhost local]# cd prometheus/
[root@localhost prometheus]# ll
总⽤量 185908
drwxr-xr-x. 2 root root 38 4⽉ 21 2022 console_libraries # Prometheus Web UI 使⽤的控制台模板库。
drwxr-xr-x. 2 root root 173 4⽉ 21 2022 consoles # Prometheus Web UI 的控制台⻚⾯模板。
-rw-r--r--. 1 root root 11357 4⽉ 21 2022 LICENSE # 开源许可协议(Apache 2.0)。
-rw-r--r--. 1 root root 3773 4⽉ 21 2022 NOTICE # 包含版权和开源软件使⽤的声明。
-rwxr-xr-x. 1 root root 99113828 4⽉ 21 2022 prometheus # Prometheus 主程序(⼆进制⽂件)。
-rw-r--r--. 1 root root 934 4⽉ 21 2022 prometheus.yml # 主配置⽂件。
-rwxr-xr-x. 1 root root 91231965 4⽉ 21 2022 promtool # prom⼯具,主要检查配置⽂件是否有误

 

 

1.2 启动prometheus

1.2.1 检测配置⽂件有⽆错误
[root@prometheus01 prometheus]# ./promtool check config prometheus.yml
Checking prometheus.yml      # 检查prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax   # 是有效的Prometheus配置⽂件语法

[root@prometheus01 prometheus]#

 

1.2.2 前台启动
[root@prometheus01 prometheus]# ./prometheus

1.2.3 后台启动

编写systemctl启动文件:

vim /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
WorkingDirectory=/usr/local/prometheus/
ExecStart=/usr/local/prometheus/prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s
[Install]
WantedBy=multi-user.target

加载服务

systemctl daemon-reload

启动服务

 systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus

1.3 验证是否启动

1.3.1 通过端⼝验证

1.3.2 在web页面访问9090端口:

http://192.168.178.121:9090/

 

 

1.3.3 prometheus配置⽂件解读 
vim /usr/local/prometheus/prometheus.yml

# my global config
global: # 全局配置
 scrape_interval: 60s # 设置 Prometheus 抓取监控⽬标数据的间隔时间,(默认是 60 秒)。这个间隔决定了数据的刷新频率。
 evaluation_interval: 60s # 设置 Prometheus 评估告警规则的间隔时间,(默认是 60 秒) 
 # scrape_timeout :设置为全局默认值(10s)。 
alerting: # 报警管理器配置
 alertmanagers: # alertmanager配置
 - static_configs: # 静态配置
 - targets: # ⽬标
 - 192.168.56.112:9093 # alertmanager服务
rule_files: # 告警规则⽂件配置
 # - "first_rules.yml"
 # - "second_rules.yml"
 # - "rules/*.yml"
scrape_configs: # exporter监控⽬标配置。
 - job_name: "prometheus" # 作业名作为标签'job=<job_name>'添加到从该配置中抓取的任何时间序列。 
 # 去找默认路径监控起来。默认路径为:'/metrics' #去192.168.66.107:9090/metrics⾥边找相对应的监控数据
 # 默认监控协议: 'http'.
 static_configs: # 静态配置
 - targets: ["localhost:9090"] # 这⾥指定的是 Prometheus ⾃⼰的地址,表示它会抓取⾃⼰的监控数据,路径是:http://localhost:9090/metrics
 labels: # 标签
 app: localhost # 标签名: 标签值

2. 安装web界⾯ Grafana

通过grafana进⾏详细的⻚⾯展示

2.1 下载grafana

官⽅地址:Grafana: The open and composable observability platform | Grafana Labs

下载地址:Download Grafana | Grafana Labs

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

 

2.1.1 解压
# 解压到指定⽬录中
tar xf grafana-enterprise-12.0.1.linux-amd64.tar.gz -C /usr/local/

 

2.1.2 解压后最好做软连接,后期升级⽅便
# 做软连接
[root@localhost local]# ln -s grafana-v12.0.1/ grafana

 

2.2 启动grafana

2.2.1 添加 grafana 为系统服务
vim /usr/lib/systemd/system/grafana.service

[Unit]
Description=Grafana
Documentation=https://grafana.com/
After=network.target

[Service]
WorkingDirectory=/usr/local/grafana/
ExecStart=/usr/local/grafana/bin/grafana-server
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

2.2.2 加载服务

systemctl daemon-reload

启动服务

systemctl start grafana
systemctl enable grafana
systemctl status grafana

 

2.2.2 通过端⼝验证

 

2.2.3 通过浏览器访问

http://192.168.178.121:3000/

 

注意:默认端⼝号是3000

** 默认账号是admin**

** 默认密码是admin**

忘记grafana密码

grafana-cli ⽤户 reset-admin-password 密码

grafana-cli admin reset-admin-password 123456

 

 

2.3 Grafanaweb界面操作

2.3.1 设置中⽂

2.3.2 配置数据源

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值