关于Prometheus监控节点node-export 改成systemd守护进程的方式

① 以下是简单的一种修改方式,将二进制离线包改成systemctl 启动方式,默认端口为:9100

[Unit]
Description=prometheus service

[Service]
ExecStart=/urs/local/bin/prometheus/prometheus  --config.file=/urs/local/bin/prometheus/prometheus.yaml

[Install]
WantedBy=multi-user.target

将prometheus服务进程 改进成守护进行进行运行

---------------------------------------------------------------------------------------------------------------------------------

②以下方式是为了避免9100被其他进程服务所占用,特意修改变化成19100端口启动方式


# cat /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=root
ExecStart=/opt/monitor/node-exporter/node_exporter-1.5.0.linux-amd64/node_exporter --web.listen-address=0.0.0.0:19100
Restart=always
RestartSec=10s

[Install]
WantedBy=multi-user.target

---------------------------------------------------------------------------------------------------------------------------------

root     121762      1  0 May06 ?        00:08:18 /opt/monitor/node-exporter/node_exporter-1.5.0.linux-amd64/node_exporter --web.listen-address=:19100

<think>我们正在Ubuntu系统上编译安装Prometheus和Grafana。编译安装意味着我们需要从源代码构建,而不是使用包管理器(如apt)直接安装预编译的包。以下是步骤指南: ###1.准备工作确保系统已更新,并安装必要的构建工具和依赖项。```bashsudoapt updatesudoapt upgrade-ysudoapt install-ybuild-essential gitcurl wget``` ###2.安装Go(因为Prometheus是用Go编写的)Prometheus需要Go环境来编译。我们将安装最新版本的Go。-访问[Go官方下载页面](https://golang.org/dl/)获取最新版本的下载链接。-例如,当前最新版本是1.22.0,则执行:```bashwget https://dl.google.com/go/go1.22.0.linux-amd64.tar.gzsudotar -C /usr/local-xzf go1.22.0.linux-amd64.tar.gz```-设置环境变量:```bashecho'export PATH=$PATH:/usr/local/go/bin'>> ~/.profileecho'exportGOPATH=$HOME/go'>> ~/.profilesource~/.profile``` -验证安装:`goversion`应显示版本信息。 ###3.编译安装Prometheus-克隆Prometheus仓库: ```bashgit clonehttps://github.com/prometheus/prometheus.gitcd prometheus``` -切换到稳定版本分支(例如v2.47.0): ```bashgit checkoutv2.47.0```-编译:```bashmakebuild``` -编译完成后,在`prometheus`目录下会生成可执行文件`prometheus`和`promtool`。-将编译好的文件移动到系统路径(可选):```bashsudocp prometheus promtool /usr/local/bin/``` -验证:`prometheus --version`###4.配置Prometheus-创建配置文件目录:```bashsudomkdir/etc/prometheus```-复制默认配置文件:```bashsudocp prometheus.yml/etc/prometheus/``` -编辑配置文件(按需修改):```bashsudonano /etc/prometheus/prometheus.yml```-创建服务文件以便使用systemd管理:```bashsudo nano/etc/systemd/system/prometheus.service``` -服务文件内容示例: ```[Unit] Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusExecStart=/usr/local/bin/prometheus \--config.file=/etc/prometheus/prometheus.yml\--storage.tsdb.path=/var/lib/prometheus/data\--web.console.templates=/etc/prometheus/consoles\--web.console.libraries=/etc/prometheus/console_librariesRestart=always[Install] WantedBy=multi-user.target```-创建Prometheus用户和目录: ```bashsudo useradd --no-create-home--shell/bin/falseprometheussudomkdir/var/lib/prometheussudochownprometheus:prometheus /var/lib/prometheussudo chown -R prometheus:prometheus/etc/prometheus``` -启动服务:```bashsudosystemctldaemon-reloadsudosystemctlstart prometheussudo systemctl enableprometheus```-检查状态:`sudo systemctl statusprometheus` ###5.编译安装GrafanaGrafana是用Go编写的,因此我们也需要编译它。但注意,Grafana的编译过程更复杂,因为它有前端部分(需要Node.js)。 ####5.1安装Node.js-安装Node.js(例如v18.x): ```bashcurl -fsSLhttps://deb.nodesource.com/setup_18.x| sudo-Ebash -sudo aptinstall -y nodejs```####5.2安装yarn```bashsudonpm install-gyarn``` ####5.3编译Grafana-克隆Grafana仓库:```bashgitclone https://github.com/grafana/grafana.gitcdgrafana```-切换到稳定版本分支(例如v10.0.0):```bashgitcheckout v10.0.0``` -编译前端:```bashyarn install--pure-lockfileyarnbuild``` -编译后端:```bashgorun build.go setupgorun build.go build```-编译完成后,二进制文件在`bin/linux-amd64/grafana-server`和`bin/linux-amd64/grafana-cli`。-将二进制文件移动到系统路径: ```bashsudo cpbin/linux-amd64/grafana-serverbin/linux-amd64/grafana-cli/usr/local/bin/ ```###6.配置Grafana-创建配置文件目录和默认配置文件: ```bashsudo mkdir /etc/grafanasudocp conf/sample.ini/etc/grafana/grafana.ini```-创建数据目录: ```bashsudo mkdir -p /var/lib/grafana/datasudomkdir -p /var/lib/grafana/pluginssudo chown -R grafana:grafana/var/lib/grafana```-创建grafana用户: ```bashsudo useradd --no-create-home --shell /bin/false grafanasudo chown grafana:grafana/etc/grafanasudo chown -R grafana:grafana/var/lib/grafana```-创建服务文件:```bashsudonano /etc/systemd/system/grafana.service```-服务文件内容示例:``` [Unit]Description=GrafanaAfter=network.target[Service]User=grafanaGroup=grafanaExecStart=/usr/local/bin/grafana-server\--config=/etc/grafana/grafana.ini\--homepath=/usr/local/share/grafana\cfg:default.paths.data=/var/lib/grafana/data \cfg:default.paths.plugins=/var/lib/grafana/pluginsRestart=on-failure[Install]WantedBy=multi-user.target``` -启动服务:```bashsudosystemctldaemon-reloadsudosystemctlstart grafanasudo systemctl enablegrafana```-检查状态:`sudo systemctl statusgrafana` ###7.访问Grafana-默认端口是3000,在浏览器中访问`http://<服务器IP>:3000`。 -默认用户名和密码是`admin`/`admin`,首次登录后会要求修改密码。 ###8.配置Grafana数据源为Prometheus-登录Grafana后,点击左侧齿轮图标(Configuration)>Data Sources>Add datasource。-选择Prometheus-在URL中填写Prometheus的地址(例如`http://localhost:9090`)。-点击Save&Test,确保连接成功。###9.创建仪表板-可以导入现有仪表板或自行创建。Prometheus官方提供了一些示例仪表板,可以在[Grafana仪表板库](https://grafana.com/grafana/dashboards)中查找。###注意事项-编译安装过程可能因版本不同而略有差异,请参考官方文档。 -生产环境建议使用官方提供的二进制包或包管理器安装,以获得更好的维护性。 §§相关问题§§1.如何验证Prometheus和Grafana是否安装成功?2.如何配置Prometheus监控其他节点?3.Grafana如何添加插件? 4.如何升级通过编译安装的Prometheus和Grafana?</think>### Ubuntu 编译安装 Prometheus 和 Grafana 监控系统配置指南 #### 一、环境准备 1. **更新系统** ```bash sudo apt update && sudo apt upgrade -y ``` 2. **安装编译依赖** ```bash sudo apt install -y build-essential git curl wget libfontconfig1 ``` --- #### 二、编译安装 Prometheus 1. **安装 Go 语言环境** ```bash wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc source ~/.bashrc go version # 验证安装 ``` 2. **编译 Prometheus** ```bash git clone https://github.com/prometheus/prometheus.git cd prometheus make build # 编译源码 ``` 3. **配置与启动** ```bash sudo mkdir /etc/prometheus sudo cp prometheus.yml /etc/prometheus/ sudo ./prometheus \ --config.file=/etc/prometheus/prometheus.yml \ --storage.tsdb.path=/var/lib/prometheus/data ``` > **验证**:访问 `http://localhost:9090` 查看 Web UI[^1]。 --- #### 三、编译安装 Grafana 1. **安装 Node.js 环境** ```bash curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs ``` 2. **编译 Grafana** ```bash git clone https://github.com/grafana/grafana.git cd grafana make build # 编译前端 go run build.go setup # 编译后端 go run build.go build ``` 3. **配置与启动** ```bash sudo mkdir /etc/grafana sudo cp conf/sample.ini /etc/grafana/grafana.ini sudo ./bin/grafana-server \ --config=/etc/grafana/grafana.ini \ --homepath=./ # 指定安装目录 ``` > **验证**:访问 `http://localhost:3000`(默认账号 `admin/admin`)[^2]。 --- #### 四、集成配置 1. **Grafana 添加 Prometheus 数据源** - 登录 Grafana → **Configuration** → **Data Sources** → **Add data source** - 选择 **Prometheus**,URL 填写 `http://localhost:9090` 2. **导入监控仪表板** - 在 Grafana 中导入官方仪表板 ID `1860`(Node Exporter 监控模板) 3. **安装监控插件(可选)** ```bash grafana-cli plugins install natel-discrete-panel # 示例插件[^3] ``` --- #### 五、系统优化 1. **创建 Systemd 服务** **Prometheus 服务文件** (`/etc/systemd/system/prometheus.service`): ```ini [Unit] Description=Prometheus After=network.target [Service] ExecStart=/path/to/prometheus --config.file=/etc/prometheus/prometheus.yml [Install] WantedBy=multi-user.target ``` 2. **配置防火墙** ```bash sudo ufw allow 9090/tcp # Prometheus sudo ufw allow 3000/tcp # Grafana ``` --- ### 验证与测试 1. **检查服务状态** ```bash curl http://localhost:9090/-/healthy # Prometheus 健康检查 curl http://localhost:3000/api/health # Grafana 健康检查 ``` 2. **生成测试指标** 安装 Node Exporter 收集系统指标: ```bash wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz tar xvfz node_exporter-*.tar.gz ./node_exporter ``` > **提示**:生产环境建议分离服务部署,并配置 HTTPS 加密访问[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值