静看光阴荏苒
不管不顾不问不说也不念

Docker+Prometheus+Grafana监控VPS运行状态

Prometheus是一个灵活的时间序列数据库和监控系统,有多个监控组件可用。Grafana是用于展示数据的平台。

这几个工具我之前介绍过,不过当时我是用来做VPS的三网监控。实际上Prometheus能做的事情很多,就比如今天要介绍的全方位监控VPS主机运行状态。

prometheus和grafana为简化安装步骤,这里直接使用docker,首先你要安装docker/docker-compose:

apt -y update
apt -y install curl
curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker
curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

然后新建一个docker-compose.yml:

mkdir -p /opt/monitor && cd /opt/monitor && nano docker-compose.yml

写入下面的配置:

version: '3.5'

volumes:
    prometheus-data:
    grafana-data:

services:
    prometheus:
        image: prom/prometheus
        container_name: prometheus
        ports:
            - 9090:9090
        volumes:
            - prometheus-data:/prometheus
            - ./prometheus.yml:/etc/prometheus/prometheus.yml
        command:
            - '--config.file=/etc/prometheus/prometheus.yml'
            - '--web.enable-admin-api'
            - '--web.enable-lifecycle'

    grafana:
        image: grafana/grafana
        container_name: grafana
        ports:
            - 3000:3000
        volumes:
            - grafana-data:/var/lib/grafana

接着新建prometheus的配置文件:

nano prometheus.yml

写入如下配置:

global:
    scrape_interval: 5s
    external_labels:
        monitor: 'imlala'

scrape_configs:
    - job_name: 'prometheus'
      static_configs:
          - targets:
                - 'localhost:9090'
            labels:
                instance: prometheus 

    - job_name: 'VPS监控'
      static_configs:
          - targets:
                - '你小鸡的VPS公网IP:9100'
            labels:
                instance: localhost
          - targets:
                - '你的另一只小鸡的公网IP:9100'
            labels:
                instance: ikoula

有多少台VPS就写多少个targets,labels是用于区分。

确认无误后up起来即可:

docker-compose up -d

如果后续你更改了prometheus.yml配置文件内容,可以直接执行下面的命令让prometheus热加载配置,不用重启prometheus容器:

curl -s -XPOST localhost:9090/-/reload

接下来我们需要在每台被监控的小鸡上安装node_exporter,node_exporter是prometheus的数据采集器。

node_exporter设计是用于监控主机系统的,因为它需要访问主机系统,所以官方也不推荐将node_exporter容器化,建议是直接部署在宿主机上。

下载解压移动到usr/bin目录下:

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
tar -xzvf node_exporter-0.18.1.linux-amd64.tar.gz
cd node_exporter-0.18.1.linux-amd64/
cp node_exporter /usr/bin

新建systemd服务文件:

nano /etc/systemd/system/node_exporter.service

写入如下配置:

[Unit]
Description=Node Exporter

[Service]
User=root
ExecStart=/usr/bin/node_exporter

[Install]
WantedBy=multi-user.target

启动/设置开机自启:

systemctl start node_exporter.service
systemctl enable node_exporter.service

访问你的VPSIP:3000应该能看到grafana的WEBUI,默认的管理员账号密码都是admin。登录进去之后,你需要添加数据源:

如果你之前没有改动过docker-compose.yml内的服务名,那么这里应该填写:

其他的保持默认即可,然后点击Save & Test,测试没问题的话就OK了。

现在你需要新建或者导入模板,自己新建的没有人家现成做的好,这里直接导入别人的模板就行:

一些预览:

预览2:

赞(6)
未经允许不得转载:荒岛 » Docker+Prometheus+Grafana监控VPS运行状态
分享到: 更多 (0)

