0Pricing
Docker & Kubernetes for Developers · 课时

使用 Prometheus 与 Grafana 进行监控

设置 Prometheus 收集指标,并使用 Grafana 进行可视化,以监控集群和应用性能。

使用 Prometheus 与 Grafana 进行监控 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Monitor Kubernetes?

When running applications in Kubernetes, it's crucial to know how they're performing. Are your pods healthy? Is the cluster overloaded? Monitoring gives you these answers.

In this lesson, we'll explore two powerful tools: Prometheus for collecting metrics and Grafana for visualizing them.

Prometheus: The Metrics Brain

Prometheus is an open-source monitoring system that collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts.

  • It uses a pull model: Prometheus scrapes (pulls) metrics from HTTP endpoints.
  • It stores data as time-series data, meaning values are recorded over time.

How Prometheus Gathers Data

Prometheus gathers metrics from various sources:

  • Exporters: Specialized tools that expose existing metrics in a Prometheus-readable format (e.g., node_exporter for host metrics, kube-state-metrics for Kubernetes object metrics).
  • Instrumented applications: Applications can directly expose a /metrics endpoint.

Prometheus then scrapes these endpoints.

Prometheus in Kubernetes Context

Deploying Prometheus in Kubernetes often involves using Helm charts or the Prometheus Operator. These help automate the setup and configuration.

Key for Kubernetes monitoring is service discovery. Prometheus can automatically find and scrape pods or services by using Kubernetes API integration, often configured via ServiceMonitor or PodMonitor custom resources.

Grafana: Your Dashboard Hub

While Prometheus collects and stores metrics, Grafana is the visualization layer. It allows you to:

  • Create rich, interactive dashboards.
  • Query multiple data sources (like Prometheus).
  • Set up alerts based on visualized data.

Grafana makes complex data easy to understand at a glance.

Connecting Grafana to Prometheus

To visualize Prometheus data in Grafana, you first need to add Prometheus as a data source.

Inside Grafana, you'll specify the URL of your Prometheus server. Once connected, you can use Grafana's interface to write queries using PromQL (Prometheus Query Language) to retrieve specific metrics.

Crafting a Grafana Dashboard

Building a dashboard in Grafana is straightforward:

  1. Create a new dashboard.
  2. Add a new panel.
  3. Select Prometheus as the data source.
  4. Write a PromQL query (e.g., sum(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) to see idle CPU usage).
  5. Choose a visualization type (graph, gauge, table, etc.).

You can combine many panels to create a comprehensive view.

Essential Kubernetes Metrics

What should you monitor in Kubernetes?

  • Node Health: CPU, memory, disk usage, network I/O.
  • Pod Status: Restarts, CPU/memory usage, network traffic.
  • API Server: Request latency, error rates.
  • Controller/Scheduler: Queue lengths, operation durations.

These metrics provide insights into your cluster's overall health and performance.

Understanding Your Dashboards

Dashboards aren't just pretty pictures; they're powerful diagnostic tools. Learn to:

  • Spot trends: Is resource usage consistently increasing?
  • Identify anomalies: Sudden spikes or drops often indicate issues.
  • Correlate events: Did a deployment cause a performance degradation?

Effective monitoring helps you proactively address problems before they impact users.

Monitoring Tools Check

Which of the following are primary functions of Prometheus and Grafana in a Kubernetes monitoring setup?

Summary & Beyond

You've learned that Prometheus is excellent for collecting and storing time-series metrics, while Grafana provides powerful, customizable dashboards for visualizing this data.

Together, they form a robust monitoring solution for Kubernetes, helping you understand your cluster's health and application performance. Next, explore advanced PromQL, setting up alerts, and integrating with other tools!

常见问题解答

「使用 Prometheus 与 Grafana 进行监控」课时是免费的吗?

是的 — 「使用 Prometheus 与 Grafana 进行监控」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「使用 Prometheus 与 Grafana 进行监控」这节课中我会学到什么?

设置 Prometheus 收集指标,并使用 Grafana 进行可视化,以监控集群和应用性能。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 Prometheus 与 Grafana 进行监控」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Kubernetes 日志记录策略
  2. 使用 Prometheus 与 Grafana 进行监控
  3. 排查常见 K8s 问题
  4. 健康检查:存活、就绪与启动探针
← 返回 Docker & Kubernetes for Developers