{"id":3306,"date":"2025-03-23T11:13:53","date_gmt":"2025-03-23T03:13:53","guid":{"rendered":"https:\/\/www.yaoge123.com\/blog\/?p=3306"},"modified":"2025-03-23T14:52:06","modified_gmt":"2025-03-23T06:52:06","slug":"grafanaprometheus-%e5%ae%9e%e7%8e%b0%e7%9b%91%e6%8e%a7%e5%8f%af%e8%a7%86%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.yaoge123.com\/blog\/archives\/3306","title":{"rendered":"Grafana+Prometheus \u5b9e\u73b0\u76d1\u63a7\u53ef\u89c6\u5316"},"content":{"rendered":"\n<p>Grafana \u662f\u6570\u636e\u53ef\u89c6\u5316\u5c55\u793a\u5e73\u53f0\uff0c\u53ef\u4ee5\u8fde\u63a5\u5305\u62ec Prometheus \u5728\u5185\u7684\u5404\u79cd\u6570\u636e\u6e90\u3002Prometheus \u662f\u4e00\u5957\u76d1\u63a7\u62a5\u8b66\u548c\u65f6\u5e8f\u6570\u636e\u5e93\u7684\u7ec4\u5408\uff0cPrometheus \u4e3b\u52a8\u53d1\u8d77TCP\u8fde\u63a5\u8bbf\u95ee\u76d1\u63a7\u5bf9\u8c61\u4e0a\u9762 Exporter \u7684\u7aef\u53e3\u83b7\u53d6\u4fe1\u606f\uff0c\u5b58\u50a8\u5728\u81ea\u5df1\u7684\u65f6\u5e8f\u6570\u636e\u5e93\u4e2d\uff0cGrafana \u5b9e\u65f6\u4ece Prometheus \u67e5\u8be2\u6570\u636e\u5c55\u793a\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u76d1\u63a7\u4e2d\u6570\u636e\u83b7\u53d6\u6709 Pull \u548c Push \u4e24\u5927\u6d41\u6d3e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pull\uff08\u62c9\uff09\u662f\u76d1\u63a7\u7cfb\u7edf\u4e3b\u52a8\u83b7\u53d6\u6570\u636e\uff0c\u76d1\u63a7\u7cfb\u7edf\u9700\u8981\u670d\u52a1\u53d1\u73b0\u673a\u5236\uff0c\u9700\u8981\u88ab\u76d1\u63a7\u5bf9\u8c61\u5f00\u653e\u7aef\u53e3\u80fd\u591f\u88ab\u8bbf\u95ee\uff0c\u4e00\u822c\u6765\u8bf4\u76d1\u63a7\u5bf9\u8c61\u914d\u7f6e\u7b80\u5355\u3001\u76d1\u63a7\u7cfb\u7edf\u914d\u7f6e\u590d\u6742\u3002\u597d\u5904\u662f\u5bb9\u6613\u53d1\u73b0\u76d1\u63a7\u5bf9\u8c61\u79bb\u7ebf\uff0c\u76d1\u63a7\u7cfb\u7edf\u63a7\u5236\u62c9\u53d6\u987a\u5e8f\u548c\u9891\u7387\uff0c\u9632\u6b62\u76d1\u63a7\u7cfb\u7edf\u8fc7\u8f7d\uff1b\u574f\u5904\u662f\u6bcf\u4e2a\u76d1\u63a7\u5bf9\u8c61\u90fd\u8981\u5f00\u653e\u7aef\u53e3\uff0c\u9632\u706b\u5899\u7b49\u5b89\u5168\u914d\u7f6e\u590d\u6742\uff1b\u9ebb\u70e6\u662f\u76d1\u63a7\u7cfb\u7edf\u9700\u8981\u7ef4\u62a4\u76d1\u63a7\u5bf9\u8c61\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>Push\uff08\u63a8\uff09\u662f\u76d1\u63a7\u5bf9\u8c61\u4e3b\u52a8\u63a8\u9001\u6570\u636e\uff0c\u76d1\u63a7\u7cfb\u7edf\u88ab\u52a8\u83b7\u53d6\uff0c\u53ea\u6709\u76d1\u63a7\u7cfb\u7edf\u9700\u8981\u5f00\u653e\u7aef\u53e3\uff0c\u76d1\u63a7\u5bf9\u8c61\u914d\u7f6e\u7a0d\u590d\u6742\uff08\u8bbe\u7f6e\u63a8\u9001\u76ee\u6807\uff09\u3002\u597d\u5904\u662f\u76d1\u63a7\u7cfb\u7edf\u4e0d\u9700\u8981\u7ef4\u62a4\u76d1\u63a7\u5bf9\u8c61\u5217\u8868\uff0c\u6709\u4e14\u4ec5\u6709\u76d1\u63a7\u7cfb\u7edf\u9700\u8981\u5f00\u653e\u7aef\u53e3\uff0c\u76d1\u63a7\u5bf9\u8c61\u53ea\u8981\u80fd\u7f51\u7edc\u53ef\u8fbe\u76d1\u63a7\u7cfb\u7edf\u5373\u53ef\uff0c\u54ea\u6015\u5728\u591a\u4e2a\u9632\u706b\u5899\u540e\u8fdb\u884c\u4e86\u591a\u6b21NAT\uff1b\u574f\u5904\u662f\u76d1\u63a7\u7cfb\u7edf\u53ef\u80fd\u8fc7\u8f7d\uff0c\u4e0d\u5bb9\u6613\u53d1\u73b0\u76d1\u63a7\u5bf9\u8c61\u79bb\u7ebf\uff1b\u9ebb\u70e6\u662f\u6bcf\u4e2a\u76d1\u63a7\u5bf9\u8c61\u90fd\u9700\u8981\u914d\u7f6e\u76d1\u63a7\u7cfb\u7edf\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u76d1\u63a7\u5bf9\u8c61\uff08\u5ba2\u6237\u7aef\uff09<\/h2>\n\n\n\n<p>Node Exporter \u662f Prometheus \u5b98\u65b9\u63d0\u4f9b\u7684\u7269\u7406\u673a\u76d1\u63a7\u5ba2\u6237\u7aef\uff0c\u6ce8\u610f\u66f4\u6539\u7248\u672c\u53f7\u4e3a\u6700\u65b0\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -q https:\/\/mirror.nju.edu.cn\/github-release\/prometheus\/node_exporter\/LatestRelease\/node_exporter-1.9.0.linux-amd64.tar.gz\ntar xf node_exporter-*\nsudo mv node_exporter-*\/node_exporter \/usr\/local\/sbin\/\nsudo chown root:root \/usr\/local\/sbin\/node_exporter\nrm -rf .\/node_exporter-*\nsudo cat &gt; \/etc\/systemd\/system\/node_exporter.service &lt;&lt; EOF\n&#91;Unit]\nDescription=Prometheus Node Exporter\nAfter=network.target\n\n&#91;Service]\nRestart=always\nExecStart=\/usr\/local\/sbin\/node_exporter\nExecReload=\/bin\/kill -HUP $MAINPID\nTimeoutStopSec=20s\nSendSIGKILL=no\n\n&#91;Install]\nWantedBy=multi-user.target\nEOF\n\nsudo systemctl enable --now node_exporter.service<\/code><\/pre>\n\n\n\n<p>DCGM-Exporter&nbsp;\u662f NVIDIA&nbsp;\u5b98\u65b9\u63d0\u4f9b\u7684GPU\u76d1\u63a7\u5ba2\u6237\u7aef\uff0c\u5b83\u57fa\u4e8eDCGM\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5 NVIDIA Data Center GPU Manager (DCGM)\uff0c\u6ce8\u610f\u66f4\u6539\u64cd\u4f5c\u7cfb\u7edf\u7248\u672c\u3001\u67b6\u6784\u3001key\u5305\u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/developer.download.nvidia.cn\/compute\/cuda\/repos\/ubuntu2204\/x86_64\/cuda-keyring_1.1-1_all.deb\nsudo dpkg -i cuda-keyring_1.1-1_all.deb\nrm -f cuda-keyring_1.1-1_all.deb\nsudo add-apt-repository \"deb https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/ubuntu2204\/x86_64\/ \/\"\nsudo apt-get update &amp;&amp; sudo apt-get install datacenter-gpu-manager-4-cuda-all\nsystemctl enable --now nvidia-dcgm<\/code><\/pre>\n\n\n\n<p>\u5b89\u88c5 Go \u7136\u540e\u7f16\u8bd1 DCGM-Exporter\uff0c\u6ce8\u610f\u66f4\u6539Go\u7684\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/mirror.nju.edu.cn\/golang\/go1.24.1.linux-amd64.tar.gz\ntar -C \/usr\/local -xzf go*.tar.gz\nrm -f go*.tar.gz\nexport PATH=$PATH:\/usr\/local\/go\/bin\ngo env -w GO111MODULE=on \ngo env -w GOPROXY=\"https:\/\/repo.nju.edu.cn\/go\/,direct\"\ngit clone https:\/\/github.com\/NVIDIA\/dcgm-exporter\ncd dcgm-exporter\nmake binary\ncp cmd\/dcgm-exporter\/dcgm-exporter \/usr\/local\/sbin\/\nmkdir \/usr\/local\/etc\/dcgm-exporter\ncp etc\/* \/usr\/local\/etc\/dcgm-exporter\/\ncd ..\nrm -rf dcgm-exporter\ncat &gt; \/etc\/systemd\/system\/dcgm-exporter.service &lt;&lt;EOF\n&#91;Unit]\nDescription=Prometheus DCGM exporter\nWants=network-online.target nvidia-dcgm.service\nAfter=network-online.target nvidia-dcgm.service\n\n&#91;Service]\nType=simple\nRestart=always\nExecStart=\/usr\/local\/sbin\/dcgm-exporter --collectors \/usr\/local\/etc\/dcgm-exporter\/default-counters.csv\n\n&#91;Install]\nWantedBy=multi-user.target\nEOF\n\nsystemctl daemon-reload\nsystemctl enable --now dcgm-exporter.service<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f DCGM \u548c DCGM-Exporter \u7684\u7248\u672c\u8981\u5339\u914d\uff0cDCGM-Exporter \u7684\u7248\u672c\u683c\u5f0f\u4e3a DCGM\u7248\u672c-Exporter\u7248\u672c\u3002\u5982\u4e0b\u5217 4.1.1-4.0.4 \u610f\u601d\u662f DCGM-Exporter \u672c\u8eab\u7684\u7248\u672c\u662f4.0.4\uff0c\u5bf9 DCGM \u7684\u7248\u672c\u8981\u6c42\u662f 4.1.1\uff0c\u7ecf\u67e5\u770b\u662f\u5339\u914d\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/local\/sbin\/dcgm-exporter --version\n2025\/03\/23 10:46:13 maxprocs: Leaving GOMAXPROCS=112: CPU quota undefined\nDCGM Exporter version 4.1.1-4.0.4\n(base) root@ubuntu:~# dcgmi --version\n\ndcgmi  version: 4.1.1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u76d1\u63a7\u7cfb\u7edf\uff08\u670d\u52a1\u7aef\uff09<\/h2>\n\n\n\n<p>\u5148\u628a Docker \u548c Docker Compose V2 \u88c5\u597d\uff0c\u7136\u540e\u7528\u5bb9\u5668\u90e8\u7f72 Grafana \u548c Prometheus\u3002\u7b80\u5355\u8d77\u89c1\u4e0d\u8003\u8651\u524d\u9762\u5957\u53cd\u5411\u4ee3\u7406\u3001Prometheus\u52a0\u8ba4\u8bc1\u3001consul \u5b9e\u73b0\u81ea\u52a8\u670d\u52a1\u53d1\u73b0\u6ce8\u518c\u3002<\/p>\n\n\n\n<p>\u5148\u5efa\u597d\u76ee\u5f55\u5e76\u66f4\u6539\u6240\u6709\u8005\/\u7ec4\uff0c\u5426\u5219\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u62a5\u6ca1\u6709\u6743\u9650<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir grafana\nmkdir prometheus\nmkdir prometheus-conf\nsudo chown 472:0 grafana\/\nsudo chown 65534:65534 prometheus<\/code><\/pre>\n\n\n\n<p>\u7f16\u8f91 docker-compose.yml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  prometheus:\n    image: prom\/prometheus\n    container_name: prometheus\n    restart: always\n    volumes:\n      - .\/prometheus:\/prometheus\n      - .\/prometheus-conf:\/etc\/prometheus\/conf\n    command:\n      - --config.file=\/etc\/prometheus\/conf\/prometheus.yml\n      - --web.console.libraries=\/usr\/share\/prometheus\/console_libraries\n      - --web.console.templates=\/usr\/share\/prometheus\/consoles\n      - --web.listen-address=0.0.0.0:9090\n      - --storage.tsdb.path=\/prometheus\n      - --storage.tsdb.retention.size=100GB  #\u6570\u636e\u5e93\u5b58\u50a8\u5bb9\u91cf\u9650\u5236\uff0c\u8d85\u8fc7\u540e\u4f1a\u81ea\u52a8\u5220\u9664\u6700\u8001\u7684\u6570\u636e\n      - --storage.tsdb.wal-compression\n  grafana:\n    image: grafana\/grafana\n    container_name: grafana\n    restart: always\n    ports:\n      - 3000:3000\n    volumes:\n      - .\/grafana:\/var\/lib\/grafana\n    environment:\n      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-piechart-panel\n      - GF_SECURITY_ADMIN_PASSWORD=yaoge123   #admin\u7684\u521d\u59cb\u5bc6\u7801\n      - GF_SERVER_ENABLE_GZIP=true\n      - GF_SERVER_DOMAIN=192.168.1.10   #\u5982\u679c\u524d\u9762\u6709\u53cd\u4ee3\u8981\u6539\n      - GF_SERVER_ROOT_URL=http:\/\/192.168.1.10   #\u5982\u679c\u524d\u9762\u6709\u53cd\u4ee3\u8981\u6539\n      - GF_AUTH_ANONYMOUS_ENABLED=true\n      - GF_AUTH_ANONYMOUS_ORG_NAME=yaoge123\n      #- GF_SERVER_SERVE_FROM_SUB_PATH=true   #\u53cd\u4ee3\u540e\u6709\u5b50\u8def\u5f84\n      #- GF_SERVER_ROOT_URL=%(protocol)s:\/\/%(domain)s:%(http_port)s\/grafana   #\u53cd\u4ee3\u540e\u6709\u5b50\u8def\u5f84\n      #- GF_SECURITY_COOKIE_SECURE=true   #\u53cd\u4ee3\u4e0a\u6709HTTPS\n    depends_on:\n     - prometheus<\/code><\/pre>\n\n\n\n<p>\u5bfc\u5165\u9ed8\u8ba4\u914d\u7f6e prometheus.yml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd prometheus-conf\nwget https:\/\/raw.githubusercontent.com\/prometheus\/prometheus\/refs\/heads\/main\/documentation\/examples\/prometheus.yml<\/code><\/pre>\n\n\n\n<p>prometheus.yml \u4e2d\u4e3b\u8981\u7f16\u8f91\u7684\u662f\u76d1\u63a7\u76ee\u6807<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># my global config\nglobal:\n  scrape_interval: 30s # Set the scrape interval to every 15 seconds. Default is every 1 minute.\u91c7\u6837\u95f4\u9694\n  evaluation_interval: 30s # Evaluate rules every 15 seconds. The default is every 1 minute.\n  scrape_timeout: 30s #\u91c7\u6837\u8d85\u65f6\uff0c\u6709\u4e00\u4e9bexporter\u8bfb\u53d6\u5f88\u6162\uff0c\u9700\u8981\u653e\u5bbd\u8d85\u65f6\u3002\n  # scrape_timeout is set to the global default (10s).\n\n# Alertmanager configuration\nalerting:\n  alertmanagers:\n    - static_configs:\n        - targets:\n          # - alertmanager:9093\n\n# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.\nrule_files:\n  # - \"first_rules.yml\"\n  # - \"second_rules.yml\"\n\n# A scrape configuration containing exactly one endpoint to scrape:\n# Here it's Prometheus itself.\nscrape_configs:\n  # The job name is added as a label `job=&lt;job_name&gt;` to any timeseries scraped from this config.\n  - job_name: \"prometheus\"\n\n    # metrics_path defaults to '\/metrics'\n    # scheme defaults to 'http'.\n\n    static_configs:\n      - targets: &#91;\"localhost:9090\"]\n       # The label name is added as a label `label_name=&lt;label_value&gt;` to any timeseries scraped from this config.\n        labels:\n          app: \"prometheus\"\n\n  - job_name: node_exporter\n    static_configs:\n    - targets: \n      - '192.168.1.101:9100'\n      - '192.168.1.102:9100'\n  - job_name: dcgm-exporter\n    static_configs:\n    - targets:\n      - '192.168.1.101:9400'\n      - '192.168.1.102:9400'<\/code><\/pre>\n\n\n\n<p>\u628a\u5bb9\u5668\u542f\u8d77\u6765 cd .. &amp;&amp; docker compose up -d<\/p>\n\n\n\n<p>\u4e0b\u9762\u5c31\u662f\u914d\u7f6e Grafana <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528 admin \u548c\u9ed8\u8ba4\u5bc6\u7801\uff08GF_SECURITY_ADMIN_PASSWORD\uff09\u767b\u5f55Grafana<\/li>\n\n\n\n<li>\u8fdb\u5165 \/connections\/datasources\/prometheus \u6dfb\u52a0\u5df2\u6709 Prometheus \u6570\u636e\u6e90\uff1aConnection: <a href=\"http:\/\/prometheus:9090\">http:\/\/prometheus:9090<\/a> <\/li>\n\n\n\n<li>\u8fdb\u5165 \/dashboard\/import \u5bfc\u5165 Node Exporter Full\uff1aGrafana Dashboard ID 1860<\/li>\n\n\n\n<li>\u4e0b\u8f7d <a href=\"https:\/\/raw.githubusercontent.com\/NVIDIA\/dcgm-exporter\/refs\/heads\/main\/grafana\/dcgm-exporter-dashboard.json\">https:\/\/raw.githubusercontent.com\/NVIDIA\/dcgm-exporter\/refs\/heads\/main\/grafana\/dcgm-exporter-dashboard.json<\/a> \uff0c\u8fdb\u5165 \/dashboard\/import \u5bfc\u5165\u8be5json<\/li>\n<\/ul>\n\n\n\n<p>\u81f3\u6b64\u4e00\u4e2a\u7b80\u5355\u7684\u76d1\u63a7\u5e73\u53f0\u5c31\u5df2\u7ecf\u642d\u5efa\u597d\u4e86\uff0c\u5728\u771f\u5b9e\u7684\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528 consul \u5b9e\u73b0\u88ab\u76d1\u63a7\u5bf9\u8c61\u7684\u7ba1\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0cPrometheus \u6700\u597d\u52a0\u4e0a\u8ba4\u8bc1\uff0cGrafana \u524d\u9762\u6700\u597d\u5957\u4e00\u4e2a NGINX \u505a\u53cd\u4ee3\u5b9e\u73b0 HTTPS\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grafana \u662f\u6570\u636e\u53ef\u89c6\u5316\u5c55\u793a\u5e73\u53f0\uff0c\u53ef\u4ee5\u8fde\u63a5\u5305\u62ec Prometheus \u5728\u5185\u7684\u5404\u79cd\u6570\u636e\u6e90\u3002Prometheu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-3306","post","type-post","status-publish","format-standard","hentry","category-xnix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOwEq-Rk","_links":{"self":[{"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/posts\/3306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/comments?post=3306"}],"version-history":[{"count":9,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/posts\/3306\/revisions"}],"predecessor-version":[{"id":3317,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/posts\/3306\/revisions\/3317"}],"wp:attachment":[{"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/media?parent=3306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/categories?post=3306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yaoge123.com\/blog\/wp-json\/wp\/v2\/tags?post=3306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}