评论 32

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    lala为什么用V2ray电脑端进不了你网站,而用手机端却能进?

    Foliage4年前 (2020-03-23) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Android 8.1.0 Android 8.1.0回复
  2. #2

    安装完成后添加Prometheus数据保存时出现HTTP错误错误网关

    Foliage4年前 (2020-03-23) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    检测宿机是不是一样操作?

    Foliage4年前 (2020-03-23) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Android 8.1.0 Android 8.1.0回复
  4. #4

    我们高端用户从不监控,坏了就扔,扔了再买 :mrgreen:

    橘子4年前 (2020-03-23) Google Chrome 77.0.3833.112 Google Chrome 77.0.3833.112 Windows 7 x64 Edition Windows 7 x64 Edition回复
  5. #5

    这么华丽吃不吃资源?

    Ashin4年前 (2020-03-25) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Android 10 Android 10回复
    • 服务端和节点都不吃,节点20M内存就可以运行node_exporter。

      LALA4年前 (2020-03-30) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  6. #6

    Xshell 6官方推出特别正版公开注册码,免费3个月可以用,有订阅Xshell 官方邮件就可看到,或者gooogle可知,博主可广告 :mrgreen:

    s74年前 (2020-03-26) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 我去了解一哈,我一直还在用Xshell5 :razz:

      LALA4年前 (2020-03-30) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 官网https://www.netsarang.com/en/free-wfh-license-covid19/

        s74年前 (2020-03-30) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Windows 10 x64 Edition Windows 10 x64 Edition回复
  7. #7

    lala,我v2ray也进不来

    iyzyi4年前 (2020-03-30) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 我这一直都是正常的呀

      LALA4年前 (2020-03-30) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 手机用V2ray可以进来,PC端V2ray进不来,这问题很久之前就是这样。

        Foliage4年前 (2020-03-31) Microsoft Edge 45.2.10 Microsoft Edge 45.2.10 iPhone iOS 13.4 iPhone iOS 13.4回复
        • 那就不清楚了,我这v2ray都挺好的,没这个问题。。

          LALA4年前 (2020-04-02) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 电脑端V2不全局不行,规则有问题。

      小伙4年前 (2020-04-11) Google Chrome 78.0.3904.108 Google Chrome 78.0.3904.108 Windows 7 x64 Edition Windows 7 x64 Edition回复
  8. #8

    运行的时候显示Cannot open self /usr/local/bin/docker-compose or archive /usr/local/bin/docker-compose.pkg

    龙行天下4年前 (2020-04-02) Microsoft Edge 80.0.361.109 Microsoft Edge 80.0.361.109 Windows 10 x64 Edition Windows 10 x64 Edition回复
  9. #9

    楼主好,请问在GUI里面如何建立alert呢?谢谢

    刚戈4年前 (2020-04-03) Google Chrome 80.0.3987.149 Google Chrome 80.0.3987.149 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 这个我还没设置过,我也不是很清楚。。

      LALA4年前 (2020-04-04) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  10. #10

    一直没有数据是怎么回事

    龙行天下4年前 (2020-04-09) Microsoft Edge 80.0.361.111 Microsoft Edge 80.0.361.111 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 检查下防火墙开了相关端口了么。

      LALA4年前 (2020-04-10) Google Chrome 81.0.4044.92 Google Chrome 81.0.4044.92 Windows 10 x64 Edition Windows 10 x64 Edition回复
  11. #11

    电脑端V2不全局不行,规则有问题。

    小伙4年前 (2020-04-11) Google Chrome 78.0.3904.108 Google Chrome 78.0.3904.108 Windows 7 x64 Edition Windows 7 x64 Edition回复
  12. #12

    没有服务端/客户端认证的相关介绍,不同小鸡部署的话,不安全
    看了下官方,客户端需要nginx的密码配置,然而这种认证方式太古老,不安全

    test4年前 (2020-04-24) Safari 13.1 Safari 13.1 Mac OS X  10.15.4 Mac OS X 10.15.4回复
  13. #13

    lala能不能写一篇 在这篇文章的情况下 配置grafana https的文章 :smile:

    logo4年前 (2020-05-12) Google Chrome 81.0.4044.138 Google Chrome 81.0.4044.138 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • traefik就可以了,搜下我博客其他用docker安装的程序,把里面的配置复制出来就能用了。

      LALA4年前 (2020-05-13) Google Chrome 81.0.4044.92 Google Chrome 81.0.4044.92 Windows 10 x64 Edition Windows 10 x64 Edition回复
  14. #14

    ‘你小鸡的VPS公网IP:9100’
    老哥,请问这里是不是不能监控私有IP?

    Jacklove2年前 (2022-05-17) Google Chrome 101.0.4951.54 Google Chrome 101.0.4951.54 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 可以监控任意ip,但前提是prometheus能访问到才行,就这篇文章里面的‘你小鸡的VPS公网IP:9100’这个配置,这里是可以填内网ip的,因为这个配置只监控本机。

      LALA2年前 (2022-05-17) Google Chrome 86.0.4240.198 Google Chrome 86.0.4240.198 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 好的,感谢。刚才我用centos7.9试了没问题,用8919模板的时候看不到流量,其它都正常,换ubuntu在添加数据源的时候报错。Error reading Prometheus: Post “http://prometheus:9090/api/v1/query”: dial tcp: lookup prometheus: Try again 大佬有解吗

        Jacklove2年前 (2022-05-17) Google Chrome 101.0.4951.54 Google Chrome 101.0.4951.54 Windows 10 x64 Edition Windows 10 x64 Edition回复
        • 这个我就不是很清楚了。。没遇到过,要不你换个模板试试看。

          LALA2年前 (2022-05-18) Google Chrome 86.0.4240.198 Google Chrome 86.0.4240.198 Windows 10 x64 Edition Windows 10 x64 Edition回复
          • 这个问题搞定了,是docker的问题,用这个curl -sSL https://get.docker.com/ | sh 提示密钥有问题,我用yum install docker后面就再报错了。
            镜像那里我修改了下image: grafana/grafana:8.4.4 最新的版本界面有点奇怪。
            它死活不显示上下行带宽,监控的主机我用的是ESXI虚拟的多台主机。
            我在公网上也有一台监控小鸡,用的grafana版本同样是8.4.4,模板同样是8919,添加同一台被监控小鸡,它就能正常显示上下行带宽,内网的那台就不行。

            8919模板一样,gr版本一样,被监控的小鸡也一样,结果不一样。。。
            Instant 取消这个勾选可以显示带宽,不过一下子显示很多行

            Jacklove2年前 (2022-05-18) Google Chrome 101.0.4951.54 Google Chrome 101.0.4951.54 Windows 10 x64 Edition Windows 10 x64 Edition
  15. #15

    hostloc 过来的,跟着弄了一下感觉还行 :mrgreen: 。就是操作比较麻烦 :evil: ,核心部分换成了脚本操作 :lol: . 文章底部添加了原文链接,希望大佬不要捶我 :razz:
    https://iitii.github.io/2022/07/14/1/

    IITII2年前 (2022-07-14) Google Chrome 103.0.0.0 Google Chrome 103.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复

分享创造快乐

广告合作资源投稿