Docker搭建Prometheus+Grafana

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


一、安装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 \
-v /root/grafana/grafana.ini:/etc/grafana/grafana.ini \
--net=host \
--name grafana \
grafana/grafana

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

三、安装node-exporter

因为node-exporter是要采集服务器各项指标的,因此不采用docker方式安装。

到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: ['localhost:9090']
        labels:
          instance: prometheus
 
  - job_name: servers
    static_configs:
    
      - targets: ['localhost: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://localhost:9090/-/reload

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

五、给node-exporter添加密码认证

以后再写

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

最終更新日 2023-07-08