{"id":241586,"date":"2022-05-08T10:59:22","date_gmt":"2022-05-08T02:59:22","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=241586"},"modified":"2022-04-29T10:00:36","modified_gmt":"2022-04-29T02:00:36","slug":"go-all-prometheus","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-all-prometheus.html","title":{"rendered":"\u5165\u95e8Prometheus"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b66\u4e60\u5b89\u88c5 Prometheus \u76d1\u63a7\u548c\u8b66\u62a5\u7cfb\u7edf\u5e76\u7f16\u5199\u5b83\u7684\u67e5\u8be2\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Prometheus \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u76d1\u63a7\u548c\u8b66\u62a5\u7cfb\u7edf\uff0c\u5b83\u76f4\u63a5\u4ece\u76ee\u6807\u4e3b\u673a\u4e0a\u8fd0\u884c\u7684\u4ee3\u7406\u7a0b\u5e8f\u4e2d\u6293\u53d6\u6307\u6807\uff0c\u5e76\u5c06\u6536\u96c6\u7684\u6837\u672c\u96c6\u4e2d\u5b58\u50a8\u5728\u5176\u670d\u52a1\u5668\u4e0a\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u50cf collectd_exporter \u8fd9\u6837\u7684\u63d2\u4ef6\u63a8\u9001\u6307\u6807\uff0c\u5c3d\u7ba1\u8fd9\u4e0d\u662f Promethius \u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u4f46\u5728\u4e3b\u673a\u4f4d\u4e8e\u9632\u706b\u5899\u540e\u9762\u6216\u4f4d\u4e8e\u5b89\u5168\u7b56\u7565\u7981\u6b62\u6253\u5f00\u7aef\u53e3\u7684\u67d0\u4e9b\u73af\u5883\u4e2d\u5b83\u53ef\u80fd\u5f88\u6709\u7528\u3002<\/p>\n

Prometheus \u662f\u4e91\u539f\u751f\u8ba1\u7b97\u57fa\u91d1\u4f1a\uff08CNCF\uff09\u7684\u4e00\u4e2a\u9879\u76ee\u3002\u5b83\u4f7f\u7528\u8054\u5408\u6a21\u578bfederation model\u8fdb\u884c\u6269\u5c55\uff0c\u8be5\u6a21\u578b\u4f7f\u5f97\u4e00\u4e2a Prometheus \u670d\u52a1\u5668\u80fd\u591f\u6293\u53d6\u53e6\u4e00\u4e2a Prometheus \u670d\u52a1\u5668\u7684\u6570\u636e\u3002\u8fd9\u5141\u8bb8\u521b\u5efa\u5206\u5c42\u62d3\u6251\uff0c\u5176\u4e2d\u4e2d\u592e\u7cfb\u7edf\u6216\u66f4\u9ad8\u7ea7\u522b\u7684 Prometheus \u670d\u52a1\u5668\u53ef\u4ee5\u6293\u53d6\u5df2\u4ece\u4e0b\u7ea7\u5b9e\u4f8b\u6536\u96c6\u7684\u805a\u5408\u6570\u636e\u3002<\/p>\n

\u9664 Prometheus \u670d\u52a1\u5668\u5916\uff0c\u5176\u6700\u5e38\u89c1\u7684\u7ec4\u4ef6\u662f\u8b66\u62a5\u7ba1\u7406\u5668\u53ca\u5176\u8f93\u51fa\u5668\u3002<\/p>\n

\u8b66\u62a5\u89c4\u5219\u53ef\u4ee5\u5728 Prometheus \u4e2d\u521b\u5efa\uff0c\u5e76\u914d\u7f6e\u4e3a\u5411\u8b66\u62a5\u7ba1\u7406\u5668\u53d1\u9001\u81ea\u5b9a\u4e49\u8b66\u62a5\u3002\u7136\u540e\uff0c\u8b66\u62a5\u7ba1\u7406\u5668\u5904\u7406\u548c\u7ba1\u7406\u8fd9\u4e9b\u8b66\u62a5\uff0c\u5305\u62ec\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u6216\u7b2c\u4e09\u65b9\u670d\u52a1\uff08\u5982 PagerDuty\uff09\u7b49\u4e0d\u540c\u673a\u5236\u53d1\u9001\u901a\u77e5\u3002<\/p>\n

Prometheus \u7684\u8f93\u51fa\u5668\u53ef\u4ee5\u662f\u5e93\u3001\u8fdb\u7a0b\u3001\u8bbe\u5907\u6216\u4efb\u4f55\u5176\u4ed6\u80fd\u5c06 Prometheus \u6293\u53d6\u7684\u6307\u6807\u516c\u5f00\u51fa\u53bb\u7684\u4e1c\u897f\u3002 \u8fd9\u4e9b\u6307\u6807\u53ef\u5728\u7aef\u70b9 \/metrics \u4e2d\u83b7\u5f97\uff0c\u5b83\u5141\u8bb8 Prometheus \u65e0\u9700\u4ee3\u7406\u76f4\u63a5\u6293\u53d6\u5b83\u4eec\u3002\u672c\u6587\u4e2d\u7684\u6559\u7a0b\u4f7f\u7528 node_exporter \u6765\u516c\u5f00\u76ee\u6807\u4e3b\u673a\u7684\u786c\u4ef6\u548c\u64cd\u4f5c\u7cfb\u7edf\u6307\u6807\u3002\u8f93\u51fa\u5668\u7684\u8f93\u51fa\u662f\u660e\u6587\u7684\u3001\u9ad8\u5ea6\u53ef\u8bfb\u7684\uff0c\u8fd9\u662f Prometheus \u7684\u4f18\u52bf\u4e4b\u4e00\u3002<\/p>\n

\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u5c06 Prometheus \u4f5c\u4e3a\u540e\u7aef\uff0c\u914d\u7f6e Grafana \u6765\u63d0\u4f9b\u6570\u636e\u53ef\u89c6\u5316\u548c\u4eea\u8868\u677f\u529f\u80fd\u3002<\/p>\n

\u7406\u89e3 Prometheus \u7684\u914d\u7f6e\u6587\u4ef6<\/strong><\/div>\n

\u6293\u53d6 \/metrics \u7684\u95f4\u9694\u79d2\u6570\u63a7\u5236\u4e86\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5e93\u7684\u7c92\u5ea6\u3002\u8fd9\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e3a scrape_interval \u53c2\u6570\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8bbe\u7f6e\u4e3a 60 \u79d2\u3002<\/p>\n

\u5728 scrape_configs \u90e8\u5206\u4e2d\u4e3a\u6bcf\u4e2a\u6293\u53d6\u4f5c\u4e1a\u8bbe\u7f6e\u4e86\u76ee\u6807\u3002\u6bcf\u4e2a\u4f5c\u4e1a\u90fd\u6709\u81ea\u5df1\u7684\u540d\u79f0\u548c\u4e00\u7ec4\u6807\u7b7e\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fc7\u6ee4\u3001\u5206\u7c7b\u5e76\u66f4\u8f7b\u677e\u5730\u8bc6\u522b\u76ee\u6807\u3002\u4e00\u9879\u4f5c\u4e1a\u53ef\u4ee5\u6709\u5f88\u591a\u76ee\u6807\u3002<\/p>\n

\u5b89\u88c5 Prometheus<\/strong><\/div>\n

\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Docker \u5b89\u88c5 Prometheus \u670d\u52a1\u5668\u548c node_exporter\u3002Docker \u5e94\u8be5\u5df2\u7ecf\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u6b63\u786e\u5b89\u88c5\u548c\u914d\u7f6e\u3002\u5bf9\u4e8e\u66f4\u6df1\u5165\u3001\u81ea\u52a8\u5316\u7684\u65b9\u6cd5\uff0c\u6211\u63a8\u8350 Steve Ovens \u7684\u6587\u7ae0\u300a\u5982\u4f55\u4f7f\u7528 Ansible \u4e0e Prometheus \u5efa\u7acb\u7cfb\u7edf\u76d1\u63a7\u300b\u3002<\/p>\n

\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u5728\u5de5\u4f5c\u76ee\u5f55\u4e2d\u521b\u5efa Prometheus \u914d\u7f6e\u6587\u4ef6 prometheus.yml\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\nglobal:\r\n  scrape_interval:     15s\r\n  evaluation_interval: 15s\r\nscrape_configs:\r\n  - job_name: 'prometheus'\r\n        static_configs:\r\n        - targets: ['localhost:9090']\r\n  - job_name: 'webservers'\r\n        static_configs:\r\n        - targets: [':9100']<\/node><\/pre>\n

\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u7528 Docker \u542f\u52a8 Prometheus\uff1a<\/p>\n

\r\n$ sudo docker run -d -p 9090:9090 -v\r\n\/path\/to\/prometheus.yml:\/etc\/prometheus\/prometheus.yml\r\nprom\/prometheus<\/pre>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPrometheus \u670d\u52a1\u5668\u5c06\u4f7f\u7528\u7aef\u53e3 9090\u3002\u5982\u679c\u6b64\u7aef\u53e3\u5df2\u5728\u4f7f\u7528\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u540e\u9762\u6dfb\u52a0\u53c2\u6570 --web.listen-address=\":\" \u6765\u66f4\u6539\u5b83\u3002<\/p>\n

\u5728\u8981\u76d1\u89c6\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e0b\u8f7d\u5e76\u8fd0\u884c node_exporter \u5bb9\u5668\uff1a<\/p>\n

\r\n$ sudo docker run -d -v \"\/proc:\/host\/proc\" -v \"\/sys:\/host\/sys\" -v\r\n\"\/:\/rootfs\" --net=\"host\" prom\/node-exporter --path.procfs\r\n\/host\/proc --path.sysfs \/host\/sys --collector.filesystem.ignored-\r\nmount-points \"^\/(sys|proc|dev|host|etc)($|\/)\"<\/pre>\n

\u51fa\u4e8e\u672c\u6587\u7ec3\u4e60\u7684\u76ee\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u5b89\u88c5 node_exporter \u548c Prometheus\u3002\u8bf7\u6ce8\u610f\uff0c\u751f\u4ea7\u73af\u5883\u4e2d\u5728 Docker \u4e0b\u8fd0\u884c node_exporter \u662f\u4e0d\u660e\u667a\u7684 \u2014\u2014 \u8fd9\u4ec5\u7528\u4e8e\u6d4b\u8bd5\u76ee\u7684\u3002<\/p>\n

\u8981\u9a8c\u8bc1 node_exporter \u662f\u5426\u6b63\u5728\u8fd0\u884c\uff0c\u8bf7\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u5bfc\u822a\u5230 http:\/\/:9100\/metrics\uff0c\u8fd9\u5c06\u663e\u793a\u6536\u96c6\u5230\u7684\u6240\u6709\u6307\u6807\uff1b\u4e5f\u5373\u662f Prometheus \u5c06\u8981\u6293\u53d6\u7684\u76f8\u540c\u6307\u6807\u3002<\/p>\n

\"\"<\/p>\n

\u8981\u786e\u8ba4 Prometheus \u670d\u52a1\u5668\u5b89\u88c5\u6210\u529f\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u5bfc\u822a\u81f3\uff1ahttp:\/\/localhost:9090\u3002<\/p>\n

\u4f60\u5e94\u8be5\u770b\u5230\u4e86 Prometheus \u7684\u754c\u9762\u3002\u5355\u51fb\u201cStatus\u201d\uff0c\u7136\u540e\u5355\u51fb\u201cTargets\u201d\u3002\u5728 \u201cStatus\u201d \u4e0b\uff0c\u4f60\u5e94\u8be5\u770b\u5230\u4f60\u7684\u673a\u5668\u88ab\u5217\u4e3a \u201cUP\u201d\u3002<\/p>\n

\"\"<\/p>\n

\u73b0\u5728\u662f\u65f6\u5019\u719f\u6089\u4e00\u4e0b PromQL\uff08Prometheus \u7684\u67e5\u8be2\u8bed\u6cd5\uff09\u53ca\u5176\u56fe\u5f62\u5316 Web \u754c\u9762\u4e86\u3002\u8f6c\u5230 Prometheus \u670d\u52a1\u5668\u4e0a\u7684 http:\/\/localhost:9090\/graph\u3002\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u67e5\u8be2\u7f16\u8f91\u5668\u548c\u4e24\u4e2a\u9009\u9879\u5361\uff1a\u201cGraph\u201d \u548c \u201cConsole\u201d\u3002<\/p>\n

Prometheus \u5c06\u6240\u6709\u6570\u636e\u5b58\u50a8\u4e3a\u65f6\u95f4\u5e8f\u5217\uff0c\u4f7f\u7528\u6307\u6807\u540d\u79f0\u6807\u8bc6\u6bcf\u4e2a\u6570\u636e\u3002\u4f8b\u5982\uff0c\u6307\u6807 node_filesystem_avail_bytes \u663e\u793a\u53ef\u7528\u7684\u6587\u4ef6\u7cfb\u7edf\u7a7a\u95f4\u3002\u6307\u6807\u7684\u540d\u79f0\u53ef\u4ee5\u5728\u8868\u8fbe\u5f0f\u6846\u4e2d\u4f7f\u7528\uff0c\u4ee5\u9009\u62e9\u5177\u6709\u6b64\u540d\u79f0\u7684\u6240\u6709\u65f6\u95f4\u5e8f\u5217\u5e76\u751f\u6210\u5373\u65f6\u5411\u91cf\u3002\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u9009\u62e9\u5668\u548c\u6807\u7b7e\uff08\u4e00\u7ec4\u952e\u503c\u5bf9\uff09\u8fc7\u6ee4\u8fd9\u4e9b\u65f6\u95f4\u5e8f\u5217\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\nnode_filesystem_avail_bytes{fstype=\"ext4\"}\r\n<\/pre>\n

\u8fc7\u6ee4\u65f6\uff0c\u4f60\u53ef\u4ee5\u5339\u914d\u201c\u5b8c\u5168\u76f8\u7b49\u201d\uff08=\uff09\u3001\u201c\u4e0d\u7b49\u4e8e\u201d\uff08!=\uff09\uff0c\u201c\u6b63\u5219\u5339\u914d\u201d\uff08=~\uff09\u548c\u201c\u6b63\u5219\u6392\u9664\u5339\u914d\u201d\uff08!~\uff09\u3002\u4ee5\u4e0b\u793a\u4f8b\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\uff1a<\/p>\n

\u8981\u8fc7\u6ee4 node_filesystem_avail_bytes \u4ee5\u663e\u793a ext4 \u548c XFS \u6587\u4ef6\u7cfb\u7edf\uff1a<\/p>\n

\r\nnode_filesystem_avail_bytes{fstype=~\"ext4|xfs\"}\r\n<\/pre>\n

\u8981\u6392\u9664\u5339\u914d\uff1a<\/p>\n

\r\nnode_filesystem_avail_bytes{fstype!=\"xfs\"}\r\n<\/pre>\n

\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7\u5f97\u5230\u4ece\u5f53\u524d\u65f6\u95f4\u5f80\u56de\u7684\u4e00\u7cfb\u5217\u6837\u672c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 s \u8868\u793a\u79d2\uff0cm \u8868\u793a\u5206\u949f\uff0ch \u8868\u793a\u5c0f\u65f6\uff0cd \u8868\u793a\u5929\uff0cw \u8868\u793a\u5468\uff0c\u800c y \u8868\u793a\u5e74\u3002\u4f7f\u7528\u65f6\u95f4\u8303\u56f4\u65f6\uff0c\u8fd4\u56de\u7684\u5411\u91cf\u5c06\u662f\u8303\u56f4\u5411\u91cf\u3002<\/p>\n

\u4f8b\u5982\uff0c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u4ece\u4e94\u5206\u949f\u524d\u5230\u73b0\u5728\u7684\u6837\u672c\uff1a<\/p>\n

\r\nnode_memory_MemAvailable_bytes[5m]\r\n<\/pre>\n

Prometheus \u8fd8\u5305\u62ec\u4e86\u9ad8\u7ea7\u67e5\u8be2\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\n100 * (1 - avg by(instance)(irate(node_cpu_seconds_total{job='webservers',mode='idle'}[5m])))\r\n<\/pre>\n

\u8bf7\u6ce8\u610f\u6807\u7b7e\u5982\u4f55\u7528\u4e8e\u8fc7\u6ee4\u4f5c\u4e1a\u548c\u6a21\u5f0f\u3002\u6307\u6807 node_cpu_seconds_total \u8fd4\u56de\u4e00\u4e2a\u8ba1\u6570\u5668\uff0cirate()\u51fd\u6570\u6839\u636e\u8303\u56f4\u95f4\u9694\u7684\u6700\u540e\u4e24\u4e2a\u6570\u636e\u70b9\u8ba1\u7b97\u6bcf\u79d2\u7684\u53d8\u5316\u7387\uff08\u610f\u5473\u7740\u8be5\u8303\u56f4\u53ef\u4ee5\u5c0f\u4e8e\u4e94\u5206\u949f\uff09\u3002\u8981\u8ba1\u7b97 CPU \u603b\u4f53\u4f7f\u7528\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528 node_cpu_seconds_total \u6307\u6807\u7684\u7a7a\u95f2\uff08idle\uff09\u6a21\u5f0f\u3002\u5904\u7406\u5668\u7684\u7a7a\u95f2\u6bd4\u4f8b\u4e0e\u7e41\u5fd9\u6bd4\u4f8b\u76f8\u53cd\uff0c\u56e0\u6b64\u4ece 1 \u4e2d\u51cf\u53bb irate \u503c\u3002\u8981\u4f7f\u5176\u4e3a\u767e\u5206\u6bd4\uff0c\u8bf7\u5c06\u5176\u4e58\u4ee5 100\u3002<\/p>\n

\"\"<\/p>\n

\u4e86\u89e3\u66f4\u591a<\/strong><\/div>\n

Prometheus \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u53ef\u6269\u5c55\u3001\u8f7b\u91cf\u7ea7\u3001\u6613\u4e8e\u4f7f\u7528\u548c\u90e8\u7f72\u7684\u76d1\u89c6\u5de5\u5177\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u51fa\u4e8e\u8fd9\u4e9b\u539f\u56e0\u548c\u5176\u4ed6\u539f\u56e0\uff0c\u8bb8\u591a\u516c\u53f8\u6b63\u5728\u5c06 Prometheus \u4f5c\u4e3a\u5176\u57fa\u7840\u8bbe\u65bd\u7684\u4e00\u90e8\u5206\u3002<\/p>\n

<\/ip><\/port><\/ip><\/p>\n","protected":false},"excerpt":{"rendered":"

Prometheus \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u76d1\u63a7\u548c\u8b66\u62a5\u7cfb\u7edf\uff0c\u5b83\u76f4\u63a5\u4ece\u76ee\u6807\u4e3b\u673a\u4e0a\u8fd0\u884c\u7684\u4ee3\u7406\u7a0b\u5e8f\u4e2d\u6293\u53d6\u6307\u6807\uff0c\u5e76\u5c06\u6536\u96c6\u7684\u6837\u672c\u96c6 […]<\/p>\n","protected":false},"author":317,"featured_media":239878,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-241586","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=241586"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241586\/revisions"}],"predecessor-version":[{"id":241590,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241586\/revisions\/241590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/239878"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=241586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=241586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=241586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}