© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
A. Satapathi, A. MishraDeveloping Cloud-Native Solutions with Microsoft Azure and .NET https://doi.org/10.1007/978-1-4842-9004-0_7

7. Implement Logging and Monitoring for Microservices Running on AKS

Ashirwad Satapathi1   and Abhishek Mishra2
(1)
Gajapati, Odisha, India
(2)
Navi MUmbai, India
 

Microservices running on Azure Kubernetes Service can have performance issues and failures. Some of the services may fail at runtime in the production environment, in which case you need to analyze the application logs to debug the failures. The services can get sluggish, in which case you may have to look at the performance metrics to figure out the performance issues. You may have to send an alert using an email or log a ticket in an ITSM tool whenever there is a failure or any anomalies in the services running in the Kubernetes cluster. You can use Azure Monitor and Application Insights to capture logs and metrics for the services running in the AKS cluster.

In this chapter we will configure Azure Monitor and Application Insights for the Math microservices application running in the AKS cluster.

Structure

In this chapter, we will explore the following topics related to monitoring and debugging services running in an AKS cluster:
  • Introduction to Azure Monitor and Application Insights

  • Create Application Insights

  • Configure logging for the Math microservices application

  • Create a logging-enabled AKS cluster

  • Monitor metrics and logs for the microservices application

Objectives

After studying this chapter, you should be able to
  • Understand the fundamentals of Application Monitor, Application Insights, and Log Analytics Workspace

  • Configure logging and monitoring for .NET-based microservices

Introduction to Azure Monitor and Application Insights

You can ingest performance metrics and logs for your applications and infrastructure to Azure Monitor. Azure Monitor collects the metrics and logs from applications and infrastructure on Azure, on-premises, or on any other cloud. As depicted in Figure 7-1, Azure Monitor can ingest logs from a variety of data sources, including Azure services, Azure subscriptions, operating systems, Azure tenants, and many more sources. Azure Monitor enables you to do the following:
  • Collect application logs and metrics using Application Insights

  • Collect AKS container logs and metrics using Container Insights

  • Collect logs and metrics for virtual machines using Virtual Machine Insights

  • Collect insights for network and storage

The ingested logs can be analyzed using Log Analytics, which collects the logs and metrics. You can run Kusto Query Language (KQL) queries to work with the ingested data in Log Analytics and derive insights from it. You can build dashboards to represent the ingested metrics and logs visually and create alerts on anomalies detected from the logs and metrics.
Figure 7-1

Azure Monitor

You can collect application logs and metrics using Application Insights. An application can have user interface, backend business and data services, and many other components, as depicted in Figure 7-2. You can enable Application Insights to capture logs from all the application components. You can capture logs from on-premises applications and applications running on Azure and other clouds. Application Insights supports log ingestion from a wide range of programming languages and frameworks like .NET, Java, PHP, Python, and many more.
Figure 7-2

Azure Application Insights

Create Application Insights

Let’s create an Application Insights where the APIs in the Math solution can ingest the logs into the Application Insights. Go to the Azure portal and click Create a resource as shown in Figure 7-3.
Figure 7-3

Create a resource

You will be navigated to the Azure Marketplace. Search for Application Insights as shown in Figure 7-4.
Figure 7-4

Search for Application Insights

Click Create as shown in Figure 7-5. This will navigate you to the screen where you can provide details for the Application Insights.
Figure 7-5

Click Create

Provide the basic details like subscription, resource group, and application insights name as shown in Figure 7-6. Choose the Resource Mode as Workspace-based. This will ensure that you redirect the logs to a Log Analytics Workspace. You can choose an existing workspace or create a new one. Click Review + create.
Figure 7-6

Provide basic details

Click Create as shown in Figure 7-7. This will create an Application Insights for you.
Figure 7-7

Click Create

Configure Logging for the Math Microservices Application

Open the Math microservices application in Visual Studio. We will enable logging for the Add API, Subtract API, and Math API services. Let’s enable Application Insights for the Add API first. Right-click the AddAPI project and click Configure Application Insights as shown in Figure 7-8.
Figure 7-8

Click Configure Application Insights

Select Azure Application Insights and then click Next as shown in Figure 7-9.
Figure 7-9

Select Azure Application Insights

Select the Application Insights we created earlier and then click Next as shown in Figure 7-10.
Figure 7-10

Select Application Insights created earlier

Click Finish as shown in Figure 7-11 to configure Application Insights for the Add API.
Figure 7-11

Click Finish

Click Close as shown in Figure 7-12 once the configuration completes successfully.
Figure 7-12

Click Close

The package Microsoft.ApplicationInsights.AspNetCore gets added to the project. The connection string for the Application Insights gets added to the appsettings.json file as shown in Listing 7-1.
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=b6591e30-e2a9-44cc-a541-b1f87fb65f94;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/"
  }
}
Listing 7-1

appsettings.json

Application Insights service gets added to the services collection in Program.cs as shown in Listing 7-2.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPINSIGHTS_CONNECTIONSTRING"]);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Listing 7-2

Program.cs

We can add the code to ingest logs into the Application Insights for the Add Controller as shown in Listing 7-3.
using Microsoft.AspNetCore.Mvc;
namespace AddAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AddController : Controller
    {
        private readonly ILogger<AddController> _logger;
        public AddController(ILogger<AddController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public int Add(int a, int b)
        {
            _logger.LogInformation("Adding Two Numbers "+ a + " and "+b);
            return a + b;
        }
    }
}
Listing 7-3

AddController.cs

Containerize the Add API and push it to the Azure container registry using the steps illustrated in the earlier chapters.

Now we’ll enable Application Insights for the Subtract API. Right-click the SubtractAPI project and click Configure Application Insights as shown in Figure 7-13.
Figure 7-13

Configure Application Insights

Select Azure Application Insights and then click Next as shown in Figure 7-14.
Figure 7-14

Select Azure Application Insights

Select the Application Insights we created earlier and then click Next as shown in Figure 7-15.
Figure 7-15

Select Application Insights created earlier

Click Finish as shown in Figure 7-16 to configure Application Insights for the Subtract API.
Figure 7-16

Click Finish

Click Close as shown in Figure 7-17 once the configuration completes successfully.
Figure 7-17

Click Close

We can add the code to ingest logs into the Application Insights for the Subtract Controller as shown in Listing 7-4.
using Microsoft.AspNetCore.Mvc;
namespace SubtractAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SubtractController : Controller
    {
        private readonly ILogger<SubtractController> _logger;
        public SubtractController(ILogger<SubtractController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public int Add(int a, int b)
        {
            _logger.LogInformation("Subtracting Two Numbers " + a + " and " + b);
            return a - b;
        }
    }
}
Listing 7-4

SubtractController.cs

Containerize the Subtract API and push it to the Azure container registry using the steps illustrated in the earlier chapters.

Finally, we’ll enable Application Insights for the Math API. Right-click the MathAPI project and click Configure Application Insights as shown in Figure 7-18.
Figure 7-18

Configure Application Insights

Select Azure Application Insights and then click Next as shown in Figure 7-19.
Figure 7-19

Select Azure Application Insights

Select the Application Insights we created earlier and then click Next as shown in Figure 7-20.
Figure 7-20

Click Application Insights created earlier

Click Finish as shown in Figure 7-21 to configure Application Insights for the Math API.
Figure 7-21

Click Finish

Click Close as shown in Figure 7-22 once the configuration completes successfully.
Figure 7-22

Click Close

We can add the code to ingest logs into the Application Insights for the Math Controller as shown in Listing 7-5. Also make sure that you provide the Cluster IP address for the Add API and Subtract API in the Math Controller code.
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
namespace MathAPI.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class MathController : Controller
    {
        private readonly ILogger<MathController> _logger;
        public MathController(ILogger<MathController> logger)
        {
            _logger = logger;
        }
        [HttpGet("Test")]
        public string Test()
        {
            return "Success";
        }
        [HttpGet("Get")]
        public string Get(string ops, int a,int b)
        {
            _logger.LogInformation("Operation "+ops+" selected for numbers "+a+" and "+ b);
            string result = "";
            string url = "";
            string queryStr = "?a=" + a + "&b=" + b;
            if (ops == "add")
            {
                url = "http://[Cluster IP]/Add" + queryStr;
            }
            else
            {
                url = "http://[Cluster IP]/subtract" + queryStr; ;
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                result = reader.ReadToEnd();
            }
            return result;
        }
    }
}
Listing 7-5

MathController.cs

Containerize the Math API and push it to the Azure container registry using the steps illustrated in the earlier chapters.

Create a Logging-Enabled AKS Cluster

Now let’s create a logging-enabled AKS cluster. We will deploy the Math API microservices application to this cluster. Go to the Azure portal and click Create a resource as shown in Figure 7-23.
Figure 7-23

Create a resource

You will be navigated to the Azure Marketplace. Click Containers and then click Kubernetes Service as shown in Figure 7-24.
Figure 7-24

Click Containers

Provide the basic details like resource group, subscription, region, and other necessary details for the Kubernetes cluster as shown in Figure 7-25.
Figure 7-25

Provide basic details

Go to the Integrations tab and select your Azure container registry as shown in Figure 7-26. Enable container monitoring and select a Log Analytics Workspace. You may use the same Log Analytics Workspace that you created for Application Insights. Click Review + create.
Figure 7-26

Click Review + create

Click Create as shown in Figure 7-27. The Kubernetes cluster will get created.
Figure 7-27

Click Create

Once the cluster gets created, start deploying the Add API and the Subtract API. Get their cluster IP address, modify the Math Controller code to use the Cluster IP address for the Add API and Subtract API, containerize the Math API, push it to the Azure container registry, and then deploy it on Azure Kubernetes Service. You can follow the steps illustrated in the previous chapters.

Monitor Metrics and Logs for the Microservices Application

Once the application is deployed, get the Load Balancer service external IP for the Math API as shown in Figure 7-28. Invoke the API using Postman as demonstrated in the previous chapters. This will generate logs and metrics for the application.
Figure 7-28

Get External IP

Go to the Application Insights for Azure Kubernetes Service and click Transactions search as shown in Figure 7-29. You can see the logs for the application here.
Figure 7-29

Click Transaction search

Click Logs to query the Log Analytics Workspace using Kusto Queries as shown in Figure 7-30.
Figure 7-30

Click Logs

Run the Kusto Query shown in Figure 7-31 to search for logs that have Operation in the log text. We have Operation in the Math API logs. Click Run.
Figure 7-31

Query Log Analytics Workspace

Run the Kusto Query shown in Figure 7-32 to search for logs that have Adding Two Numbers in the log text. We have this text in the Add API logs. Click Run.
Figure 7-32

Query Log Analytics Workspace

Now let’s investigate metrics for the cluster. Click Insights. In the Cluster tab you can see the compute metrics such as Node CPU Utilization %, as shown in Figure 7-33, and Node Memory Utilization %, as shown in Figure 7-34.
Figure 7-33

Node CPU Utilization % metrics

Figure 7-34

Node Memory Utilization % metrics

You can check for other compute metrics in the Cluster tab.

Click the Nodes tab to get insights like compute utilization for the nodes in the cluster, as shown in Figure 7-35. You can drill down and view other essentials like processes running in the nodes and many more.
Figure 7-35

Nodes tab of Application Insights

Click the Containers tab to get insights for the containers running in the nodes, as shown in Figure 7-36.
Figure 7-36

Containers tab of Application Insights

Summary

In this chapter, we explored the basic concepts of Application Insights and Azure Monitor. Then we created an Application Insight and modified the Math microservices application to ingest logs to the Application Insights. We then queried the logs and viewed the metrics in Application Insights.

In the next chapter, you will learn how to build an IoT-based solution using Azure and .NET.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset