小编给大家分享一下centos7.8如何安装prometheus和grafana,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
安装准备
本实践中以 2台已安装centos7.8的虚机作为实践环境安装prometheus2.8.1和grafana,并在prometheus中配置对这2台主机的监控。
具体安装规划如下:
hostname | ip | os | cpu | memory | disk | 备注 |
---|
prometheus-host | 172.22.3.148 | centos7.8 | 2c | 2g | 20g | prometheus grafana node_exporter |
prometheus-node | 172.22.3.149 | centos7.8 | 2c | 2g | 20g | node_exporter |
https://github.com/prometheus/prometheus/releases/download/v2.24.0/prometheus-2.24.0.linux-amd64.tar.gz
https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm
https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
备注:如无说明,以下部署均在prometheus-host部署
安装prometheus
# cd /opt
# wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz
# cd /opt
# tar -zxf prometheus-2.8.1.linux-amd64.tar.gz
# mv prometheus-2.8.1.linux-amd64 prometheus
# cd /opt/prometheus
# ./prometheus
## 创建service
# vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
## 设置自启动
# systemctl enable prometheus
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.
## 启动服务
# systemctl start prometheus
浏览器地址栏输入 http://172.22.3.148:9090
安装grafana
# cd /opt
# wget https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm
# cd /opt
# yum install grafana-7.3.6-1.x86_64.rpm
## 安装完成后会自动生成grafana-server的service
systemctl daemon-reload
systemctl start grafana-server
## 设置自启动
# systemctl enable grafana-server
浏览器地址栏输入 http://172.22.3.148:3000
使用默认用户名密码 admin/admin登录
首次登录后需要设置新密码如上。
granfa安装后需要配置数据源
选择Prometheus,点击Select
只需要设置URL为http://localhost:9090
其它保持默认,直接点“Save & Test”按钮即可
弹出保存成功提示,点“Back”按钮返回。
可以看到grafana已经保存了一个数据源。
安装node_exporter并用prometheus配置主机监控
# cd /usr/local
# wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
# cd /usr/local
# tar -zxf node_exporter-1.0.1.linux-amd64.tar.gz
# mv node_exporter-1.0.1.linux-amd64/node_exporter ./bin/
## 创建service
# vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
## 设置自启动
# systemctl enable node_exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/node_exporter.service to /usr/lib/systemd/system/node_exporter.service.
## 启动服务
# systemctl start node_exporter
修改prometheus配置文件prometheus.yml,在scrape_configs下增加如下配置
- job_name: 'prometheus-host'
file_sd_configs:
- files: ['/opt/prometheus/sd_config/prometheus-host.yml']
refresh_interval: 5s
新增/opt/prometheus/sd_config/prometheus-host.yml文件,内容如下
cat /opt/prometheus/sd_config/prometheus-host.yml
- targets:
- 172.22.3.149:9100
备注:每次修改配置完成,用promtool检测配置文件是否正确
# /opt/prometheus/promtool check config /opt/prometheus/prometheus.yml
重启prometheus
# systemctl restart prometheus
浏览器地址栏输入 http://172.22.3.148:9090/targets
可以看到targets已经增加了对主机prometheus-host的监控
点“+”按钮,弹出导入面板窗口
如图输入8919,点Load,grafana会直接从官方网站导入编号为8919的面板如下
选择数据源Prometheus,继续“Import”,显示node监控界面显示如下
在prometheus-nodes安装node_exporter并用prometheus配置主机监控
修改prometheus配置文件prometheus.yml,在scrape_configs下增加如下配置
- job_name: 'prometheus-nodes'
file_sd_configs:
- files: ['/opt/prometheus/sd_config/prometheus-nodes.yml']
refresh_interval: 5s
新增/opt/prometheus/sd_config/prometheus-nodes.yml文件,内容如下
cat /opt/prometheus/sd_config/prometheus-nodes.yml
- targets:
- 172.22.3.148:9100
重启prometheus
# systemctl restart prometheus
浏览器地址栏输入 http://172.22.3.148:9090/targets
可以看到targets已经增加了对主机prometheus-nodes的监控
访问grafana,也可以看到node监控界面多了prometheus-nodes的显示
以上是“centos7.8如何安装prometheus和grafana”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!