Setting Up Observability in Spring Boot Apps on Azure Kubernetes Service (AKS) with Logging, Tracing, and Metrics (Loki, Jaeger, Prometheus, Grafana, Azure Monitor): Complete Guide

This Cloud Component Diagram represents the observability architecture for a Spring Boot Microservice running on Azure Kubernetes Service (AKS) with logging, tracing, and metrics monitoring.

Key Components

1️⃣ Spring Boot Microservice (App) on AKS

  • Sends logs to Loki (Logging)
  • Sends traces to Jaeger via OpenTelemetry
  • Exposes metrics to Prometheus
  • Sends data to Azure Monitor

2️⃣ Observability Stack

  • Loki collects logs → Grafana visualizes them
  • Jaeger receives traces → Allows trace viewing
  • Prometheus scrapes metrics → Grafana visualizes them

3️⃣ User Interaction

  • Grafana provides real-time monitoring
  • Azure Monitor collects logs & metrics for deeper analysis

This architecture ensures full observability in a cloud-native microservices environment.

Here is a complete guide to implement Observability in Spring Boot Apps on Azure Kubernetes Service (AKS) from scratch. This setup will cover logging, tracing, and metrics using tools like Loki, Jaeger, Prometheus, Grafana, and Azure Monitor.


1. Prerequisites

Before starting, ensure you have the following:

  • Azure Subscription with an active AKS Cluster (Azure Kubernetes Service).
  • kubectl & Helm installed.
  • Docker installed to build the container images.
  • Azure CLI for managing Azure resources.
  • Spring Boot Microservice project set up (you can use any Spring Boot app).

2. Setting Up Azure Kubernetes Service (AKS)

2.1 Create AKS Cluster

  1. Log in to Azure using the Azure CLI:
    az login
  2. Create a resource group for the AKS cluster:
    az group create --name myResourceGroup --location eastus
  3. Create the AKS cluster:
    az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys
    
  4. Connect to the AKS cluster using kubectl:
    az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

3. Spring Boot Microservice Setup

3.1 Create a Spring Boot Application

  1. Add Actuator & Prometheus Dependencies in pom.xml:

    <dependencies>
        <!-- Micrometer Prometheus Registry (For metrics collection by Prometheus) -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        
        <!-- Spring Boot Starter Logging (Default logging for Spring Boot, consider using Logback with Loki for centralized logging) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
    
        <!-- OpenTelemetry Spring Boot Starter (For distributed tracing) -->
        <dependency>
            <groupId>io.opentelemetry.instrumentation</groupId>
            <artifactId>opentelemetry-spring-boot-starter</artifactId>
            <version>1.31.0</version>
        </dependency>
        
        <!-- Optional: Loki Logback Appender (For sending logs to Loki) -->
        <dependency>
            <groupId>com.github.loki4j</groupId>
            <artifactId>loki-logback-appender</artifactId>
            <version>1.4.1</version>
        </dependency>
    
        <!-- Optional: Spring Boot Actuator (Provides additional monitoring and management features) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    
  2. Configure Prometheus Metrics in application.yml:

    management:
      endpoints:
        web:
          exposure:
            include: "metrics, prometheus"
  3. Configure Jaeger Tracing in application.yml:

    otel:
      exporter:
        jaeger:
          endpoint: http://jaeger:14268/api/traces
      traces:
        sampler: always_on
  4. Build your Spring Boot application:

    mvn clean install

4. Dockerize Spring Boot App

  1. Create a Dockerfile in the project root:

    FROM openjdk:11-jre-slim
    COPY target/my-spring-boot-app.jar /app.jar
    ENTRYPOINT ["java", "-jar", "/app.jar"]
  2. Build Docker image:

    docker build -t my-spring-boot-app .
  3. Push the image to Azure Container Registry (ACR):

    az acr create --resource-group myResourceGroup --name myACR --sku Basic
    az acr login --name myACR
    docker tag my-spring-boot-app myacr.azurecr.io/my-spring-boot-app:v1
    docker push myacr.azurecr.io/my-spring-boot-app:v1

5. Install Loki, Prometheus, Grafana, and Jaeger on AKS

5.1 Install Helm

  1. Add Helm repositories for Prometheus, Loki, and Jaeger:
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm repo add grafana https://grafana.github.io/helm-charts
    helm repo add jaegertracing https://jaegertracing.github.io/helm-charts
    helm repo update

5.2 Deploy Loki (Logging)

  1. Install Loki using Helm:

    helm install loki grafana/loki-stack --set promtail.enabled=true
    
  2. Check the logs:

    kubectl logs -f -l app.kubernetes.io/name=loki

5.3 Deploy Prometheus (Metrics)

  1. Install Prometheus:

    helm install prometheus prometheus-community/kube-prometheus-stack
  2. Expose Prometheus for testing:

    kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090:9090

5.4 Deploy Jaeger (Tracing)

  1. Install Jaeger:

    helm install jaeger jaegertracing/jaeger
    
  2. Expose Jaeger UI:

    kubectl port-forward svc/jaeger-query 16686:80

6. Deploy Spring Boot Application on AKS

  1. Create Kubernetes Deployment YAML for Spring Boot App (deployment.yml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-boot-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: spring-boot-app
      template:
        metadata:
          labels:
            app: spring-boot-app
        spec:
          containers:
            - name: spring-boot-app
              image: myacr.azurecr.io/my-spring-boot-app:v1
              ports:
                - containerPort: 8080
  2. Apply the deployment:

    kubectl apply -f deployment.yml
  3. Expose the Spring Boot application:

    kubectl expose deployment spring-boot-app --type=LoadBalancer --name=spring-boot-app-service
    

7. Verify Observability Setup

  1. Logs in Grafana:

    • Open Grafana and configure Loki as a data source.
    • View logs from your Spring Boot app in the Logs tab.
  2. Metrics in Prometheus:

    • Access Prometheus at http://localhost:9090.
    • Verify metrics scraping by querying http_requests_total and others.
  3. Traces in Jaeger:

    • Access Jaeger at http://localhost:16686.
    • Verify traces and transactions for requests made to your Spring Boot app.

8. Azure Monitor Integration

  1. Enable Azure Monitor on AKS:

    az aks enable-addons --resource-group myResourceGroup --name myAKSCluster --addons monitoring
    
  2. View Logs and Metrics in Azure Portal:

    • Go to Azure MonitorLogs and Metrics to see the real-time data from your Spring Boot app.

9. Conclusion

You have now set up full observability for your Spring Boot App on Azure Kubernetes Service (AKS) using Prometheus (Metrics), Loki (Logging), Jaeger (Tracing), and Azure Monitor.

This architecture allows you to easily monitor, trace, and log your microservices, ensuring better performance, troubleshooting, and scalability in the cloud.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete