商城网站建设天软科技,网址seo优化排名,网站建设书籍推荐,工业风 网站建设1.简介
本文档为prometheus和grafana安装配置手册#xff0c;prometheus和grafana的内容、和操作过程#xff0c;详细介绍了服务监控配置、dashboard配置、告警配置等操作。
2.部署说明
Prometheus基于Golang编写#xff08;需要安装#xff09;#xff0c;编译后的软件…1.简介
本文档为prometheus和grafana安装配置手册prometheus和grafana的内容、和操作过程详细介绍了服务监控配置、dashboard配置、告警配置等操作。
2.部署说明
Prometheus基于Golang编写需要安装编译后的软件包不依赖于任何的第三方依赖。用户只需要下载对应平台的二进制包解压并且添加基本的配置即可正常启动Prometheus Server。 Grafana提供离线的rpm安装包直接安装即可。
2.1环境配置
操作系统版本 centos7.2IP地址规划 prometheus servergrafana10.3.10.135
node_explorer: 10.3.10.136Prometheus版本 prometheus-2.14.0Grafana版本: grafana-6.5.1-12.2软件介质
Prometheus安装包 prometheus-2.14.0.linux-amd64.tar.gzgrafana安装包 grafana-6.5.1-1.x86_64.rpmnode_explorer安装包: node_exporter-0.18.1.linux-amd64.tar.gzmysqld_exporte_node安装包 mysqld_exporter-0.12.1.linux-amd64.tar.gz3.安装配置
3.1 安装Prometheus
添加用户prometheus # useradd --no-create-home --shell /bin/false prometheus创建相关目录 # mkdir /etc/prometheus
# mkdir /var/lib/prometheus改目录权限 # chown prometheus:prometheus /etc/prometheus
# chown prometheus:prometheus /var/lib/prometheus解压 移动到相应目录 # tar xvf prometheus-2.14.0.linux-amd64.tar.gz # cp prometheus-2.14.0.linux-amd64/prometheus /usr/local/bin/
# cp prometheus-2.14.0.linux-amd64/promtool /usr/local/bin/# cp -r prometheus-2.14.0.linux-amd64/consoles/ /etc/prometheus/
# cp -r prometheus-2.14.0.linux-amd64/console_libraries/ /etc/prometheus/
# cp prometheus-2.14.0.linux-amd64/prometheus.yml /etc/prometheus/# chown prometheus:prometheus /usr/local/bin/prometheus
# chown prometheus:prometheus /usr/local/bin/promtool# chown -R prometheus:prometheus /etc/prometheus/consoles
# chown -R prometheus:prometheus /etc/prometheus/console_libraries
# chown -R prometheus:prometheus /etc/prometheus/prometheus.yml 配置Prometheus service端口为9090 # vi /etc/systemd/system/prometheus.service
[Unit]
DescriptionPrometheus
Wantsnetwork-online.target
Afternetwork-online.target[Service]
Userprometheus
Groupprometheus
Typesimple
ExecStart/usr/local/bin/prometheus \--config.file /etc/prometheus/prometheus.yml \--storage.tsdb.path /var/lib/prometheus/ \--web.console.templates/etc/prometheus/consoles \
--web.console.libraries/etc/prometheus/console_libraries \
--web.enable-lifecycle \--web.listen-address:9090[Install]
WantedBymulti-user.targetPrometheus启动参数说明 --config.file -- 指明prometheus的配置文件路径 --web.enable-lifecycle -- 指明prometheus配置更改后可以进行热加载 --storage.tsdb.path -- 指明监控数据存储路径 --storage.tsdb.retention --指明数据保留时间
说明: prometheus在2.0之后默认的热加载配置没有开启, 配置修改后, 需要重启prometheus server才能生效, 这对于生产环境的监控是不可容忍的, 所以我们需要开启prometheus server的配置热加载功能. 在启动prometheus时加上参数 web.enable-lifecycle , 可以启用配置的热加载, 配置修改后, 热加载配置: curl -X POST http://localhost:9090/-/reload加载systemd服务 启动 # 加载
systemctl daemon-reload
# 开机启动
systemctl enable prometheus.service
# 启动
systemctl start prometheus
# 查看状态
systemctl status prometheus访问URL http://10.3.10.135:9090/graph
3.2 安装Grafana
rpm包上传到服务器上后直接执行 # yum localinstall grafana-6.5.1-1.x86_64.rpm安装完之后进行Grafana配置的修改修改端口 vim /etc/grafana/grafana.ini ,关于配置说明请见官方文档、Grafana配置和Grafana 配置文件详解 http_port 3000加载systemd服务 启动 直接启动
# systemctl start grafana-server
#加载
# systemctl daemon-reload
#开启
# systemctl start grafana-server
#查看状态
# systemctl status grafana-server
#开启启动
# systemctl enable grafana-server.service日志数据目录 日志目录 配置为在引导时启动 数据目录 /var/lib/grafana/grafana.db访问URL granafa默认端口为3000可以在浏览器中输入
http://10.3.10.135:3000/ granafa首次登录账户名和密码admin/admin可以修改 用户名admin
密码admin
3.3安装Exporter
在3.1和3.2操作中已经把Prometheus和Grafana安装完毕现在开始安装Exporter来采集数据。 Exporter是Prometheus的一类数据采集组件的总称。它负责从目标处搜集数据并将其转化为Prometheus支持的格式。与传统的数据采集组件不同的是它并不向中央服务器发送数据而是等待中央服务器主动前来抓取默认的抓取地址为 http://CURRENT_IP:9100/metrics
3.3.1 Linux主机监控
node_exporter的作用是用于机器系统数据收集监控服务器CPU、内存、磁盘、I/O、网络等等信息。
3.3.1.1安装node_exporter
准备介质上传到服务器 node_exporter-0.18.1.linux-amd64.tar.gz解压 tar xvf node_exporter-0.18.1.linux-amd64.tar.gz
cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/sbin/帮助 node_exporter --help开启端口9199 node_exporter --web.listen-address:9199添加用户prometheus useradd --no-create-home --shell /bin/false prometheus加载systemd服务 启动 vi /etc/systemd/system/node_exporter.service [Unit]
Descriptionnode_exporter
Documentationhttps://prometheus.io/
Afternetwork.target
[Service]
Typesimple
Userprometheus
ExecStart/usr/sbin/node_exporter --web.listen-address:9199
Restarton-failure
[Install]
WantedBymulti-user.targetsystemctl 启动 # 加载
systemctl daemon-reload
# 开机启动
systemctl enable node_exporter.service
# 启动
systemctl start node_exporter
# 查看状态
systemctl status node_exporter启动 node_exporter 服务并设置开机启动 # systemctl daemon-reload
# systemctl enable node_exporter.service
# systemctl start node_exporter.service
# systemctl status node_exporter.service
# systemctl restart node_exporter.service
# systemctl start node_exporter.service
# systemctl stop node_exporter.service此时node_exporter启动后已经开始采集指标信息node_exporter启动成功后, 你就可以通过如下api看到你的监控数据了(将下面的node_exporter_server_ip替换成你的node_exporter的IP地址, 放到浏览器中访问就可以了 ). 可以通过地址访问 http://10.3.10.135:9199/metrics
3.3.1.2可视化监控配置
上面已经安装部署好了Prometheus、Grafana和Exporter现在需要把这3者联系起来进行数据展示。如已经安装了node_exporter需要把node_exporter采集的信息配置到Prometheus上。即修改Prometheus配置文件 为了更好的展示, 接下来我们将这个api 配置到 prometheus server中, 并通过grafana进行展示. 将 node_exporter 加入 prometheus.yml配置中 vi /etc/prometheus/prometheus.yml- job_name: Linuxfile_sd_configs:- files: [/etc/prometheus/sd_cfg/Linux.yml]refresh_interval: 5s并在文件/etc/prometheus/sd_cfg/Linux.yml中写入如下内容 - targets: [IP地址:9100]labels:name: Linux-node1[这里建议给每个主机打个有意义的标签,方便识别.]例如
这样做的好处是, 方便以后配置监控自动化, 规范化, 将每一类的监控放到自己的配置文件中, 方便维护. 当然, 如果你的服务器少, 要监控的组件少的话, 你也可以将配置都写入prometheus的主配置文件prometheus.yml中
重启Prometheus systemctl restart Prometheus重载prometheus配置 curl -X POST http://localhost:9090/-/reload3.3.1.3查看监控端是否介入 发现 target 中只有 prometheus server, 因为我们还没有加入其他监控, 下面进行介绍, 后续还将介绍如何监控 mysql, redis, RabbitMQ, Kafka, nginx, java等常见服务.
3.3.1.4 Grafana导入Linux模板
Granfana 导入Linux主机监控图表
推荐图表IDhttps://grafana.com/dashboards/11074
操作步骤
Grafana首页--左上角图标--Dashboard--import Upload已下载至本地的json文件或者使用dashboard id如这里的11074如下
数据源选择prometheus即添加的数据源name点击Import按钮如下 配置完保存后即可看到逼格非常高的系统主机节点监控信息包括系统运行时间, 内存和CPU的配置, CPU、内存、磁盘、网络流量等信息, 以及磁盘IO、CPU温度等信息。 3.3.2 Mysql数据库监控
mysqld_exporter的作用是用于MySQL数据库数据收集。
3.3.2.1安装mysqld_exporter
准备介质上传到服务器 mysqld_exporter-0.12.1.linux-amd64.tar.gz解压 tar xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
cp mysqld_exporter /usr/sbin/帮助 mysqld_exporter --helpmysqld_exporter需要连接Mysql首先为它创建用户并赋予所需要的权限 登录mysql为exporter创建账号并授权
mysql set global validate_password_policyLOW;
# 创建数据库用户。
mysql CREATE USER exporterlocalhost IDENTIFIED BY exporter;
# 可查看主从运行情况查看线程及所有数据库。
mysql GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO exporterlocalhost;
mysql flush privileges;2创建.my.cnf文件mysql配置文件、运行时可免密码连接数据库 vi /usr/local/mysql_exporter/.my.cnf
[client]
host10.3.10.136
port3306
userroot
passwordIntel123456systemctl启动mysqld_exporter编辑系统启动文件 vi /etc/systemd/system/mysqld_exporter.service
[Unit]
Descriptionmysqld_exporter
Documentationhttps://prometheus.io/
Afternetwork.target
[Service]
Typesimple
Userprometheus
ExecStart/usr/sbin/mysqld_exporter --web.listen-address:9104 --config.my-cnf/usr/local/mysql_exporter/.my.cnf
Restarton-failure
[Install]
WantedBymulti-user.target启动 # systemctl daemon-reload
# systemctl enable mysqld_exporter
# systemctl restart mysqld_exporter
# systemctl status mysqld_exporter也可以直接运行 ./mysqld_exporter --config.my-cnf/usr/local/mysql_exporter/.my.cnf此时mysqld_exporter启动后已经开始采集指标信息可以通过地址访问
http://10.3.10.136:9104/metrics 3.3.2.2可视化配置
Prometheus配置: 修改prometheus.yml加入MySql节点将mysqld_exporter加入 prometheus.yml配置中 vi /etc/prometheus/prometheus.yml- job_name: Mysqlfile_sd_configs:- files: [/etc/prometheus/sd_cfg/Mysql.yml]refresh_interval: 5s
并在文件/etc/prometheus/sd_cfg/Mysql.yml中写入如下内容 - targets: [10.3.10.136:9104]labels:instance: node_exporter_mysql_10.3.10.136检查并重启服务 promtool check config /etc/prometheus/prometheus.yml重启prometheus生效 # systemctl restart prometheus
# systemctl status prometheus3.3.2.3 查看监控端是否介入 3.3.2.4 Granfana 导入Mysql 监控图表
Granfana 导入Mysql监控图表
推荐图表IDhttps://grafana.com/dashboards/7362
操作步骤
Grafana首页--左上角图标--Dashboard--import Upload已下载至本地的json文件或者使用dashboard id如这里的7362如下
数据源选择prometheus即添加的数据源name点击Import按钮如下 Mysql监控展示