DEV Community

Cover image for Deploying a Complete ASP.NET Framework Web Application on Amazon EKS
Danh Hoàng Hiếu Nghị
Danh Hoàng Hiếu Nghị

Posted on

Deploying a Complete ASP.NET Framework Web Application on Amazon EKS

Deploying a web application using containers can enhance scalability and manageability, especially in cloud environments. Amazon Elastic Kubernetes Service (EKS) allows you to run Kubernetes applications in a highly available and scalable manner. In this blog post, we will walk through the steps to deploy a traditional ASP.NET Framework web application on Amazon EKS.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting Up AWS EKS
  4. Containerizing the ASP.NET Framework Application
  5. Pushing the Docker Image to Amazon ECR
  6. Deploying the Application on EKS
  7. Configuring Database Access
  8. Testing the Application
  9. Monitoring and Maintenance
  10. Conclusion
  11. References

Introduction

ASP.NET Framework applications are traditionally hosted on Windows Servers, but by leveraging containers, we can run these applications in a Kubernetes environment. This guide will provide a step-by-step approach to deploying an ASP.NET Framework web application using Amazon EKS, making it easier to manage and scale.

Prerequisites

Before you start, make sure you have the following:

  • AWS Account: Sign up for an AWS account if you don’t have one.
  • AWS CLI: Install the AWS Command Line Interface on your local machine.
  • kubectl: Install kubectl to interact with your EKS cluster.
  • Docker: Install Docker to build and manage containers.
  • Familiarity with Kubernetes: Basic understanding of Kubernetes concepts will be helpful.

Setting Up AWS EKS

Creating an EKS Cluster

  1. Log in to the AWS Management Console: Go to the AWS Management Console.
  2. Navigate to EKS: Search for EKS in the services and select it.
  3. Create a New Cluster:
    • Click on Add cluster and select Create.
    • Provide a name for your cluster and select the appropriate Kubernetes version.
    • Configure the VPC settings. You can create a new VPC or select an existing one.
    • Review and create the cluster. This process can take several minutes.

Configuring kubectl

  1. Update kubeconfig: After the cluster is created, run the following command to update your kubeconfig file:
   aws eks --region <region> update-kubeconfig --name <cluster_name>
Enter fullscreen mode Exit fullscreen mode
  1. Verify the connection: Run the following command to verify that you can connect to the EKS cluster:
   kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Setting Up the Node Group

  1. Create a Node Group:
    • Go back to the EKS dashboard and select your cluster.
    • Under the Compute tab, click Add Node Group.
    • Configure the node group settings, including instance types and scaling options.
    • Review and create the node group. Wait for the nodes to become active.

Containerizing the ASP.NET Framework Application

Creating a Dockerfile

  1. Create a Dockerfile: In the root of your ASP.NET Framework project, create a file named Dockerfile with the following content:
   # Use the official ASP.NET Framework base image
   FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8

   # Set the working directory
   WORKDIR /inetpub/wwwroot

   # Copy the application files
   COPY ./bin/Release/ .

   # Expose the port that the app runs on
   EXPOSE 80
Enter fullscreen mode Exit fullscreen mode

Building the Docker Image

  1. Build the Docker Image: Open a terminal in the directory containing the Dockerfile and run the following command to build the image:
   docker build -t my-aspnet-app .
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Image: Check the images on your local machine to ensure it was built successfully:
   docker images
Enter fullscreen mode Exit fullscreen mode

Pushing the Docker Image to Amazon ECR

  1. Create an ECR Repository:

    • Navigate to the ECR service in the AWS Management Console.
    • Click on Create repository.
    • Provide a name for your repository (e.g., my-aspnet-app), and create it.
  2. Authenticate Docker to ECR:

   aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
Enter fullscreen mode Exit fullscreen mode
  1. Tag the Docker Image:
   docker tag my-aspnet-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
Enter fullscreen mode Exit fullscreen mode
  1. Push the Docker Image to ECR:
   docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
Enter fullscreen mode Exit fullscreen mode

Deploying the Application on EKS

Creating Kubernetes Deployment

  1. Create a Deployment YAML file: Create a file named deployment.yaml with the following content:
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: my-aspnet-app
   spec:
     replicas: 2
     selector:
       matchLabels:
         app: my-aspnet-app
     template:
       metadata:
         labels:
           app: my-aspnet-app
       spec:
         containers:
           - name: my-aspnet-app
             image: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
             ports:
               - containerPort: 80
Enter fullscreen mode Exit fullscreen mode
  1. Apply the Deployment:
   kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Creating Kubernetes Service

  1. Create a Service YAML file: Create a file named service.yaml with the following content:
   apiVersion: v1
   kind: Service
   metadata:
     name: my-aspnet-app-service
   spec:
     type: LoadBalancer
     ports:
       - port: 80
         targetPort: 80
     selector:
       app: my-aspnet-app
Enter fullscreen mode Exit fullscreen mode
  1. Apply the Service:
   kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Get the Load Balancer's URL:

After a few minutes, run the following command to retrieve the external URL for your application:

   kubectl get services
Enter fullscreen mode Exit fullscreen mode

Look for the EXTERNAL-IP of the my-aspnet-app-service. This is the URL you will use to access your application.

Configuring Database Access

If your ASP.NET application requires a database, ensure it is accessible from your EKS cluster.

Setting Up SQL Server

  1. Use Amazon RDS: For production-grade applications, consider using Amazon RDS to set up a SQL Server database instance.
  2. Connect to the Database: Update your application’s web.config file to include the database connection string. For example:
   <connectionStrings>
       <add name="DefaultConnection" 
            connectionString="Server=my-rds-instance.abcdefg123456.us-east-1.rds.amazonaws.com;Database=YourDatabaseName;User Id=YourUsername;Password=YourPassword;"
            providerName="System.Data.SqlClient" />
   </connectionStrings>
Enter fullscreen mode Exit fullscreen mode

Testing the Application

  1. Access the Application: Open a web browser and navigate to the external URL obtained from the kubectl get services command.
  2. Check Functionality: Test various functionalities of your application to ensure everything works as expected.

Monitoring and Maintenance

  1. Monitoring: Use Amazon CloudWatch to monitor the performance of your EKS cluster and applications.
  2. Logs: Implement logging within your ASP.NET application to capture errors and critical information.
  3. Auto-scaling: Configure horizontal pod autoscaling based on resource usage to handle traffic spikes.

Conclusion

Deploying an ASP.NET Framework web application on Amazon EKS allows you to leverage the benefits of containerization and Kubernetes orchestration. By following this guide, you can easily set up a scalable and manageable web application infrastructure in the cloud.

As you continue to explore AWS and EKS, consider utilizing advanced features like CI/CD pipelines, automated backups, and security best practices to

enhance your deployment further.

References

Top comments (0)