监控与可视化:Ubuntu上部署 Prometheus 和 Grafana 教程

简介

在现代基础设施中,监控和可视化系统性能是至关重要的。Prometheus和Grafana作为两个强大的开源工具,分别提供了系统监控和数据可视化的功能。本篇文章将带你一步步在Ubuntu系统上部署这两个工具,让你能够深入了解系统性能,并及时解决任何问题。

部署这两个项目,有以下特点:

  • 实时监控:实时收集和分析系统指标。
  • 数据可视化:通过Grafana创建直观的仪表板,展示关键性能数据。
  • 灵活配置:根据个人需求定制Prometheus和Grafana的配置。

功能特点简介

Prometheus和Grafana的组合提供了以下核心优势:

  • 高性能监控:Prometheus能够高效地收集和存储时间序列数据,为系统性能监控提供了强大的支持。
  • 直观的仪表板:Grafana提供了丰富的图表和仪表板,使得数据可视化变得简单直观。
  • 灵活的配置:用户可以根据需要自定义Prometheus的监控规则和Grafana的仪表板。

接下来,我将一步一步教大家如何进行安装和部署!!

准备工作

服务器准备

必要前提:

  • 一个充满求知欲的大脑。
  • 一台 Linux 服务器(推荐腾讯云、阿里云或雨云等)。

我将以 雨云 为例,带大家创建一台自己的云服务器,以便学习本篇文章的内容。

注册链接: https://rainyun.ivwv.site

创建雨云服务器

以下步骤仅供参考,请根据实际需求选择配置。

  • 点击 云产品云服务器立即购买
  • 选择距离你较近的区域,以保证低延迟。
  • 按照自己需求选择配置,选择Ubuntu 22.04 版本,按照自己需求是否预装Docker。
  • 最后按照提示进行购买。
  • 购买后等待机器部署完毕,点击刚刚创建好的服务器,进入管理面板,找到远程连接相关信息。
  • 我们使用 PowerShell 进行 SSH 远程连接到服务器,Win+R 打开运行窗口,输入 powershell 后点击确定。
  • 输入 ssh root@你的服务器IP 例如 ssh root@154.9.227.239 回车后,首次需要输入 yes,再次回车后即可登录服务器。
  • 到此为止,我们的云服务器就远程连接上了。

开始部署

安装Prometheus

更新系统包

首先更新你的Ubuntu系统包,以确保你拥有最新系统版本。

bash 复制代码
apt-get update && apt-get upgrade -y

下载Prometheus

访问Prometheus的官方网站Prometheus官网并导航到下载页面,选择适合你的Ubuntu版本的包。

安装Prometheus

下载完成后,解压Prometheus tar包并移动到你希望的位置。

bash 复制代码
wget https://github.com/prometheus/prometheus/releases/download/v3.0.1/prometheus-3.0.1.linux-amd64.tar.gz
tar zxvf prometheus-3.0.1.linux-amd64.tar.gz
cd prometheus-3.0.1.linux-amd64

配置Prometheus

Prometheus附带了一个名为"prometheus.yml"的默认配置文件,你可以根据监控需求进行自定义。

bash 复制代码
cat prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

启动Prometheus

通过执行以下命令启动Prometheus服务器:

bash 复制代码
./prometheus --config.file=prometheus.yml

验证Prometheus安装

在浏览器中访问http://your_domain:9090。如果Prometheus运行正确,你将看到Prometheus的Web界面。

安装Grafana

添加Grafana仓库

运行以下命令以将Grafana仓库添加到你的系统中:

bash 复制代码
sudo apt install -y apt-transport-https
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

安装Grafana

再次更新包列表并安装Grafana:

bash 复制代码
sudo apt-get update
sudo apt-get install grafana

启动Grafana

启用并启动Grafana服务:

bash 复制代码
sudo systemctl enable --now grafana-server

访问Grafana

在浏览器中访问http://your_server-ip:3000。需要设置新的管理员密码。按照屏幕上的指示完成设置即可。

执行以下命令以将管理员密码设置为新值:

bash 复制代码
sudo systemctl stop grafana-server
sudo grafana-cli admin reset-admin-password <new_password>
sudo systemctl start grafana-server

<new_password>替换为你希望的新密码。

配置Prometheus作为数据源

在Grafana Web界面中,点击"Connections " > "Data Sources "。添加一个新的数据源并选择Prometheus 。提供你的Prometheus 服务器的URL(例如,http://your_domain:9090)并保存配置。

创建仪表板

Grafana提供了丰富的预构建仪表板和可视化工具。探索仪表板库或创建自定义仪表板,有效监控系统指标。

恭喜你!你已成功在Ubuntu系统上安装了Prometheus和Grafana。通过这个监控设置,你现在可以深入了解你的基础设施性能,并及时解决任何问题。尝试Grafana的可视化选项,创建适合你监控需求的个性化仪表板。继续探索Prometheus和Grafana的能力,释放它们的全部潜力。

Grafana 推荐模板以及效果展示

Node Exporter Full : https://grafana.com/grafana/dashboards/11600-docker-container/

Docker Container: https://grafana.com/grafana/dashboards/1860-node-exporter-full/

也可以自己在 Grafana 的模板商店里面选择合适自己的模板 https://grafana.com/grafana/dashboards/

相关链接

雨云 - 新一代云服务提供商: https://rainyun.ivwv.site

我的博客:https://blog.ivwv.site

相关推荐
热爱编程的OP2 小时前
PyTorch 快速入门
pytorch·机器学习·开源
一丝晨光4 小时前
如何构建ObjC语言编译环境?构建无比简洁的clang编译ObjC环境?Windows搭建Swift语言编译环境?
linux·c语言·开发语言·windows·macos·objective-c·clang
m0_465215794 小时前
IP服务模型
服务器·网络·tcp/ip
shelby_loo5 小时前
使用 Docker 部署 pSQL 服务器 的教程
服务器·docker·容器
passionup5 小时前
linux 命令笔记
linux·运维·笔记
Linux运维老纪5 小时前
K8s 分布式存储后端(K8s Distributed Storage Backend)
服务器·分布式·云原生·容器·kubernetes·云计算·运维开发
夏尔Gaesar6 小时前
Ubuntu20.04 磁盘空间扩展教程
ubuntu
韦德说7 小时前
【开源事故】77.7K Star 的 Hugo 作者亲自回信!但他第一句话就让我彻底慌了……
后端·开源·go
入眼皆含月7 小时前
《运维:技术的基石,服务的保障》
运维
熬夜苦读学习8 小时前
linux 进程补充
linux·运维·服务器