docker安装prometheus+Granfan并监控容器

docker安装普罗米修斯+Granfan并监控容器

一、基本概念

​ 1、之间的关系
​ prometheus与grafana之间是相辅相成的关系。作为完美的分布式监控系统的Prometheus,就想布加迪威龙一样示例和动力强劲。在猛的车也少不了仪表盘来观察。于是优雅的可视化平台Grafana出现了。

​ 简而言之Grafana作为可视化的平台,平台的数据从Prometheus中取到来进行仪表盘的展示。而Prometheus这源源不断的给Grafana提供数据的支持。

​ 2、基本工作原理

  • Exporter 监控工具,获取数据
  • Prometheus 普罗米修斯时序数据库,用来存储和查询你的监控数据
  • Grafana 仪表盘

二、了解概念之- Prometheus

    Prometheus是一个开源的系统监控和报警系统,由Google BorgMon监控系统的开源版本发展而来。它使用Go语言开发,能够监控和告警各种系统,包括网络、存储、服务器和应用等。Prometheus通过采集大量监控数据并存储到时间序列数据库中,然后通过查询语言PromQL进行数据查询和可视化。它已经成为继k8s之后第二大在CNCF托管的项目,被广泛应用于各种数据中心环境的监控。

三、了解概念之—Grafana

    Grafana是一个开源的度量分析和可视化套件,常被用于数据分析和监控。它支持多种不同的数据源,包括Graphite、Elasticsearch、InfluxDB、Prometheus等,并能够将数据以各种图形的方式展示出来,比如折线图、柱状图、热图等。Grafana的仪表盘可以用来展示各种信息,比如系统性能、网络延迟、用户行为等。Grafana还支持自定义查询和可视化的功能,可以根据不同的需求来定制数据展示。

四、环境准备

系统 IP 配置 角色
cnetos7.9 192.168.8.180 2c4g NodeEXporter、cAdvisor、 Prometheus Server、Grafana
cnetos7.9 192.168.8.181 2c4g NodeEXporter、cAdvisor

将包分别存放至两台机器

google-cadvisor.tar.gz grafana.tar.gz node-exporter.tar.gz prometheus.tar.gz

组件说明

Prometheus Server: 普罗米修斯的主服务器,端口号9090
NodeEXporter: 负责收集Host硬件信息和操作系统信息,端口号9100
cAdvisor:负责收集Host上运行的容器信息,端口号占用8080
Grafana:负责展示普罗米修斯监控界面,端口号3000

altermanager:等待接收prometheus发过来的告警信息,altermanager再发送给定义的收件人

1.安装node-EXporter

作用: 收集硬件和系统信息
#导入镜像
[root@localhost docker]# docker load -i node-exporter.tar.gz
1e604deea57d: Loading layer [==================================================>]  1.458MB/1.458MB
6b83872188a9: Loading layer [==================================================>]  2.455MB/2.455MB
4f3f7dd00054: Loading layer [==================================================>]   20.5MB/20.5MB
Loaded image: prom/node-exporter:latest

#运行容器
[root@localhost docker]# docker run -d -p 9100:9100 -v /proc:/host/proc -v /sys:/host/sys -v /:/rootfs --net=host prom/node-exporter --path.procfs /host/proc --path.sysfs /host/sys --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
WARNING: Published ports are discarded when using host network mode
3f3abcc55d5ab28922a268d6fe60a1f9382e71808367ca1c15d2c2eb89ae7bc8
[root@localhost docker]#

#查看运行状态
[root@localhost docker]# docker ps -a
CONTAINER ID   IMAGE                COMMAND                   CREATED          STATUS          PORTS     NAMES
3f3abcc55d5a   prom/node-exporter   "/bin/node_exporter …"   44 seconds ago   Up 40 seconds             upbeat_agnesi
[root@localhost docker]# 

验证收集效果http://192.168.8.180:9100/metrics

2、安装cAdvisor

作用: 收集节点容器信息

#导入镜像
[root@localhost docker]# docker load -i google-cadvisor.tar.gz
cd7100a72410: Loading layer [==================================================>]  4.403MB/4.403MB
9ea477e6d99e: Loading layer [==================================================>]  33.09MB/33.09MB
66b3c2e84199: Loading layer [==================================================>]  32.88MB/32.88MB
Loaded image: google/cadvisor:latest
[root@localhost docker]#


#运行容器
[root@localhost docker]# docker run -v /:/rootfs:ro -v /var/run:/var/run/:rw -v /sys:/sys:ro -v /var/lib/docker:/var/lib/docker:ro -p 8080:8080 --detach=true --name=cadvisor --net=host google/cadvisor
WARNING: Published ports are discarded when using host network mode
477a383ea1943384f96a70dc1a9cd8d101565a5e3cca861cff6b6c0ed990efc5
[root@localhost docker]#

#查看运行状态
[root@localhost docker]# docker ps -a
CONTAINER ID   IMAGE                COMMAND                   CREATED          STATUS          PORTS     NAMES
477a383ea194   google/cadvisor      "/usr/bin/cadvisor -…"   32 seconds ago   Up 31 seconds             cadvisor
3f3abcc55d5a   prom/node-exporter   "/bin/node_exporter …"   5 minutes ago    Up 5 minutes              upbeat_agnesi
[root@localhost docker]# 

http://192.168.8.180:8080/containers/

验证收集效果传递

3、部署prometheus-server服务

先启动一个prometheus服务,目的是复制他的配置文件,修改配置文件,prometheus挂载这个文件

[root@localhost docker]# docker load -i prometheus.tar.gz 
405a8b5041f1: Loading layer [==================================================>]  140.1MB/140.1MB
6dfd7aef3fcd: Loading layer [==================================================>]  131.8MB/131.8MB
8459ad25bfbd: Loading layer [==================================================>]  3.584kB/3.584kB
6cd41730d304: Loading layer [==================================================>]  13.82kB/13.82kB
dc12aa49f027: Loading layer [==================================================>]  28.16kB/28.16kB
f61f35ce8414: Loading layer [==================================================>]  13.31kB/13.31kB
89911f5a9a27: Loading layer [==================================================>]  5.632kB/5.632kB
e3df5cefd22c: Loading layer [==================================================>]  139.8kB/139.8kB
64d73d1a1b52: Loading layer [==================================================>]  1.536kB/1.536kB
b4d8bcc5b63b: Loading layer [==================================================>]  6.144kB/6.144kB
Loaded image: prom/prometheus:latest
[root@localhost docker]#


[root@localhost docker]# mkdir -pv  /prometheus
mkdir: 已创建目录 "/prometheus"
[root@localhost docker]#

#运行为容器,将容器的/etc/prometheus/prometheus.yml配置文件拷贝到宿主机
[root@localhost docker]# docker run -d --name test -P prom/prometheus
e449972ad85a035b62ae3aaca013c7f59d5000e7701df331b4bde060fcb37aa9
[root@localhost docker]#

#将容器内的/etc/prometheus/prometheus.yml文件拷贝到宿主机的/prometheus
[root@localhost docker]# docker cp test:/etc/prometheus/prometheus.yml /prometheus
Successfully copied 2.56kB to /prometheus

[root@localhost docker]# ll -l /prometheus/
总用量 4
-rw-r--r--. 1 root root 934 8月  27 19:11 prometheus.yml
[root@localhost docker]# 

#编辑prometheus配置文件,在static_configs下面修改为
#以下添加的ip都将会被监控起来
[root@localhost docker]# more /prometheus/prometheus.yml 
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["192.168.8.180:9090","192.168.8.180:8080","localhost:9100","192.168.8.181:8080","192.168.8.181:9100"]
[root@localhost docker]#

4、删除test容器并重新运行prometheus

[root@localhost docker]# docker rm -f test
test

[root@localhost docker]# docker run -d --name prometheus --net host -p 9090:9090 -v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
WARNING: Published ports are discarded when using host network mode
9274dfcd700dae2b9e4b23fa2212ffcc34a4cdc2ae767d6d0bfacc83763cf8ac

[root@localhost docker]# docker ps -a
CONTAINER ID   IMAGE                COMMAND                   CREATED          STATUS          PORTS     NAMES
9274dfcd700d   prom/prometheus      "/bin/prometheus --c…"   4 seconds ago    Up 3 seconds              prometheus
477a383ea194   google/cadvisor      "/usr/bin/cadvisor -…"   15 minutes ago   Up 15 minutes             cadvisor
3f3abcc55d5a   prom/node-exporter   "/bin/node_exporter …"   20 minutes ago   Up 20 minutes             upbeat_agnesi
[root@localhost docker]# 

5、访问测试

6、在prometheus服务器上部署grafana

grafana主要概念
  • 插件:扩展功能作用,完成不能完成的事
  • 数据源:连接数据源,通过数据源提供数据 来出图
  • dashboard:展示面板,出什么样的图
[root@localhost docker]# docker load -i grafana.tar.gz 
d4fc045c9e3a: Loading layer [==================================================>]  7.667MB/7.667MB
89b7e78c1264: Loading layer [==================================================>]   2.56kB/2.56kB
63c0e4417684: Loading layer [==================================================>]  8.806MB/8.806MB
de6ac4ba26dc: Loading layer [==================================================>]  9.713MB/9.713MB
6523361408ca: Loading layer [==================================================>]    190kB/190kB
11f6d7670b43: Loading layer [==================================================>]  104.4kB/104.4kB
6271cd8eca5a: Loading layer [==================================================>]  244.4MB/244.4MB
4beb5a56ae2e: Loading layer [==================================================>]  215.2MB/215.2MB
9c67d537403a: Loading layer [==================================================>]  37.89kB/37.89kB
e7392c8d1dd0: Loading layer [==================================================>]   5.12kB/5.12kB
Loaded image: grafana/grafana:latest
[root@localhost docker]#

[root@localhost docker]# mkdir -pv  /grafana
mkdir: 已创建目录 "/grafana"
[root@localhost docker]# chmod 777 -R /grafana

[root@localhost docker]# docker run -d -p 3000:3000 --name grafana -v /grafana:/var/lib/grafana -e "GF_SECURITY_ADMIN_PASSWORD=admin" grafana/grafana
736af9f6c1f55a2dbf8a7d3bf7de4d17f9555a2f8340bce8ccc2b0984fd09b77
[root@localhost docker]#


"GF_SECURITY_ADMIN_PASSWORD="  #是要设置grafana的登录密码


1.

访问测试

http://192.168.8.180:3000/login

默认用户名密码

用户名:admin

密码:admin