Skip to content

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OCI Container Instance Monitoring Demo

Production-ready monitoring solution for OCI Container Instances with sidecar-based Prometheus metrics collection, log forwarding, and automatic Management Agent registration.

🎯 Overview

This demo provides comprehensive container monitoring using a modern sidecar architecture:

  • πŸ“Š Sidecar-Based Monitoring: Management Agent, Prometheus, and Log Forwarder run as sidecar containers
  • πŸ”„ Automatic Registration: Management Agent auto-registers with your OCI tenancy
  • πŸ“ Log Forwarding: Automatic log collection and forwarding to OCI Logging
  • πŸ”’ Network Security: NSG with automatic IP detection for secure access
  • πŸ“¦ One-Command Deployment: Fully automated infrastructure provisioning
  • 🐳 Multi-Exporter Support: cAdvisor, Node Exporter, and application-specific exporters

πŸ—οΈ Sidecar Architecture (7 Containers)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              OCI Container Instance - 7 Containers                       β”‚
β”‚              Public IP: YOURIP                                           β”‚
β”‚                                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚  β”‚  Application    β”‚  β”‚  Official Oracle β”‚  β”‚  Prometheus      β”‚      β”‚
β”‚  β”‚  Container      β”‚  β”‚  Mgmt Agent      β”‚  β”‚  Sidecar         β”‚      β”‚
β”‚  β”‚  + cAdvisor     β”‚  β”‚  Sidecar v1.9.0  β”‚  β”‚                  β”‚      β”‚
β”‚  β”‚  + Node Export. β”‚  β”‚                  β”‚  β”‚  - Aggregates    β”‚      β”‚
β”‚  β”‚                 β”‚  β”‚  - Auto-registers│◄──    all metrics   β”‚      β”‚
β”‚  β”‚  - App :80      β”‚  β”‚  - ConfigFile    β”‚  β”‚  - Scrapes :9090 β”‚      β”‚
β”‚  β”‚  - Metrics :9090│◄──    with input.rspβ”‚  β”‚  - Prometheus DB β”‚      β”‚
β”‚  β”‚  - cAdvisor:8080β”‚  β”‚  - Resource      β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β”‚  β”‚  - Node Exp:9100β”‚  β”‚    Principal     β”‚                            β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  - Sends to OCI  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚           β”‚           β”‚    Monitoring    β”‚  β”‚  Grafana         β”‚      β”‚
β”‚           β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  Sidecar         β”‚      β”‚
β”‚           β”‚                                 β”‚                  β”‚      β”‚
β”‚           β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  - Dashboards    β”‚      β”‚
β”‚           β”‚           β”‚  Log Forwarder   β”‚  β”‚  - Prometheus DS β”‚      β”‚
β”‚           └──────────►│  Sidecar         β”‚  β”‚  - Port :3000    β”‚      β”‚
β”‚      Writes logs      β”‚                  β”‚  β”‚  - admin/admin   β”‚      β”‚
β”‚      to /logs         β”‚  - Monitors /logs│◄─┼──Queries────────│      β”‚
β”‚                       β”‚  - Batch forward β”‚  β”‚    Prometheus    β”‚      β”‚
β”‚                       β”‚  - OCI Logging   β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β”‚                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                            β”‚
β”‚                                                                          β”‚
β”‚  Shared Volumes:                                                         β”‚
β”‚  β€’ /metrics  - Shared for metrics exchange                              β”‚
β”‚  β€’ /logs     - Shared for log collection                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                   β”‚
              β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
              β”‚OCI          β”‚    β”‚OCI           β”‚
              β”‚Monitoring   β”‚    β”‚Logging       β”‚
              β”‚             β”‚    β”‚              β”‚
              β”‚Namespace:   β”‚    β”‚Log Group:    β”‚
              β”‚container_   β”‚    β”‚container-    β”‚
              β”‚monitoring   β”‚    β”‚instance-logs β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Έ Screenshots & Component Integration

This section demonstrates the complete monitoring stack in action, showing how all 7 containers work together to provide comprehensive observability.

Note: Screenshot image files should be placed in the /screenshots/ directory. See /screenshots/README.md for required files and naming conventions.

Important: Throughout this documentation, YOURIP is used as a placeholder for your Container Instance's public IP address. Replace it with your actual deployment IP when accessing the services.

1. Grafana Metrics Explorer

Grafana provides a powerful visualization interface for all collected metrics. The screenshot below shows the Metrics Explorer with container-level metrics:

Grafana Metrics Explorer

What you see:

  • Real-time container CPU and memory usage
  • Network I/O metrics for all containers
  • Custom application metrics from the application container
  • Historical data visualizations with configurable time ranges
  • Prometheus datasource pre-configured and connected

Access: http://YOURIP:3000 (Username: admin, Password: admin)

2. Application Monitoring Stack Overview

The main application webpage provides a comprehensive overview of the monitoring architecture:

Application Webpage

What you see:

  • Visual representation of the 7-container sidecar architecture
  • Component health status indicators
  • Links to access each monitoring component directly
  • System information and deployment details
  • Real-time health check status

Access: http://YOURIP/

3. Prometheus Target Health

Prometheus continuously scrapes metrics from all configured exporters. This view shows the health status of all scrape targets:

Prometheus Targets

What you see:

  • cAdvisor (localhost:8080) - Container metrics - Status: UP
  • Node Exporter (localhost:9100) - Host system metrics - Status: UP
  • Application Metrics (localhost:9090) - Custom app metrics - Status: UP
  • Last scrape time and duration for each target
  • Error indicators if any target is down

Access: http://YOURIP:9090/targets

4. cAdvisor Detailed Metrics

cAdvisor provides detailed container-level metrics in Prometheus format. This shows the raw metrics output:

cAdvisor Metrics

What you see:

  • container_cpu_usage_seconds_total - CPU usage per container
  • container_memory_usage_bytes - Current memory usage
  • container_network_receive_bytes_total - Network ingress
  • container_network_transmit_bytes_total - Network egress
  • container_fs_reads_bytes_total - Filesystem read operations
  • Labels for container identification (name, id, image)

Access: http://YOURIP:8080/metrics

Component Integration Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Complete Metrics & Logs Flow                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. APPLICATION CONTAINER
   β”œβ”€ Generates custom application metrics
   β”œβ”€ Writes logs to /logs/application.log
   └─ Exposes health check endpoint

2. cAdVISOR (Port 8080)
   β”œβ”€ Monitors all 7 containers in the instance
   β”œβ”€ Collects CPU, memory, network, filesystem metrics
   └─ Exposes metrics at :8080/metrics

3. NODE EXPORTER (Port 9100)
   β”œβ”€ Monitors host system (container instance)
   β”œβ”€ Collects CPU, memory, disk, network interface stats
   └─ Exposes metrics at :9100/metrics

4. PROMETHEUS (Port 9090)
   β”œβ”€ Scrapes cAdvisor every 60s
   β”œβ”€ Scrapes Node Exporter every 60s
   β”œβ”€ Scrapes Application metrics every 60s
   β”œβ”€ Stores time-series data locally
   β”œβ”€ Provides query interface (PromQL)
   └─ Exposes aggregated metrics at :9090/metrics

5. GRAFANA (Port 3000)
   β”œβ”€ Connects to Prometheus as datasource
   β”œβ”€ Queries metrics using PromQL
   β”œβ”€ Visualizes data in pre-configured dashboards
   └─ Provides alerting capabilities

6. MANAGEMENT AGENT (Background)
   β”œβ”€ Scrapes Prometheus endpoint (:9090/metrics)
   β”œβ”€ Forwards metrics to OCI Monitoring service
   β”œβ”€ Sends to namespace: container_monitoring
   └─ Enables viewing metrics in OCI Console

7. LOG FORWARDER (Background)
   β”œβ”€ Monitors /logs directory for new log entries
   β”œβ”€ Reads: application.log, prometheus.log, cadvisor.log
   β”œβ”€ Batches log entries (100 per batch)
   └─ Forwards to OCI Logging using Resource Principal

Verification Steps

After deployment, verify the integration:

# 1. Check all containers are ACTIVE
oci container-instances container list \
  --container-instance-id <instance-id> \
  --query 'data[*].{Name:"display-name",State:"lifecycle-state"}' \
  --output table

# 2. Test Application
curl http://YOURIP/
curl http://YOURIP/health

# 3. Test Prometheus
curl http://YOURIP:9090/-/healthy
curl http://YOURIP:9090/api/v1/targets

# 4. Test cAdvisor
curl http://YOURIP:8080/metrics | grep container_cpu

# 5. Test Node Exporter
curl http://YOURIP:9100/metrics | grep node_cpu

# 6. Access Grafana
open http://YOURIP:3000
# Login: admin/admin

# 7. Verify OCI Monitoring (metrics should appear within 2-3 minutes)
oci monitoring metric list \
  --compartment-id $COMPARTMENT_OCID \
  --namespace container_monitoring

# 8. Verify OCI Logging
oci logging-search search-logs \
  --search-query "search \"<log-group-ocid>\" | sort by datetime desc" \
  --time-start "2025-01-01T00:00:00.000Z" \
  --time-end "2025-12-31T23:59:59.999Z"

✨ Key Features

Automated Monitoring

  • βœ… Management Agent Auto-Registration: Automatically registers with OCI tenancy using install key
  • βœ… Sidecar-Based Collection: No agent installation needed on host
  • βœ… Prometheus Integration: Metrics collected via Prometheus protocol
  • βœ… OCI Monitoring: Metrics appear in container_monitoring namespace
  • βœ… Real-time Collection: 60-second default scrape interval (configurable)

