简要介绍如何在docker环境下利用GrafanaPrometheus对系统进行监控。

软件安装

给Grafana软件集成LDAP实现单点登录一文中简要说明了如何基于docker安装Grafana,本节出于简化使用与维护的目的,将GrafanaPrometheus合并到一个docker-compose.yml文件中,相关的文件如下

docker-compose.yml文件

version: "3"
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: "prometheus_custom"
    restart: always
    ports:
      - "9090:9090"
    volumes:
      - "$PWD/prometheus.yml:/etc/prometheus/prometheus.yml"
      - "$PWD/prometheus_data:/prometheus"
  grafana:
    image: grafana/grafana
    container_name: "grafana_custom"
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - "$PWD/grafana_data:/var/lib/grafana"
      - "$PWD/ldap.toml:/etc/grafana/ldap.toml"
    environment:
      # 管理员账号
      - GF_SECURITY_ADMIN_USER=admin
      # 管理员密码
      - GF_SECURITY_ADMIN_PASSWORD=xxx
      - GF_AUTH_LDAP_ENABLED=true

ldap.toml文件,用于Grafana接入LDAP

[[servers]]
host = "10.10.0.55"
port = 389
use_ssl = false
start_tls = false
ssl_skip_verify = false
bind_dn = "cn=xxx,dc=chinahirain,dc=com"
bind_password = 'xxx'
search_filter = "(uid=%s)"
search_base_dns = ["dc=chinahirain,dc=com"]

[servers.attributes]
name = "givenName"
surname = "displayName"
username = "uid"
#member_of = "cn"
email =  "mail"

[[servers.group_mappings]]
group_dn = "grafana-admins"
org_role = "Admin"

[[servers.group_mappings]]
group_dn = "grafana-editors"
org_role = "Editor"

[[servers.group_mappings]]
group_dn = "*"
org_role = "Viewer"

prometheus.yml文件用于配置要接入Prometheus的系统

global:
  scrape_interval:     15s # 默认抓取周期
  external_labels:
    monitor: 'codelab-monitor'
scrape_configs:
  - job_name: 'node-exporter' #服务的名称
    scrape_interval: 5s
    metrics_path: /metrics  #获取指标的url
    static_configs:
      # 这个为监听指定服务服务的ip和port
      - targets: ['10.30.31.56:9188','10.30.5.113:9188']

上述这些文件必须位于同一目录下,之后通过docker-compose启动

root@lucumt:~/grafana# ls
docker-compose.yml  grafana_data  ldap.toml  prometheus.yml  prometheus_data
root@lucumt:~/grafana# docker-compose up -d

系统接入

主要基于node exporter实现,参考 centos7安装node export 一文进行相关操作。

  1. 在安装有node exporter服务器上的/usr/local/bin/目录下复制名为node_exporter的文件到目标机器的/usr/local/bin/目录下,并将其赋予可执行权限

    chmod +x /usr/local/bin/node_exporter
    
  2. 执行vi /etc/systemd/system/node_exporter.service并输入下述内容

    [Unit]
    Description=cicd_exporter
    After=network.target
    
    [Service]
    ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9188
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  3. 执行下述命令将其设置为Linux服务同时开机自启动

    systemctl daemon-reload && systemctl start node_exporter && systemctl enable node_exporter
    
  4. 在目标机器的浏览器中输入http://127.0.0.1:9188若能正常显示,则表示node_exporter安装成功

  5. 在安装Grafana的服务器上找到前述的prometheus.yml,在其targets下面添加对应的node_exporter服务,然后通过docker-compose restart重启相关服务

  6. Grafana中查看新添加的服务器是否集成成功