参考链接:
https://blog.rj-bai.com/post/158.html
Prometheus默认端口:9090 Grafana默认端口:3000 node_exporter默认端口:9104 mysqld_exporter默认端口:9100 alertmanager默认端口:9093 InfluxDB默认端口:8086
1.安装prometheus 2.安装node_exporter、mysqld_exporter 3.配置prometheus获取node_exporter与mysqld_exporter数据 4.安装Grafana 5.导入Grafana官网模板,对数据基本呈现 6.将prometheus数据存放到外部存储influxDB,而非自带tsdb 7.简单配置alertmanager
https://prometheus.io/download/
tar -zxf prometheus-2.19.2.linux-amd64.tar.gz -C /usr/local ln -s /usr/local/prometheus-2.19.2.linux-amd64 /usr/local/prometheus
# cat > /usr/lib/systemd/system/prometheus.service <<EOF [Unit] Description=prometheus server daemon [Service] Restart=on-failure ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml [Install] WantedBy=multi-user.target EOF # systemctl daemon-reload && systemctl start prometheus.service
# vi prometheus.yml global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: rule_files: scrape_configs: - job_name: 'prometheus' # 这个目录下的所有主机监控,将会归集到job为prometheus的标签下,要单独设置,需要修改此配置文件单独设置。 file_sd_configs: - files: ['/usr/local/prometheus/files_sd_configs/*.yml'] refresh_interval: 5s - job_name: 'nodes' file_sd_configs: - files: ['/usr/local/prometheus/nodes_sd_configs/*.yml'] refresh_interval: 5s
# 配置本机
# cd files_sd_configs/ # vi localhost.yml - targets: ['localhost:9090'] labels: name: czh02
# 利用kill -hup 发送signal hup热更新程序,使程序热加载配置文件
# ps aux | grep prometheus.yml | grep -v grep | awk {'print $2'} | xargs kill -hup
# grafana主配置文件在/usr/local/grafana-7.1.0/conf目录下,default.ini可以配置grafana数据目录
# http或https以及使用sqllite还是mysql/postgresql,都可以根据实际情况配置。
# wget https://dl.grafana.com/oss/release/grafana-7.1.0.linux-amd64.tar.gz # tar -zxvf grafana-7.1.0.linux-amd64.tar.gz -C /usr/local # groupadd grafana # useradd -g grafana grafana # passwd grafana # ln -s /usr/local/grafana-7.1.0 /usr/local/grafana-server # chown -R grafana:grafana /usr/local/grafana*
# cat > /usr/lib/systemd/system/grafana.service <<EOF [Unit] Description=grafana server [Service] User=grafana Group=grafana Restart=on-failure ExecStart=/usr/local/grafana-server/bin/grafana-server -homepath /usr/local/grafana-server web [Install] WantedBy=multi-user.target EOF
# systemctl daemon-reload # 重载配置文件 # systemctl start grafana # 启动grafana # systemctl status grafana # 查看grafana状态 # systemctl enable grafana # 配置开机自启动
http://ipaddr:3000,默认账号admin/admin登陆,先导入几个官网模板。
模板如果基于prometheus,则可以配置数据源prometheus,
模板如果基于mysql,则需要配置mysql hostname port user password等作为数据源。
模板如果基于prometheus node_exporter/mysqld_exporter则可以以prometheus作为数据源。
点击Grafana左侧设置,添加prometheus数据源,输入prometheus数据源IP地址跟端口即可。
# tar -zxvf node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local # cd /usr/local # ln -s node_exporter-1.0.1.linux-amd64/ node_exporter # cat > /usr/lib/systemd/system/node_exporter.service <<EOF [Unit] Description=node_exporter [Service] Restart=on-failure ExecStart=/usr/local/node_exporter/node_exporter [Install] WantedBy=multi-user.target EOF
# systemctl daemon-reload # systemctl start node_exporter.service # systemctl enable node_exporter
# 将默认收集暴露出去,默认启动之后即已经暴露,可以不做下面步骤,可以尝试访问IP:9100/metrics
# curl -s 127.0.0.1:9100/metrics | head
# 解压安装
# tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/ # ln -s mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
# 配置mysql_exproter连接数据库配置信息
# vi /usr/local/mysqld_exporter/my.cnf [client] host=127.0.0.1 user=root password=111111 port=20000
# 配置开机自启动
# cat > /usr/lib/systemd/system/mysqld_exporter.service <<EOF [Unit] Description=mysqld_exporter [Service] User=mysql Group=mysql Restart=on-failure ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/my.cnf [Install] WantedBy=multi-user.target EOF
# systemctl daemon-reload # systemctl start mysqld_exporter.service # systemctl enable mysqld_exporter.service # systemctl status mysqld_exporter.service
# 由于prometheus主配置文件配置了基于文件发现主机,所以在相应目录添加exporter主机配置即可
# cd /usr/local/prometheus/nodes_sd_configs # vi nodes_mysql02.yml - targets: ['192.168.204.82:9100'] labels: name: mysql02 # vi mysql_mysql02.yml - targets: ['192.168.204.82:9104'] labels: name: mysql02_mysql_server - targets: ['192.168.204.81:9104'] labels: name: mysql01_mysql_server
# 可以使用kill命令发送hup信号,重载配置文件,相当于热加载
# ps aux | grep prometheus.yml | grep -v grep | awk {'print $2'} | xargs kill -hup
# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.1.x86_64.rpm # yum localinstall influxdb-1.8.1.x86_64.rpm
# 修改influxDB数据存放目录以及wal日志存放目录,如果想修改默认端口8086,可以修改
# vi /etc/influxdb/influxdb.conf [data] # The directory where the TSM storage engine stores TSM files. dir = "/influxdb/data" # The directory where the TSM storage engine stores WAL files. wal-dir = "/influxdb/wal" [logging] level = "warn" # 修改级别,不输出info日志信息 [http] # The bind address used by the HTTP service. # bind-address = ":8086" log-enabled = false # 禁用http请求日志,防止prometheus不停刷新对influx读写请求日志 access-log-path = "/influxdb/influxdb.log" # http请求日志路径,防止将默认/var/log/messages刷新过快
# systemctl stauts influxdb # systemctl start influxdb # systemctl enable influxdb
有以下两种方法创建influx数据库:
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"
# influx Connected to http://localhost:8086 version 1.8.1 InfluxDB shell version: 1.8.1 > CREATE DATABASE "prometheus" > show databases; name: databases name ---- _internal prometheus
# 修改prometheus主配置文件,配置influx数据库api
# vi /usr/local/prometheus/prometheus.yml remote_write: - url: "http://localhost:8086/api/v1/prom/write?db=prometheus" remote_read: - url: "http://localhost:8086/api/v1/prom/read?db=prometheus"
重启prometheus
# systemctl restart prometheus
Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由。
它支持丰富的告警通知渠道,可以将告警信息转发到邮箱、企业微信、钉钉等。
# tar -zxvf alertmanager-0.21.0.linux-amd64.tar.gz -C /usr/local/ # ln -s /usr/local/alertmanager-0.21.0.linux-amd64/ /usr/local/alertmanager
# alertmanager主配置文件 # cat alertmanager.yml # 第一部分,全局配置,解析超时时间 global: resolve_timeout: 5m # 下面这部分是配置告警,配置告警怎么发送,怎么分配 route: group_by: ['alertname'] # alertmanager中的分组,选哪个标签作为分组的依据 group_wait: 10s # 分组等待时间,拿到第一条告警后等待10s,如果有其他的一起发送出去 group_interval: 10s # 各个分组之前发搜告警的间隔时间 repeat_interval: 1h # 重复告警时间,默认1小时 receiver: 'web.hook' # 接收者 # 下面这部分是配置告警的接收者,我要发送给谁。 receivers: - name: 'web.hook' webhook_configs: - url: 'http://127.0.0.1:5001/' # 这里用于配置告警收敛的,主要就是减少发送告警 inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'dev', 'instance']
# 讲解完默认主配置文件,下面配置使用邮箱发送
# cat alertmanager.yml global: resolve_timeout: 5m smtp_smarthost: 'smtp.qq.com:465' smtp_from: 'xxx@qq.com' smtp_auth_username: 'xxxx@qq.com' smtp_auth_password: 'xxxx' smtp_require_tls: false route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1m receiver: 'email' receivers: - name: 'email' email_configs: - to: 'xx@xxx.com'
# 验证配置文件
# ./amtool check-config alertmanager.yml Checking 'alertmanager.yml' SUCCESS Found: - global config - route - 0 inhibit rules - 1 receivers - 0 templates
# 默认为9093端口,如果需要修改可以启动时加 --web.listen-address=":9093"选项。
# cat > /usr/lib/systemd/system/alertmanager.service <<EOF [Unit] Description=alertmanager [Service] Restart=on-failure ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml [Install] WantedBy=multi-user.target EOF
# 编辑prometheus主配置文件,增加下面部分
# vi /usr/local/prometheus/prometheus.yml alerting: alertmanagers: - static_configs: - targets: - 127.0.0.1:9093 rule_files: - "rules/*.yml"
# cd /usr/local/prometheus
# mkdir rules
# cd rules
# vi alert.yml
groups:
- name: exporter.rules # 定义这组告警的组名,同性质的,都是监控实例exporter是否开启的模板
rules:
- alert: exporter_offline # 告警名称
expr: up == 0 # 告警表达式,监控up指标,如果等于0就进行下面的操作
for: 1m # 持续一分钟为0进行告警
labels: # 定义告警级别
severity: ERROR
annotations:
summary: "实例 {{ $labels.instance }} 采集器离线。"
description: "实例 {{ $labels.instance }} job 名为 {{ $labels.job }} 的采集器离线。"
# 启动alertmanager
# systemctl start alertmanager # systemctl status alertmanager # systemctl enable alertmanager

# alertmanager.yml文件配置告警全局配置,告警发送规则,分组规则,告警抑制规则。
# prometheus的rules告警规则文件,配置告警阈值,告警严重程度,告警规则,具体可参考官网。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31439444/viewspace-2706983/ ,如需转载,请注明出处,否则将追究法律责任。