Log Management

  • βœ… Automatic Log Forwarding: Sidecar monitors and forwards logs to OCI Logging
  • βœ… Shared Volume Pattern: Application writes to /logs, forwarder reads and sends
  • βœ… Batch Processing: Efficient batching with configurable size
  • βœ… Resource Principal Auth: Secure, credential-less authentication

Network Security

  • βœ… Automatic IP Detection: NSG automatically configured with your public IP
  • βœ… Port-Based Rules: Secure access to all monitoring ports
  • βœ… Least Privilege: Only necessary ports exposed to your IP
  • πŸ”’ Private Subnet Recommended: For production deployments, use private subnets without public IP assignment. Public IPs shown in examples are for testing only.

Container Images in Deployment

  1. Official Oracle Management Agent - v1.9.0 from Oracle Container Registry (no custom build needed)
  2. Prometheus Sidecar - Custom image: Aggregates metrics from all local exporters
  3. Application Container - Custom image: Sample nginx app with health endpoints
  4. Log Forwarder Sidecar - Custom image: Monitors /logs and forwards to OCI Logging
  5. Grafana - Official grafana/grafana:latest with auto-configured Prometheus datasource
  6. cAdvisor - Official google/cadvisor:latest for container metrics
  7. Node Exporter - Official prom/node-exporter:latest for host metrics

πŸ“‹ Prerequisites

Required Tools

  • Terraform >= 1.0.0
  • OCI CLI configured with valid credentials
  • Docker for building container images
  • Git for cloning repository

OCI Resources Required

  • OCI tenancy with appropriate compartment
  • VCN with subnet
  • OCIR (Oracle Cloud Infrastructure Registry) access
  • IAM permissions for:
    • Container Instances
    • Management Agents
    • Monitoring
    • Logging
    • Networking (NSG creation)

OCI IAM Policies

The solution automatically creates necessary IAM policies and dynamic groups. Required root-level policies:

# Allow Container Instances to use OCI services
Allow dynamic-group <prefix>-container-instance-dg to manage all-resources in compartment <compartment>

# Allow Management Agent operations
Allow dynamic-group <prefix>-container-instance-dg to manage management-agents in compartment <compartment>
Allow dynamic-group <prefix>-container-instance-dg to use metrics in compartment <compartment>

# Allow log forwarding
Allow dynamic-group <prefix>-container-instance-dg to use log-content in compartment <compartment>

πŸš€ Quick Start

1. Clone Repository

git clone https://github.com/your-username/oci-container-monitoring-demo.git
cd oci-container-monitoring-demo

2. Configure Environment

# Copy example configuration
cp config/oci-monitoring.env.example config/oci-monitoring.env

# Edit configuration with your OCI details
vi config/oci-monitoring.env

Required Configuration:

# OCI Authentication
export OCI_REGION="eu-frankfurt-1"
export OCI_TENANCY_OCID="ocid1.tenancy.oc1..aaaa..."
export OCI_COMPARTMENT_OCID="ocid1.compartment.oc1..aaaa..."

# Networking
export VCN_OCID="ocid1.vcn.oc1..aaaa..."
export SUBNET_OCID="ocid1.subnet.oc1..aaaa..."

# OCIR Credentials (for pushing custom images)
export OCIR_USERNAME="<namespace>/<username>"
export OCIR_PASSWORD="<auth_token>"

# Container Configuration
export CONTAINER_INSTANCE_NAME="monitoring-demo"

# Sidecar Architecture (Enable all sidecars)
export ENABLE_SHARED_VOLUMES="true"
export ENABLE_MANAGEMENT_AGENT_SIDECAR="true"
export ENABLE_PROMETHEUS_SIDECAR="true"
export ENABLE_LOG_FORWARDER_SIDECAR="true"

3. Build and Push Container Images

cd docker
./build-all.sh

What this does:

  • Builds 4 container images:
    1. Management Agent Sidecar
    2. Prometheus Sidecar
    3. Application with Metrics
    4. Log Forwarder Sidecar
  • Pushes images to OCIR
  • Automatically updates config/oci-monitoring.env with image URLs

Output:

βœ“ Configuration file updated successfully!

Updated image URLs:
  MGMT_AGENT_SIDECAR_IMAGE="fra.ocir.io/.../mgmt-agent-sidecar:1.0.0"
  PROMETHEUS_SIDECAR_IMAGE="fra.ocir.io/.../prometheus-sidecar:1.0.0"
  APP_WITH_METRICS_IMAGE="fra.ocir.io/.../app-with-metrics:1.0.0"
  LOG_FORWARDER_SIDECAR_IMAGE="fra.ocir.io/.../log-forwarder-sidecar:1.0.0"

4. Deploy Infrastructure

cd ..
./scripts/deploy.sh deploy

Deployment includes:

  • βœ… IAM policies and dynamic groups
  • βœ… Management Agent install key creation
  • βœ… NSG with automatic IP detection
  • βœ… Container Instance with all sidecars
  • βœ… OCI Logging log group and logs
  • βœ… Optional monitoring alarms

5. Verify Deployment

Check Management Agent Registration

# List Management Agents in your compartment
oci management-agent agent list \
  --compartment-id $OCI_COMPARTMENT_OCID \
  --lifecycle-state ACTIVE \
  --query 'data[*].{"Name":"display-name","State":"lifecycle-state","Host":"host"}' \
  --output table

Expected Output:

+---------------------------------+---------+---------------------------+
| Name                            | State   | Host                      |
+---------------------------------+---------+---------------------------+
| <hostname>-mgmt-agent          | ACTIVE  | monitoring-demo-app       |
+---------------------------------+---------+---------------------------+

View Metrics in OCI Console

  1. Navigate to: Observability & Management β†’ Monitoring β†’ Metrics Explorer
  2. Select your compartment
  3. Choose namespace: container_monitoring
  4. Select metrics:
    • container_cpu_usage_seconds_total
    • container_memory_usage_bytes
    • app_requests_total (custom metric)

View Logs in OCI Console

  1. Navigate to: Observability & Management β†’ Logging β†’ Logs
  2. Find log group: container-instance-logs
  3. View logs:
    • Application Log - Application stdout/stderr
    • System Log - Container system logs

Check Container Status

# Get container instance details
oci container-instances container-instance get \
  --container-instance-id <instance-ocid> \
  --query 'data.{"State":"lifecycle-state","Containers":"containers[*].{Name:display-name,State:lifecycle-state}"}' \
  --output json

Expected Output (7 Containers):

{
  "State": "ACTIVE",
  "Containers": [
    {
      "Name": "monitoring-demo-app",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-cadvisor",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-node-exporter",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-mgmt-agent-sidecar",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-prometheus-sidecar",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-log-forwarder-sidecar",
      "State": "ACTIVE"
    },
    {
      "Name": "monitoring-demo-grafana-sidecar",
      "State": "ACTIVE"
    }
  ]
}

Access Grafana Dashboard

Grafana is pre-configured with Prometheus datasource and provides powerful visualization capabilities.

Access URL: http://YOURIP:3000

Default Credentials:

  • Username: admin
  • Password: admin

What to verify:

  1. Login to Grafana at http://YOURIP:3000
  2. Navigate to Explore β†’ Metrics Explorer for ad-hoc queries
  3. Check Data Sources β†’ Prometheus should be pre-configured pointing to http://localhost:9090
  4. Query metrics using PromQL:
    • container_cpu_usage_seconds_total - Container CPU usage
    • container_memory_usage_bytes - Container memory usage
    • rate(container_network_receive_bytes_total[5m]) - Network ingress rate
    • Custom application metrics from your application container
  5. Create custom dashboards by importing or building from scratch

Access Prometheus Directly

# Prometheus web UI
curl http://YOURIP:9090

# Or open in browser
open http://YOURIP:9090

# Query API example
curl 'http://YOURIP:9090/api/v1/query?query=up'

Access Container Metrics Exporters

# cAdvisor - Container metrics (detailed container statistics)
curl http://YOURIP:8080/metrics

# Node Exporter - Host metrics (system-level stats)
curl http://YOURIP:9100/metrics

# Application metrics (custom metrics from your app)
curl http://YOURIP/metrics

πŸ“Š Complete Workflow

Phase 1: Build Container Images (docker/build-all.sh)

1. Login to OCIR
2. Build Management Agent Sidecar
   └─ Oracle Management Agent v1.9.0
   └─ Configure auto-registration with Resource Principal
3. Build Prometheus Sidecar
   └─ Prometheus server with scrape configuration
   └─ Targets: cAdvisor, Node Exporter, Application
4. Build Application Container
   └─ Nginx with health check endpoints
   └─ Generates metrics for demonstration
5. Build Log Forwarder Sidecar
   └─ Python app with OCI SDK
   └─ Watches /logs directory and forwards to OCI Logging
6. Push all 4 custom images to OCIR
7. AUTO-UPDATE config/oci-monitoring.env
   └─ Updates all image URLs automatically!

Note: Grafana, cAdvisor, and Node Exporter use official public images

Phase 2: Terraform Infrastructure (./scripts/deploy.sh deploy)

1. Initialize Terraform
2. Detect Your Public IP (automatic)
   └─ Uses https://ifconfig.me/ip
3. Create IAM Resources
   β”œβ”€ Dynamic Group for Container Instances
   β”œβ”€ Policies for Management Agent
   β”œβ”€ Policies for Monitoring
   └─ Policies for Logging
4. Create NSG with Your IP
   β”œβ”€ HTTP/HTTPS: 80, 443
   β”œβ”€ Prometheus: 9090
   β”œβ”€ cAdvisor: 8080
   β”œβ”€ Node Exporter: 9100
   └─ Optional exporters: 9104, 9113, 9115, 9121, 9187
5. Create Management Agent Install Key
6. Create Logging Resources
   β”œβ”€ Log Group
   β”œβ”€ Application Log
   └─ System Log
7. Deploy Container Instance with 7 Containers
   β”œβ”€ Application Container (nginx)
   β”œβ”€ cAdvisor (container metrics)
   β”œβ”€ Node Exporter (host metrics)
   β”œβ”€ Management Agent Sidecar (forwards to OCI)
   β”œβ”€ Prometheus Sidecar (aggregates metrics)
   β”œβ”€ Log Forwarder Sidecar (forwards logs)
   └─ Grafana (visualization)
8. Attach NSG to Container Instance
9. Configure Shared Volumes
   β”œβ”€ /metrics (Prometheus access)
   └─ /logs (Log Forwarder access)

Phase 3: Container Startup & Registration

Application Container:
1. Starts nginx web server on port 80
2. Serves health check endpoint at /health
3. Generates sample metrics
4. Writes logs to /logs/application.log

cAdvisor Container:
1. Starts cAdvisor on port 8080
2. Monitors all 7 containers in the instance
3. Collects CPU, memory, network, filesystem metrics
4. Exposes metrics at :8080/metrics

Node Exporter Container:
1. Starts Node Exporter on port 9100
2. Monitors host system resources
3. Collects CPU, memory, disk, network interface stats
4. Exposes metrics at :9100/metrics

Prometheus Sidecar:
1. Loads prometheus.yml configuration
2. Scrapes cAdvisor (:8080/metrics) every 60s
3. Scrapes Node Exporter (:9100/metrics) every 60s
4. Scrapes Application metrics every 60s
5. Stores time-series data in local database
6. Provides query interface at :9090
7. Exposes aggregated metrics at :9090/metrics

Grafana Sidecar:
1. Starts Grafana on port 3000
2. Auto-configures Prometheus datasource (localhost:9090)
3. Installs default plugins
4. Ready for dashboard creation and metric visualization
5. Login: admin/admin

Management Agent Sidecar:
1. Downloads Management Agent RPM from OCI Object Storage
2. Extracts and installs agent using rpm2cpio
3. Creates response file with install key from environment
4. Runs setup.sh β†’ REGISTERS WITH OCI
   β”œβ”€ Validates install key with OCI service
   β”œβ”€ Generates communication wallet
   β”œβ”€ Generates security artifacts
   └─ Registers with Management Agent service
5. Configures Prometheus plugin (prometheusPluginConfig.json)
6. Starts agent (agentcore start)
7. Begins scraping Prometheus endpoint (:9090/metrics) every 60s
8. Forwards metrics to OCI Monitoring namespace: container_monitoring

Log Forwarder Sidecar:
1. Initializes OCI Logging client with Resource Principal
2. Monitors /logs directory using file watchers
3. Watches multiple log files: application.log, prometheus.log, etc.
4. Detects new log entries in real-time
5. Batches logs (100 entries per batch)
6. Forwards batches to OCI Logging using Resource Principal
7. Continues monitoring for new logs (no polling, event-driven)

Phase 4: Monitoring & Verification

Local Access (via Public IP: YOURIP):
1. Grafana Dashboard: http://YOURIP:3000
   β”œβ”€ Login: admin/admin
   β”œβ”€ Explore metrics using Metrics Explorer
   β”œβ”€ Create custom dashboards
   └─ Query Prometheus datasource with PromQL
2. Prometheus UI: http://YOURIP:9090
   β”œβ”€ View targets health status
   β”œβ”€ Execute PromQL queries
   └─ Graph metrics
3. cAdvisor: http://YOURIP:8080
   └─ View detailed container metrics
4. Node Exporter: http://YOURIP:9100/metrics
   └─ View host system metrics
5. Application: http://YOURIP/
   └─ Health check: http://YOURIP/health

OCI Management Agent Console:
1. Navigate to: Observability & Management β†’ Management Agents
2. Find agent: <hostname>-mgmt-agent
3. Status: ACTIVE
4. Plugin: Prometheus (Enabled)

OCI Monitoring Console:
1. Navigate to: Observability & Management β†’ Monitoring β†’ Metrics Explorer
2. Namespace: container_monitoring
3. View metrics (appear within 2-3 minutes after deployment):
   β”œβ”€ container_cpu_usage_seconds_total
   β”œβ”€ container_memory_usage_bytes
   β”œβ”€ container_network_receive_bytes_total
   β”œβ”€ container_network_transmit_bytes_total
   β”œβ”€ node_cpu_seconds_total
   β”œβ”€ node_memory_MemAvailable_bytes
   └─ Custom application metrics (if configured)

