目录
声明:
- 本博客所有文章采用的授权方式为 自由转载-非商用-非衍生-保持署名 ,转载请务必注明出处,谢谢。
- 本博客欢迎转发,但请保留原作者信息。
- 内容系本人学习、研究和总结,如有雷同,实属荣幸。
社交平台:
基础环境
环境/组件 | 版本 | 下载地址 |
---|---|---|
操作系统 | CentOS7.6 | https://archive.kernel.org/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso |
Prometheus | 2.25.0 | https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz |
Go | 1.16 | https://golang.org/dl/go1.16.linux-amd64.tar.gz |
Grafana | yum install latest | https://mirror.tuna.tsinghua.edu.cn/help/grafana/ |
安装Go环境
解压安装
mkdir -p /opt/monitor
tar zxf go1.16.linux-amd64.tar.gz -C /opt/monitor
配置环境变量
echo "export PATH=\$PATH:/opt/monitor/go/bin" >> /etc/profile
source /etc/profile
验证
go version
go version go1.16 linux/amd64
安装Prometheus
安装
tar zxf prometheus-2.25.0.linux-amd64.tar.gz -C /opt/monitor/
mv /opt/monitor/prometheus-2.25.0.linux-amd64 /opt/monitor/prometheus
配置开机自启动
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus service
[Service]
User=root
ExecStart=/opt/monitor/prometheus/prometheus --config.file=/opt/monitor/prometheus/prometheus.yml --storage.tsdb.path=/opt/monitor/prometheus/data
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable prometheus
启动服务
systemctl start prometheus
ps -ef | grep prometheus
root 6366 1 0 12:14 ? 00:00:00 /opt/monitor/prometheus/prometheus --config.file=/opt/monitor/prometheus/prometheus.yml --storage.tsdb.path=/opt/monitor/prometheus/data
验证
浏览器打开IP:9090端口即可打开 prometheus 自带的监控页面:
安装Grafana
普罗米修斯默认的页面可能没有那么直观,我们可以安装grafana使监控看起来更直观。
配置清华大学的yum源
打开浏览器输入地址:https://mirror.tuna.tsinghua.edu.cn/help/grafana/
,复制CentOS/Redhat用户部分:
vim /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm
repo_gpgcheck=0
enabled=1
gpgcheck=0
yum makecache
安装Grafana
yum install grafana -y
启动服务
systemctl daemon-reload
systemctl enable grafana-server
systemctl start grafana-server
访问Grafana
浏览器访问IP:3000端口,即可打开grafana页面,默认用户名密码都是admin,初次登录会要求修改默认的登录密码:
添加Prometheus数据源
点击主界面的“Add your first data source”并选择Prometheus:
Dashboards页面选择“Prometheus 2.0 Stats”进行Import:
Settings页面填写普罗米修斯地址并保存:
切换到我们刚才添加的“Prometheus 2.0 Stats”即可看到整个监控页面:
一些常用监控举例
监控Linux机器(node_exporter)
- 下载地址:
https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz
- 被监控的机器安装node_exporter:
mkdir -p /opt/monitor
tar zxf node_exporter-1.1.1.linux-amd64.tar.gz -C /opt/monitor/
mv /opt/monitor/node_exporter-1.1.1.linux-amd64 /opt/monitor/node_exporter
- 启动服务:
配置开机自启动:
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter service
[Service]
User=root
ExecStart=/opt/monitor/node_exporter/node_exporter
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable node_exporter
启动服务:
systemctl start node_exporter
ps -ef | grep node_exporter
root 16379 1 0 13:12 ? 00:00:00 /opt/monitor/node_exporter/node_exporter
- Prometheus配置文件添加监控项:
vim /opt/monitor/prometheus/prometheus.yml
- job_name: 'linux-node1'
static_configs:
- targets: ['192.168.0.112:9100']
labels:
instance: linux-node1
重启Prometheus:
systemctl restart prometheus
- grafana导入画好的dashboard:
Json文件下载地址:
修改名字,选择我们前文创建好的数据源,点击导入即可:
下面这个提示是grafana缺少相关显示需要用到的插件piechart,grafana的默认插件目录是/var/lib/grafana/plugins,可以将下载好的插件解压到这个目录,重启grafana即可
插件下载地址:
unzip -q grafana-piechart-panel-5f249d5.zip -d /var/lib/grafana/plugins/
systemctl restart grafana-server
查看已安装插件:
/usr/sbin/grafana-cli plugins ls
installed plugins:
grafana-piechart-panel @ 1.3.3
再刷新grafana页面,即可看到我们刚才设置好的node监控:
监控MySQL(mysqld_exporter)
https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
Json文件下载地址:
- 被监控的机器安装mysqld_exporter:
mkdir -p /opt/monitor
tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt/monitor/
mv /opt/monitor/mysqld_exporter-0.12.1.linux-amd64 /opt/monitor/mysqld_exporter
cd /opt/monitor
- 设置配置文件,user为数据库登录用户,password为这个用户的密码:
vim .my.cnf
[client]
user=root
password=123456
- 配置开机自启动并启动服务:
vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter service
[Service]
User=root
ExecStart=/opt/monitor/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/monitor/.my.cnf
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
vim /usr/lib/systemd/system/mysqld_exporter.service
systemctl daemon-reload
systemctl enable mysqld_exporter
systemctl start mysqld_exporter
systemctl status mysqld_exporter
- prometheus配置文件中加入mysql监控并重启:
vim /opt/monitor/prometheus/prometheus.yml
- job_name: 'mysqld-node1'
static_configs:
- targets: ['192.168.1.235:9104']
labels:
instance: mysqld-xwiki
重启服务:
systemctl restart prometheus
- 设置数据源:
grafana界面添加mysql数据源:
添加需要被监控的数据库及相关信息:
- 导入已经画好的dashboard,数据源选择刚刚创建好的mysql数据源即可:
在选择数据源的时候发现没有MySQL的数据源,怀疑是json文件有问题。
监控Redis(redis_exporter)
下载地址:
- 安装redis_exporter:
mkdir -p /opt/monitor
tar zxf redis_exporter-v0.15.0.linux-amd64.tar.gz -C /opt/monitor
mv /opt/monitor/redis_exporter-v0.15.0.linux-amd64 /opt/monitor/redis_exporter
- 配置开机自启动并启动服务:
vim /usr/lib/systemd/system/redis_exporter.service
[Unit]
Description=redis_exporter service
[Service]
User=root
ExecStart=/opt/monitor/redis_exporter/redis_exporter redis//192.168.0.116:6379 -web.listenaddress 192.168.0.116:9121
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
vim /usr/lib/systemd/system/redis_exporter.service
systemctl daemon-reload
systemctl enable redis_exporter
systemctl start redis_exporter
systemctl status redis_exporter
- prometheus配置文件中加入redis监控并重启:
vim /opt/monitor/prometheus/prometheus.yml
- job_name: 'redis-node1'
static_configs:
- targets: ['192.168.0.116:9121']
labels:
instance: redis-mmba
重启服务:
systemctl restart prometheus
- grafana导入画好的dashboard:
Json文件下载地址:
未完成!
Grafana常用网站
- grafana dashboard:
https://grafana.com/dashboards
- grafana plugins:
https://grafana.com/plugins