组件介绍
Prometheus Server:
普罗米修斯的主服务器
node_exporter – 用于机器系统数据收集
mysqld-exporter 用于MySQL数据库数据收集
Cadvisor 用于收集宿主机上的docker容器数据
Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化。它内置了以下数据源的支持:
安装运行Prometheus(docker版)
环境准备
主机名 | IP地址 | 安装服务 |
docker-server1 | 192.168.100.5 | docker、node-export、grafana、grafana |
docker-server2 | 192.168.100.6 | docker、node-export |
下载docker镜像
[root@docker-server1 ~]# docker pull prom/node-exporter
[root@docker-server1 ~]# docker pull prom/mysqld-exporter
[root@docker-server1 ~]# docker pull google/cadvisor:latest
[root@docker-server1 ~]# docker pull prom/prometheus
[root@docker-server1 ~]# docker pull grafana/grafana
[root@docker-server1 ~]# docker pull influxdb:1.8.10
#如果有需要可以将镜像下载出来
[root@docker-server1 ~]# docker save -a centos-prometheus prom/prometheus
[root@docker-server1 ~]# docker save -a centos-grafana grafana/grafana
[root@docker-server1 ~]# docker save -a centos-node-exporter prom/node-exporter
[root@docker-server1 ~]# docker save -a centos-mysqld-exporter pprom/mysqld-exporter
[root@docker-server1 ~]# docker save -a centos-cadvisor google/cadvisor
[root@docker-server1 ~]# docker save -o centos-influxdb-1.8.10 influxdb:1.8.10
启动node-exporter
[root@docker-server1 ~]# docker run -d --name node-exporter --restart=always -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro" prom/node-exporter
浏览器访问192.168.100.5/metrics
启动mysql-export
#如果没有mysql还需要下载mysql镜像
[root@docker-server1 ~]# docker run -itd -p 3306:3306 --name=mysql --privileged -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.32
#查询容器ip
[root@docker-server1 ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql
172.20.66.6
#启动mysql—export
[root@docker-server1 ~]# docker run -d --name mysqld_exporter --restart=always -p 9104:9104 -e DATA_SOURCE_NAME="root:Password123456@(172.20.0.2:3306)/" prom/mysqld-exporter
浏览器访问
启动cadvisor
[root@docker-server1 ~]# docker run -v /:/rootfs:ro -v /var/run:/var/run:rw -v /sys:/sys:ro -v /var/lib/docker/:/var/lib/docker:ro -v /dev/disk/:/dev/disk:ro -p 8080:8080 -d --name=cadvisor --restart=always google/cadvisor:latest
浏览器访问
启动influxdb
[root@docker-server1 ~]# docker run -d -p 8083:8083 -p 8086:8086 --expose 8090 --expose 8099 --name influxDbService influxdb:1.8.10
5861b41fc461e749072a20e30d06269d2f293e89c469df25fc8ce8d9779f8a21
[root@docker-server1 ~]# docker exec -it influxDbService /bin/bash
root@5861b41fc461:/# cd /etc/influxdb/
root@5861b41fc461:/etc/influxdb# cp influxdb.conf influxdb.conf.bak
root@5861b41fc461:/etc/influxdb# influx
Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
> create database prometheus;
> show databases;
name: databases
name
----
_internal
prometheus
> exit
root@5861b41fc461:/etc/influxdb# exit
exit
启动prometheus
#新建目录,编辑配置文件prometheus.yml
[root@docker-server1 ~]# mkdir /opt/prometheus
[root@docker-server1 ~]# cd /opt/prometheus/
[root@docker-server1 ~]# vim prometheus.yml
global:
scrape_interval: 60s
evaluation_interval: 60s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus
- job_name: linux
static_configs:
- targets: ['172.20.66.2:9100']
labels:
instance: localhost
- targets: ['172.20.13.2:9100']
labels:
instance: 192.168.100.6
- job_name: cadvisor
static_configs:
- targets: ['172.20.66.5:8080']
labels:
instance: cAdvisor
- job_name: mysqld
static_configs:
- targets: ['172.20.66.7:9104']
labels:
instance: mysql-exporter
remote_write:
- url: "http://172.20.66.8:8086/api/v1/prom/write?db=prometheus"
remote_read:
- url: "http://172.20.66.8:8086/api/v1/prom/read?db=prometheus"
#注意,每个job中的targets是你容器id,需要自己查,查询命令如下
[root@docker-server1 ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' cadvisor
172.20.66.5
[root@docker-server1 ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' 跟容器ID或名称
#最后启动prometheus-server
[root@docker-server1 ~]# docker run -d --name prometheus --restart=always -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
浏览器访问
192.168.100.5:9090/targets
查看数据是否存储到influxdb
[root@docker-server1 ~]# docker exec -it influxDbService bash
root@5861b41fc461:/# influx
Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
> use prometheus;
Using database prometheus
> show MEASUREMENTS;
#这里会显示一堆,随便查一个
> select * from scrape_series_added limit 5;
name: scrape_series_added
time __name__ instance job value
---- -------- -------- --- -----
1683599331922000000 scrape_series_added 192.168.100.6 linux 647
1683599333113000000 scrape_series_added mysql-exporter mysqld 128
1683599364776000000 scrape_series_added prometheus prometheus 486
1683599375207000000 scrape_series_added cAdvisor cadvisor 2627
1683599383493000000 scrape_series_added localhost linux 647
>
#只要有数据即可
启动grafana
#新建空文件夹grafana-storage,用来存储数据
[root@docker-server1 ~]# mkdir /opt/grafana-storage
[root@docker-server1 ~]# chmod 777 -R /opt/grafana-storage
[root@docker-server1 ~]# docker run -d --name grafana --restart=always -p 3000:3000 --name=grafana -v /opt/grafana-storage:/var/lib/grafana grafana/grafana
浏览器访问
192.168.100.5:3000
默认会先跳转到登录页面,默认的用户名和密码都是admin
登录之后,它会要求你重置密码。你还可以再输次admin密码!
这里name写prometheus,url写Prometheus的ip(docker容器私网ip)+端口,点击下面的Save & Test,如果出现绿色的,说明ok了