經過docker部署prometheus、node-exporter、alertmanager和grafana。prometheus最新版本:2.19.2html
系統 | ip | 角色 | cpu | 內存 | hostname |
---|---|---|---|---|---|
CentOS 7.8 | 192.168.30.135 | prometheus、node1 | >=2 | >=2G | prometheus |
CentOS 7.8 | 192.168.30.136 | altermanager、node2 | >=2 | >=2G | altermanager |
CentOS 7.8 | 192.168.30.137 | grafana、node3 | >=2 | >=2G | grafana |
systemctl stop firewalld && systemctl disable firewalldsed -i 's/=enforcing/=disabled/g' /etc/selinux/config && setenforce 0
curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo yum makecache fast yum install -y docker-ce systemctl start docker && systemctl enable docker
docker pull prom/node-exporter:latest docker run -d -p 9100:9100 --name node-exporter prom/node-exporter:latest docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95252704e558 prom/node-exporter:latest "/bin/node_exporter" 3 seconds ago Up 2 seconds 0.0.0.0:9100->9100/tcp node-exporternetstat -lntp |grep docker tcp6 0 0 :::9100 :::* LISTEN 9076/docker-proxy
mkdir /home/prom && cd /home/prom docker pull prom/alertmanager:latest docker pull timonwong/prometheus-webhook-dingtalk #釘釘告警vim config.yml
targets: webhook: url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx #修改成釘釘機器人的webhook mention: all: true
vim alertmanager.yml
global: resolve_timeout: 5m smtp_smarthost: 'smtp.163.com:465' #郵箱smtp服務器代理,啓用SSL發信, 端口通常是465 smtp_from: 'alert@163.com' #發送郵箱名稱 smtp_auth_username: 'alert@163.com' #郵箱名稱 smtp_auth_password: 'password' #郵箱密碼或受權碼 smtp_require_tls: falseroute: receiver: 'default' group_wait: 10s group_interval: 1m repeat_interval: 1h group_by: ['alertname']inhibit_rules:- source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'instance'] receivers:- name: 'default' email_configs: - to: 'receiver@163.com' send_resolved: true webhook_configs: - url: 'http://192.168.30.136:8060/dingtalk/webhook/send' send_resolved: true
docker run -d -p 8060:8060 -v /home/prom/config.yml:/etc/prometheus-webhook-dingtalk/config.yml --name alertdingtalk timonwong/prometheus-webhook-dingtalk docker run -d -p 9093:9093 -p 9094:9094 -v /home/prom/alertmanager.yml:/etc/alertmanager/alertmanager.yml --name alertmanager prom/alertmanager:latest docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bed08b1b251d prom/alertmanager:latest "/bin/alertmanager -…" 3 seconds ago Up 2 seconds 0.0.0.0:9093-9094->9093-9094/tcp alertmanager 1449d7d0a445 timonwong/prometheus-webhook-dingtalk "/bin/prometheus-web…" 21 seconds ago Up 20 seconds 0.0.0.0:8060->8060/tcp alertdingtalk cf90ea931188 prom/node-exporter:latest "/bin/node_exporter" 23 minutes ago Up 23 minutes 0.0.0.0:9100->9100/tcp node-exporternetstat -lntp |grep docker tcp6 0 0 :::9100 :::* LISTEN 14970/docker-proxy tcp6 0 0 :::8060 :::* LISTEN 17666/docker-proxy tcp6 0 0 :::9093 :::* LISTEN 17808/docker-proxy tcp6 0 0 :::9094 :::* LISTEN 17794/docker-proxy
mkdir -p /home/prom/data && chmod 777 /home/prom/data && cd /home/prom docker pull prom/prometheus:latest vim alert-rules.yml
groups: - name: node-alert rules: - alert: NodeDown expr: up{job="node"} == 0 for: 5m labels: severity: critical instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} down" description: "Instance: {{ $labels.instance }} 已經宕機 5分鐘" value: "{{ $value }}" - alert: NodeCpuHigh expr: (1 - avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="idle"}[5m]))) * 100 > 80 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} cpu使用率太高" description: "CPU 使用率超過 80%" value: "{{ $value }}" - alert: NodeCpuIowaitHigh expr: avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="iowait"}[5m])) * 100 > 50 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} cpu iowait 使用率太高" description: "CPU iowait 使用率超過 50%" value: "{{ $value }}" - alert: NodeLoad5High expr: node_load5 > (count by (instance) (node_cpu_seconds_total{job="node",mode='system'})) * 1.2 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} load(5m) 太高" description: "Load(5m) 太高,超出cpu核數 1.2倍" value: "{{ $value }}" - alert: NodeMemoryHigh expr: (1 - node_memory_MemAvailable_bytes{job="node"} / node_memory_MemTotal_bytes{job="node"}) * 100 > 90 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} memory 使用率太高" description: "Memory 使用率超過 90%" value: "{{ $value }}" - alert: NodeDiskRootHigh expr: (1 - node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"} / node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"}) * 100 > 90 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk(/ 分區) 使用率太高" description: "Disk(/ 分區) 使用率超過 90%" value: "{{ $value }}" - alert: NodeDiskBootHigh expr: (1 - node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"} / node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"}) * 100 > 80 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk(/boot 分區) 使用率太高" description: "Disk(/boot 分區) 使用率超過 80%" value: "{{ $value }}" - alert: NodeDiskReadHigh expr: irate(node_disk_read_bytes_total{job="node"}[5m]) > 20 * (1024 ^ 2) for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk 讀取字節數 速率太高" description: "Disk 讀取字節數 速率超過 20 MB/s" value: "{{ $value }}" - alert: NodeDiskWriteHigh expr: irate(node_disk_written_bytes_total{job="node"}[5m]) > 20 * (1024 ^ 2) for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk 寫入字節數 速率太高" description: "Disk 寫入字節數 速率超過 20 MB/s" value: "{{ $value }}" - alert: NodeDiskReadRateCountHigh expr: irate(node_disk_reads_completed_total{job="node"}[5m]) > 3000 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk iops 每秒讀取速率太高" description: "Disk iops 每秒讀取速率超過 3000 iops" value: "{{ $value }}" - alert: NodeDiskWriteRateCountHigh expr: irate(node_disk_writes_completed_total{job="node"}[5m]) > 3000 for: 5m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk iops 每秒寫入速率太高" description: "Disk iops 每秒寫入速率超過 3000 iops" value: "{{ $value }}" - alert: NodeInodeRootUsedPercentHigh expr: (1 - node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/"} / node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/"}) * 100 > 80 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk(/ 分區) inode 使用率太高" description: "Disk (/ 分區) inode 使用率超過 80%" value: "{{ $value }}" - alert: NodeInodeBootUsedPercentHigh expr: (1 - node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/boot"} / node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/boot"}) * 100 > 80 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} disk(/boot 分區) inode 使用率太高" description: "Disk (/boot 分區) inode 使用率超過 80%" value: "{{ $value }}" - alert: NodeFilefdAllocatedPercentHigh expr: node_filefd_allocated{job="node"} / node_filefd_maximum{job="node"} * 100 > 80 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} filefd 打開百分比太高" description: "Filefd 打開百分比 超過 80%" value: "{{ $value }}" - alert: NodeNetworkNetinBitRateHigh expr: avg by (instance) (irate(node_network_receive_bytes_total{device=~"eth0|eth1|ens33|ens37"}[1m]) * 8) > 20 * (1024 ^ 2) * 8 for: 3m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} network 接收比特數 速率太高" description: "Network 接收比特數 速率超過 20MB/s" value: "{{ $value }}" - alert: NodeNetworkNetoutBitRateHigh expr: avg by (instance) (irate(node_network_transmit_bytes_total{device=~"eth0|eth1|ens33|ens37"}[1m]) * 8) > 20 * (1024 ^ 2) * 8 for: 3m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} network 發送比特數 速率太高" description: "Network 發送比特數 速率超過 20MB/s" value: "{{ $value }}" - alert: NodeNetworkNetinPacketErrorRateHigh expr: avg by (instance) (irate(node_network_receive_errs_total{device=~"eth0|eth1|ens33|ens37"}[1m])) > 15 for: 3m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} 接收錯誤包 速率太高" description: "Network 接收錯誤包 速率超過 15個/秒" value: "{{ $value }}" - alert: NodeNetworkNetoutPacketErrorRateHigh expr: avg by (instance) (irate(node_network_transmit_packets_total{device=~"eth0|eth1|ens33|ens37"}[1m])) > 15 for: 3m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} 發送錯誤包 速率太高" description: "Network 發送錯誤包 速率超過 15個/秒" value: "{{ $value }}" - alert: NodeProcessBlockedHigh expr: node_procs_blocked{job="node"} > 10 for: 10m labels: severity: warning instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} 當前被阻塞的任務的數量過多" description: "Process 當前被阻塞的任務的數量超過 10個" value: "{{ $value }}" - alert: NodeTimeOffsetHigh expr: abs(node_timex_offset_seconds{job="node"}) > 3 * 60 for: 2m labels: severity: info instance: "{{ $labels.instance }}" annotations: summary: "instance: {{ $labels.instance }} 時間誤差過大" description: "Time 節點的時間誤差超過 3m" value: "{{ $value }}"
vim prometheus.yml
global: scrape_interval: 15s evaluation_interval: 15salerting: alertmanagers: - static_configs: - targets: - 192.168.30.136:9093rule_files: - "*rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.30.135:9090'] - job_name: 'node' static_configs: - targets: ['192.168.30.135:9100','192.168.30.136:9100','192.168.30.137:9100'] - job_name: 'alertmanager' static_configs: - targets: ['192.168.30.136:9093']
docker run -d -p 9090:9090 \ -v /home/prom/prometheus.yml:/etc/prometheus/prometheus.yml \ -v /home/prom/alert-rules.yml:/etc/prometheus/alert-rules.yml \ -v /home/prom/data:/prometheus --name prometheus prom/prometheus:latest docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a15519b970c prom/prometheus:latest "/bin/prometheus --c…" 3 seconds ago Up 2 seconds 0.0.0.0:9090->9090/tcp prometheus 95252704e558 prom/node-exporter:latest "/bin/node_exporter" 16 hours ago Up 16 hours 0.0.0.0:9100->9100/tcp node-exporternetstat -lntp |grep docker tcp6 0 0 :::9090 :::* LISTEN 9701/docker-proxy tcp6 0 0 :::9100 :::* LISTEN 9076/docker-proxy
訪問ip:9090
,node
容器部署完成,且規則配置完畢。接下來部署grafana,圖形化展現。linux
mkdir -p /home/prom/grafana && chmod 777 /home/prom/grafana docker pull grafana/grafana:latest docker run -d -p 3000:3000 -v /home/prom/grafana:/var/lib/grafana --name=grafana grafana/grafana:latest docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17d5c5d415b5 grafana/grafana:latest "/run.sh" 4 seconds ago Up 3 seconds 0.0.0.0:3000->3000/tcp grafana bd53df9eb9fa prom/node-exporter:latest "/bin/node_exporter" 16 hours ago Up 16 hours 0.0.0.0:9100->9100/tcp node-exporternetstat -lntp |grep docker tcp6 0 0 :::9100 :::* LISTEN 6558/docker-proxy tcp6 0 0 :::3000 :::* LISTEN 13123/docker-proxy
grafana容器配置文件:/etc/grafana/grafana.ini
。web
訪問ip:3000
,初始帳號密碼爲admin/admin
,會要求更改密碼。docker
選擇數據源Prometheus
——http://ip:9090
,導入模板8919
,vim
選擇alertmanager節點(192.168.30.136),關閉node-exporter容器。centos
docker stop node-exporter docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bed08b1b251d prom/alertmanager:latest "/bin/alertmanager -…" 38 minutes ago Up 38 minutes 0.0.0.0:9093-9094->9093-9094/tcp alertmanager 1449d7d0a445 timonwong/prometheus-webhook-dingtalk "/bin/prometheus-web…" 38 minutes ago Up 38 minutes 0.0.0.0:8060->8060/tcp alertdingtalk
收到釘釘和郵件故障告警,api
docker start node-exporter docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bed08b1b251d prom/alertmanager:latest "/bin/alertmanager -…" 47 minutes ago Up 47 minutes 0.0.0.0:9093-9094->9093-9094/tcp alertmanager 1449d7d0a445 timonwong/prometheus-webhook-dingtalk "/bin/prometheus-web…" 47 minutes ago Up 47 minutes 0.0.0.0:8060->8060/tcp alertdingtalk cf90ea931188 prom/node-exporter:latest "/bin/node_exporter" About an hour ago Up 2 seconds 0.0.0.0:9100->9100/tcp node-exporter
收到釘釘和郵件恢復告警,服務器
測試宕機完成,告警沒有問題。curl
docker部署 prometheus + grafana 完成,整個部署過程相比傳統部署方式要簡便不少。