2026重新梳理systemctl和docker安装 Prometheus三件套+node-exportor-grafana安装

张开发
2026/6/11 10:36:12 15 分钟阅读
2026重新梳理systemctl和docker安装 Prometheus三件套+node-exportor-grafana安装
2026重新梳理systemctl和docker安装 Prometheus三件套node-exportor-grafana安装一、Prometheus 安装脚本最终完善版#!/bin/bashset-eecho 开始安装 Prometheus # 清理旧环境systemctl stop prometheus2/dev/null systemctl disable prometheus2/dev/nulluserdel-rprometheus2/dev/nullrm-rf/usr/local/prometheus /var/lib/prometheus /etc/systemd/system/prometheus.service# 解压已下载好包tarzxf prometheus-2.19.2.linux-amd64.tar.gzmvprometheus-2.19.2.linux-amd64 /usr/local/prometheus# 创建用户groupaddprometheus2/dev/nulluseradd-gprometheus-m-d/var/lib/prometheus-s/sbin/nologin prometheus2/dev/null# 创建数据目录mkdir-p/var/lib/prometheus/datachown-Rprometheus:prometheus /usr/local/prometheus /var/lib/prometheus# 生成 systemdcat/etc/systemd/system/prometheus.serviceEOF [Unit] DescriptionPrometheus Afternetwork.target [Service] Userprometheus Groupprometheus ExecStart/usr/local/prometheus/prometheus \ --config.file/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path/var/lib/prometheus/data \ --web.listen-address0.0.0.0:9090 Restarton-failure LimitNOFILE65536 [Install] WantedBymulti-user.target EOF# 启动systemctl daemon-reload systemctlenableprometheus systemctl restart prometheusecho Prometheus 安装完成 systemctl status prometheus --no-pager二、Node Exporter 安装脚本最终完善版#!/bin/bashset-eecho 开始安装 Node Exporter # 清理旧环境systemctl stop node_exporter2/dev/null systemctl disable node_exporter2/dev/nullrm-rf/usr/local/node_exporter /etc/systemd/system/node_exporter.service# 解压tarzxf node_exporter-1.3.1.linux-amd64.tar.gzmvnode_exporter-1.3.1.linux-amd64 /usr/local/node_exporter# 用户不存在则创建idprometheus/dev/null||useradd-m-d/var/lib/prometheus-s/sbin/nologin prometheus# 权限chown-Rprometheus:prometheus /usr/local/node_exporter# systemdcat/etc/systemd/system/node_exporter.serviceEOF [Unit] DescriptionNode Exporter Afternetwork.target [Service] Userprometheus Groupprometheus ExecStart/usr/local/node_exporter/node_exporter \ --web.listen-address:9100 \ --collector.systemd \ --collector.processes Restarton-failure [Install] WantedBymulti-user.target EOF# 启动systemctl daemon-reload systemctlenablenode_exporter systemctl restart node_exporterecho Node Exporter 安装完成 systemctl status node_exporter --no-pager三、Grafana 安装脚本最终极简稳定版#!/bin/bashset-eecho 开始安装 Grafana # 安装 repocat/etc/yum.repos.d/grafana.repoEOF [grafana] namegrafana baseurlhttps://packages.grafana.com/oss/rpm repo_gpgcheck1 enabled1 gpgkeyhttps://packages.grafana.com/gpg.key sslverify1 sslcacert/etc/pki/tls/certs/ca-bundle.crt EOF# 安装yuminstall-ygrafana# 自动添加 Prometheus 数据源mkdir-p/etc/grafana/provisioning/datasourcescat/etc/grafana/provisioning/datasources/prometheus.yamlEOF apiVersion: 1 datasources: - name: Prometheus type: prometheus access: proxy url: http://localhost:9090 isDefault: true EOF# 启动systemctl daemon-reload systemctlenablegrafana-server systemctl restart grafana-serverecho Grafana 安装完成 echo访问: http://IP:3000 账号密码: admin/adminsystemctl status grafana-server --no-pager四、配套 prometheus.yml最终完整版global:scrape_interval:15sevaluation_interval:15salerting:alertmanagers:-static_configs:-targets:[localhost:9093]rule_files:-alerting_rules.ymlscrape_configs:-job_name:prometheusstatic_configs:-targets:[localhost:9090]-job_name:node_exporterstatic_configs:-targets:[localhost:9100]Prometheus Node Exporter Grafana 主配置 权限 数据目录 systemd给你CentOS7 专用 · 100% 可直接运行三组件一键 Docker / Docker Compose 方案Prometheus Node Exporter Grafana干净、不污染系统、讲课最爽、博客最帅方案1Docker Compose 一键启动最推荐 ✅1先装 docker docker-composeCentOS7#!/bin/bash# 安装 Dockeryuminstall-yyum-utils yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yuminstall-ydocker-ce docker-ce-cli containerd.io systemctl startdockersystemctlenabledocker# 安装 docker-composecurl-Lhttps://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname-s)-$(uname-m)-o/usr/local/bin/docker-composechmodx /usr/local/bin/docker-compose2docker-compose.yml完整最终版 ✅直接新建一个文件夹放这个文件即可version:3services:# 1. Node Exporter (监控机器)node_exporter:image:prom/node-exporter:latestcontainer_name:node_exporterrestart:alwaysports:-9100:9100volumes:-/:/host:ro,rslavecommand:---path.rootfs/hostnetwork_mode:host# 2. Prometheus (数据存储查询)prometheus:image:prom/prometheus:v2.19.2container_name:prometheusrestart:alwaysports:-9090:9090volumes:-./prometheus.yml:/etc/prometheus/prometheus.yml-prom_data:/prometheusnetwork_mode:host# 3. Grafana (可视化面板)grafana:image:grafana/grafana:10.1.5container_name:grafanarestart:alwaysports:-3000:3000volumes:-grafana_data:/var/lib/grafananetwork_mode:hostvolumes:prom_data:grafana_data:3配套 prometheus.yml和上面放一起global:scrape_interval:15sscrape_configs:-job_name:prometheusstatic_configs:-targets:[localhost:9090]-job_name:node_exporterstatic_configs:-targets:[localhost:9100]4一键启动docker-composeup-d方案2纯 Docker 命令版无 compose直接复制运行 ✅#!/bin/bash# 1. 启动 Node Exporterdockerrun-d\--namenode_exporter\--nethost\--restartalways\-v/:/host:ro,rslave\prom/node-exporter\--path.rootfs/host# 2. 启动 Prometheusdockerrun-d\--nameprometheus\--nethost\--restartalways\-v./prometheus.yml:/etc/prometheus/prometheus.yml\prom/prometheus:v2.19.2# 3. 启动 Grafanadockerrun-d\--namegrafana\--nethost\--restartalways\grafana/grafana:10.1.5最终访问信息Prometheus: http://IP:9090Node Exporter: http://IP:9100/metricsGrafana: http://IP:3000账号admin密码admin

更多文章