DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Pulsar on KubeSphere: Installing Distributed Messaging and Streaming Platform
  • Main Features and Benefits of Google Kubernetes Engine
  • Watching the Requests Go By: Reconstructing an API Spec with APIClarity
  • Implementing iOS Accessibility: A Developer's Practical Guide

Trending

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Build Self-Managing Data Pipelines With an LLM Agent
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Tutorial: Installing and Using Prometheus in Kubernetes

Tutorial: Installing and Using Prometheus in Kubernetes

This guide will help you set up Prometheus to monitor your Kubernetes cluster effectively.

By 
Aditya Bhuyan user avatar
Aditya Bhuyan
·
Feb. 16, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

This article will lead you through installing and configuring Prometheus, a popular open-source monitoring and alerting toolset, in a Kubernetes context. Prometheus is extensively used for cloud-native applications since it is built to monitor and gather metrics from many services and systems. This post will walk you through setting up Prometheus to successfully monitor your Kubernetes cluster.

Prerequisites

Before you begin, ensure you have the following prerequisites in place:

  1. A running Kubernetes cluster.
  2. kubectl command-line tool installed and configured.
  3. Helm installed in your Kubernetes cluster for easy package management.

Step 1: Setting Up Prometheus

In this step, we will deploy Prometheus to your Kubernetes cluster.

Create a Namespace

Let’s start by creating a Kubernetes namespace for Prometheus:

 
kubectl create namespace prometheus


Deploy Prometheus With Helm

Helm is a package manager for Kubernetes that makes it easy to deploy complex applications. We will use Helm to deploy Prometheus.

 
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/prometheus --namespace prometheus


Prometheus Helm chart provides default configurations, which are suitable for a basic setup.

Step 2: Accessing the Prometheus UI

To access the Prometheus web UI, you can set up a port-forward to the Prometheus server:

 
kubectl port-forward -n prometheus prometheus-prometheus-server-0 9090


Now you can access the Prometheus web UI at http://localhost:9090. Here, you can explore metrics, query data, and create custom alerts.

Step 3: Setting Up Prometheus Targets

Prometheus monitors services by scraping metrics endpoints. You need to specify which services Prometheus should scrape. This is done using Kubernetes ServiceMonitors.

Create a ServiceMonitor

Create a ServiceMonitor resource to define which services Prometheus should scrape. Here is an example ServiceMonitor for monitoring a sample app:

 
 apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: sample-app-monitor
      namespace: prometheus
    spec:
      selector:
        matchLabels:
          app: sample-app
      endpoints:
      - port: web


Apply the ServiceMonitor:

 
kubectl apply -f sample-app-monitor.yaml


Step 4: Alerting With Prometheus

Prometheus allows you to set up alerts based on the collected metrics.

Create Alerting Rules

You can define alerting rules in a PrometheusRule resource. Here is an example of a rule that alerts when the sample app’s error rate is high:

 
groups:
    - name: example
      rules:
      - alert: HighErrorRate
        expr: |
          rate(http_server_requests_total{job="sample-app", status="500"}[5m])
          /
          rate(http_server_requests_total{job="sample-app"}[5m]) > 0.01
        for: 10m
        labels:
          severity: page
        annotations:
          summary: High error rate on {{ $labels.instance }}


Apply the PrometheusRule:

 
kubectl apply -f sample-app-alert-rules.yaml


Configuring Alertmanager

Prometheus sends alerts to an Alertmanager, which can route them to various receivers, like email, Slack, or a webhook.

Here’s a basic Alertmanager configuration:

 
route:
group_by: ['alertname', 'job']
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: 'slack'
receivers:
- name: 'slack'
slack_configs:
- send_resolved: true
title: 'Alert'
text: '{{ .CommonAnnotations.summary }}'
api_url: 'https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK'


Apply the Alertmanager configuration:

 
kubectl apply -f alertmanager-config.yaml


Step 5: Advanced Configurations

  • Storage Configuration: In production, it’s essential to configure persistent storage for Prometheus to ensure data durability.
  • Node Exporter and Other Exporters: You can set up Node Exporter, Blackbox Exporter, and other exporters to collect more specific metrics.
  • Remote Write and Remote Read: To scale Prometheus horizontally, consider configuring remote write and remote read to use a remote storage solution like Thanos or Cortex.

Step 6: Clean Up

Remember to clean up any resources you created during this tutorial when you no longer need them.

Conclusion

Prometheus is a robust monitoring and alerting tool that may assist you in maintaining the health of your Kubernetes applications and infrastructure. Prometheus allows you to gather and analyze numerous metrics, set up alarms, and obtain insights into the behavior of your system. This article will provide you with a basic grasp of how to set up Prometheus in a Kubernetes system.

Consult the official Prometheus documentation and investigate further subjects like as Grafana integration, custom exporters, and long-term storage options to continue investigating and exploiting Prometheus efficiently.

Please keep in mind that this tutorial is only a starting point; you may need to modify it to fit your individual use case and requirements.

Kubernetes UI Use case app cluster remote

Published at DZone with permission of Aditya Bhuyan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Pulsar on KubeSphere: Installing Distributed Messaging and Streaming Platform
  • Main Features and Benefits of Google Kubernetes Engine
  • Watching the Requests Go By: Reconstructing an API Spec with APIClarity
  • Implementing iOS Accessibility: A Developer's Practical Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook