Wednesday, November 1, 2023

Deploy backend API Microservice to Azure Container Apps

 Ref: https://bitoftech.net/2022/08/25/deploy-microservice-application-azure-container-apps/

This articles gives co plete steps to deploy basic web api to Azure using Azure container apps and settting access to api either internal or external in docker config. 

Other taks like creating UI as micro site and communication between components has been eplained very good and links given to github and other components .  Articles gives complete picture of tasks to develop an basic microservice using azure container and dockers.

In this post, we will start by creating the solution skeleton by adding our first microservice named “Web API – Backend” as illustrated in the architecture diagram, then start provisioning Azure resources needed to deploy the service to Azure Container Apps using Azure CLI and Azure Portal.

As a prerequisite you need to have Visual Studio 2022 version 17.2 or higher, or VS Code to develop the solution, for my case I’ve started the Project using Visual Studio 2022 and then switched to VS code as debugging Dapr enabled applications and the extensions available in VS code makes developing the solution in VS code much easier. A recommendation is to use VS Code to build the solution, yet you can use Visual Studio 2022 and VS Code for local debugging.

The source code for this tutorial is available on GitHub. You can check the demo application too.

Setting up the Web API Backend Project

Step 1: Create the Web API Project

Create an empty solution and name it “TasksTracker.ContainerApps” then add a new ASP.NET Core Web API project named “TasksTracker.TasksManager.Backend.Api”, Configuration will be as the image below, check “Enable Docker” as we are going to containerize the application and deploy it to ACR, make sure “Linux” is selected for the Docker OS setting.
NetCore6WebApi

Step 2: Add Models (DTO)

Add a new folder named “Models” and create a new file named “TaskModel.cs” and paste the code below, those are the DTOs that will be used across the projects.

Step 3: Add In-memory Tasks Manager Service

We will work initially with data in memory to keep things simple with very limited dependency on any other components or data store and focus on the deployment of the backend API to Azure Container Apps, then we will switch this implementation with a concrete one where we are going to store data in Redis and Azure Cosmos DB using Dapr State Store building block, so to add the Fake Tasks Manager service (In-memory), create new folder named “Services” and add a new file named “ITasksManager.cs”, this will be the interface of Tasks Manager service. Paste the code below:

Now add a new file named “FakeTasksManager.cs” and paste the code below:

The code above is self-explanatory, it generates 10 tasks and stores them in a list in memory, as well it has some operations to add/remove/update those tasks.

Step 4: Register FakeTasksManager on project startup

Open file “Program.cs” and register the newly created service by adding the line highlighted below

Step 5: Create API endpoints to manage tasks

Now we will add a new controller named “TasksController.cs” under the folder “Controllers” which will contain simple action methods to enable basic CRUD operations on tasks.

Step 6: Add Dockerfile to the Web API project

In case you didn’t check the “Enable Docker” checkbox when creating the project we need to add a Dockerfile to the Web API project, so ensure you are on the root of the project and add file named “Docekrfile” and paste the code below:

Notes:

  • If you are using Visual Studio 2022, you can right-click on the Web API project and then select “Add->Docker Support”, it will generate the same file for you.
  • If you want to run the project locally on Docker, you need to install Docker Desktop, it is straight forward installation and can be installed from here. My recommendation is to have Docker Desktop installed as we are going to need it when introducing Dapr state Management API.

Now you can run your application locally to test, try to issue GET request to the endpoint https://localhost:7088/api/tasks?createdby=tjoudeh@bitoftech.net  and you should receive an array of tasks similar to the below:

Deploy the Web API Backend Project to Azure Container Apps

There are several ways to deploy the Web API to Azure Container Apps, this is a good link to view different ways, for this tutorial we will use Azure CLI/PowerShell.

Step 1: Install/Upgrade Azure Container Apps Extension

Open the PowerShell console on your machine, and sign in to Azure from the CLI. Run the below command, and follow the prompts to complete the authentication process.

If you have multiple Azure subscriptions under the account you authenticated with you need to select the subscription you want to provision resources under

Ensure you’re running the latest version of the CLI via the upgrade command.

Now install or update the Azure Container Apps extension for the CLI.

Register the “Microsoft.App” and “Microsoft.OperationalInsights” namespaces if you haven’t already registered them in your Azure subscription.

Step 2: Provision Azure Resources

Now we will be defining some variables in the PowerShell console to use them across the series of posts, you should change the values of those variables to be able to create the resources successfully, some of those variables should be unique across all Azure subscriptions such as Azure Container Registry name.

Now we will create a resource group to organize the services related to our application

Next, we will create an Azure Container Registry (ACR) instance in the resource group to store images of all Microservices we are going to build during this tutorial, make sure that you set the admin-enabled flag to true in order to seamlessly authenticate Azure container app when trying to create the container app using the image stored in ACR

Now we will build the Web API project on ACR and push the docker image to ACR. Use the below command to initiate the image build and push process using ACR. The “.” at the end of the command represents the docker build context, in our case, we need to be on the parent directory which hosts the .csproject.

Once this step is completed you can verify the results by going to the Azure portal and check that a new repository named “tasksmanager/tasksmanager-backend-api” has been created and there is a new docker image with a “latest” tag is created, it should look like the below

Azure Container Registry

Step 3: Provision Azure Container Env and Container App

Next is to create Azure Container Apps Environment, as shared previously, it acts as a secure boundary around a group of all container apps we are going to provision during this tutorial, to create it, run the below command:

Note: If you are using the CLI command to create Container Apps Environment, it is better to set the property “–dapr-instrumentation-key” when creating the Environment, so you need to provision Application Insights resource to get its instrumentation key, more details on this post.

The last step here is to create and deploy the Web API to Azure Container App following the below command:

Some notes on the above command:

  • Ingress param is set to “external” which means that this container app (Web API) project will be accessible from the public internet. When Ingress is set to “Internal” or “External” it will be assigned a fully qualified domain name (FQDN). Important notes about IP addresses and domain names can be found here.
  • The target port param is set to 80, this is the port our Web API container listens to for incoming requests.
  • We didn’t specify the ACR registry username and password, “az containerapp create” command was able to look up ACR username and password and add them as a secret under the created Azure container app for future container updates.
  • The minimum and the maximum number of replicas are set, more about this when we talk about Autoscaling. In the meantime, only 1 single instance of this container app will be provisioned as Auto scale is not configured.
  • We are setting the size of the Container App, the total amount of CPUs and memory requested for the container app must add up to certain combinations, for full details check the link here
  • The “query” property will filter the response coming from the command and just return the FQDN.

For full details on all available parameters for this command, please visit this page.

Step 4: Verify the deployment of the Azure Container App.

Now we can navigate to the Azure Portal and select the resource group named “tasks-tracker-rg” created, you should see the 4 recourses created below. By default when you create an Azure Container Environment, a Log Analytics workspace will be created, we will cover this in the Monitoring and observability post.

Now copy the FQDN (Application URL) of the Azure container app named “tasksmanager-backend-api” and issue GET request similar to this one: https://tasksmanager-backend-api.agreeablestone-8c14c04c.eastus.azurecontainerapps.io/api/tasks/?createdby=tjoudeh@bitoftech.net
And you should receive an array of the 10 tasks similar to the below image


In the next post, we will see how we will add a new Frontend Web App as a microservice and how it will communicate with the backend API.

Tuesday, October 31, 2023

Microservices in Azure: Revolutionizing Modern Application Development

 

Introduction:

In the era of digital transformation, organizations are embracing cloud computing and innovative architectural patterns to build scalable, resilient, and highly adaptable applications. Microservices architecture has emerged as a powerful approach to design and develop complex applications that can evolve independently. Microsoft Azure, one of the leading cloud platforms, provides a robust set of tools and services to support the development, deployment, and management of microservices-based applications. In this article, we will explore the concept of microservices, delve into the benefits it offers, and discuss how Azure empowers developers and businesses to harness the true potential of this architectural paradigm.

  1. Understanding Microservices Architecture:

Microservices architecture breaks down an application into smaller, loosely coupled services that communicate with each other through APIs. This section will explain the fundamental principles of microservices architecture, such as single responsibility, bounded context, and autonomous deployment. It will also highlight the advantages of microservices, including scalability, fault isolation, independent deployment, and improved development speed. Furthermore, it will address the challenges and considerations associated with microservices, such as service coordination, data management, and distributed system complexity.

2. Azure as a Microservices Platform:

Azure provides a range of services that facilitate the development and management of microservices-based applications. This section will focus on key Azure services and their capabilities for microservices architecture.

2.1 Azure Service Fabric:

Azure Service Fabric is a distributed systems platform that enables developers to build highly scalable and reliable microservices applications. It provides features such as service discovery, automatic scaling, load balancing, and monitoring. This subsection will delve into the overview and features of Azure Service Fabric, including Reliable Services and Reliable Actors, as well as scaling and load balancing mechanisms.

2.2 Azure Kubernetes Service (AKS):

Azure Kubernetes Service (AKS) simplifies the deployment, scaling, and management of containerized applications. This subsection will introduce Kubernetes, the underlying technology, and explain how AKS can be leveraged for microservices deployment. Key features such as automatic scaling, self-healing, and integration with Azure DevOps will be discussed.

2.3 Azure Functions:

Azure Functions provides a serverless computing environment that enables the development of event-driven microservices. This subsection will cover the concept of serverless computing, explain the benefits of Azure Functions, and discuss how it can be used for building scalable and event-triggered microservices. Integration with other Azure services, such as Event Grid and Service Bus, will also be explored.

2.4 Azure API Management:

Azure API Management serves as an API gateway for managing and securing microservices APIs. This subsection will discuss the role of an API gateway in a microservices architecture, explain the features and benefits of Azure API Management, and highlight its capabilities for security, governance, and analytics.


3. Developing Microservices in Azure:

This section will provide guidance on developing microservices in Azure, covering important aspects of the development process.

3.1 Choosing the Right Technology Stack:

Selecting the appropriate technology stack is crucial for implementing microservices. This subsection will explore various technology options available in Azure, such as ASP.NET Core, Node.js, and Java, and discuss factors to consider when choosing the technology stack.

3.2 Designing Microservices for Azure:

Designing microservices in Azure involves considering factors like service decomposition, bounded contexts, and domain-driven design principles. This subsection will delve into best practices for designing microservices in Azure and highlight how Azure-specific services, such as Service Bus and Event Grid, can be utilized for inter-service communication.

3.3 Implementing Communication Patterns:

Effective communication between microservices is vital. This subsection will explore different communication patterns, such as synchronous HTTP-based communication, asynchronous messaging, and event-driven architectures. It will also discuss the use of Azure Service Bus, Event Grid, and Azure Relay for implementing these patterns.

3.4 Managing Data in Microservices:

Managing data in microservices architecture presents unique challenges. This subsection will discuss various data management strategies, including the use of different types of databases (SQL, NoSQL), event sourcing, and CQRS (Command Query Responsibility Segregation). Azure-specific services like Azure Cosmos DB and Azure SQL Database will be explored for data persistence.

3.5 Testing and Continuous Integration/Deployment:

Testing and continuous integration/deployment (CI/CD) are essential for the success of microservices-based applications. This subsection will highlight the importance of automated testing and CI/CD pipelines in Azure. It will also discuss Azure DevOps and other tools that facilitate automated testing, CI/CD, and deployment strategies for microservices in Azure.

  1. Securing and Monitoring Microservices in Azure:
  2. Securing and monitoring microservices is crucial to ensure application reliability and data protection. This section will cover key aspects of security and monitoring in Azure microservices.

4.1 Security Considerations:

Microservices architecture presents unique security challenges. This subsection will discuss security considerations specific to microservices, including authentication, authorization, secure communication, and secrets management. It will explore how Azure Key Vault and Azure Active Directory (AAD) can be utilized for secure identity and access management.

4.2 Logging, Monitoring, and Telemetry:

Effective monitoring and gathering of telemetry data are essential for microservices-based applications. This subsection will discuss best practices for logging, monitoring, and gathering telemetry from microservices in Azure. It will cover Azure Application Insights, Azure Monitor, and Azure Log Analytics as key tools for monitoring and troubleshooting microservices.

Conclusion:

Microservices architecture, with its ability to enable scalability, independent deployment, and rapid innovation, has revolutionized modern application development. Microsoft Azure provides a robust platform and an extensive suite of services that empower developers and organizations to leverage the true potential of microservices.

By adopting microservices architecture in Azure, businesses can achieve greater flexibility, agility, and scalability in their applications. Azure Service Fabric offers a distributed systems platform that simplifies the development and management of microservices, providing features such as service discovery, automatic scaling, and load balancing. Azure Kubernetes Service (AKS) enables seamless container orchestration, allowing for easy deployment and management of microservices at scale. Azure Functions provides a serverless environment for developing event-driven microservices, while Azure API Management ensures secure and efficient management of microservices APIs.

When developing microservices in Azure, it is crucial to choose the right technology stack, considering factors such as programming language preference, scalability requirements, and integration capabilities. Designing microservices with Azure involves following best practices, including service decomposition, bounded contexts, and domain-driven design principles. Effective communication patterns and data management strategies, coupled with automated testing and CI/CD pipelines, ensure the reliability and efficiency of microservices-based applications.

Security and monitoring are paramount in microservices architecture, and Azure provides robust solutions for securing microservices and monitoring their performance. Azure's security offerings, such as Azure Key Vault and Azure Active Directory, help protect sensitive data and manage access control. Logging, monitoring, and telemetry tools like Azure Application Insights and Azure Monitor provide comprehensive insights into the health and performance of microservices, enabling proactive troubleshooting and optimization.

The future of microservices in Azure looks promising, as the platform continues to evolve and introduce new services and features to support modern application development. By embracing microservices architecture in Azure, businesses can drive digital transformation, accelerate innovation, and deliver scalable and resilient applications that meet the demands of the rapidly evolving market.

In conclusion, microservices architecture in Azure empowers organizations to build flexible, scalable, and resilient applications. By leveraging the extensive range of Azure services, businesses can unlock the true potential of microservices, driving their success in the era of digital transformation.

Performance optimization techniques in ReactJS

Summary: Helps to learn how to measure performance improvements. As the majority of modern web applications rely on what React.js brings to ...