© 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_6

6. Running APIs on Azure Container Apps

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

Azure Container Apps helps you to run containerized applications and microservices with ease in an underlying Kubernetes environment. You only need to focus on building, containerizing, and deploying the application to the Azure Container Apps Environment. All the complexities involved with the Kubernetes cluster are abstracted from you. You need not worry about building a Kubernetes deployment manifest file and running kubectl or helm commands to deploy the manifest file on the underlying Kubernetes platform.

In this chapter, you will learn how to deploy the Math microservices application to Azure Container Apps. We will also explore the basics of Azure Container Apps.

In this chapter, we will explore the following topics related to Azure Container Apps:
  • Introduction to Azure Container Apps

  • Create an Azure Container Apps Environment with the Add API and Subtract API

  • Modify the Math API and push it to Azure Container Registry

  • Deploy the Math API to the Azure Container Apps Environment

After completing this chapter, you should understand the fundamentals of Azure Container Apps and be able to run .NET-based microservices on Azure Container Apps.

Introduction to Azure Container Apps

Azure Container Apps is a serverless container offering on Azure that is built on top of Kubernetes. It abstracts all complexities involved in deploying and managing the Kubernetes cluster, enabling you to focus on building your application and containerizing it. You can deploy your application containers with ease without needing to write any Kubernetes deployment manifest files. However, you do not have any control over the underlying Kubernetes cluster and there is no mechanism that will help you in running kubectl commands to interact with the Kubernetes cluster.

Because Azure Container Apps is a serverless offering, it can scale based on the incoming traffic. It supports event-driven scaling using Kubernetes Event-Driven Autoscaling (KEDA). For example, Azure Container Apps can scale when the number of items in the service bus queue increases. When the number of queue item increases, it fires an event that drives autoscaling for Azure Container Apps. Whenever there is no workload, you do not get charged. For example, if there are no items in the Service Bus queue to be processed, no container pod will be created and you will not get charged. Azure Container Apps can scale based on any of the following. In contrast, Azure Kubernetes Service can scale based on CPU or memory usage only.
  • CPU or memory usage

  • HTTP requests

  • Events like items in Azure Service Bus Queue

  • KEDA-supported events

Multiple Azure container apps run inside an Azure Container Apps Environment. Each of the container apps runs multiple containers in a replica, as shown in Figure 6-1. It can also execute multiple versions or revisions of containers, and you can define the traffic percentage that will hit each of the container versions.
Figure 6-1

Azure Container Apps

You can configure Ingress for the application running inside Container Apps and expose the access to the Internet. You may choose to restrict access to the container to the other container apps running inside the Azure Container Apps Environment. Figure 6-2 demonstrates an Azure Container Apps Environment hosting three Azure container apps. The container app running the Math API is exposed outside the Container Apps Environment for consumption. The Add API container app and Subtract API container app are accessible within the Container Apps Environment. The Math API container app can access the Add API container app and Subtract API container app.
Figure 6-2

Math Microservice on Azure Container Apps

Note

If you need greater control over the Kubernetes cluster, then you should use Azure Kubernetes Service. Azure Container Apps abstracts the underlying Kubernetes platform and helps you deploy and manage containers with ease.

Create Azure Container Apps Environment with Add API and Subtract API

Let’s create an Azure Container Apps Environment with a container app for the Add API and a container app for the Subtract API. We need to limit access to the container apps inside the Container Apps Environment and they should not be exposed to the Internet. Go to the Azure portal and click Create a resource as shown in Figure 6-3.
Figure 6-3

Click Create a resource

Click Containers and then click Container App as shown in Figure 6-4. You get all container offerings on Azure in the Containers category.
Figure 6-4

Click Container App

In the respective fields shown in Figure 6-5, provide the subscription details, resource group, container app name, and the location where you want to create the container app. We need to create the container app inside a Container Apps Environment, so click Create new as shown in Figure 6-5 to create a Container Apps Environment.
Figure 6-5

Provide basic details

Provide the container environment name and click Create as shown in Figure 6-6.
Figure 6-6

Click Create

We need to select the container image that we have in the Azure container registry. We have containerized the Add API and the Subtract API in Chapter 4 and have pushed them to Azure Container Registry. Click Next : App settings as shown in Figure 6-7 to select the container image.
Figure 6-7

Click Next : App settings

Select the Container Registry, container image for the Add API, and the container as shown in Figure 6-8.
Figure 6-8

Provide image details

Enable HTTP Ingress and limit it to the Container Apps Environment as shown in Figure 6-9. Click Review + create.
Figure 6-9

Click Review + create

Click Create as shown in Figure 6-10 to spin up the Container Apps Environment along with the Add API container app.
Figure 6-10

Click Create

Once the container app gets created, navigate to the container app in the portal. Go to the Overview tab as shown in Figure 6-11.
Figure 6-11

Click Overview

Copy the Application URL as shown in Figure 6-12. We will use it in the Math API code to access the Add API service running inside the container app. Click the Container Apps Environment for the container app as shown in Figure 6-12.
Figure 6-12

Copy Application URL

In the Container Apps Environment, click Apps and then click Create as shown in Figure 6-13. We need to create another container app for the Subtract API in that environment.
Figure 6-13

Click Apps

Provide the subscription details, resource group, container app name, and the location where you want to create the container app, as shown in Figure 6-14. Select the Container Apps Environment. We need to select the Subtract API container image that we have in the Azure container registry. Click Next : App Settings as shown in Figure 6-14 to select the container image.
Figure 6-14

Click Next : App settings

Select the Container Registry, container image for the Subtract API, and the container as shown in Figure 6-15.
Figure 6-15

Provide image details

Enable HTTP Ingress and limit it to the Container Apps Environment as shown in Figure 6-16. Click Review + create.
Figure 6-16

Click Review + create

Click Create to spin up the Subtract API container app inside the Container Apps Environment. Once the container app gets created, go to the Overview tab and copy the Application URL as shown in Figure 6-17. We will use this URL in the Math API code to invoke the Subtract API.
Figure 6-17

Copy Application URL

Modify Math API and Push It to Azure Container Registry

Now let’s modify the Math API and add the Container App application URL for the Add API and Subtract API as shown in Listing 6-1.
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
    {
        [HttpGet("Test")]
        public string Test()
        {
            return "Success";
        }
        [HttpGet("Get")]
        public string Get(string ops, int a,int b)
        {
            string result = "";
            string url = "";
            string queryStr = "?a=" + a + "&b=" + b;
            if (ops == "add")
            {
                url = "[Add API Container App URL]/Add" + queryStr;
            }
            else
            {
                url = "[Subtract API Container App URL]/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 6-1

MathController.cs

Containerize the application and push it to Azure Container Registry as demonstrated in Chapter 4.

Deploy Math API to Azure Container Apps Environment

Now let’s the Math API container image in the Azure Container App running in the Container Apps Environment. Go to the Overview section of one of the container apps you created earlier and click the Container Apps Environment. In the Container Apps Environment, click the Apps section and then click Create as shown in Figure 6-18.
Figure 6-18

Click Apps

Provide the subscription details, resource group, container app name, and the location where you want to create the container app, as shown in Figure 6-19. Select the Container Apps Environment. We need to select the Math API container image that we have in the Azure container registry. Click Next : App Settings as shown in Figure 6-19 to select the container image.
Figure 6-19

Click Next : App settings

Select the Container Registry, container image for the Subtract API, and the container as shown in Figure 6-20.
Figure 6-20

Provide image details

Enable HTTP Ingress and expose it outside the Container Apps Environment as shown in Figure 6-21 so that it can be accessed over the Internet. Click Review + create.
Figure 6-21

Click Review + create

Click Create as shown in Figure 6-22 to spin up the container app.
Figure 6-22

Click Create

Once the container app gets created, go to the Overview tab and copy the Application URL as shown in Figure 6-23. We will use this URL to invoke the Math API in Postman.
Figure 6-23

Copy Application URL

Go to Postman and invoke the URL for the Math API as shown in Listing 6-2.
https://[Math Container App URL]/Math/Get?ops=add&a=4&b=6
Listing 6-2

Math Container App URL

Figure 6-24 shows the result in Postman.
Figure 6-24

Postman results

Summary

In this chapter, you learned the basic concepts of Azure Container Apps. We created the Add container app for the Add API and the Subtract container app for the Subtract API inside the Container Apps Environment. We then modified the Math API service to invoke the container app URLs for the Add API and Subtract API. We containerized the Math API and pushed it to Azure Container Registry and then hosted it inside the Math container app.

In the next chapter, you will learn how to implement logging and monitoring for the microservices running inside AKS.

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

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