Blog Post

IIS Support Blog
5 MIN READ

Hosting Azure Bot SDK C# on Azure Kubernetes Service (AKS)

meenakshiBalekar's avatar
Jan 20, 2025

Azure Bot Service (ABS) is a comprehensive offering from Microsoft that provides tools to build, test, and deploy intelligent bots. Pairing it with Azure Kubernetes Service (AKS) allows for scalable and efficient management of bot applications. This blog will guide you through the prerequisites, detailed steps to create and deploy your bot on AKS, and best practices for maintaining and troubleshooting your bot service.

Prerequisites

Before diving into the deployment process, ensure you have the following:

  • Azure Subscription
  • Basic knowledge of C# and .NET
  • Azure CLI installed
  • Docker installed
  • Kubectl installed
  • An existing Azure Bot Service

 

Step-by-Step Guide

Creating the Azure Bot

Create a new Azure Bot Service:

  • Navigate to the Azure Portal.
  • Click on "Create a resource" and select "AI + Machine Learning".
  • Choose "Azure Bot" and fill in the required fields such as resource group, bot handle, and region.
  • Select the SDK language as C# and choose the Echo Bot template.
  • Click "Create" to provision the bot service.

 

Download the Bot Code:

  • Once the bot service is created, navigate to the resource and the required details :

    Create Azure bot

  • Extract the downloaded code to your local development environment, for this sample I have made use of Welcome bot you can choose as per your requirement 

    Azure Bot Sample

 

Creating Azure Kubernetes Service (AKS)


Provision AKS

  • In the Azure Portal, click on "Create a resource" and select "Containers".

       Choose "Azure Kubernetes Service" and fill in the details such as resource group, cluster               name, and region.

  • Configure the node size and count as per your requirements and click "Review + create".
  • Once validated, click "Create" to provision the AKS cluster.

 

Configure Kubectl

  • Open a terminal and run the following command to configure `kubectl` to work with your AKS cluster

    az aks get-credentials --resource-group --name

 

Building the Docker Image


Create a Dockerfile

In the root of your bot project directory, create a file named `Dockerfile` with the following content:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src COPY ["YourBotProject.csproj", "./"] RUN dotnet restore "./YourBotProject.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "YourBotProject.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "YourBotProject.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "YourBotProject.dll"]

 

Build the Docker Image

  • Open a terminal and navigate to your project directory.

  • Run the following command to build the Docker image:

 

docker build -t yourbotimage:latest .

 

Push the Docker Image to Azure Container Registry (ACR)

  • Create an ACR if you don’t have one

az acr create --resource-group --name --sku Basic
  • Log in to the ACR:

az acr login --name
  • Tag your Docker image:

docker tag yourbotimage:latest .azurecr.io/yourbotimage:latest
  • Push the image to ACR

docker push .azurecr.io/yourbotimage:latest

Linking ABS and AKS

Create a Deployment YAML File

Create a file named `deployment.yaml` with the following content

apiVersion: apps/v1 kind: Deployment metadata: name: yourbot-deployment spec: replicas: 2 selector: matchLabels: app: yourbot template: metadata: labels: app: yourbot spec: containers: - name: yourbot image: .azurecr.io/yourbotimage:latest ports: - containerPort: 80

 

Deploy to AKS

Run the following command to deploy the bot to your AKS cluster:

kubectl apply -f deployment.yaml

 

Expose the Bot Service

Create a service YAML file named `service.yaml`

apiVersion: v1 kind: Service metadata: name: yourbot-service spec: type: LoadBalancer selector: app: yourbot ports: - protocol: TCP port: 80 targetPort: 80


While creating an Azure Bot and hosting it on Azure Kubernetes Service (AKS), the process involves containerizing your bot and deploying it within Kubernetes clusters. This step is a critical part of making your bot accessible to external clients, like users interacting with it through channels like Microsoft Teams, Slack, or a website.

In Kubernetes, containers (which run your bot) are typically isolated within pods. To allow external traffic (such as requests from users via a bot channel) to reach the bot's container, you need to expose the bot to the outside world. This is done through the creation of a Service resource in Kubernetes.

  • The Service provides a stable endpoint (an IP address or DNS name) to route external traffic to the internal pods running your bot's containerized application.
  • Kubernetes services are used to load-balance traffic to multiple pods if necessary, ensuring that requests are distributed evenly.

The service.yaml file also defines how traffic is routed to the correct set of containers (pods). Kubernetes manages multiple pods that may contain instances of your bot, and the service ensures that requests from external sources are correctly forwarded to one of the bot's running pods.

Creating and exposing the service with a YAML file is essential for routing traffic to your bot in a Kubernetes environment. Without this step, your bot would not be accessible to external users, and it would not be able to interact with channels like Microsoft Teams, Slack, or websites. It effectively sets up the networking and load-balancing layer to ensure that your bot is available and scalable when deployed on Azure Kubernetes Service.

Deploy the service:

kubectl apply -f service.yaml

 

Publishing the SDK Code on AKS

 

Once your service is running, you need to update the Azure Bot Service configuration to point to your AKS service:


Get the External IP

Run the following command to get the external IP of your service:

kubectl get svc yourbot-service

 

Update Bot Configuration in Azure

  • Navigate to your bot service in the Azure Portal.

  • Click on "Settings" and update the messaging endpoint to `http:///api/messages`.

  • Click "Save" to apply the changes.

 

Maintaining Connectivity

 

Ensure your bot remains connected by:

  • Regularly monitoring the health of your AKS cluster.
  • Setting up auto-scaling policies.
  • Implementing retry logic in your bot code to handle transient failures.

 

Testing Your Bot

Using the Bot Framework Emulator

  • Download and install the Bot Framework Emulator from the official website.
  • Open the emulator and enter the messaging endpoint of your bot.
  • Test various scenarios to ensure your bot responds correctly.

 

End-to-End Testing

 

  • Deploy your bot to various channels like Microsoft Teams, Slack, etc.
  • Test the bot's functionality in a real-world environment to ensure it performs as expected.

 

Conclusion

Deploying an Azure Bot SDK C# application on AKS involves several steps, from creating the bot and AKS cluster to building Docker images and maintaining connectivity. By following this guide, you can ensure a smooth deployment process and maintain a robust and scalable bot service.

 

Regular monitoring and testing are essential to ensure the bot performs optimally and can handle various scenarios and loads. 

 

Happy bot building!

Updated Jan 15, 2025
Version 1.0
No CommentsBe the first to comment