**Prometheus+Grafana+cAdvisor+node_exporter+Alertmanager 監控docker容器和配置釘釘報警java
特色:node
多維數據模型(有metric名稱和鍵值對肯定的時間序列) 靈活的查詢語言 不依賴分佈式存儲 支持圖表和dashboard等多種方式 經過pull方式採集時間序列,經過http協議傳輸 監控數據經過服務或者靜態配置來發現 支持經過中介網關的push時間序列的方式
功能:python
在業務層用做埋點系統:
Prometheus支持多種語言(Go,java,python,ruby官方提供客戶端,其餘語言有第三方開源客戶端)。咱們能夠經過客戶端方面的對核心業務進行埋點。以下單流程、添加購物車流程。mysql
在應用層用做應用監控系統:linux
一些主流應用能夠經過官方或第三方的導出器,來對這些應用作核心指標的收集。如redis,mysql。git
在系統層用做系統監控:
除了經常使用軟件, prometheus也有相關係統層和網絡層exporter,用以監控服務器或網絡。github
集成其餘的監控:golang
prometheus還能夠經過各類exporte,集成其餘的監控系統,收集監控數據,如AWS CloudWatch,JMX,Pingdom等等web
Prometheus架構:
prometheus是一個用Go編寫的時序數據庫,能夠支持多種語言客戶端,redis
安裝go 語言環境 在監控服務器上安裝prometheus 在被監控環境上安裝export 安裝grafana
一、安裝go 語言環境
wget -c https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz vim /etc/profile 在文件的最後添加以下內容: export PATH=$PATH:/usr/local/go/bin 保存退出後,接着讓配置文件馬上生效: source /etc/profile 驗證一下是否成功,用go version 來驗證 go version
2:安裝prometheus
官網下載地址:https://prometheus.io/download/ wget -c https://github.com/prometheus/prometheus/releases/download/v2.21.0-rc.0/prometheus-2.21.0-rc.0.linux-amd64.tar.gz tar -vxf prometheus-2.21.0-rc.0.linux-amd64.tar.gz -C /usr/local/ mv prometheus-2.21.0.linux-amd64/ prometheus
ln -sv /usr/local/prometheus-2.21.0-rc.0.linux-amd64/ /usr/local/prometheus 默認方式啓動: ./prometheus & 更換其餘端口啓動: ./prometheus --config.file=prometheus.yml --web.listen-address=:9010 &
瀏覽器輸入IP 默認端口9090
三、設置用戶
添加用戶,後期用此帳號啓動服務
groupadd prometheus useradd -g prometheus -s /sbin/nologin prometheus
賦權
cd ~ chown -R prometheus:prometheus /usr/local/prometheus/
建立prometheus運行數據目錄
mkdir -p /var/lib/prometheus chown -R prometheus:prometheus /var/lib/prometheus/
四、設置開機啓動
touch /usr/lib/systemd/system/prometheus.service chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service vim /usr/lib/systemd/system/prometheus.service [Unit] Description=Prometheus Documentation=https://prometheus.io/ After=network.target [Service] # Type設置爲notify時,服務會不斷重啓 Type=simple User=prometheus # --storage.tsdb.path是可選項,默認數據目錄在運行目錄的./dada目錄中 ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus Restart=on-failure [Install] WantedBy=multi-user.target
設置開機啓動
systemctl enable Prometheus systemctl start prometheus
繪圖:
http://192.168.10.117:9090/metrics ,查看從exporter具體能抓到的數據,以下:
訪問:prometheus,在輸入框中任意輸入1個exporter能抓取得值,點 擊"Execute"與"Execute"按鈕,便可見相應抓取數據的圖形,同時可對 時間與unit作調整,
Node_exporter收集機器的系統數據,這裏採用prometheus官方提供的exporter
一、下載安裝
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-arm64.tar.gz tar -xvzf node_exporter-0.18.1.linux-arm64.tar.gz cd /usr/local mv node_exporter-0.18.1.linux-arm64.tar.gz /usr/local/node_exporter
設置用戶
groupadd prometheus useradd -g prometheus -s /sbin/nologin prometheus chown -R prometheus:prometheus /usr/local/node_exporter/
設置開機啓動
vim /usr/lib/systemd/system/node_exporter.service [Unit] Description=node_exporter Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target 啓動: systemctl enable node_exporter systemctl start node_exporter 直接啓動方式: 修改端口添加: --web.listen-address=":9013" & 命令:./node_exporter --web.listen-address=":9013" & prometheus.yml文件下添加: - job_name: 'docker' static_configs: - targets: [192.168.10.117:9012']
二、驗證
訪問prometheus,查看node1主機已經可被監控
下載地址:https://grafana.com/grafana/download 選擇須要的版本和安裝的系統
wget https://dl.grafana.com/oss/release/grafana-7.1.5-1.x86_64.rpm sudo yum install grafana-7.1.5-1.x86_64.rpm
將grafana加入到系統服務並啓動
sudo /sbin/chkconfig --add grafana-server sudo service grafana-server start 啓動後,打開URL,默認端口爲3000,http://192.168.10.117:3000/ 用戶名和密碼都爲 admin
修改grafana默認訪問端口 我修改成9008
一、授予grafana二進制權限 sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/grafana-server 二、將端口9008重定向到Grafana端口3000 iptables -t nat -A PREROUTING -p tcp --dport 9011 -j REDIRECT --to-port 3000 三、修改配置文件/etc/grafana/grafana.ini中端口爲9008,並重啓
啓動: service grafana-server start
使用8919面板:https://grafana.com/grafana/dashboards/8919
這裏使用docker部署
一、 拉取鏡像 docker pull docker.io/google/cadvisor 二、運行cadvisor容器,並配置 docker run \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --publish=8080:8080 \ --detach=true \ --name=cadvisor \ google/cadvisor:latest 啓動: docker restart 448ded02b68d 三、啓動容器:docker restart 117dc405fbf5
curl:ip:8080端口:
一、登錄 訪問:http://localhost:3000,默認帳號/密碼:admin/admin
二、添加Premetheyus數據源:
三、配置Premetheyus地址和源
四、能夠先測試一下
五、查看數據源:
六、進入剛纔逛街的數據源中:
七、OK配置完成
九、dashboard導入
從grafana官網下載相關dashboard到本地,也能夠直接導入到grafana中
https://grafana.com/dashboards/1860 一、配置面板:
二、本地導入儀表盤 複製上方鏈接、
一、複製釘釘羣地址:
二、添加關鍵詞: 添加了以下關鍵字:alert、promethues、alertmanager、webhook。可自行決定添加幾個。
三、Grafana配置:
四、報錯問題: 在打開Include image時報錯 沒有安裝grafana-cli 解決方法: yum -y install libatk-bridge* libXss* libgtk* grafana-cli plugins install grafana-image-renderer 五、重啓grafana:service grafana-server restart
發送報警測試:報警的時候就有圖片啦
官網地址:https://grafana.com/grafana/dashboards?search=docker 一、配置圖形面板:
https://grafana.com/grafana/dashboards/11277 二、查看圖形內容:
https://grafana.com/dashboards/193面板: Prometheus監控端查docker數據 container_cpu_load_average_10s
1. 下載&安裝 wget https://github.com/prometheus/alertmanager/releases/download/v0.19.0/alertmanager-0.19.0.linux-amd64.tar.gz tar xzf alertmanager-0.19.0.linux-amd64.tar.gz mv alertmanager-0.19.0.linux-amd64 /usr/local/alertmanager cd /alertmanager cp alertmanager.yml alertmanager.yml 配置文件默認: cat alertmanager.yml global: resolve_timeout: 5m route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1h receiver: webhook receivers: - name: webhook webhook_configs: - url: 'http://127.0.0.1:5001/' inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'dev', 'instance'] 三、啓動alertmanager ./alertmanager --config.file=alertmanager.yml 2>&1 1>altermanager.log & 查看端口 netstat -anpt | grep 9093 四、報警規則 cat dingding.yml
groups: - name: test-rule rules: - alert: 主機狀態 expr: up == 0 for: 2m labels: status: warning annotations: summary: "{{$labels.instance}}:服務器關閉" description: "{{$labels.instance}}:服務器關閉"
五、修改prometheus配置文件 修改alerting和dingding # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"] # - alertmanager:9093 rule_files: - "rules.yml" 六、將釘釘接入 Prometheus AlertManager WebHook 插件下載地址:https://github.com/timonwong/prometheus-webhook-dingtalk
安裝
mkdir -p /usr/lib/golang/src/github.com/timonwong/ cd /usr/lib/golang/src/github.com/timonwong/ git clone https://github.com/timonwong/prometheus-webhook-dingtalk.git cd prometheus-webhook-dingtalk make
啓動 不想寫了