背景

新增两台物理机用于压测,为了监控服务器状况,现须要对服务器进行监控,所有须要装置prometheus+grafana来监控服务。

筹备

  • 机器:

    机器1机器2
    192.168.1.103192.168.1.102
    mock服务,压测脚本业务服务
    prometheus+ grafana+node-exporter+cadvisornode-exporter+cadvisor

阐明:

   cadvisor: 用于收集容器的信息   node-exporter: 用于收集机器主机的信息   prometheus:别离把node-exporter和cadivsor的数据对立汇合   grafana: 页面web展现
  • 同步工夫
    因为物理机是新装的centos7.9的零碎,本地工夫和windows的工夫不统一,会导致后续的grafana不能拉取到数据no data. 同步网络工夫很重要。 date 查看工夫
  • 敞开防火墙或者凋谢相应的端口 systemctl status firewalld 查看防火墙状况
  • 机器的网口名要统一
    目前我的两台机器网口名一个是:p2p1, 一个是:enp3s0. 导致grafana页面填写收集的表达式device不能对立,很麻烦。
    node_network_receive_bytes_total{instance=~'$node', device="enp3s0", job="host"}
    ifconfig 查看网口名
装置ntpdyum install -y ntpd systemctl enable ntpdsystemctl  start ntpd systemctl status ntpd 同步工夫ntpdate -u ntp.sjtu.edu.cn 

开始部署

  • node-exporter
docker run  -v "/proc:/host/proc" -v  "/sys:/host/sys" -v "/:/rootfs" --net=host --restart=always --name node-exporter prom/node-exporter --path.procfs /host/proc --path.sysfs /host/sys --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"

阐明: node-exporter肯定要以host模式装置,bridge模式,不能收集到网口的数据。

  • cadvisor
    docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys/sys:ro --volume=/home/docker-file/:/var/lib/docker:ro --volume=/dev/disk:/dev/disk:ro --net=host --detach=true --name=cadvisor --restart=always google/cadvisor:latest

阐明: --volume=/home/docker-file:/var/lib/docker 这个指向的是本地docker服务运行的门路,我这里是指向了/home/docker-file门路,如果没有改变的话就是/var/lib/docker。df -h 能够查看门路。

  • prometheus
    docker run -d -v /home/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml --restart=always --net=host --name=prometheus prom/prometheus

prometheus.yaml

global:   scrape_interval: 15s   evaluation_interval: 15s   external_labels:      monitor: 'edc-lab-monitor'scrape_configs:  - job_name: 'prometheus'    static_configs:      - targets: ['192.168.1.103:9090']    - job_name: 'host'     static_configs:      - targets: ['192.168.1.102:9100', '192.168.1.103:9100']    - job_name: 'container'    static_configs:      - targets: ['192.168.1.102:8080', '192.168.1.103:8080']  

能够通过192.168.1.103:9090查看prometheus的数据收集状况,
在搜寻框中搜寻node_network_receive_bytes_total 能够查看网口进来的数据。

  • grafana
    docker run -d -i -p 3000:3000 -e "GF_SERVER_ROOT_URL=http://grafana.server.name" -e "GF_SECURITY_ADMIN_PASSWORD=admin123" --restart=always --name=grafana grafana/grafana

GF_SECURITY_ADMIN_PASSWORD:web登陆admin的明码

登陆grafana

http://192.168.1.103:3000/login 输出用户名: admin 明码:admin123

  1. 增加data sources

选中prometheus

填写http连贯:http://192.168.1.103:9090 而后点击save and test ok的话,数据增加胜利,不胜利,查看服务或者防火墙等。

  1. 增加grafana模版:
    点击左侧的“Home”-->Dashboards。在New按键下,抉择import

    罕用的模版是9276,193, 315, 6139,3146等,其余的能够在grafana官网上查找。

    抉择promethues, import胜利。

查看页面,分组抉择"host",能够看到主机的信息,容器的话,该模版中没显示进去,能够其余的容器模块来看容器的信息。