OCI Logging Console:
1. Navigate to: Observability & Management β†’ Logging β†’ Logs
2. Log Group: container-instance-logs
3. Logs:
   β”œβ”€ Application Log (from /logs/application.log)
   └─ Additional logs (prometheus, cadvisor, etc. if configured)

πŸ—‚οΈ Project Structure

oci-container-monitoring-demo/
β”œβ”€β”€ config/
β”‚   └── oci-monitoring.env          # Main configuration (auto-updated by build-all.sh)
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ build-all.sh                # Builds all 4 images + AUTO-UPDATES .env
β”‚   β”œβ”€β”€ management-agent/
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   └── entrypoint.sh           # Agent registration & startup
β”‚   β”œβ”€β”€ prometheus/
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   └── prometheus.yml
β”‚   β”œβ”€β”€ app-with-metrics/
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   └── app.py                  # Sample app with /metrics
β”‚   └── log-forwarder/
β”‚       β”œβ”€β”€ Dockerfile
β”‚       β”œβ”€β”€ log-forwarder.py        # Monitors /logs and forwards to OCI
β”‚       └── config.json.template
β”œβ”€β”€ terraform/
β”‚   β”œβ”€β”€ main.tf                     # Main config + NSG module
β”‚   β”œβ”€β”€ variables.tf
β”‚   β”œβ”€β”€ outputs.tf                  # Includes detected IP & NSG info
β”‚   └── modules/
β”‚       β”œβ”€β”€ container-instance/     # Container Instance + sidecars
β”‚       β”œβ”€β”€ iam/                    # Policies + dynamic groups
β”‚       β”œβ”€β”€ logging/                # OCI Logging resources
β”‚       β”œβ”€β”€ management-agent/       # Install key creation
β”‚       └── nsg/                    # Network Security Group
β”œβ”€β”€ scripts/
β”‚   └── deploy.sh                   # Deployment orchestration
└── README.md                       # This file

πŸ“‘ Management Agent Prometheus Data Source

The Management Agent uses the Prometheus plugin to collect metrics and forward them to OCI Monitoring. This section explains how it's configured and how metrics flow from Prometheus to OCI.

How It Works

Application Metrics (:9090/metrics)
    ↓
Prometheus Sidecar (aggregates)
    ↓
Management Agent (scrapes Prometheus)
    ↓
OCI Monitoring (container_monitoring namespace)

Configuration

The Management Agent Prometheus plugin is automatically configured using the ConfigFile volume with input.rsp:

# Automatically created by Terraform in ConfigFile volume
ManagementAgentInstallKey=${MGMT_AGENT_INSTALL_KEY}
AgentDisplayName=${CONTAINER_INSTANCE_NAME}-mgmt-agent
CredentialWalletPassword=${AUTO_GENERATED_PASSWORD}
Service.plugin.prometheus.download=true  # Downloads Prometheus plugin

Prometheus Plugin Configuration

The plugin configuration is automatically created at agent startup:

Location: /opt/oracle/mgmt_agent/agent_inst/config/prometheus/prometheusPluginConfig.json

{
  "entities": [
    {
      "namespace": "oci_prometheus_metrics",
      "metricNamespace": "container_monitoring",
      "resourceGroup": "monitoring-demo",
      "prometheusConfig": {
        "sourceUrl": "http://localhost:9090/metrics",
        "scrapeInterval": "60s",
        "scrapeTimeout": "10s"
      }
    }
  ]
}

Viewing Metrics in OCI Console

Step 1: Navigate to OCI Monitoring

  1. Go to Observability & Management β†’ Monitoring β†’ Metrics Explorer
  2. Select your compartment
  3. Choose namespace: container_monitoring (NOT oci_prometheus_metrics)

Step 2: Query Metrics Available metric families:

  • container_cpu_usage_seconds_total - CPU usage per container
  • container_memory_usage_bytes - Memory usage per container
  • container_network_receive_bytes_total - Network ingress
  • container_network_transmit_bytes_total - Network egress
  • node_cpu_seconds_total - Host CPU metrics
  • node_memory_MemAvailable_bytes - Host memory metrics
  • Custom application metrics (if exposed)

Step 3: Create Charts

  • Use PromQL-like queries in OCI Metrics Explorer
  • Set aggregation intervals (1m, 5m, 1h)
  • Create alarms based on thresholds

Verification

Check Management Agent Status:

# List Management Agents
oci management-agent agent list \
  --compartment-id $OCI_COMPARTMENT_OCID \
  --lifecycle-state ACTIVE \
  --query 'data[*].{"Name":"display-name","State":"lifecycle-state"}' \
  --output table

Check Prometheus Plugin:

# In Management Agent container logs, look for:
βœ“ Prometheus plugin configured
βœ“ Agent is now collecting and forwarding metrics to OCI Monitoring

Verify Metrics Flow:

# Test Prometheus endpoint
curl http://YOURIP:9090/metrics

# Test specific metric query
curl 'http://YOURIP:9090/api/v1/query?query=container_memory_usage_bytes'

# Check OCI Monitoring for recent data points (metrics appear within 2-3 minutes)
oci monitoring metric list \
  --compartment-id $OCI_COMPARTMENT_OCID \
  --namespace container_monitoring

Customizing Scrape Configuration

To modify scrape intervals or add additional scrape targets, update terraform.tfvars:

# Metrics Configuration
prometheus_scrape_interval  = 60   # seconds
prometheus_scrape_timeout   = 10   # seconds
metrics_namespace          = "container_monitoring"

Then re-apply terraform:

cd terraform
terraform apply -auto-approve

πŸ”§ Configuration Reference

Sidecar Architecture Variables

# Enable sidecar pattern
export ENABLE_SHARED_VOLUMES="true"

# Management Agent Sidecar
export ENABLE_MANAGEMENT_AGENT_SIDECAR="true"
export MGMT_AGENT_SIDECAR_IMAGE="fra.ocir.io/.../mgmt-agent-sidecar:1.0.0"
export MGMT_AGENT_SIDECAR_MEMORY_GB="1.0"
export MGMT_AGENT_SIDECAR_OCPUS="0.25"

# Prometheus Sidecar
export ENABLE_PROMETHEUS_SIDECAR="true"
export PROMETHEUS_SIDECAR_IMAGE="fra.ocir.io/.../prometheus-sidecar:1.0.0"
export PROMETHEUS_SIDECAR_MEMORY_GB="1.0"
export PROMETHEUS_SIDECAR_OCPUS="0.25"

# Log Forwarder Sidecar
export ENABLE_LOG_FORWARDER_SIDECAR="true"
export LOG_FORWARDER_SIDECAR_IMAGE="fra.ocir.io/.../log-forwarder-sidecar:1.0.0"
export LOG_FORWARDER_SIDECAR_MEMORY_GB="0.5"
export LOG_FORWARDER_SIDECAR_OCPUS="0.125"

Prometheus Exporter Configuration

# Base exporters (always recommended)
export ENABLE_PROMETHEUS_EXPORTERS="true"  # cAdvisor + Node Exporter

# Application-specific exporters (optional)
export ENABLE_NGINX_EXPORTER="false"      # Nginx metrics
export ENABLE_REDIS_EXPORTER="false"      # Redis metrics
export ENABLE_POSTGRES_EXPORTER="false"   # PostgreSQL metrics
export ENABLE_MYSQL_EXPORTER="false"      # MySQL metrics
export ENABLE_BLACKBOX_EXPORTER="false"   # Endpoint probing

Metrics Configuration

export PROMETHEUS_SCRAPE_INTERVAL="60"    # Seconds between scrapes
export PROMETHEUS_SCRAPE_TIMEOUT="10"     # Scrape timeout
export PROMETHEUS_METRICS_PORT="9090"     # Metrics endpoint port
export PROMETHEUS_METRICS_PATH="/metrics" # Metrics endpoint path
export METRICS_NAMESPACE="container_monitoring"  # OCI Monitoring namespace

Logging Configuration

export ENABLE_LOGGING="true"              # Enable OCI Logging
export LOG_GROUP_NAME="container-instance-logs"
export LOG_RETENTION_DAYS="30"
export ENABLE_AUDIT_LOGS="true"

πŸ” Troubleshooting

Management Agent Not Registered

Check container logs:

oci container-instances container list \
  --container-instance-id <instance-id> \
  --query 'data[?contains("display-name", `mgmt-agent`)].id' \
  --raw-output | head -1 | xargs -I {} \
  oci container-instances container retrieve-logs \
  --container-id {}

Common issues:

  1. Install key expired - Create new install key
  2. IAM policies missing - Verify dynamic group and policies
  3. Network connectivity - Check NSG and subnet route tables
  4. Agent already registered - Agent persists in volume, container restart uses existing registration

No Metrics in OCI Monitoring

Verify Management Agent status:

# In Management Agent sidecar container logs, look for:
βœ“ Management Agent registered successfully with OCI
βœ“ Management Agent started successfully
βœ“ Agent is now collecting and forwarding metrics

Check Prometheus endpoint:

# From within container instance:
curl http://localhost:9090/metrics

Verify namespace:

  • Metrics appear in namespace: container_monitoring
  • NOT in oci_prometheus_metrics (that's the plugin namespace)

Logs Not Appearing in OCI Logging

Check Log Forwarder status:

# View log forwarder container logs
oci container-instances container list \
  --container-instance-id <instance-id> \
  --query 'data[?contains("display-name", `log-forwarder`)].id' \
  --raw-output | head -1 | xargs -I {} \
  oci container-instances container retrieve-logs \
  --container-id {}

Verify log OCID:

# Check that LOG_OCID environment variable is set in log forwarder
# It should match the application_log_ocid from logging module

NSG Blocking Access

Check your current IP:

curl https://ifconfig.me/ip

Verify NSG rules:

oci network nsg-security-rule list \
  --network-security-group-id <nsg-id> \
  --query 'data[*].{"Direction":"direction","Source":"source","Port":"tcp-options.destination-port-range"}' \
  --output table

Update NSG if IP changed:

# Re-run terraform to update NSG with new IP
cd terraform
terraform apply -auto-approve

πŸ“ˆ Monitoring Best Practices

Metric Collection

  1. Use appropriate scrape intervals: Balance between data resolution and resource usage
  2. Configure retention: Set appropriate retention in OCI Monitoring
  3. Use labels effectively: Leverage container, instance, and custom labels

Log Management

  1. Set retention policies: Configure log retention based on compliance needs
  2. Use log levels: Structure logs with severity levels (INFO, WARN, ERROR)
  3. Monitor log volume: Watch for unusual log volume spikes

Resource Allocation

  1. Sidecar sizing: Allocate appropriate CPU/memory to sidecars
  2. Monitor sidecar health: Check sidecar container status regularly
  3. Scale appropriately: Increase resources if sidecars are CPU/memory constrained

Security

  1. Rotate install keys: Periodically rotate Management Agent install keys
  2. Review IAM policies: Ensure least privilege access
  3. Monitor NSG rules: Keep allowed IP addresses up to date
  4. Use Resource Principal: Avoid hardcoded credentials

πŸ› οΈ Advanced Topics

Custom Metrics

Add custom metrics to your application:

from prometheus_client import Counter, Histogram, generate_latest

# Define metrics
request_count = Counter('app_requests_total', 'Total requests', ['method', 'endpoint'])
request_duration = Histogram('app_request_duration_seconds', 'Request duration')

# Instrument your code
@request_duration.time()
def handle_request():
    request_count.labels(method='GET', endpoint='/api').inc()
    # Your code here

# Expose metrics endpoint
@app.route('/metrics')
def metrics():
    return generate_latest()

Multi-Instance Deployment

Scale container instances:

# In config/oci-monitoring.env
export CONTAINER_COUNT="3"  # Deploy 3 replicas

Custom Dashboards

Create custom Grafana dashboards by importing JSON from OCI Monitoring:

  1. Export metrics from OCI Monitoring
  2. Convert to Grafana format
  3. Import into Grafana instance

πŸ“š Additional Resources

OCI Documentation

Prometheus

Related Projects

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see LICENSE file for details.

πŸ†˜ Support

For issues and questions:

🎯 Roadmap

Completed:

  • Grafana deployment - Now included with pre-configured Prometheus datasource
  • Official Oracle Management Agent - Using v1.9.0 from Oracle Container Registry
  • Automated registration - ConfigFile volume with Resource Principal authentication

Future enhancements planned:

  • Multi-region deployment
  • Alert manager integration
  • Additional custom metric dashboards
  • Automated backup/restore
  • HA configuration examples
  • Log forwarder improvements (OCI Logging integration optimization)

πŸ”– Version History

v2.0.0 (Current - November 2025)

  • βœ… Official Oracle Management Agent v1.9.0 - No custom builds required
  • βœ… Grafana Sidecar - Pre-configured with Prometheus datasource for powerful visualization
  • βœ… 7 Container Architecture - App, cAdvisor, Node Exporter, Mgmt Agent, Prometheus, Log Forwarder, Grafana
  • βœ… ConfigFile Volume - Automatic agent registration with input.rsp
  • βœ… Resource Principal Auth - No hardcoded credentials for secure authentication
  • βœ… Complete Observability Stack - Metrics, logs, and visualization in one deployment
  • βœ… Production Deployment - Currently running at YOURIP with all 7 containers ACTIVE
  • βœ… Enhanced Documentation - Screenshots and detailed component integration documentation

Current Deployment Status:

  • Public IP: YOURIP
  • All 7 containers: ACTIVE and healthy
  • Grafana: Accessible at :3000 (admin/admin)
  • Prometheus: Collecting metrics from all exporters
  • Log Forwarder: Successfully forwarding to OCI Logging
  • Management Agent: Registered and sending metrics to OCI Monitoring

v1.0.0 (Previous)

  • βœ… Sidecar-based architecture for all components
  • βœ… Custom Management Agent builds (deprecated in v2.0.0)
  • βœ… Log forwarding with OCI Logging integration
  • βœ… NSG with automatic IP detection
  • βœ… Complete Terraform automation

Built with ❀️ for OCI Container Instances

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages