• Prometheus 使用cadvisor采集docker容器监控数据


    前言cadvisor

    cadvisor_exporter 是一个用于将 cAdvisor 监控数据暴露给 Prometheus 的开源软件。它充当一个代理,运行在与 cAdvisor 相同的主机上,并通过查询 cAdvisor 提供的 API 来获取容器的性能指标和状态信息。

    cadvisor_exporter 从 cAdvisor 收集各种容器的资源使用情况,如 CPU 利用率、内存使用量、文件系统使用情况、网络流量等,并将这些指标数据转换成 Prometheus 可以理解的格式。然后,它将这些指标数据暴露给 Prometheus,使得 Prometheus 可以定期从 cadvisor_exporter 暴露的 HTTP 端点获取数据,并将其存储、分析和可视化。

    通过与 Prometheus 结合使用,cadvisor_exporter 可以帮助管理员监视和管理容器集群中各个容器的性能和状态。管理员可以使用 Prometheus 查询语言构建自定义的监控规则和警报条件,以便在容器出现问题或性能异常时及时发出警报。

    download cadvisor

    • cadvisor采集数据
    • docker 方式部署cadvisor
    • 端口 8082
    • 安装目录/opt/cadvisor

     cadvisor 一键安装监控脚本 

    vim /cadvisor_install.sh
    1. #!/bin/bash
    2. # -*- coding: utf-8 -*-
    3. # Author: make.han
    4. # Email: CIASM@CIASM
    5. # Date: 2024/01/16
    6. OPT_CATALOGUE=/opt
    7. CADVISOR_EXPORTER_PREFIX=/opt/cadvisor
    8. CADVISOR_EXPORTER_URL=https://github.com/google/cadvisor/releases/download/v0.47.2/cadvisor-v0.47.2-linux-amd64
    9. CADVISOR_EXPORTER_BINARY=cadvisor-v0.47.2-linux-amd64
    10. CADVISOR_EXPORTER_NEW=cadvisor
    11. CADVISOR_EXPORTER_PORT=8082
    12. install_cadvisor_exporter (){
    13. if [ ! -d ${CADVISOR_EXPORTER_PREFIX} ];then
    14. # selinux stop
    15. setenforce 0
    16. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    17. if [ $? -eq 0 ];then
    18. # create directory
    19. mkdir -p $CADVISOR_EXPORTER_PREFIX
    20. echo "download cadvisor"
    21. wget -N -P $CADVISOR_EXPORTER_PREFIX $CADVISOR_EXPORTER_URL
    22. echo "Compile and install & config"
    23. mv $CADVISOR_EXPORTER_PREFIX/$CADVISOR_EXPORTER_BINARY $CADVISOR_EXPORTER_PREFIX/$CADVISOR_EXPORTER_NEW
    24. # Grant execution permission
    25. chmod +x $CADVISOR_EXPORTER_PREFIX/$CADVISOR_EXPORTER_NEW
    26. # cadvisor environment variable
    27. sed -i '$aexport CADVISOR_HOME='$CADVISOR_EXPORTER_PREFIX'' /etc/profile
    28. sed -i '$aexport PATH=\$CADVISOR_HOME/:\$PATH' /etc/profile
    29. source /etc/profile
    30. echo "cadvisor Start Service"
    31. cat >>/usr/lib/systemd/system/cadvisor.service<
    32. [Unit]
    33. Description=cadvisor
    34. Documentation=https://github.com/google/cadvisor/tree/master/docs
    35. After=network.target
    36. [Service]
    37. Type=simple
    38. ExecStart=$CADVISOR_EXPORTER_PREFIX/$CADVISOR_EXPORTER_NEW -port $CADVISOR_EXPORTER_PORT
    39. Restart=on-failure
    40. [Install]
    41. WantedBy=multi-user.target
    42. EOF
    43. echo " cadvisor Example Set the automatic startup service"
    44. systemctl daemon-reload
    45. systemctl enable --now cadvisor
    46. # cadvisor version
    47. cadvisor -version | awk '{print $3}'
    48. echo "firewall cadvisor_exporter port"
    49. firewall-cmd --zone=public --add-port=$CADVISOR_EXPORTER_PORT/tcp --permanent && firewall-cmd --reload
    50. echo -e "\033[32mThe cadvisor_exporter Install Sussess...\033[0m"
    51. else
    52. echo -e "\033[33mThe cadvisor_exporter Install Failed...\033[0m"
    53. exit 1
    54. fi
    55. else
    56. echo -e "\033[31mThe cadvisor_exporter Install already...\033[0m"
    57. fi
    58. }
    59. main (){
    60. install_cadvisor_exporter
    61. }
    62. main

    执行部署

    bash /cadvisor_install.sh

    页面访问

    • http://YOU_IP:8082 

    Prometheus 数据采集配置

    1. scrape_configs:
    2. - job_name: 'web'
    3. scrape_interval: 5s
    4. static_configs:
    5. - targets:
    6. - localhost:9090
    7. - job_name: 'docker-cadvisor-monitoring'
    8. scrape_interval: 5s
    9. static_configs:
    10. - targets:
    11. - 192.168.11.230:8082

    Grafana图形展示

    第一种图形Import>13631>Prometheus

    第二种图形Import>11600>Prometheus

    第三种图形Import>13496>Prometheus

    第四种种图形Import>18351>Prometheus

    • 模板下载
    • grafana 版本要求9

     第五种种图形Import>19908>Prometheus

    • 模板下载
    • grafana 版本要求10

  • 相关阅读:
    Mybatis-Plus 3.5.2 + Freemake代码生成器
    【Java集合框架】20 ——HashTable 类
    yangwebrtc x86_64环境搭建
    备战数学建模34-BP神经网络预测2
    【信号去噪】基于遗传算法优化VMD实现信号去噪附matlab代码
    Linux快速入门
    JVM学习——1——虚拟机基础概念
    react antd实现upload上传文件前form校验,同时请求带data
    企业选择快鲸开源scrm系统的7大理由
    【初识 Docker | 基础篇】 Docker 搭建仓库
  • 原文地址:https://blog.csdn.net/hanzheng260561728/article/details/127996386