Docker搭建Prometheus+Grafana

投稿日時は 2023-05-07  2410 表示


一、安装Prometheus

创建文件夹

mkdir -p /root/prometheus

更改文件夹权限

chown 65534:65534 -R /root/prometheus

启动容器

docker run --restart=always -d \
-v /root/prometheus/data:/prometheus/data \
-v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
--net=host \
--name prometheus \
prom/prometheus \
--web.enable-lifecycle \
--web.enable-admin-api \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus/data/

二、安装Grafana

创建文件夹

mkdir -p /root/grafana

更改文件夹权限

chown 472:472 -R /root/grafana

启动容器

docker run --restart=always -d \
-v /root/grafana:/var/lib/grafana \
--net=host \
--name grafana \
grafana/grafana

访问 http://ip地址:3000,默认用户名密码admin,添加数据源,选择Prometheus

三、安装node-exporter

一键安装:

bash <(curl -sSLf "https://raw.githubusercontent.com/midori01/common-scripts/main/node-exporter/install.sh")

手动安装:

到https://github.com/prometheus/node_exporter/releases下载对应版本的二进制文件,并在每一个需要被监控的服务器上安装

以下以 Linux AMD64 1.5.0 版本为例,其他版本请自行替换文件名

wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz

解压

tar -zxvf node_exporter-1.5.0.linux-amd64.tar.gz

赋予权限

chmod +x /root/node_exporter-1.5.0.linux-amd64/node_exporter

创建服务

cat > /etc/systemd/system/node-exporter.service <<EOF
[Unit]
Description=node exporter

[Service]
Type=simple
ExecStart=/root/node_exporter-1.5.0.linux-amd64/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动服务并设置开机自启

systemctl daemon-reload

systemctl start node-exporter.service

systemctl enable node-exporter.service

四、配置Prometheus

编辑prometheus配置文件

vi /root/prometheus/prometheus.yml

配置文件示例

global:
  scrape_interval:     15s
  evaluation_interval: 15s
 
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['127.0.0.1:9090']
        labels:
          instance: prometheus
 
  - job_name: node-exporter
    static_configs:
    
      - targets: ['127.0.0.1:9100']
        labels:
          instance: Server1
          
      - targets: ['x.x.x.x:9100']
        labels:
          instance: Server2

      - targets: ['x.x.x.x:9100']
        labels:
          instance: Server3

热加载配置

curl -X POST http://127.0.0.1:9090/-/reload

访问 http://ip地址:9090/targets 稍等片刻就可以看到节点上线了

五、开启grafana匿名访问、给node-exporter、prometheus添加密码认证

太累不写了,请自行搜索相关资料

最终效果:https://lvlv.lv/grafana

最終更新日 2024-12-07