Skip to content
SumGuy's Ramblings
Go back

Observability and Monitoring for Containers

In modern DevOps practices, observability and monitoring are indispensable for maintaining the health, performance, and reliability of applications, especially when they are containerized. Containers create a dynamic and ephemeral environment, making traditional monitoring approaches less effective. Fortunately, tools like Prometheus, Grafana, and Jaeger make it easier to achieve effective observability and monitoring for containerized applications.

In this article, we will provide a comprehensive guide on how to implement observability and monitoring solutions for containerized applications using Prometheus, Grafana, and Jaeger.

1. Why Observability and Monitoring Matter

Observability and monitoring allow you to:

In containerized environments, applications are broken down into microservices, each running in its own container, often orchestrated by Kubernetes. This architecture demands advanced observability solutions to effectively manage and monitor these distributed services.

2. Introduction to Prometheus, Grafana, and Jaeger

Prometheus

Prometheus is an open-source systems monitoring and alerting toolkit. Key features include:

Grafana

Grafana is an open-source platform for monitoring and observability. It allows you to:

Jaeger

Jaeger is an open-source, end-to-end distributed tracing tool. It helps with:

3. Setting Up Prometheus for Container Monitoring

Let’s start by setting up Prometheus to monitor container metrics.

Step 1: Install Prometheus

Assuming you are using Docker, create a prometheus.yml configuration file:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'docker'
    static_configs:
      - targets: ['172.17.0.1:9323']

Here, we are adding Prometheus itself and Docker as scrape targets.

Step 2: Run Prometheus in a Docker Container

docker run -d --name=prometheus -p 9090:9090 \
  -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus

This command runs Prometheus in a Docker container and binds it to port 9090. The configuration file is mapped into the container.

Step 3: Exposing Docker Metrics

We need the Docker daemon to expose metrics. If you’re using Docker for Mac or Docker for Windows, the following steps are different. For Linux, use this configuration:

Ensure the Docker daemon runs with the --metrics-addr flag to expose metrics.

Step 4: Verify Prometheus Setup

Visit http://localhost:9090 in your browser to access the Prometheus interface. You should be able to see metrics being scraped.

4. Visualizing Metrics with Grafana

Grafana allows you to create dashboards that visualize metrics collected by Prometheus.

Step 1: Install Grafana

Run the following command to start Grafana in a Docker container:

docker run -d --name=grafana -p 3000:3000 grafana/grafana

Step 2: Configure Grafana to Use Prometheus

Step 3: Create a Dashboard

Grafana will visualize the data in a graph, allowing you to monitor CPU usage across containers in real-time.

5. Distributed Tracing with Jaeger

Jaeger enables you to trace requests through various microservices, giving visibility into performance and latency issues.

Step 1: Install Jaeger

Run the following command to start Jaeger in a Docker container:

docker run -d --name=jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 14250:14250 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.6

(find the latest jaegar image at this getting started pagegetting started page )

Step 2: Instrument Your Application

Use Jaeger client libraries to instrument your application. For example, in a Python Flask app:

from flask import Flask
from jaeger_client import Config
from flask_opentracing import FlaskTracing

app = Flask(__name__)

def init_tracer(service):
    config = Config(
        config={
            'sampler': {'type': 'const', 'param': 1},
            'logging': True,
        },
        service_name=service,
    )
    return config.initialize_tracer()

tracer = init_tracer('my-service')
flask_tracing = FlaskTracing(tracer, True, app)

@app.route('/')
def home():
    with tracer.start_span('home-span') as span:
        span.log_kv({'event': 'home-page'})
        return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Step 3: View Traces

Open Jaeger’s UI by navigating to http://localhost:16686. You should be able to see traces and inspect the details of each request, including any performance bottlenecks.

6. Putting it All Together: A Practical Example

We will now combine Prometheus, Grafana, and Jaeger to monitor a simple containerized microservices application.

Step 1: Docker Compose Setup

Create a docker-compose.yml file:

services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"

  jaeger:
    image: jaegertracing/all-in-one:1.21
    ports:
      - "16686:16686"
      - "14268:14268"

Add any application services and configure them to expose Prometheus metrics and Jaeger traces.

Step 2: Run Docker Compose

In your terminal, navigate to the directory containing docker-compose.yml and run:

docker-compose up -d

This command will start Prometheus, Grafana, Jaeger, and your application services.

Step 3: Set Up Dashboards and Alerts in Grafana

Step 4: Collect Traces

Ensure your application services are instrumented with Jaeger. Use Jaeger’s UI to view traces across services.

7. Conclusion

Observability and monitoring are crucial for maintaining the health and performance of containerized applications. By using Prometheus, Grafana, and Jaeger, you can gain deep insights into your systems, detect issues early, and ensure smooth operation.

With Prometheus, you can collect and query metrics efficiently. Grafana allows you to visualize these metrics and set up alerts. Jaeger provides powerful distributed tracing capabilities to troubleshoot request flows and performance issues.

By combining these tools, you can achieve comprehensive observability for your containerized applications, helping you maintain high reliability and performance standards.


Share this post on:

Previous Post
LVM The Linux Sysadmin’s Guide to Flexible Storage
Next Post
Docker vs Podman: Key Differences