普罗米修斯(Prometheus) + grafana的搭建和使用

实验案例:一台服务器server,一台客户端client,时间同步(chrony)提前做好!!!

IP角色系统
192.168.100.50Prometheus 服务端CentOS 7
192.168.100.60node_exporter 客户端CentOS 7

服务器的操作:

部署go环境(Golang官网下载地址:https://golang.org/dl/ 按照包版本:go1.18.2.linux-amd64.tar.gz)

[root@server ~]# tar -zxvf go1.18.2.linux-amd64.tar.gz -C /usr/local
[root@server ~]#  vim /etc/profile
export PATH=$PATH:/usr/local/go/bin
[root@server ~]# source /etc/profile
[root@server ~]# go version
go version go1.18 linux/amd64

在网上拉取prometheus 的压缩包并解压至指定目录,改名,查看它的的版本号,方便grafana后续使用

[root@server ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.51.1/prometheus-2.51.1.linux-amd64.tar.gz
[root@server ~]# tar -zxvf  prometheus-2.51.1.linux-amd64.tar.gz -C /usr/local
[root@server ~]# mv prometheus-2.51.1.linux-amd64/ prometheus
[root@server ~]# ./prometheus --version

启动prometheus的服务:

[root@server /usr/local/prometheus]# ./prometheus --config.file=prometheus.yml

浏览器访问192.168.100.50:9090,查看服务是否启动成功

然后将prometheus配置为系统服务:

vi  /usr/lib/systemd/system/prometheus.service
#添加以下内容
[Unit]
Description=https://prometheus.io
  
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090
​
[Install]                 
WantedBy=multi-user.target
[root@server ~]# systemctl daemon-reload  #生效系统文件
[root@server ~]# pkill prometheus  #暂停上次启动的服务
[root@server ~]# systemctl enable prometheus  #服务
[root@server ~]# systemctl start prometheus  #再重新启动服务

客户端操作:

在网上拉取node_exporter 压缩包并解压至指定目录

[root@client ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
[root@client ~]# tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@client ~]# mv node_exporter-1.1.2.linux-amd64/ node_exporter

启动node_exporter,并添加到系统服务

# 直接启动
[root@client /usr/local/node_exporter]# ./node_exporter &
​
# 启动后会监听9100端口
​
# 添加为服务方式启动
[root@client ~]# vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target 
​
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
​
[Install]
WantedBy=multi-user.target
​
# 启动node_exporter
systemctl daemon-reload
pkill node_exporter
systemctl enable node_exporter
systemctl start node_exporter

服务端添加配置文件增加被监控项

[root@server /usr/local/prometheus]# vi prometheus.yml
#在底部添加以下三行
- job_name: 'linux'
    static_configs:
    - targets: ['192.168.100.60:9100']

打开网页192.168.100.50:9000查看被监控项是否被添加

当然除了监控主机状态还可以监控其他服务 

例如:mysql(前提是mysql提前装好,mysql和mariadb不一样哦)

提取mysqld-exporter的压缩包

[root@client ~]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@client ~]# tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
[root@client /usr/local]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
[root@client /usr/local/mysqld_exporter]# vim .my.cnf
[client]
user=root
password=000000

启动mysqld-exporter

[root@client /usr/local/mysqld_exporter]# ./mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
[root@client /usr/local/mysqld_exporter]# ps -ef |grep exporter
root       3447   3398  0 01:31 pts/1    00:00:02 ./node_exporter
root       4647   3398  0 02:13 pts/1    00:00:00 ./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
root       4654   3398  0 02:13 pts/1    00:00:00 grep --color=auto exporter
[root@client /usr/local/mysqld_exporter]# ss -lntp |grep 4647
LISTEN     0      128         :::9104                    :::*                   users:(("mysqld_exporter",pid=4647,fd=3))
[root@client /usr/local/mysqld_exporter]# 

服务端添加被监控项:

[root@server /usr/local/prometheus]# vi prometheus.yml
  - job_name: 'mysql'
    static_configs:
    - targets: ['192.168.100.60:9104']
#重启prometheus服务
[root@server /usr/local/prometheus]# systemctl restart prometheus.service

在浏览器查看mysqld-exporter添加是否成功

通过grafana展示prometheus数据

#如果下载不下来,可以去官网下载然后上传https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/Packages
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.4.1-1.x86_64.rpm
[root@server ~]# yum install initscripts fontconfig -y
[root@server ~]# yum install -y grafana-enterprise-10.4.1-1.x86_64.rpm
[root@server ~]# systemctl start grafana-server.service 

启动后访问地址192.168.100.50:3000

初始化密码和用户名都是admin

进入grafana后添加datasources数据源,grafana模板对接的是prometheus数据源,所以添加的ip我觉得只能是服务端ip:9090,纯属个人经验猜测,如果有误,欢迎评论指导,谢谢啦

界面右上角的+号选择import dashboard导入模板

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
普罗米修斯Grafana 是一对常用的开源监控工具,可以用来监控 Kubernetes 集群中的容器。下面是一个基本的监控流程: 1. 在 Kubernetes 集群中部署普罗米修斯Prometheus)和 Grafana。 2. 在 Kubernetes 集群中创建一个 ServiceMonitor,用来指定需要监控的应用程序或服务。 3. 在应用程序或服务中添加 Prometheus 客户端库,用来将指标数据推送到 Prometheus。 4. 在 Grafana 中配置数据源为 Prometheus,并创建仪表盘(Dashboard)来展示监控数据。 具体操作可参考以下步骤: 1. 部署 PrometheusGrafana 可以使用 Helm 工具来部署 PrometheusGrafana。首先,需要添加 PrometheusGrafana 的 Helm 仓库: ``` helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo add grafana https://grafana.github.io/helm-charts ``` 接着,使用 Helm 安装 PrometheusGrafana: ``` helm install prometheus prometheus-community/kube-prometheus-stack helm install grafana grafana/grafana ``` 2. 创建 ServiceMonitor 在 Kubernetes 集群中创建一个 ServiceMonitor,用来指定需要监控的应用程序或服务。以下是一个示例: ``` apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: my-app-monitor namespace: default spec: selector: matchLabels: app: my-app endpoints: - port: http ``` 其中,selector 指定需要监控的应用程序或服务的标签,endpoints 指定需要监控的端口。 3. 添加 Prometheus 客户端库 在应用程序或服务中添加 Prometheus 客户端库,用来将指标数据推送到 Prometheus。例如,在 Node.js 应用程序中,可以使用 prom-client 库: ``` const express = require('express') const promBundle = require('express-prom-bundle') const metricsMiddleware = promBundle({includeMethod: true}) const app = express() app.use(metricsMiddleware) app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(3000, () => { console.log('Example app listening on port 3000!') }) ``` 这样就可以在应用程序中添加 Prometheus 指标,例如: ``` const promClient = require('prom-client') const counter = new promClient.Counter({ name: 'my_counter', help: 'This is my counter' }) setInterval(() => { counter.inc() }, 1000) ``` 这个 Counter 每秒钟会加 1。 4. 配置 GrafanaGrafana 中配置数据源为 Prometheus,并创建仪表盘来展示监控数据。 首先,需要在 Grafana 中配置 Prometheus 数据源。进入 Grafana 界面,点击左侧菜单栏的“Configuration”,选择“Data Sources”,点击“Add data source”,选择“Prometheus”,输入 Prometheus 的地址,保存。 接着,可以创建一个仪表盘,用来展示监控数据。点击左侧菜单栏的“+”,选择“Dashboard”,然后选择“Add new panel”,选择需要展示的指标,保存。 以上是一个基本的监控流程,可以根据实际情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值